Difference between revisions of "Ks0436 keyestudio Ultimate Starter Kit For Little Inventor (Zero-based Arduino Learning Robot)"

From Keyestudio Wiki
Jump to: navigation, search
(Circuit 5: Buzzer)
(Circuit 6: Button)
(2 intermediate revisions by the same user not shown)
Line 740: Line 740:
 
*UNO Baseplate  
 
*UNO Baseplate  
 
[[File:Ks0436-14.png|200px]]
 
[[File:Ks0436-14.png|200px]]
*Passive Buzzer x 1
+
*Button x 1
[[File:Ks0436-55.png|100px]]
+
[[File:Ks0436-63.png|100px]]
* Jumper wires x 2
+
* Red LED x 1
 +
[[File:Ks0436-13.png|200px]]
 +
* 220Ω Resistor x 1
 +
* 10KΩ Resistor x 1
 +
[[File:Ks0436-15.png|200px]]
 +
* Jumper wires x 5
 
[[File:Ks0436-16.png|200px]]
 
[[File:Ks0436-16.png|200px]]
 
* USB cable x 1
 
* USB cable x 1
 
[[File:Ks0436-17.png|200px]]
 
[[File:Ks0436-17.png|200px]]
  
 +
<br>
 +
'''Component Introduction:'''<br>
 +
 +
<span style="color:brown"> '''Push Button:''' </span>
 +
[[File:Ks0436-63.png|100px|right]]
 +
Also known as momentary switches, buttons only remain in their ON state as long as they’re being actuated, or pressed. <br>
 +
Most often momentary switches are best used for intermittent user-input cases: reset button and keypad buttons. <br>
 +
These switches have a nice, tactile, “clicky” feedback when you press them. <br>
 +
Note that the different colors are just aesthetic. All of the buttons included behave the same, no matter their color. <br>
 +
[[File:Ks0436-63.png|100px]][[File:Ks0436-63.png|100px]]
 +
 +
<br>
 +
'''Hookup Guide:'''<br>
 +
See the wiring diagram below to connect everything.<br>
 +
<br>
 +
'''Schematics:''' <br>
 +
[[File:Ks0436-63.png|100px]][[File:Ks0436-63.png|100px]]
 +
 +
<br>
 +
'''Upload Code:''' <br>
 +
* 1)Connect the UNO Board to a USB port on your computer.
 +
* 2)Open the program in the code folder- Circuit 6 Button, or directly copy and paste the code below on the Ardunio IDE.
 +
* 3)Select UPLOAD to program the sketch on the UNO Board.
 +
 +
<br>
 +
Now, let's begin the compiling. When the button is pressed, the LED will be on. After the previous study, the coding should be easy for you. In this program, we add a statement of judgment. Here, we use an '''if ()''' statement. <br>
 +
Arduino IDE is based on C language, so statements of C language such as while, switch, can certainly be used for Arduino program. <br>
 +
When we press the button, pin 7 will output high level. We can program pin 11 to output high level and turn on the LED. When pin 7 outputs low level, pin 11 also outputs low level and the LED remains off. <br>
 +
 +
<pre>
 +
int ledpin=11;// initialize pin 11
 +
int inpin=7;// initialize pin 7
 +
int val;// define val
 +
void setup()
 +
{
 +
pinMode(ledpin,OUTPUT);// set LED pin as “output”
 +
pinMode(inpin,INPUT);// set button pin as “input”
 +
}
 +
void loop()
 +
{
 +
val=digitalRead(inpin);// read the level value of pin 7 and assign if to val
 +
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
 +
{ digitalWrite(ledpin,LOW);}
 +
else
 +
{ digitalWrite(ledpin,HIGH);}
 +
}
 +
</pre>
 +
 +
 +
<br>
 +
'''Code Explanation:''' <br>
 +
*Single line comments start with // and everything up until the end of that line is considered a comment.  <br>
 +
Comments are a great way to leave notes in your code explaining why you wrote it the way you did.  <br>
 +
 +
*To declare a standard input, use the line '''pinMode(pin, INPUT)''';  <br>
 +
<span style="color:red"> digitalRead(pin);  </span>
 +
Check to see if an input pin is reading HIGH (5V) or LOW (0V). Returns TRUE (1) or FALSE (0) depending on the reading. <br>
 +
 +
<br>
 +
'''What You Will See:''' <br>
 +
When the button is pressed, LED is on; otherwise, LED remains off. <br>
 +
After the above process, the button controlled LED experiment is completed. <br>
 +
The simple principle of this experiment is widely used in a variety of circuit and electric appliances. <br>
 +
[[File:Ks0436-63.png|100px]]
 +
 +
 +
<br>
 +
<span style="color:brown"> '''Troubleshooting:''' </span>
 +
*'''The buttons are not working'''  <br>
 +
First, make sure that the wiring is correct. It is easy to misalign a wire with a button leg. Second, make sure that you have declared your buttons as inputs. <br>
 +
 +
 +
<br>
 +
<big>'''Think about:'''</big>  <br>
 +
How to make responder effect? <br>
 +
Create your own exciting game. You can try to use three buttons to control three LEDs, one button used as reset button to make a responder. <br>
 +
See what will happen?<br>
 +
 +
 +
<br>
 +
'''Upload Code:'''
 +
<pre>
 +
int redled=8;    // set red LED as “output”
 +
int yellowled=7;  // set yellow LED as “output”
 +
int greenled=6;  // set green LED as “output”
 +
int redpin=5;    // initialize pin for red button
 +
int yellowpin=4;  // initialize pin for yellow button
 +
int greenpin=3;  // initialize pin for green button
 +
int restpin=2;  // initialize pin for reset button
 +
int red;
 +
int yellow;
 +
int green;
 +
void setup()
 +
{
 +
pinMode(redled,OUTPUT);
 +
pinMode(yellowled,OUTPUT);
 +
pinMode(greenled,OUTPUT);
 +
pinMode(redpin,INPUT);
 +
pinMode(yellowpin,INPUT);
 +
pinMode(greenpin,INPUT);
 +
}
 +
void loop()  // repeatedly read pins for buttons
 +
{
 +
red=digitalRead(redpin);
 +
yellow=digitalRead(yellowpin);
 +
green=digitalRead(greenpin);
 +
if(red==LOW)RED_YES();   
 +
if(yellow==LOW)YELLOW_YES();
 +
if(green==LOW)GREEN_YES();
 +
}
 +
 +
void RED_YES()// execute the code until red light is on; end cycle when reset button is pressed
 +
{
 +
  while(digitalRead(restpin)==1)
 +
  {
 +
  digitalWrite(redled,HIGH);
 +
  digitalWrite(greenled,LOW);
 +
  digitalWrite(yellowled,LOW);
 +
  }
 +
  clear_led();
 +
}
 +
void YELLOW_YES()// execute the code until yellow light is on; end cycle when reset button is pressed
 +
{
 +
  while(digitalRead(restpin)==1)
 +
  {
 +
  digitalWrite(redled,LOW);
 +
  digitalWrite(greenled,LOW);
 +
  digitalWrite(yellowled,HIGH);
 +
  }
 +
  clear_led();
 +
}
 +
void GREEN_YES()// execute the code until green light is on; end cycle when reset button is pressed
 +
{
 +
  while(digitalRead(restpin)==1)
 +
  {
 +
  digitalWrite(redled,LOW);
 +
  digitalWrite(greenled,HIGH);
 +
  digitalWrite(yellowled,LOW);
 +
  }
 +
  clear_led();
 +
}
 +
void clear_led()// all LED off
 +
{
 +
  digitalWrite(redled,LOW);
 +
  digitalWrite(greenled,LOW);
 +
  digitalWrite(yellowled,LOW);
 +
}
 +
</pre>
 +
 +
<br>
 +
[[File:Ks0436-63.png|100px]]<br>
 +
Press the button to turn corresponding LED on and off.
 +
 +
<br>
 
<br>
 
<br>

Revision as of 16:29, 12 July 2019

Zero-based Arduino Learning Robot


WELCOME TO THE KIT GUIDE

This is your map for navigating beginning embedded electronics. This kit contains all the information you will need to build the circuit and robot projects for the keyestudio UNO Board.
When you’re done with this guide, you will have built five great projects and acquired the know-how to create countless more.
Now enough talk — let’s start something!


INTRODUCTION

The UNO Board Platform

The keyestudio UNO Board is your development platform.

keyestudio UNO R3 Board Advanced

The UNO Board is essentially a small, portable computer, also known as a microcontroller.
It is capable of taking inputs (such as the push of a button or a reading from a light sensor) and interpreting that information to control various outputs (like blinking an LED light or spinning an electric motor).
This board is capable of taking the world of electronics and relating it to the physical world in a real and tangible way.

The keyestudio UNO Board is a microcontroller board based on the ATmega328P microprocessor.
It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, 2 ICSP headers and a reset button.
It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it via an external DC power jack (DC 7-12V) or via female headers Vin/GND(DC 7-12V) to get started.


Hardware Overview:
KS0341 引脚.jpg



The Breadboard Platform

A breadboard is a circuit-building platform that allows you to connect multiple components without using a soldering iron.

400 transparent breadboard

This is a half-size transparent breadboard, good for small projects.
It has 2 power rails on both sides, a standard double-strip in the middle with 30 columns and 10 rows - a total of 400 tie in points.
This tiny breadboard also has a self-adhesive on the back, so you can stick it onto an Arduino protoshield or keyestudio chassis.


Breadboard Use:
Ks0436-2.png


The Baseplate Assembly

Before you can build circuits, you’ll want to first assemble the breadboard baseplate.

Baseplate

This keyestudio baseplate makes circuit building easier by keeping the UNO Board microcontroller and the breadboard connected without the worry of disconnecting or damaging your circuit.

1)To begin with, collect your parts: the UNO Board, breadboard, screwdriver, keyestudio baseplate and four baseplate screws.
framless


2)Align the UNO board with its spot on the baseplate.
The text on it should face the same direction as the text on the breadboard and the baseplate.
Using four screws, affix the UNO Board to the four stand-off holes found on the baseplate.
Ks0436-5.png


3)Peel the adhesive backing off the breadboard.
Ks0436-6.png


4)Carefully align the breadboard over its spot on the baseplate. The text on the breadboard should face the same direction as the text on the baseplate. Firmly press the breadboard to the baseplate to adhere it.
Ks0436-7.png

Your baseplate is now assembled!
Ks0436-8.png



The Arduino IDE

In order to get your UNO Board up and running.
you'll need to download the newest version of the Arduino software from www.arduino.cc (it's free!).
KS0313-3.png

This software, known as the Arduino IDE (Integrated Development Environment), will allow you to program the UNO Board to do exactly what you want. It’s like a word processor for coding.


With an internet-capable computer, open up your favorite browser and type the following URL into the address bar to download the software of any versions:
https://www.arduino.cc/en/Main/OldSoftwareReleases#1.5.x


Open Arduino IDE
Downloaded the software package, unzip the folder package, double click the Arduino icon to open.
Ks0313图片1.png

framless


The functions of each button on the Toolbar are listed below:
IDE.png

IDE 1.png Verify/Compile Check the code for errors
IDE 2.png Upload Upload the current Sketch to the Arduino
IDE 3.png New Create a new blank Sketch
IDE 4.png Open Show a list of Sketches
IDE 5.png Save Save the current Sketch
IDE 6.png Serial Monitor Display the serial data being sent from the Arduino



Select Your Board And Serial Port
NOTE: select the Arduino UNO, but if you are not sure which ports should choose. Go to your computer panel, and check the Port out in the Device Manger.
framless

framless



The Parts List

The Kit contains extensive electronic components. Below shows you a part of kit components:
framless



GET STARTED WITH CIRCUIT PROJECTS!

There are 20 circuit projects total. Each project will introduce new concepts and components, which will be described in more detail as you progress through the circuits.
As you work your way through each circuit, you will learn to use new and more complicated parts to accomplish increasingly complex tasks.
The project will set the foundation for the rest and will aid in helping you understand the fundamentals of circuit building and electricity!

Ks0436-12.png


Circuit 1: Blinking an LED


About this circuit:
Blinking an LED is the classic starting point for learning how to program embedded electronic components.
In this circuit, you’ll write code that makes an LED blink on and off.


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Red LED x 1

Ks0436-13.png

  • 220Ω Resistor x 1

Ks0436-15.png

  • Jumper wires x 2

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png


Component Introduction:

LIGHT-EMITTING DIODES (LEDS):
They come in different colors, brightnesses and sizes. LEDs have a positive (+) leg and a negative (-) leg, and they will only let electricity flow through them in one direction.

Ks0436-18.png

LEDs can also burn out if too much electricity flows through them, so you should always use a resistor to limit the current when you wire an LED into a circuit.


RESISTORS: resist the flow of electricity.
Ks0436-19.png
You can use them to protect sensitive components like LEDs. The strength of a resistor (measured in ohms) is marked on the body of the resistor using small colored bands.
Each color stands for a number, which you can look up using a resistor chart.

Ks0436-20.png Ks0436-21.png


Wiring Diagram:
Check out the schematics and wiring diagram below to see how everything is connected.

Schematics:

Ks0436-22.png Ks0436-23.png


With the 220Ω resistor in place, the LED should be quite bright.
If you swap out the 220Ω resistor for the 1kΩ resistor, then the LED will appear a little dimmer.
At the moment, you have Digital pin 10 going to one leg of the resistor, the other leg of the resistor going to the positive side of the LED and the other side of the LED going to GND.


Upload Code:

  • 1)Connect the UNO Board to a USB port on your computer.
  • 2)Open the program in the code folder- Circuit 1 LED, or directly copy and paste the code below on the Ardunio IDE.
  • 3)Select UPLOAD to program the sketch on the UNO Board.
int ledPin = 10; // define digital pin 10.
void setup()
{
pinMode(ledPin, OUTPUT);// define pin with LED connected as output.
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on.
delay(1000); // wait for a second.
digitalWrite(ledPin, LOW); // set the LED off.
delay(1000); // wait for a second
}


Code Explanation:

  • Single line comments start with // and everything up until the end of that line is considered a comment.

Comments are a great way to leave notes in your code explaining why you wrote it the way you did.

  • The first line of code is:

int ledPin = 10; // define digital pin 10
As the comment above it explains, this is giving a name to the pin that the LED is connected to. You can change the connection pin here.

  • Next, we have the “setup” and “loop” function

void setup()
Every Arduino sketch must have a 'setup' function, and the place where you might want to add instructions of your own is between the { and the }. In this case, there is just one command there, which, as the comment states tells the Arduino board that we are going to use the LED pin as an output.

void loop()
It is also mandatory for a sketch to have a “loop” function. Unlike the 'setup' function that only runs once, after a reset, the 'loop' function will, after it has finished running its commands, immediately start again.

  • Input Or Output ?:

pinMode(ledPin, OUTPUT)
Before you can use one of the digital pins, you need to tell the UNO Board whether it is an INPUT or OUTPUT.
We use a built-in “function” called pinMode() to make pin 10 a digital output.

Digital Output:
digitalWrite(ledPin, HIGH)
digitalWrite(ledPin, LOW)
When you’re using a pin as an OUTPUT, you can command it to be HIGH (output 5 volts) or LOW (output 0 volts).

  • What happens when you change the number in one or both of the delay(1000)

This delay period is in milliseconds, so if you want the LED to blink twice as fast, change the value, try 500 or 2000.
Upload the sketch again and you should see the LED start to blink more quickly or slowly.


What You Will See:
Ks0436-24.png

After downloading this program, in the experiment, you will see the LED flashing on for one second, then off for one second.
The blinking LED experiment is now completed. Thank you!
Ks0436-25.png



Troubleshooting:

  • Upload failed?

Make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Check out the Board and Serial Port.



Circuit 2: Light Extension


About this circuit:
After mastering LED Blinking, let’s move on to how to control lots of LEDs. The principle is almost the same.
In this circuit, you’ll write code that makes six LEDs blink on and off one by one.
Furthermore, you can also use three LEDs to simulate the traffic light. Now let’s start something different!


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Red LED x 6

Ks0436-13.png

  • 220Ω Resistor x 6

Ks0436-15.png

  • Jumper wires x 7

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png


Wiring Diagram:
Check out the wiring below to see how everything is connected.


Schematics:
Ks0436-26.pngKs0436-27.png



Upload Code:

  • 1)Connect the UNO Board to a USB port on your computer.
  • 2)Open the program in the code folder- Circuit 2 Light, or directly copy and paste the code below on the Ardunio IDE.
  • 3)Select UPLOAD to program the sketch on the UNO Board.
int BASE = 2 ;  // the I/O pin for the first LED
int NUM = 6;   // the number of LEDs
void setup()
{
   for (int i = BASE; i < BASE + NUM; i ++) 
   {
     pinMode(i, OUTPUT);   // set I/O pins as output
   }
}
void loop()
{
   for (int i = BASE; i < BASE + NUM; i ++) 
   {
     digitalWrite(i, LOW);    // set I/O pins as “low”, turn off LEDs one by one.
     delay(200);        // delay
   }
   for (int i = BASE; i < BASE + NUM; i ++) 
   {
     digitalWrite(i, HIGH);    // set I/O pins as “high”, turn on LEDs one by one
     delay(200);        // delay
   }  
}


Code Explanation:

  • Single line comments start with // and everything up until the end of that line is considered a comment.

Comments are a great way to leave notes in your code explaining why you wrote it the way you did.

  • int BASE = 2 ; // set the I/O pin for the first LED
  • int NUM = 6; // the number of LEDs

Ks0436-28.png


What You Will See:
Ks0436-29.png


You can see the six LEDs blink on and off one by one. It seems like light chasing.
If it doesn’t, make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Check out the Board and Serial Port.
Ks0436-30.png



Troubleshooting:

  • Upload failed?

Make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Check out the Board and Serial Port.

  • LED Not Lighting Up?

Make sure you connect the LED correctly or try other LEDs.



How to make a traffic light?


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Red LED x 1

Ks0436-13.png

  • Yellow LED x 1

Ks0436-31.png

  • Green LED x 1

Ks0436-32.png

  • 220Ω Resistor x 3

Ks0436-15.png

  • Jumper wires x 4

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png


Hookup Guide:
Check out the hookup table below to see how everything is connected.
Schematics:
Ks0436-33.pngKs0436-34.png


Source Code:

int redled =10; // initialize digital pin 8.
int yellowled =7; // initialize digital pin 7.
int greenled =4; // initialize digital pin 4.
void setup()
{
pinMode(redled, OUTPUT);// set the pin with red LED as “output”
pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output”
pinMode(greenled, OUTPUT); // set the pin with green LED as “output”
}
void loop()
{
digitalWrite(greenled, HIGH);//// turn on green LED
delay(5000);// wait 5 seconds
digitalWrite(greenled, LOW); // turn off green LED
for(int i=0;i<3;i++)// blinks for 3 times
{
delay(500);// wait 0.5 second
digitalWrite(yellowled, HIGH);// turn on yellow LED
delay(500);// wait 0.5 second
digitalWrite(yellowled, LOW);// turn off yellow LED
} 
delay(500);// wait 0.5 second
digitalWrite(redled, HIGH);// turn on red LED
delay(5000);// wait 5 second
digitalWrite(redled, LOW);// turn off red LED
}


Ks0436-35.png

<br< Done uploading the code, you can see your own designed traffic light.
This circuit design is very similar with the one in LED chase effect.
The green light will turn on for 5 seconds, and then off, followed by the yellow light blinking for 3 times, and then red light turns on for 5 seconds, circularly and repeatedly. Ks0436-36.png



Circuit 3: Potentiometer


About this circuit:
In the previous project, you have learned how to turn on or off an LED.
In this circuit, you’ll use a potentiometer as an input device to control the speed at which your LED blinks.
Here may involves a new concept PWM to control the brightness of an LED. Check more details below.


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Potentiometer x 1

Ks0436-37.png

  • Red LED x 1

Ks0436-13.png

  • 220Ω Resistor x 1

Ks0436-15.png

  • Jumper wires x 6

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png



Component Introduction:

POTENTIOMETER:
Potentiometers (also known as“trimpots”or“knobs”) are one of the basic inputs for electronic devices.

Ks0436-37.png

A potentiometer is a 3-pin variable resistor.
When powered with 5V, the middle pin outputs a voltage between 0V and 5V, depending on the position of the knob on the potentiometer.
By tracking the position of the knob with your UNO Board, you can make volume controls, speed controls, angle sensors and a ton of other useful inputs for your projects.
Ks0436-38.png


PWM Control:
PWM is short for Pulse Width Modulation.

Ks0436-39.png

Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means.
Digital control is used to create a square wave of different duty cycle, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. 
In the graphic, the green lines represent a regular time period.
KS0313(4)-1.png

This duration or period is the inverse of the PWM frequency.
In other words, with Arduino's PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each.
A call to analogWrite() is on a scale of 0-255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
To be simply, we all know that the voltage output of Arduino Digital port only has two states, LOW and HIGH, corresponding to the voltage output of 0V and 5V.
If merely make use of LOW and HIGH state, it cannot control the brightness of an LED light. However, if convert the voltage output of 0 Volts and 5 Volts into the value within 0-255, this way you can change the value within 0-255 to control the brightness of light.
The Arduino controller has totally 6 PWM outputs, which are Digital 3, 5, 6, 9, 10 and 11.
These pins can be used as Digital output or Analog output. If used as Analog output, it needs to call the analogWrite() function of ARDUINO, and this analogWrite() function can be controlled in the range of 0-255.
Ks0436-41.png


Hookup Guide:
The input of potentiometer is analog, so we connect it to analog port, and LED to PWM pin.
Different PWM signals can regulate the LED brightness.
Ks0436-42.pngKs0436-43.png


Upload Code:

  • 1)Connect the UNO Board to a USB port on your computer.
  • 2)Open the program in the code folder- Circuit 3 Potentiometer, or directly copy and paste the code below on the Ardunio IDE.
  • 3)Select UPLOAD to program the sketch on the UNO Board.
int potpin=0;// initialize analog pin 0
int ledpin=11;//initialize digital pin 11(PWM output)
int val=0;// Temporarily store variables' value from the sensor
void setup()
{
pinMode(ledpin,OUTPUT);// define digital pin 11 as “output”
Serial.begin(9600);// set baud rate at 9600
// attention: for analog ports, they are automatically set up as “input”
}
void loop()
{
val=analogRead(potpin);// read the analog value from the sensor and assign it to val
Serial.println(val);// display value of val
analogWrite(ledpin,val/4);// turn on LED and set up brightness(maximum output of PWM is 255)
delay(10);// wait for 0.01 second
}


Code Explanation:

  • Single line comments start with // and everything up until the end of that line is considered a comment.

Comments are a great way to leave notes in your code explaining why you wrote it the way you did.

  • The first line of code is:

int potpin=0;// initialize analog pin 0
A variable is a placeholder for values that may change in your code. You must introduce, or“declare,”variables before you use them.
Here we’re declaring a variable called potPosition of type int (integer).

  • Serial.begin(9600)

Serial commands can be used to send and receive data from your computer. Notice that the baud rate, 9600, is the same as the one we selected in the monitor. This is the speed at which the two devices communicate, and it must match on both sides.

  • val=analogRead(potpin)

We use the analogRead() function to read the value on an analog pin.

  • Serial.println(val);

This is the line that actually prints the trimpot value to the monitor.


What You Will See:
You should see the LED brighter or dim in accordance with your potentiometer.
Ks0436-45.png

You should see numeric values print out in the monitor. Turning the potentiometer changes the value.
Ks0436-46.png



Troubleshooting:

  • Upload failed?

Make sure you have assembled the circuit correctly and verified and uploaded the code to your board. Ensure that you are on the correct Board and Serial Port.

  • No values in Serial Monitor?

Make sure that you have selected the correct baud rate, 9600.


Circuit 4: Photo Resistor


About this circuit:
In the previous circuit, you have learned how to use a potentiometer as an input device to control the LED’s brightness.
In this circuit, you’ll be using a photoresistor, which changes resistance based on how much light the sensor receives.
Using this sensor you can make a simple night-light that turns on when the room gets dark and turns off when it is bright.


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Potentiometer x 1

Ks0436-47.png

  • Red LED x 1

Ks0436-13.png

  • 220Ω Resistor x 1

Ks0436-15.png

  • 10KΩ Resistor x 1
  • Jumper wires x 5

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png



Component Introduction:

Photo Resistor:
Photo resistor (Photovaristor) is a resistor whose resistance varies according to different incident light strength.

Photo resistor.png

It's made based on the photoelectric effect of semiconductor.
If the incident light is intense, its resistance reduces; if the incident light is weak, the resistance increases. 
Photo resistor is widely applied to various light control circuit, such as light control and adjustment, optical switches, etc.
Ks0436-49.png

Photovaristor is an element that changes its resistance as light strength changes. So we will need to read the analog values.
We can refer to the PWM experiment, replacing the potentiometer with photovaristor. When there is change in light strength, there will be corresponding change on the LED.


Hookup Guide:
The photoresistors, like regular resistors, are not polarized and can be installed in either direction.
Ks0436-50.png Ks0436-51.png



Upload Code:

  • 1)Connect the UNO Board to a USB port on your computer.
  • 2)Open the program in the code folder- Circuit 4 Photoresistor, or directly copy and paste the code below on the Ardunio IDE.
  • 3)Select UPLOAD to program the sketch on the UNO Board.

After the connection, let's begin the program compiling. The program is similar to the one of PWM.
For change details, please refer to the Sample Code below. 

int potpin=0;// initialize analog pin 0, connected with photovaristor
int ledpin=11;// initialize digital pin 11, output regulating the brightness of LED
int val=0;// initialize variable val
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin 11 as“output”
Serial.begin(9600);// set baud rate at“9600”
}
void loop()
{
val= map(analogRead(potpin),0,1023,0,255);
Serial.println(val);// display the value of val
analogWrite(ledpin,val);// turn on the LED and set up brightness(maximum output value 255)
delay(10);// wait for 0.01 
}


Ks0436-52.png

What You Will See:
You can change the light intensity around the photovaristor and see corresponding brightness change of the LED.
See the light intensity on the serial monitor.
Ks0436-53.pngKs0436-54.png



Circuit 5: Buzzer


About this circuit:
In this project, you will learn how to make tones with a buzzer.


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Passive Buzzer x 1

Ks0436-55.png

  • Jumper wires x 2

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png


Component Introduction:
Passive Buzzer:
Buzzers can be categorized as active and passive ones.
The difference between the two is that an active buzzer has a built-in oscillating source, so it will generate a sound when electrified.
A passive buzzer does not have such a source, so DC signal cannot drive it beep. Different frequencies produce different sounds. By the buzzer, you can even play a song.

Ks0436-56.pngKs0436-57.png



Hookup Guide:
Wiring the buzzer connected to the UNO board, the red (positive) to the pin8, black wire (negative) to the GND.
Ks0436-58.png Ks0436-59.png



Upload Code:

  • 1)Connect the UNO Board to a USB port on your computer.
  • 2)Open the program in the code folder- Circuit 5 Buzzer, or directly copy and paste the code below on the Ardunio IDE.
  • 3)Select UPLOAD to program the sketch on the UNO Board.

After the connection, let's begin the program compiling.

int buzzer=8;// select digital IO pin for the buzzer
void setup() 
{ 
pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output 
} 
void loop() 
{ unsigned char i,j;//define variable
while(1) 
{ for(i=0;i<80;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(1);//delay1ms 
digitalWrite(buzzer,LOW);//not sound
delay(1);//ms delay 
} 
for(i=0;i<100;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
digitalWrite(buzzer,LOW);//not sound
delay(2);//2ms delay 
}
} 
} 


Test Reult:
Done uploading the code to the board, the passive buzzer will make a tone.
Ks0436-60.png


Troubleshooting:

  • No sound is playing?

Check your wiring of the buzzer. It’s easy to misalign a pin with a jumper wire or reverse the buzzer.


What about active buzzer?
Ks0436-61.png

The use method is almost the same.
Think about it and try to make an audible beep from active buzzer.

Test code is showed below:

int buzzer=2;// initialize digital IO pin that controls the buzzer
void setup() 
{ 
  pinMode(buzzer,OUTPUT);// set pin mode as “output”
} 
void loop() 
{
digitalWrite(buzzer, HIGH); // produce sound
}


From the test code, we can know the buzzer’s positive lead (long lead) is connected to Digital pin 2 of UNO R3. The short lead is connected to ground.
Ks0436-62.png


Troubleshooting:

  • No sound playing?

Check your wiring of the buzzer. It’s easy to misalign a pin with a jumper wire or reverse the buzzer.


Circuit 6: Button


About this circuit:
Buttons are all around us, from the keys on your keyboard to the buttons on your remote control.
In this circuit, you’ll learn how to use the button to control the LED on and off using digital input.


What You Need:

  • UNO Baseplate

Ks0436-14.png

  • Button x 1

Ks0436-63.png

  • Red LED x 1

Ks0436-13.png

  • 220Ω Resistor x 1
  • 10KΩ Resistor x 1

Ks0436-15.png

  • Jumper wires x 5

Ks0436-16.png

  • USB cable x 1

Ks0436-17.png


Component Introduction:

Push Button:

Ks0436-63.png

Also known as momentary switches, buttons only remain in their ON state as long as they’re being actuated, or pressed.
Most often momentary switches are best used for intermittent user-input cases: reset button and keypad buttons.
These switches have a nice, tactile, “clicky” feedback when you press them.
Note that the different colors are just aesthetic. All of the buttons included behave the same, no matter their color.
Ks0436-63.pngKs0436-63.png


Hookup Guide:
See the wiring diagram below to connect everything.

Schematics:
Ks0436-63.pngKs0436-63.png


Upload Code:

  • 1)Connect the UNO Board to a USB port on your computer.
  • 2)Open the program in the code folder- Circuit 6 Button, or directly copy and paste the code below on the Ardunio IDE.
  • 3)Select UPLOAD to program the sketch on the UNO Board.


Now, let's begin the compiling. When the button is pressed, the LED will be on. After the previous study, the coding should be easy for you. In this program, we add a statement of judgment. Here, we use an if () statement.
Arduino IDE is based on C language, so statements of C language such as while, switch, can certainly be used for Arduino program.
When we press the button, pin 7 will output high level. We can program pin 11 to output high level and turn on the LED. When pin 7 outputs low level, pin 11 also outputs low level and the LED remains off.

int ledpin=11;// initialize pin 11
int inpin=7;// initialize pin 7
int val;// define val
void setup()
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”
}
void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to val
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
{ digitalWrite(ledpin,LOW);}
else
{ digitalWrite(ledpin,HIGH);}
}



Code Explanation:

  • Single line comments start with // and everything up until the end of that line is considered a comment.

Comments are a great way to leave notes in your code explaining why you wrote it the way you did.

  • To declare a standard input, use the line pinMode(pin, INPUT);

digitalRead(pin); Check to see if an input pin is reading HIGH (5V) or LOW (0V). Returns TRUE (1) or FALSE (0) depending on the reading.


What You Will See:
When the button is pressed, LED is on; otherwise, LED remains off.
After the above process, the button controlled LED experiment is completed.
The simple principle of this experiment is widely used in a variety of circuit and electric appliances.
Ks0436-63.png



Troubleshooting:

  • The buttons are not working

First, make sure that the wiring is correct. It is easy to misalign a wire with a button leg. Second, make sure that you have declared your buttons as inputs.



Think about:
How to make responder effect?
Create your own exciting game. You can try to use three buttons to control three LEDs, one button used as reset button to make a responder.
See what will happen?



Upload Code:

int redled=8;     // set red LED as “output”
int yellowled=7;  // set yellow LED as “output”
int greenled=6;   // set green LED as “output”
int redpin=5;     // initialize pin for red button
int yellowpin=4;  // initialize pin for yellow button
int greenpin=3;   // initialize pin for green button
int restpin=2;   // initialize pin for reset button
int red;
int yellow;
int green;
void setup()
{
pinMode(redled,OUTPUT);
pinMode(yellowled,OUTPUT);
pinMode(greenled,OUTPUT);
pinMode(redpin,INPUT);
pinMode(yellowpin,INPUT);
pinMode(greenpin,INPUT);
}
void loop()  // repeatedly read pins for buttons
{
red=digitalRead(redpin);
yellow=digitalRead(yellowpin);
green=digitalRead(greenpin);
if(red==LOW)RED_YES();    
if(yellow==LOW)YELLOW_YES();
if(green==LOW)GREEN_YES();
}

void RED_YES()// execute the code until red light is on; end cycle when reset button is pressed
{
  while(digitalRead(restpin)==1)
  {
   digitalWrite(redled,HIGH);
   digitalWrite(greenled,LOW);
   digitalWrite(yellowled,LOW);
  }
  clear_led();
}
void YELLOW_YES()// execute the code until yellow light is on; end cycle when reset button is pressed
{
  while(digitalRead(restpin)==1)
  {
  digitalWrite(redled,LOW);
  digitalWrite(greenled,LOW);
  digitalWrite(yellowled,HIGH);
  }
  clear_led();
}
void GREEN_YES()// execute the code until green light is on; end cycle when reset button is pressed
{
  while(digitalRead(restpin)==1)
  {
  digitalWrite(redled,LOW);
  digitalWrite(greenled,HIGH);
  digitalWrite(yellowled,LOW);
  }
  clear_led();
}
void clear_led()// all LED off
{
  digitalWrite(redled,LOW);
  digitalWrite(greenled,LOW);
  digitalWrite(yellowled,LOW);
}


Ks0436-63.png
Press the button to turn corresponding LED on and off.