Ks0069(72, 73) keyestudio Basic Starter Kit for Arduino Starters: Difference between revisions

From Keyestudio Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 4: Line 4:


<br>
<br>
==1. Kit Introduction==
==Kit Introduction==
This is the  basic Starter Kit, developed specially for those beginners who are interested in Arduino.  <br>
This is the  basic Starter Kit, developed specially for those beginners who are interested in Arduino.  <br>
You will have a set of Arduino's most common and useful electronic components. What's more, we will offer you a detailed tutorials including project introduction and their source codes.<br>
You will have a set of Arduino's most common and useful electronic components. What's more, we will offer you a detailed tutorials including project introduction and their source codes.<br>
Line 11: Line 11:


<br>
<br>
==2.Kit Contents ==
==Kit Contents ==


{| width="80%" cellspacing="0" border="1"
{| width="80%" cellspacing="0" border="1"
Line 186: Line 186:


<br>
<br>
==4.Project Details ==
<br>
==Getting Started with Arduino==


=== '''Project 1: Hello World'''===  
===Installing Arduino Software===
When you get the UNO development board, first you should install the Arduino software and driver. <br>
We usually use the Windows software Arduino 1.5.6 version. You can download it from the link below: <br>
https://www.arduino.cc/en/Main/OldSoftwareReleases#1.5.x<br>
Or you can browse the ARDUINO website to download the latest version from this link, https://www.arduino.cc, pop up the following interface.
<br>[[Image:KS0313-1.png|600px|frameless]]<br>


'''Introduction:'''
Then click the '''SOFTWARE''' on the browse bar, you will have two options ONLINE TOOLS and DOWNLOADS.
<br>[[Image:KS0313-2.png|600px|frameless]]<br>


As for starters, we will begin with something simple. In this project, you only need an Arduino and a USB cable to start the "Hello World!" experiment. This is a communication test of your Arduino and PC, also a primer project for you to have your first try of the Arduino world!<br>
Click '''DOWNLOADS''', it will appear the latest software version of ARDUINO 1.8.5 shown as below. 
<br>[[Image:KS0313-3.png|600px|frameless]]<br>


'''Hardware Required:'''<br>
In this software page, on the right side you can see the version of development software for different operating systems. ARDUINO has a powerful compatibility. You should download the software that is compatible with the operating system of your computer.<br>
1. Arduino board x1<br>
We will take '''WINDOWS system''' as an example here. There are also two options under Windows system, one is installed version, the other is non-installed version.
2. USB cable x1<br>
For simple installed version, first click '''Windows Installer''', you will get the following page.<br>
<br>[[Image:KS0313-4.png|600px|frameless]]<br>
<br>[[Image:KS0313-5.png|600px|frameless]]<br>


'''Sample Code:'''<br>
This way you just need to click JUST DOWNLOAD, then click the downloaded file to install it. <br>
After installing driver for Arduino, let's open Arduino software and compile code that enables Arduino to print "Hello World!" under your instruction. Of course, you can compile code for Arduino to continuously echo "Hello World!" without instruction. A simple If () statement will do the instruction trick. With the onboard LED connected to pin 13, we can instruct the LED to blink first when Arduino gets an instruction and then print "Hello World!”.  
For non-installed version, first click Windows ZIP file, you will also get the pop-up interface as the above figure.<br>
Click JUST DOWNLOAD, and when the ZIP file is downloaded well to your computer, you can directly unzip the file and click the icon of ARDUINO software to start it. <br>


<pre>
<br>
int val;//define variable val
===Installing Arduino (Windows)===
int ledpin=13;// define digital interface 13
Install Arduino with the exe. Installation package. Here we provide you with [https://drive.google.com/open?id=1ivTOKCKgkmEuBtZpLI75XKryGI-xx-xF Arduino-1.5.6-r2-windows package], you can directly click the icon to install it.
void setup()
<br>[[File:Arduino Setup 1.jpg|800px|frameless|thumb]]<br>
{
  Serial.begin(9600);// set the baud rate at 9600 to match the software set up. When connected to a specific device, (e.g. bluetooth), the baud rate needs to be the same with it.
  pinMode(ledpin,OUTPUT);// initialize digital pin 13 as output. When using I/O ports on an Arduino, this kind of set up is always needed.
}
void loop()
{
  val=Serial.read();// read the instruction or character from PC to Arduino, and assign them to Val.
  if(val=='R')// determine if the instruction or character received is “R”.
  {  // if it’s “R”,   
    digitalWrite(ledpin,HIGH);// set the LED on digital pin 13 on.  
    delay(500);
digitalWrite(ledpin,LOW);// set the LED on digital pin 13 off.   delay(500);
 
    Serial.println("Hello World!");// display“Hello World!”string.
  }
}
</pre>


'''Result:'''<br>
Click“I Agree”to see the following interface.
Screenshot<br>
<br>[[File:Arduino Setup 2.jpg|800px|frameless|thumb]]<br>


<br>[[File:693.png|500px|frameless|thumb]]<br>
Click “Next”. Pop up the interface below.
<br>[[File:Arduino Setup 3.jpg|800px|frameless|thumb]]<br>


Click serial port monitor<br>
You can press Browse… to choose an installation path or directly type in the directory you want.<br>
Input R<br>
Then click “Install” to initiate installation.
LED 13 will blink once;<br>
<br>[[File:Arduino Setup 4.jpg|800px|frameless|thumb]]<br>
PC will receive information from Arduino: Hello World<br>


<br>[[File:694.png|500px|frameless|thumb]]<br>
Wait for the installing process, if appear the interface of Window Security, just continue to click Install to finish the installation.
After you choose the right port, the experiment should be easy for you!
<br>[[File:Arduino1.5.6- setup 5.png|800px|frameless|thumb]]<br>


All right, up to now, you have completed the Arduino setup! The following icon will appear on your PC desktop.
<br>[[Image:Ks0313图片1.png|600px|frameless]]<br>
Double-click the icon of Arduino to enter the desired development environment shown as below.
<br>[[Image:717.png|600px|frameless]]<br>
<br>
The functions of each button on the Toolbar are listed below:
<br>[[Image:IDE.png|600px|frameless]]<br>


=== '''Project 2: LED Blinking'''===  
{| class="wikitable" cellpadding="1" cellspacing="1"
|-
!scope="row" |[[Image:IDE 1.png|600px|frameless]] Verify/Compile
| Check the code for errors
|-
!scope="row" |[[Image:IDE 2.png|600px|frameless]] Upload
| Upload the current Sketch to the Arduino
|-
!scope="row" |[[Image:IDE 3.png|600px|frameless]] New
| Create a new blank Sketch
|-
! scope="row" |[[Image:IDE 4.png|600px|frameless]] Open
| Show a list of Sketches
|-
! scope="row" |[[Image:IDE 5.png|600px|frameless]] Save
| Save the current Sketch
|-
! scope="row" |[[Image:IDE 6.png|600px|frameless]] Serial Monitor
| Display the serial data being sent from the Arduino
|-
|}
 
<br>
 
===Installing Driver===
Next, we will introduce the driver installation for development board. The driver installation may have slight differences in different computer systems. So in the following let’s move on to the driver installation in the WIN 7 system. <br>
The Arduino folder contains both the Arduino program itself and the drivers that allow the Arduino to be connected to your computer by a USB cable. Before we launch the Arduino software, you are going to install the USB drivers.<br>


'''Introduction:'''
Plug one end of your USB cable into the Arduino and the other into a USB socket on your computer.<br>
When you connect the UNO board to your computer at the first time, right click the icon of your '''''“Computer” —>for “Properties”—> click “Device manager”''''', under “Other Devices”, you should see an icon for “Unknown device” with a little yellow warning triangle next to it. This is your Arduino.<br>
<br>[[Image:Driver 1.png|600px|frameless]]<br>
Then right-click on the device and select the top menu option (Update Driver Software...) shown as the figure below.. 
<br>[[Image:Driver 2.png|600px|frameless]]<br>


Blinking LED experiment is quite simple. In the "Hello World!" program, we have come across LED. This time, we are going to connect an LED to one of the digital pins rather than using LED13, which is soldered to the board. Except an Arduino and an USB cable, we will need extra parts as below:<br>
It will then be prompted to either “Search Automatically for updated driversoftware” or “Browse my computer for driver software”. Shown as below. <br> In this page, select “Browse my computer for driver software”.
<br>[[Image:Driver 3.png|600px|frameless]]<br>


'''Hardware Required:'''<br>
After that, select the option to browseand navigate to the “drivers” folder of Arduino installation.
1. Red M5 LED*1 <br>
<br>[[Image:KS0286-4.png|800px|frameless]]<br>
2. 220Ω resistor*1<br>
 
3. Breadboard*1<br>
Click “Next” and you may get a security warning, if so, allow the software to be installed. Shown as below.
4. Breadboard jumper wires* several <br>
<br>[[Image:Driver 5.png|600px|frameless]]<br>
 
Once the software has been installed, you will get a confirmation message. Installation completed, click “Close”.
<br>[[Image:Driver 6.png|600px|frameless]]<br>
 
Up to now, the driver is installed well. Then you can right click “Computer” —>“Properties”—>“Device manager”, you should see the device as the figure shown below. 
<br>[[Image:Driver 7.png|600px|frameless]]<br>


We follow below diagram from the experimental schematic link. Here we use digital pin 10. We connect LED to a 220 ohm resistor to avoid high current damaging the LED.
<br>
===Example Use: Displaying Hello World===
'''Overview'''<br>
It is very simple. You can use only a main board and a USB cable to display the “Hello World!”. It is a communication experiment between the control board and PC. This is an entry experiment for you to enter the Arduino programming world.<br>
Note that need to use a serial communication software, [http://wiki.keyestudio.com/index.php/Download_Arduino_IDE_and_Install_Driver Arduino IDE]. <br>
In the above part, you can check the detailed use of Arduino IDE.<br>
<br>
'''Component Required'''  <br>
* UNO R3 control board*1
* USB cable*1


'''Connection for UNO R3:'''
'''Component Introduction'''<br>
<br>[[File:695.png|500px|frameless|thumb]]<br>
[[image:UNO R3--.png|thumb|300px|right|Keyestudio UNO R3 Board]]
[http://wiki.keyestudio.com/index.php/Ks0001_keyestudio_UNO_R3_BOARD Keyestudio UNO R3 development board] is a microcontroller board based on the ATmega328P ([http://www.atmel.com/dyn/resources/prod_documents/doc8161.pdf datasheet]), fully compatible with ARDUINO UNO REV3. 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.<br>
It contains everything needed to support the microcontroller; simply connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get started.


'''Connection for 2560 R3:'''
'''Connect It Up'''<br>
<br>[[File:696.png|500px|frameless|thumb]]<br>
Connect the UNO board to your computer using the USB cable. The green power LED should go on.
<br>[[Image:Step2.jpg|600px|frameless]]<br>


'''Sample Code:'''<br>
'''Upload the Code'''<br>
Below is an example code for displaying the Hello World!
<pre>
<pre>
int ledPin = 10; // define digital pin 10.
int val;
int ledpin=13;
void setup()
void setup()
{
{
pinMode(ledPin, OUTPUT);// define pin with LED connected as output.
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}
}
void loop()
void loop()
{
{
digitalWrite(ledPin, HIGH); // set the LED on.
val=Serial.read();
delay(1000); // wait for a second.
if(val=='R')
digitalWrite(ledPin, LOW); // set the LED off.
{
delay(1000); // wait for a second
digitalWrite(ledpin,HIGH);
}</pre>
delay(500);
digitalWrite(ledpin,LOW);
delay(500);
Serial.println("Hello World!");
}
}
</pre>
 
'''Select the Arduino Board''' <br>
Open the Arduino IDE, you’ll need to click the “Tools”, then select the Board that corresponds to your Arduino.
<br>[[Image:Board.jpg|600px|frameless]]<br>
 
<br>
'''Select your serial port'''<br>
Select the serial device of the Arduino board from the Tools | Serial Port menu.  <br>
<span style="color: red">'''Note: ''' to avoid errors, the COM Port should keep the same as the Ports shown on Device Manager. </span>
<br>[[Image:Manager.jpg|600px|frameless]]<br>
<br>[[Image:Port.jpg|600px|frameless]]<br>
 
Then click verify button to check the errors. If compiling successfully, the message "Done compiling." will appear in the status bar.
<br>[[Image:Compile 2.jpg|600px|frameless]]<br>


'''Result:'''<br>
After that, click the “Upload” button to upload the code. Wait a few seconds - you should see the RX and TX leds on the board flashing. If the upload is successful, the message "Done uploading." will appear in the status bar. <br>
After downloading this program, in the experiment, you will see the LED connected to pin 10 turning on and off, with an interval approximately one second.
The blinking LED experiment is now completed. Thank you!


=== '''Project 3: PWM'''===
<span style="color: red">'''(Note: ''' If you have an Arduino Mini, NG, or other board, you'll need to physically present the reset button on the board immediately before pressing the upload button.)</span><br>
<br>[[Image:Upload 2.jpg|600px|frameless]]<br>


'''Introduction:'''
<br>
'''Open the Serial Monitor''' <br>
After that, click the serial monitor button to open the serial monitor.
<br>[[Image:UNO monitor.png|700px|frameless]]<br>
Then set the baud rate as 9600, enter an “R” and click Send, you should see the RX led on the board blink once, and then D13 led blink once, finally "Hello World!" is showed on the monitor, and TX led blink once. <br>
Congrats! Your first simple program is complete.
<br>[[Image:ks0248 monitor 2.png|700px|frameless]]<br>


PWM, short for Pulse Width Modulation, is a technique used to encode analog signal level into digital ones. A computer cannot output analog voltage but only digital voltage values such as 0V or 5V. So we use a high resolution counter to encode a specific analog signal level by modulating the duty cycle of PMW. The PWM signal is also digitalized because in any given moment, fully on DC power supply is either 5V (ON), or 0V (OFF). The voltage or current is fed to the analog load (the device that uses the power) by repeated pulse sequence being ON or OFF. Being on, the current is fed to the load; being off, it's not. With adequate bandwidth, any analog value can be encoded using PWM. The output voltage value is calculated via the on and off time. Output voltage = (turn on time/pulse time) * maximum voltage value<br>
<br>
<br>[[File:697.png|500px|frameless|thumb]]<br>
<br>
PWM has many applications: lamp brightness regulating, motor speed regulating, sound making, etc.
== Project Details==
The following are the three basic parameters of PMW:
<br>
<br>[[File:698.png|500px|frameless|thumb]]<br>
===Project 1: Hello World===
1. The amplitude of pulse width (minimum / maximum)<br>
2. The pulse period (The reciprocal of pulse frequency in 1 second)<br>
3. The voltage level(such as:0V-5V)<br>
There are 6 PMW interfaces on Arduino, namely digital pin 3, 5, 6, 9, 10, and 11. In previous experiments, we have done "button-controlled LED", using digital signal to control digital pin, also one about potentiometer. This time, we will use a potentiometer to control the brightness of the LED.<br>


'''Hardware Required:'''<br>
'''Introduction''' <br>
1. Potentiometer*1 <br>
As for starters, we will begin with something simple. In this project, you only need an Arduino and a USB cable to start the "Hello World!" experiment. <br>
2. Red M5 LED*1<br>
This is a communication test of your Arduino and PC, also a primer project for you to have your first try of the Arduino world!
3. 220Ω resistor<br>
4. Breadboard*1 <br>
5. Breadboard jumper wires*several<br>
The input of potentiometer is analog, so we connect it to analog port, and LED to PWM port. Different PWM signal can regulate the brightness of the LED.<br>


'''Connection for UNO R3:'''
'''Hardware required'''<br>
<br>[[File:699.png|500px|frameless|thumb]]<br>
* Arduino board x1
* USB cable x1


'''Connection for 2560 R3:'''
'''Sample program'''<br>
<br>[[File:6910.png|500px|frameless|thumb]]<br>
After installing driver for Arduino, let's open Arduino software and compile code that enables Arduino to print "Hello World!" under your instruction. <br>
Of course, you can compile code for Arduino to continuously echo "Hello World!" without instruction. <br>
A simple '''If () statement''' will do the instruction trick. With the onboard LED connected to pin 13, we can instruct the LED to blink first when Arduino gets an instruction and then prints "Hello World!”. <br>


'''Sample Code:'''<br>
In the program compiling process, we will use the analogWrite (PWM interface, analog value) function. In this experiment, we will read the analog value of the potentiometer and assign the value to PWM port, so there will be corresponding change to the brightness of the LED. One final part will be displaying the analog value on the screen. You can consider this as the "analog value reading" project adding the PWM analog value assigning part. Below is a sample program for your reference.
<pre>
<pre>
int potpin=0;// initialize analog pin 0
int val;//define variable val
int ledpin=11;//initialize digital pin 11(PWM output)
int ledpin=13;// define digital interface 13
int val=0;// Temporarily store variables' value from the sensor
void setup()
void setup()
{
{
pinMode(ledpin,OUTPUT);// define digital pin 11 as “output”
  Serial.begin(9600);// set the baud rate at 9600 to match the software set up. When connected to a specific device, (e.g. bluetooth), the baud rate needs to be the same with it.
Serial.begin(9600);// set baud rate at 9600
  pinMode(ledpin,OUTPUT);// initialize digital pin 13 as output. When using I/O ports on an Arduino, this kind of set up is always needed.
// attention: for analog ports, they are automatically set up as “input”
}
}
void loop()
void loop()
{
{
  val=Serial.read();// read the instruction or character from PC to Arduino, and assign them to Val.
  if(val=='R')// determine if the instruction or character received is “R”.
  {  // if it’s “R”,   
    digitalWrite(ledpin,HIGH);// set the LED on digital pin 13 on.
    delay(500);
digitalWrite(ledpin,LOW);// set the LED on digital pin 13 off.    delay(500);
    Serial.println("Hello World!");// display“Hello World!”string.
  }
}
</pre>


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
}</pre>
<br>[[File:6911.png|500px|frameless|thumb]]<br>
'''Result:'''<br>
After downloading the program, when we rotate the potentiometer knob, we can see changes of the displaying value, also obvious change of the LED brightness on the breadboard.


=== '''Project 4: Traffic Light'''===
'''Result Show'''
<br>[[Image:ks0402 1-1.png|700px|frameless]]<br>
<br>[[Image:ks0402 1-2.png|700px|frameless]]<br>
<br>
Done compiling and uploading the code, click to open serial port monitor and set the baud rate to 9600.
Enter an “R” and click Send.
LED 13 will blink once, and your PC will receive information from Arduino: Hello World.
<br>[[Image:ks0402 1-3.png|700px|frameless]]<br>
Now, the experiment is complete, so easy! <br>


'''Introduction:'''
<br>
In the previous program, we have done the LED blinking experiment with one LED. Now, it’s time to up the stakes and do a bit more complicated experiment-traffic lights. Actually, these two experiments are similar. While in this traffic lights experiment, we use 3 LEDs with different color other than 1 LED. <br>
<span style="color: red"> '''Note: if upload failed?''' </span><br>
Check whether you select the correct Board and Port in Tools.
<br>[[Image:ks0402 1-4.png|700px|frameless]]<br>


'''Hardware Required:'''<br>
<br>
1. Arduino board *1<br>
2. USB cable *1<br>
3. Red M5 LED*1 <br>
4. Yellow M5 LED*1<br>
5. Green M5 LED*1<br>
6. 220Ω resistor *3 <br>
7. Breadboard*1 <br>
8. Breadboard jumper wires* several<br>


'''Connection for UNO R3:'''
<br>
<br>[[File:6912.png|500px|frameless|thumb]]<br>


'''Connection for 2560 R3:'''
===Project 2: LED blinking===
<br>[[File:6913.png|500px|frameless|thumb]]<br>


'''Sample Code:'''<br>
'''Introduction'''<br>
Since it is a simulation of traffic lights, the blinking time of each LED should be the same with those in traffic lights system. In this program, we use Arduino delay () function to control delay time, which is much simpler than C language.
Blinking LED experiment is quite simple. In the "Hello World!" program, we have come across LED. This time, we are going to connect an LED to one of the digital pins rather than using the built-in LED13 of UNO board. Except an Arduino and an USB cable, we will need extra parts below.
 
'''Hardware required'''<br>
* Arduino board *1
* Red M5 LED*1
* 220Ω resistor*1
* Breadboard*1
* USB cable *1
* Jumper wire* 2
 
[[Image:ks0402 2-2.png|700px|frameless]]
 
We follow the connection diagram below to connect the components. <br>
Here we use digital pin 10. We connect LED to a 220ohm resistor to avoid high current damaging the LED.
 
'''Connection for UNO R3:'''
<br>[[File:Pro2-1.png|500px|frameless|thumb]]<br>
 
'''Connection for 2560 R3:'''
<br>[[File:Pro2-2.png|500px|frameless|thumb]]<br>
 
 
'''Sample program'''
<pre>
<pre>
int redled =10; // initialize digital pin 8.
int ledPin = 10; // define digital pin 10.
int yellowled =7; // initialize digital pin 7.
int greenled =4; // initialize digital pin 4.
void setup()
void setup()
{
{
pinMode(redled, OUTPUT);// set the pin with red LED as “output”
pinMode(ledPin, OUTPUT);// define pin with LED connected 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()
void loop()
{
{
digitalWrite(greenled, HIGH);//// turn on green LED
digitalWrite(ledPin, HIGH); // set the LED on.
delay(5000);// wait 5 seconds
delay(1000); // wait for a second.
digitalWrite(ledPin, LOW); // set the LED off.
delay(1000); // wait for a second
}
</pre>
 
 
'''Test Result'''<br>
After downloading this program, in the experiment, you will see the LED connected to pin 10 turning on and off, with an interval approximately one second.<br>
The blinking LED experiment is now completed. Thank you!
<br>[[File:Pro2-3.png|500px|frameless|thumb]]<br>


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
}</pre>


'''Result:'''<br>
<br>
When the uploading process is completed, we can see traffic lights of our own design.
Note: this circuit design is very similar with the one in LED chase effect.
The green light will be on for 5 seconds, and then off., followed by the yellow light blinking for 3 times, and then the red light on for 5 seconds, forming a cycle. Cycle then repeats.
Experiment is now completed, thank you.


=== '''Project 5: LED Chasing Effect'''===  
===Project 3: PWM ===


'''Introduction:'''<br>
'''Introduction'''<br>
We often see billboards composed of colorful LEDs. They are constantly changing to form various effects. In this experiment, we compile a program to simulate chase effect.<br>
PWM, short for Pulse Width Modulation, is a technique used to encode analog signal level into digital ones. <br>
A computer cannot output analog voltage but only digital voltage values such as 0V or 5V. So we use a high resolution counter to encode a specific analog signal level by modulating the duty cycle of PMW.<br>
The PWM signal is also digitalized because in any given moment, fully on DC power supply is either 5V (ON), or 0V (OFF). <br>
The voltage or current is fed to the analog load (the device that uses the power) by repeated pulse sequence being ON or OFF. Being on, the current is fed to the load; being off, it's not. <br>


'''Hardware Required:'''<br>
With adequate bandwidth, any analog value can be encoded using PWM. The output voltage value is calculated via the on and off time. <br>
1. Led x6<br>
<span style=color:red> Output voltage = (turn on time/pulse time) * maximum voltage value  </span><br>
2. 220Ω resistor x6 <br>
3. Colorful breadboard wires<br>


'''Connection for UNO R3:'''
<br>[[File:697.png|500px|frameless|thumb]]<br>  
<br>[[File:6914.png|500px|frameless|thumb]]<br>
<br>
PWM has many applications: lamp brightness regulating, motor speed regulating, sound making, etc.


'''Connection for 2560 R3:'''
'''The following are the three basic parameters of PMW:'''
<br>[[File:6915.png|500px|frameless|thumb]]<br>
<br>[[File:698.png|500px|frameless|thumb]]<br>


'''Sample Code:'''<br>
* 1. The amplitude of pulse width (minimum / maximum)
* 2. The pulse period (The reciprocal of pulse frequency in 1 second)
* 3. The voltage level (such as 0V-5V )


<pre>
There are 6 PMW pins on Arduino board, namely digital pin 3, 5, 6, 9, 10, and 11. <br>
int BASE = 2 ;  // the I/O pin for the first LED
In previous experiments, we have done "button-controlled LED", using digital signal to control digital pin. <br>
int NUM = 6;  // number of LEDs
This time, we will use a potentiometer to control the brightness of LED.
[[image:768.png|thumb|700px|right|potentiometer ]]


void setup()
<br>
{
'''Hardware required'''<br>
  for (int i = BASE; i < BASE + NUM; i ++)
* Arduino board *1
  {
* Potentiometer *1
    pinMode(i, OUTPUT);  // set I/O pins as output
* Red M5 LED *1
  }
* 220Ω resistor *1
* Breadboard *1
* USB cable *1
* Breadboard jumper wire * 6
 
The input of potentiometer is analog, so we connect it to analog port, and LED to PWM port. Different PWM signal can regulate the brightness of the LED.<br>
 
<br>
'''Connection for UNO R3:'''
<br>[[File:Ks0402 3-1.png|600px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Ks0402 3-2.png|600px|frameless|thumb]]<br>
 
<br>
'''Sample Code:'''<br>
In the program compiling process, we will use the '''analogWrite''' (PWM interface, analog value) function. <br>
In this experiment, we will read the analog value of the potentiometer and assign the value to PWM port, so there will be corresponding change to the brightness of the LED. <br>
One final part will be displaying the analog value on the monitor window. <br>
You can consider this as the "analog value reading" project adding the PWM analog value assigning part. <br>
Below is a sample program for your reference.


<pre>
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()
void loop()
{
{
  for (int i = BASE; i < BASE + NUM; i ++)
 
  {
val=analogRead(potpin);// read the analog value from the sensor and assign it to val
    digitalWrite(i, LOW);   // set I/O pins as “low”, turn off LEDs one by one.
Serial.println(val);// display value of val
    delay(200);       // delay
analogWrite(ledpin,val/4);// turn on LED and set up brightness(maximum output of PWM is 255)
  }
delay(10);// wait for 0.01 second
  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
  } 
}
}
</pre>
</pre>


'''Result:'''<br>
You can see the LEDs blink by sequence.


=== '''Project 6: Button-controlled LED'''===  
<br>[[File:6911.png|500px|frameless|thumb]]<br>
 
'''Test Result'''<br>
After uploading the program to the board, rotate the potentiometer knob, you can see the changes of displaying value, and also obvious change of LED brightness.
<br>[[File:Pro3-1.png|500px|frameless|thumb]]<br>
<br>
 
<br>
 
===Project 4: Traffic light===


'''Introduction:'''<br>
'''Introduction'''<br>
I/O port means interface for INPUT and OUTPUT. Up until now, we have only used its OUTPUT function. In this experiment, we will try to use the input function, which is to read the output value of device connecting to it. We use 1 button and 1 LED using both input and output to give you a better understanding of the I/O function. Button switches, familiar to most of us, are a switch value (digital value) component. When it's pressed, the circuit is in closed (conducting) state.<br>
In the previous program, we have done the LED blinking experiment with one LED. Now, it’s time to up the stakes and do a bit more complicated experiment-traffic lights. Actually, these two experiments are similar. While in this traffic lights experiment, we use 3 LEDs with different colors rather than 1 LED. <br>


'''Hardware Required:'''<br>
'''Hardware required'''
1. Button switch*1 <br>
* Arduino board *1
2. Red M5 LED*1 <br>
* USB cable *1
3. 220Ω resistor*1<br>
* Red M5 LED*1  
4. 10KΩ resistor*1 <br>
* Yellow M5 LED*1
5. Breadboard*1<br>
* Green M5 LED*1
6. Breadboard jumper wires*several<br>
* 220Ω resistor *3
* Breadboard*1  
* Breadboard jumper wires* several


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6916.png|500px|frameless|thumb]]<br>
<br>[[File:Pro4-1.png|500px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro4-2.png|500px|frameless|thumb]]<br>
 


'''Connection for 2560 R3:'''
'''Sample program'''<br>
<br>[[File:6917.png|500px|frameless|thumb]]<br>
Since it is a simulation of traffic lights, the blinking time of each LED should be the same with those in traffic lights system. <br>
In this program, we use Arduino '''delay ()''' function to control delay time, which is much simpler than C language.<br>


'''Sample Code:'''<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 etc. 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.
<pre>
<pre>
int ledpin=11;// initialize pin 11
int redled =10; // initialize digital pin 8.
int inpin=7;// initialize pin 7
int yellowled =7; // initialize digital pin 7.
int val;// define val
int greenled =4; // initialize digital pin 4.
void setup()
void setup()
{
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(redled, OUTPUT);// set the pin with red LED as “output”
pinMode(inpin,INPUT);// set button pin as “input”
pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output”
 
pinMode(greenled, OUTPUT); // set the pin with green LED as “output”
}
}
void loop()
void loop()
{
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to val
digitalWrite(greenled, HIGH);//// turn on green LED
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
delay(5000);// wait 5 seconds
{ digitalWrite(ledpin,LOW);}
else
{ digitalWrite(ledpin,HIGH);}
}</pre>


'''Result:'''<br>
digitalWrite(greenled, LOW); // turn off green LED
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. You can easily come across it in your every day life. One typical example is when you press a certain key of your phone, the backlight will be on.
for(int i=0;i<3;i++)// blinks for 3 times
 
{
=== '''Project 7: Active Buzzer'''===
delay(500);// wait 0.5 second
 
digitalWrite(yellowled, HIGH);// turn on yellow LED
'''Introduction:'''
delay(500);// wait 0.5 second
Active buzzer is widely used on computer, printer, alarm, electronic toy, telephone, timer etc as a sound making element. It has an inner vibration source. Simply connect it with 5V power supply, it can buzz continuously. <br>
digitalWrite(yellowled, LOW);// turn off yellow LED
 
}
'''Hardware Required:'''<br>
delay(500);// wait 0.5 second
1. Buzzer*1<br>
digitalWrite(redled, HIGH);// turn on red LED
2. Key *1 <br>
delay(5000);// wait 5 seconds
3. Breadboard*1 <br>
digitalWrite(redled, LOW);// turn off red LED
4. Breadboard jumper wires*several<br>
}
</pre>


'''Connection for UNO R3:'''
<br>[[File:6918.png|500px|frameless|thumb]]<br>


'''Connection for 2560 R3:'''
'''Test Result'''<br>
<br>[[File:6919.png|500px|frameless|thumb]]<br>
When the uploading process is completed, you can see your own traffic light design. <br>
When connecting the circuit, pay attention to the positive & the negative poles of the buzzer. In the photo, you can see there are red and black lines. When the circuit is finished, you can begin programming.
The green light will be on for 5 seconds, and then off, followed by the yellow light blinking for 3 times, and then the red light on for 5 seconds, forming a cycle. Cycle then repeats.<br>
Experiment is now completed. Thank you.<br>
<span style="color: red"> '''Note:''' this circuit design is very similar with the following LED chasing effect.</span>
<br>[[File:Pro4-3.png|500px|frameless|thumb]]<br>


'''Sample Code:'''<br>
<br>
Program is simple. You control the buzzer by outputting high/low level. 
<pre>
int buzzer=8;// 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
}</pre>
 
'''Result:'''<br>
After downloading the program, the buzzer experiment is completed. You can see the buzzer is ringing.


=== '''Project 8: Passive Buzzer'''===  
===Project 5: LED chasing ===
[[image:ks0402 5-1.png|thumb|300px|right|LED]]


'''Introduction:'''
'''Introduction'''<br>
We can use Arduino to make many interactive works of which the most commonly used is acoustic-optic display. All the previous experiment has something to do with LED. However, the circuit in this experiment can produce sound. Normally, the experiment is done with a buzzer or a speaker while buzzer is simpler and easier to use. The buzzer we introduced here is a passive buzzer. It cannot be actuated by itself, but by external pulse frequencies. Different frequencies produce different sounds. We can use Arduino to code the melody of a song, which is actually quite fun and simple.  <br>
We often see billboards composed of colorful LEDs. They are constantly changing to form various light effects. In this experiment, we compile a program to simulate LED chasing effect.<br>
The long lead of LED is the positive side; short lead is negative. <br>


'''Hardware Required:'''<br>
'''Hardware required'''<br>
1.Passive buzzer*1<br>
* Led *6
2. Key *1 <br>
* Arduino board *1
3. Breadboard*1 <br>
* 220Ω resistor *6
4. Breadboard jumper wires* several<br>
* Breadboard *1
* USB cable*1
* Breadboard wire *13


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6920.png|500px|frameless|thumb]]<br>
<br>[[File:Pro5-1.png|500px|frameless|thumb]]<br>
 
<br>
'''Connection for 2560 R3:'''
'''Connection for 2560 R3:'''
<br>[[File:6921.png|500px|frameless|thumb]]<br>
<br>[[File:Pro5-2.png|500px|frameless|thumb]]<br>


'''Sample Code:'''<br>


'''Sample Code:'''
<pre>
<pre>
int buzzer=8;// select digital IO pin for the buzzer
int BASE = 2 ; // the I/O pin for the first LED
void setup()  
int NUM = 6;  // number of LEDs
{  
void setup()
pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output  
{
}  
  for (int i = BASE; i < BASE + NUM; i ++)
void loop()  
  {
{ unsigned char i,j;//define variable
    pinMode(i, OUTPUT);   // set I/O pins as output
while(1)
  }
{ for(i=0;i<80;i++)// output a frequency sound
}
{ digitalWrite(buzzer,HIGH);// sound
void loop()
delay(1);//delay1ms
{
digitalWrite(buzzer,LOW);//not sound
  for (int i = BASE; i < BASE + NUM; i ++)  
 
  {
delay(1);//ms delay  
    digitalWrite(i, LOW);   // set I/O pins as “low”, turn off LEDs one by one.
}  
    delay(200);       // delay
 
  }
for(i=0;i<100;i++)// output a frequency sound
  for (int i = BASE; i < BASE + NUM; i ++)  
{  
  {
digitalWrite(buzzer,HIGH);// sound
    digitalWrite(i, HIGH);   // set I/O pins as “high”, turn on LEDs one by one
digitalWrite(buzzer,LOW);//not sound
    delay(200);       // delay
delay(2);//2ms delay  
  } 
}
}
}
}
</pre>
</pre>


'''Result:'''<br>
After downloading the program, the buzzer experiment is completed.


=== '''Project 9: RGB LED'''===
'''Result'''<br>
You can see the LEDs blink by sequence.
<br>[[File:Pro5-3.png|500px|frameless|thumb]]<br>


'''Introduction:'''
<br>
Tricolor principle to display various colors<br>
PWM controlling ports to display full color<br>
Can be driven directly by Arduino PWM interfaces <br>


'''Hardware Required:'''<br>
===Project 6: Button-controlled LED===
Arduino controller × 1<br>
USB cable × 1<br>
'''Introduction''' <br>
Full-color LED module × 1<br>
I/O port means interface for INPUT and OUTPUT. Up until now, we have only used the OUTPUT function.  <br>
In this experiment, we will try to use the input function, which is to read the output value of device. We use 1 button and 1 LED using both input and output to give you a better understanding of the I/O function.  <br>
Button switches, familiar to most of us, are a switch value (digital value) component. When it's pressed, the circuit is in closed (conducting) state.
[[File:Ks0402 6-1.png|200px|frameless|right]]
 
'''Hardware required''' <br>
* Button switch *1
* Red M5 LED *1
* 220Ω resistor *1
* 10KΩ resistor *1
* Breadboard *1
* USB cable *1
* Breadboard jumper wire * 6


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6922.png|500px|frameless|thumb]]<br>
<br>[[File:Pro6-1.png|500px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro6-2.png|500px|frameless|thumb]]<br>
 


'''Connection for 2560 R3:'''
'''Sample program''' <br>
<br>[[File:6923.png|500px|frameless|thumb]]<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. <br>
When pin 7 outputs low level, pin 11 also outputs low level and the LED remains off.


'''Sample Code:'''<br>
<pre>
<pre>
int redpin = 11; //select the pin for the red LED
int ledpin=11;// initialize pin 11
int bluepin =10; // select the pin for the blue LED
int inpin=7;// initialize pin 7
int greenpin =9;// select the pin for the green LED
int val;// define val
 
void setup()
int val;
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”


void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  Serial.begin(9600);
}
}
 
void loop()
void loop()  
{
{
for(val=255; val>0; val--)
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
  analogWrite(11, val);
{ digitalWrite(ledpin,LOW);}
  analogWrite(10, 255-val);
else
  analogWrite(9, 128-val);
{ digitalWrite(ledpin,HIGH);}
  delay(1);
}
  }
</pre>
for(val=0; val<255; val++)
 
  {
  analogWrite(11, val);
  analogWrite(10, 255-val);
  analogWrite(9, 128-val);
  delay(1);  
  }
Serial.println(val, DEC);
}</pre>


'''Result:'''<br>
'''Result''' <br>
Directly copy the above code into arduino IDE, and click upload [[File:690.png|500px|frameless|thumb]], wait a few seconds, you can see a full-color LED
When the button is pressed, LED is on; otherwise, LED remains off. Congrats! 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. You can easily come across it in your every day life.  <br>
One typical example is when you press a certain key of your phone, the backlight will be on.  <br>
<br>[[File:Pro6-3.png|500px|frameless|thumb]]<br>


<br>


=== '''Project 10: Photo Resistor'''===  
===Project 7: Active buzzer===
<br>[[File:Active buzzer.png|200px|frameless|thumb]]<br>


'''Introduction:'''
'''Introduction'''<br>
After completing all the previous experiments, we acquired some basic understanding and knowledge about Arduino application. We have learned digital input and output, analog input and PWM. Now, we can begin the learning of sensors applications.<br>
Active buzzer is a sound making element, widely used on computer, printer, alarm, electronic toy, telephone, timer, etc. <br>
Photo resistor (Photovaristor) is a resistor whose resistance varies according to different incident light strength. 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. Photovaristor is commonly applied in the measurement of light, light control and photovoltaic conversion (convert the change of light into the change of electricity).<br>
It has an inner vibration source. Simply connect it with 5V power supply, it can buzz continuously. <br>
Photo resistor is also being widely applied to various light control circuit, such as light control and adjustment, optical switches etc.We will start with a relatively simple experiment regarding photovaristor application. Photovaristor is an element that changes its resistance as light strenth 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.


'''Hardware Required:'''<br>
'''Hardware required'''<br>
Photo resistor*1 <br>
* Buzzer *1
Red M5 LED*1 <br>
* Arduino board *1
10KΩresistor*1<br>
* Breadboard *1  
220Ωresistor*1<br>
* USB cable *1
Bread board*1<br>
* Breadboard jumper wires * 2
Bread board jumper wires*several<br>


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6924.png|500px|frameless|thumb]]<br>
<br>[[File:Pro7-1.png|500px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro7-2.png|500px|frameless|thumb]]<br>
<br>
When connecting the circuit, pay attention to the positive and negative poles of the buzzer. In the photo, you can see there are red and black lines. When the circuit is finished, you can begin programming. <br>


'''Connection for 2560 R3:'''
'''Sample program'''<br>
<br>[[File:6925.png|500px|frameless|thumb]]<br>
Program is simple. You control the buzzer by outputting high/low level. <br>


'''Sample Code:'''<br>
After the connection, let's begin the program compiling. The program is similar to the one of PWM. For change detail, please refer to the sample program below. 
<pre>
<pre>
int potpin=0;// initialize analog pin 0, connected with photovaristor
int buzzer=8;// initialize digital IO pin that controls the buzzer
int ledpin=11;// initialize digital pin 11, output regulating the brightness of LED
void setup()
int val=0;// initialize variable va
{
void setup()
  pinMode(buzzer,OUTPUT);// set pin mode as “output”
}
void loop()  
{
{
pinMode(ledpin,OUTPUT);// set digital pin 11 as “output”
digitalWrite(buzzer, HIGH); // produce sound
Serial.begin(9600);// set baud rate at “9600”
}
}
void loop()
</pre>
{
val=analogRead(potpin);// read the analog value of the sensor and assign it to val
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
}</pre>


'''Result:'''<br>
After downloading the program, you can change the light strength around the photovaristor and see corresponding brightness change of the LED. Photovaristors has various applications in our everyday life. You can make other interesting interactive projects base on this one. 


'''Result'''<br>
After downloading the program, the buzzer experiment is completed. <br>
You can hear the buzzer is ringing.
<br>[[File:Pro7-3.png|500px|frameless|thumb]]<br>
<br>


=== '''Project 11: Flame Sensor'''===
<br>


'''Introduction:'''
===Project 8: Passive buzzer===
Flame sensor (Infrared receiving triode) is specially used on robots to find the fire source. This sensor is of high sensitivity to flame. Below is a photo of it.
<br>[[File:Passive buzzer.png|250px|frameless|thumb]]<br>  
<br>[[File:6926.png|500px|frameless|thumb]]<br>


'''Working Principle:'''<br>
'''Introduction''' <br>
Flame sensor is made based on the principle that infrared ray is highly sensitive to flame. It has a specially designed infrared receiving tube to detect fire, and then convert the flame brightness to fluctuating level signal. The signals are then input into the central processor and be dealt with accordingly.
We can use Arduino to make many interactive works of which the most commonly used is acoustic-optic display. <br>
All the previous experiment has something to do with LED. However, the circuit in this experiment can produce sound. Normally, the experiment is done with a buzzer or a speaker while buzzer is more simpler and easier to use. <br>
The buzzer we introduced here is a passive buzzer. It cannot be actuated by itself, but by external pulse frequencies. Different frequencies produce different sounds. We can use Arduino to code the melody of a song, which is quite fun and simple.
[[image:ks0402 8-1.png|thumb|600px|right|]]


'''Sensor Connection:'''<br>
'''Hardware required'''<br>
The shorter lead of the receiving triode is for negative, the other one for positive. Connect negative to 5V pin, positive to resistor; connect the other end of the resistor to GND, connect one end of a jumper wire to a clip which is electrically connected to sensor positive, the other end to analog pin. As shown below:
* Passive buzzer*1
<br>[[File:Flame alarm.png|500px|frameless|thumb]]<br>
* Arduino board *1
* Breadboard*1
* USB cable *1
* Breadboard jumper wire * 2


'''Hardware Required:'''<br>
'''Connection for UNO R3:'''<br>
1. Flame sensor *1 <br>
Here we connect the passive buzzer to digital pin 8.
2. Buzzer *1<br>
<br>[[File:Pro8-1.png|500px|frameless|thumb]]<br>
3. 10K resistor x1 <br>
<br>
4. Breadboard jumper wires: several<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro8-2.png|500px|frameless|thumb]]<br>


'''Experiment connection:'''<br>
1)Connecting buzzer:<br>
Connect the controller board, prototype board, breadboard and USB cable according to the Arduino tutorial. Connect the buzzer to digital pin 8.


2)Connecting flame sensor:<br>
'''Sample Code:'''<br>
Connect the sensor to analog pin 0.
<pre>
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
}
}
}
</pre>


'''Connection for UNO R3:'''
'''Result''' <br>
<br>[[File:6928.png|500px|frameless|thumb]]<br>
After downloading the program, buzzer experiment is finished. You can hear the buzzer beep.
<br>[[File:Pro8-3.png|500px|frameless|thumb]]<br>
<br>


'''Connection for 2560 R3:'''
=== Project 9: RGB LED===
<br>[[File:6929.png|500px|frameless|thumb]]<br>
<br>[[File:RGB LED.png|300px|frameless|thumb]]<br>


'''Experiment principle:'''<br>
'''Introduction:''' <br>
When it's approaching a fire, the voltage value the analog port reads differs. If you use a multimeter, you can know when there is no fire approaching, the voltage it reads is around 0.3V; when there is fire approaching, the voltage it reads is around 1.0V, tthe nearer the fire, the higher the voltage.
Tricolor principle to display various colors;<br>
So in the beginning of the program, you can initialize voltage value i (no fire value); Then, continuously read the analog voltage value j and obtain difference value k=j-i; compare k with 0.6V (123 in binary) to determine whether or not there is a fire approaching; if yes, the buzzer will buzz.
PWM controlling ports to display full color;<br>
Can be driven directly by Arduino PWM interfaces. <br>
[[File:Ks0402 9-1.png|700px|right|RGB LED]]


'''Sample Code:'''<br>
'''Hardware Required:'''<br>
<pre>
* Arduino controller × 1<br>
int flame=0;// select analog pin 0 for the sensor
* USB cable × 1<br>
int Beep=9;// select digital pin 9 for the buzzer
* Full-color LED module × 1<br>
int val=0;// initialize variable
void setup()
{
  pinMode(Beep,OUTPUT);// set LED pin as “output”
pinMode(flame,INPUT);// set buzzer pin as “input”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
  val=analogRead(flame);// read the analog value of the sensor
  Serial.println(val);// output and display the analog value
  if(val>=600)// when the analog value is larger than 600, the buzzer will buzz
  { 
  digitalWrite(Beep,HIGH);
  }else
  { 
    digitalWrite(Beep,LOW);
    }
  delay(500);
}</pre>


'''Result:'''<br>
'''Connection for UNO R3:'''
This program can simulate an alarm when there is a fire. Everything is normal when there is no fire; when there is, the alarm will be set off immediately.
<br>[[File:Pro9-1.png|500px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro9-2.png|500px|frameless|thumb]]<br>


=== '''Project 12: LM35 Temperature Sensor'''===


'''Introduction:'''
'''Sample Code:'''<br>
LM35 is a common and easy-to-use temperature sensor. It does not require other hardware. You just need an analog port to make it work. The difficulty lies in compiling the code to convert the analog value it reads to celsius temperature.
<pre>
 
int redpin = 11; //select the pin for the red LED
'''Hardware Required:'''<br>
int bluepin =10; // select the pin for the blue LED
1. LM35*1 <br>
int greenpin =9;// select the pin for the green LED
2. Breadboard*1<br>
3. Breadboard jumper wires*several<br>


'''Connection for UNO R3:'''
int val;
<br>[[File:6930.png|500px|frameless|thumb]]<br>


'''Connection for 2560 R3:'''
void setup() {
<br>[[File:6931.png|500px|frameless|thumb]]<br>
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  Serial.begin(9600);
}


'''Sample Code:'''<br>
void loop()  
<pre>
int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
void setup()
{
{
Serial.begin(9600);// set baud rate at”9600”
for(val=255; val>0; val--)
}
  {
void loop()
  analogWrite(11, val);
{
  analogWrite(10, 255-val);
int val;// define variable
  analogWrite(9, 128-val);
int dat;// define variable
  delay(1);  
val=analogRead(0);// read the analog value of the sensor and assign it to val
  }
dat=(125*val)>>8;// temperature calculation formula
for(val=0; val<255; val++)
Serial.print("Tep:");// output and display characters beginning with Tep
  {
Serial.print(dat);// output and display value of dat
  analogWrite(11, val);
Serial.println("C");// display “C” characters
  analogWrite(10, 255-val);
delay(500);// wait for 0.5 second
  analogWrite(9, 128-val);
}</pre>
  delay(1);  
  }
Serial.println(val, DEC);
}
</pre>
 


'''Result:'''<br>
'''Result:'''<br>
After downloading the program, you can open the monitoring window to see current temperature.
Directly copy the above code into arduino IDE, and click upload [[File:690.png|500px|frameless|thumb]], wait for a few seconds, you can see a full-color LED.
<br>[[File:6932.png|500px|frameless|thumb]]<br>
<br>[[File:Pro9-3.png|500px|frameless|thumb]]<br>
 
<br>


=== '''Project 13: Tilt Switch'''===  
=== Project 10: Photo Resistor===  
<br>[[File:Photo resistor.png|500px|frameless|thumb]]<br>


'''Introduction:'''
'''Introduction:''' <br>
Tilt switch controlling the ON and OFF of LED
After completing all the previous experiments, you may acquire some basic understanding and knowledge about Arduino application. We have introduced digital input and output, analog input and PWM. Now, let's begin the learning of sensors applications.<br>
Photo resistor (Photovaristor) is a resistor whose resistance varies from the different incident light strength. 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.  <br>
Photovaristor is commonly applied in the measurement of light, light control and photovoltaic conversion (convert the change of light into the change of electricity).<br>
Photo resistor is also widely applied to various light control circuit, such as light control and adjustment, optical switches, etc.  <br>
We will start with a relatively simple experiment regarding to photovaristor application. Photovaristor is an element that will change its resistance as light strength changes. So need to read the analog values.  <br>
You can refer to the PWM experiment, replacing the potentiometer with photovaristor. When there is change in light strength, it will cause corresponding change on the LED. <br>


'''Hardware Required:'''<br>
'''Hardware Required:'''<br>
1. Ball switch*1<br>
* Photo resistor*1 <br>
2. Led *1 <br>                                                                                                                                                                                                                                                                                  
* Red M5 LED*1 <br>
3. 220Ω resistor*1<br>
* 10KΩresistor*1<br>
4. 10KΩ resistor*1<br>
* 220Ωresistor*1<br>
4. Breadboard jumper wires:several<br>
* Breadboard*1<br>
* Breadboard jumper wires*several<br>


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6933.png|500px|frameless|thumb]]<br>
<br>[[File:Pro10-1.png|500px|frameless|thumb]]<br>
 
<br>
'''Connection for 2560 R3:'''
'''Connection for 2560 R3:'''
<br>[[File:6934.png|500px|frameless|thumb]]<br>
<br>[[File:Pro10-2.png|500px|frameless|thumb]]<br>


Connect the controller board, shield, breadboard and USB cable according to Arduino tutorial. Connect the LED to digital pin 8, ball switch to analog pin 5.
'''Sample Code:'''<br>
After the connection, let's begin the program compiling. The program is similar to the one of PWM. <br>
For change detail, please refer to the sample program below. <br>


'''Experiment Principle:'''
When one end of the switch is below horizontal position, the switch is on. The voltage of the analog port is about 5V (1023 in binary). The LED will be on. When the other end of the switch is below horizontal position, the switch is off. The voltage of the analog port is about 0V (0 in binary). The LED will be off. In the program, we determine whether the switch is on or off according to the voltage value of the analog port, whether it's above 2.5V (512 in binary) or not.
'''Sample Code:'''<br>
<pre>
<pre>
void setup()  
int potpin=0;// initialize analog pin 0, connected with photovaristor
{  
int ledpin=11;// initialize digital pin 11, output regulating the brightness of LED
  pinMode(8,OUTPUT);// set digital pin 8 as “output”  
int val=0;// initialize variable va
}  
void setup()
void loop()  
{
{  
pinMode(ledpin,OUTPUT);// set digital pin 11 as “output”
int i;// define variable i
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(potpin);// read the analog value of the sensor and assign it to val
Serial.println(val);// display the value of val
analogWrite(ledpin,val/4);// turn on the LED and set up brightness(maximum output value 255)
delay(10);// wait for 0.01
}
</pre>


while(1)
'''Result:'''<br>
{
After downloading the program, you can change the light strength around the photovaristor and see corresponding brightness change of the LED. <br> Photovaristors has various applications in our everyday life. You can make other interesting interactive projects based on this one.
  i=analogRead(5);// read the voltage value of analog pin 5
<br>[[File:Pro10-3.png|500px|frameless|thumb]]<br>
  if(i>512)// if larger that 512(2.5V)
<br>
  {
    digitalWrite(8,LOW);// turn on LED
  }
  else// otherwise
  {
    digitalWrite(8,HIGH);// turn off LED
  }
}
}
</pre>


'''Result:'''<br>
<br>
Hold the breadboard with your hand. Tilt it to a certain extent, the LED will be on.
=== Project 11: Flame Sensor===
If there is no tilt, the LED will be off.
The principle of this experiment can be applied to relay control.
Experiment completed.
Thank you!


'''Introduction:''' <br>
Flame sensor (Infrared receiving triode) is specially used on robots to find the fire source. This sensor is of high sensitivity to flame.
[[File:Pro11-1.png|250px|frameless|right]]


=== '''Project 14: IR Remote Control'''===
'''Working Principle:'''<br>
Flame sensor is based on the principle that infrared ray is highly sensitive to flame. It has an infrared receiving tube specially designed to detect fire, and then convert the flame brightness into fluctuating level signal. The signals are then input into the central processor and be dealt with accordingly.


'''Introduction:'''
'''Sensor Connection:'''<br>
What is an infrared receiver?<br>
The shorter lead of the receiving triode is for negative, the other one for positive. Connect negative to 5V pin, positive to resistor; connect the other end of the resistor to GND, connect one end of a jumper wire to a clip which is electrically connected to sensor positive, the other end to analog pin. As shown below:
The signal from the infrared remote controller is a series of binary pulse code. To avoid interference from other infrared signals during the wireless transmission, the signal is pre-modulate at a specific carrier frequency and then send out by a infrared emission diode. The infrared receiving device needs to filter out other wave and receive signal at that specific frequency and modulate it back to binary pulse code, known as demodulation.<br>
<br>[[File:Flame alarm.png|500px|frameless|thumb]]<br>
 
'''Working Principal:'''<br>
The built-in receiver converts the light signal it received from the sender into feeble electrical signal. The signal will be amplified by the IC amplifier. After automatic gain control, band-pass filtering, demodulation, wave shaping, it returns to the original code. The code is then input to the code identification circuit by the receiver's signal output pin.
The pin and the connection of the infrared receiving head.<br>
Pin and wiring of infrared receiver:<br>
<br>[[File:6935.png|500px|frameless|thumb]]<br>
 
Infrared receiver has 3 pins.
When you use it, connect VOUT to analog pin, GND to GND, VCC to +5V.
2. Infrared remote control experiment


'''Hardware Required:'''<br>
'''Hardware Required:'''<br>
Infrared remote controller x1<br>
* Flame sensor *1
Infrared receiver x1 <br>
* Control board *1
LED x6<br>
* Buzzer *1
220Ω resistor x6 <br>
* 10K resistor *1
Multi-color breadboard wires x several<br>
* USB cable *1
* Breadboard jumper wire * 6


'''Connection:'''
'''Experiment Connection:'''<br>
1)Connecting buzzer:<br>
Connect the controller board, prototype board, breadboard and USB cable according to the Arduino tutorial. Connect the buzzer to digital pin 8.


First, connect the controller board; then connect the infrared receiver as the above mentioned, connect VOUT to digital pin 11, connect the LEDs with resistors and connect the resisters to pin 2,3,4,5,6,7. 
2)Connecting flame sensor:<br>
Connect the sensor to analog pin 0.


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6936.png|500px|frameless|thumb]]<br>
<br>[[File:Pro11-2.png|500px|frameless|thumb]]<br>
 
<br>
'''Connection for 2560 R3:'''
'''Connection for 2560 R3:'''
<br>[[File:6937.png|500px|frameless|thumb]]<br>
<br>[[File:Pro11-3.png|500px|frameless|thumb]]<br>


'''Experimental Principle:'''
<br>
If you want to decode code of a remote controller, you must first know how it's coded. The coding method we use here is NEC protocol. Below is a brief introduction.
'''Experiment Principle:'''<br>
·NEC protocol:<br>
When it's approaching a fire, the voltage value the analog port reads differs. If you use a multimeter, when there is no fire approaching, the voltage it reads is around 0.3V; when there is fire approaching, the voltage it reads is around 1.0V. The nearer the fire is, the higher the voltage is. <br>
Features:<br>
So in the beginning of the program, you can initialize voltage value '''i''' (no fire value); Then, continuously read the analog voltage value '''j''' and obtain difference value '''k=j-i'''; compare k with 0.6V (123 in binary) to determine whether there is a fire approaching or not; if yes, the buzzer will buzz. <br>
(1) 8 bit address and 8 bit command length<br>
(2) address and command are transmitted twice for reliability<br>
(3) pulse distance modulation<br>
(4) carrier frequency of 38 KHZ <br>
(5) bit time of 1.125ms or 2.25ms<br>
Protocol is as below:<br>
* Definition of logical 0 and 1 is as below


<br>[[File:6938.png|500px|frameless|thumb]]<br>


*Pulse transmitted when button is pressed and immediately released.
'''Sample Code:'''<br>
<br>[[File:6939.png|500px|frameless|thumb]]<br>
<pre>
int flame=0;// select analog pin 0 for the sensor
int Beep=9;// select digital pin 9 for the buzzer
int val=0;// initialize variable
void setup()
{
  pinMode(Beep,OUTPUT);// set LED pin as “output”
pinMode(flame,INPUT);// set buzzer pin as “input”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
  val=analogRead(flame);// read the analog value of the sensor
  Serial.println(val);// output and display the analog value
  if(val>=600)// when the analog value is larger than 600, the buzzer will buzz
  { 
  digitalWrite(Beep,HIGH);
  }else
  { 
    digitalWrite(Beep,LOW);
    }
  delay(500);
}
</pre>


The picture above shows a typical pulse train of the NEC protocol. With this protocol the LSB is transmitted first. In this case Address $59 and Command $16 is transmitted. A message is started by a 9ms AGC burst, which was used to set the gain of the earlier IR receivers. This AGC burst is then followed by a 4.5ms space, which is then followed by the address and command. Address and Command are transmitted twice. The second time all bits are inverted and can be used for verification of the received message. The total transmission time is constant because every bit is repeated with its inverted length. If you are not interested in this reliability, you can ignore the inverted values, or you can expend the Address and Command to 16 bits each! 
* Pulse transmitted when button is pressed and released after a period of time
<br>[[File:6940.png|500px|frameless|thumb]]<br>


A command is transmitted only once, even when the key on the remote control remains pressed. Every 110ms a repeat code is transmitted for as long as the key remains down. This repeat code is simply a 9ms AGC pulse followed by a 2.25ms space and a 560µs burst. 
'''Result:'''<br>
This program can simulate an alarm when there is a fire. Everything is normal when there is no fire; when there is fire, the alarm will be set off immediately.
<br>[[File:Pro11-4.png|500px|frameless|thumb]]<br>


*Repeat pulse
<br>
<br>[[File:6941.png|500px|frameless|thumb]]<br>
 
=== Project 12: LM35 Temperature Sensor===
 
[[File:Ks0402 12-1.png|500px|frameless|right]]
 
'''Introduction:''' <br>
LM35 is a common and easy-to-use temperature sensor. It does not require other hardware. You just need an analog port to make it work. The difficulty lies in compiling the code to convert the analog value it reads to celsius temperature.
<br>[[File:LM35.png|500px|frameless|thumb]]<br>
 
'''Hardware Required:'''<br>
1.LM35*1  <br>
2.Control board *1 <br>
3.Breadboard*1  <br>
4.USB cable *1  <br>
5.Breadboard jumper wire *5  <br>
 
'''Connection for UNO R3:'''
<br>[[File:Pro12-1.png|500px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro12-2.png|500px|frameless|thumb]]<br>


Note: when the pulse enters the integrated receiver, there will be decoding, signal amplifying and wave shaping process. So you need to make sure the level  of the output is just the opposite from that of the signal sending end. That is when there is no infrared signal, the output end is in high level; when there is infrared signal, the output end is in low level. You can see the pulse of the receiving end in the oscilloscope. Try to better understand the program base on what you see.


'''Sample Code:'''<br>
'''Sample Code:'''<br>
<pre>
<pre>
#include <IRremote.h>
int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
int RECV_PIN = 11;
void setup()
int LED1 = 2;
{
int LED2 = 3;
Serial.begin(9600);// set baud rate at”9600”
int LED3 = 4;
}
int LED4 = 5;
void loop()
int LED5 = 6;
{
int LED6 = 7;
int val;// define variable
long on1  = 0x00FFA25D;
int dat;// define variable
long off1 = 0x00FFE01F;
val=analogRead(0);// read the analog value of the sensor and assign it to val
long on2 = 0x00FF629D;
dat=(125*val)>>8;// temperature calculation formula
long off2 = 0x00FFA857;
Serial.print("Tep:");// output and display characters beginning with Tep
long on3 = 0x00FFE21D;
Serial.print(dat);// output and display value of dat
long off3 = 0x00FF906F;
Serial.println("C");// display “C” characters
long on4 = 0x00FF22DD;
delay(500);// wait for 0.5 second
long off4 = 0x00FF6897;
}
long on5 = 0x00FF02FD;
</pre>
long off5 = 0x00FF9867;
 
long on6 = 0x00FFC23D;
'''Result:'''<br>
long off6 = 0x00FFB047;
After downloading the program, you can open the monitoring window to see the current temperature.
IRrecv irrecv(RECV_PIN);
<br>[[File:6932.png|700px|frameless|thumb]]<br>
decode_results results;
 
// Dumps out the decode_results structure.
 
// Call this after IRrecv::decode()
<br>
// void * to work around compiler issue
 
//void dump(void *v) {
=== Project 13: Tilt Switch===  
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN)
    {
    Serial.println("Could not decode message");


    }
'''Introduction:''' <br>
  else
Tilt switch controlling the LED ON and OFF.
  {
[[File:Tilt switch.png|250px|right|Ball Tilt Switch]]
    if (results->decode_type == NEC)
 
      {
'''Hardware Required:'''<br>
      Serial.print("Decoded NEC: ");
1.Ball switch*1 <br>
      }
2.Control board *1  <br>
    else if (results->decode_type == SONY)
3.Led *1  <br>                                                                                                                                                                                                                                                                   
      {
4.220Ω resistor*1  <br>
      Serial.print("Decoded SONY: ");
5.10KΩ resistor*1  <br>
      }
6.USB cable *1  <br>  
    else if (results->decode_type == RC5)
7.Breadboard jumper wire *5  <br>
      {
      Serial.print("Decoded RC5: ");
      }
    else if (results->decode_type == RC6)
      {
      Serial.print("Decoded RC6: ");
      }
    Serial.print(results->value, HEX);
    Serial.print(" (");
    Serial.print(results->bits, DEC);
    Serial.println(" bits)");
  }
    Serial.print("Raw (");
    Serial.print(count, DEC);
    Serial.print("): ");


  for (int i = 0; i < count; i++)
'''Connection for UNO R3:'''<br>
    {
Connect the ball tilt switch, LED and resistors to control board. Connect the LED to digital pin 8, ball switch to analog pin 5.
      if ((i % 2) == 1) {
<br>[[File:Pro13-1.png|500px|frameless|thumb]]<br>
      Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
<br>
    }
'''Connection for 2560 R3:'''
    else 
<br>[[File:Pro13-2.png|500px|frameless|thumb]]<br>
    {
      Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
    }
    Serial.print(" ");
    }
      Serial.println("");


    }
<br>
'''Experiment Principle:'''<br>
When one end of the switch is below horizontal position, the switch is on. The voltage of the analog port is about 5V (1023 in binary). The LED will be on.<br>
When the other end of the switch is below horizontal position, the switch is off. The voltage of the analog port is about 0V (0 in binary). The LED will be off. <br>
In the program, we determine whether the switch is on or off according to the voltage value of the analog port, whether it's above 2.5V (512 in binary) or not. <br>
 
'''Sample Code:'''<br>
<pre>
void setup()
{
  pinMode(8,OUTPUT);// set digital pin 8 as “output”
}  
void loop()
{
int i;// define variable i
while(1)
{
  i=analogRead(5);// read the voltage value of analog pin 5
  if(i>512)// if larger that 512(2.5V)
  {
    digitalWrite(8,LOW);// turn on LED
  }
  else// otherwise
  {
    digitalWrite(8,HIGH);// turn off LED
  }
}
}
</pre>


void setup()
{
  pinMode(RECV_PIN, INPUT); 
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT); 
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}


int on = 0;
'''Result:'''<br>
unsigned long last = millis();
Hold the breadboard with your hand. Tilt it to a certain extent, the LED will be on. If there is no tilt, the LED will be off.<br>
The principle of this experiment can be applied to relay control. <br>
Experiment is completed.Thank you!
<br>[[File:Pro13-3.png|500px|frameless|thumb]]<br>
<br>


void loop()
===Project 14: IR Remote Control===
{
 
  if (irrecv.decode(&results))
<br>[[File:ks0077 14-1.png|300px|frameless|thumb]]<br>
  {
 
    // If it's been at least 1/4 second since the last
'''Introduction:'''<br>
    // IR received, toggle the relay
What is an infrared receiver? <br>
    if (millis() - last > 250)
The signal from the infrared remote controller is a series of binary pulse code. To avoid interference from other infrared signals during the wireless transmission, the signal is pre-modulate at a specific carrier frequency and then send out by an infrared emission diode. The infrared receiving device needs to filter out other wave and receive signal at that specific frequency and modulate it back to binary pulse code, known as demodulation.<br>
      {
      on = !on;
//      digitalWrite(8, on ? HIGH : LOW);
      digitalWrite(13, on ? HIGH : LOW);
      dump(&results);
      }
    if (results.value == on1 )
      digitalWrite(LED1, HIGH);
    if (results.value == off1 )
      digitalWrite(LED1, LOW);
    if (results.value == on2 )
      digitalWrite(LED2, HIGH);
    if (results.value == off2 )
      digitalWrite(LED2, LOW);


    if (results.value == on3 )
'''Working Principle:'''<br>
      digitalWrite(LED3, HIGH);
The built-in receiver converts the light signal it received from the sender into feeble electrical signal. The signal will be amplified by the IC amplifier. After automatic gain control, band-pass filtering, demodulation, wave shaping, it returns to the original code. The code is then input to the code identification circuit by the receiver's signal output pin.<br>
    if (results.value == off3 )
The pin and the connection of the infrared receiving head.<br>
      digitalWrite(LED3, LOW);
    if (results.value == on4 )
      digitalWrite(LED4, HIGH);
    if (results.value == off4 )
      digitalWrite(LED4, LOW);
    if (results.value == on5 )
      digitalWrite(LED5, HIGH);
    if (results.value == off5 )
      digitalWrite(LED5, LOW);
    if (results.value == on6 )
      digitalWrite(LED6, HIGH);
    if (results.value == off6 )
      digitalWrite(LED6, LOW);       
    last = millis();     
    irrecv.resume(); // Receive the next value
  }}
</pre>


Program Function<br>
'''Pin and wiring of infrared receiver:'''
<br>[[File:ks0077 14-2.png|500px|frameless|thumb]]<br>


Decode the coded pulse signal emitted by the remote controller; execute corresponding action according to the results of the decoding. In this way, you will be able to control your device with remote controller. 
'''Infrared receiver has 3 pins.''' <br>
When you use it, connect VOUT to analog pin, GND to GND, VCC to +5V.
<br>[[File:Pro14-4.png|500px|frameless|thumb]]<br>


'''Result:'''<br>
<br>
Screen Shot <br>
'''Hardware Required:'''<br>
<br>[[File:6942.png|500px|frameless|thumb]]<br>
* Infrared remote controller x1
* Infrared receiver x1
* LED x6
* 220Ω resistor x6
* Breadboard wire x 10


Note:add IRremote folder into installation directory \Arduino\compiler libraries, or you will not be able to compile.
For example:C:\Program Files\Arduino\libraries


'''Connection Diagram:''' <br>
First, connect the controller board; then connect the infrared receiver as the above mentioned, connect VOUT to digital pin 11, connect the LEDs with resistors and connect the resistors to pin2,3,4,5,6,7.  <br>


=== '''Project 15: Analog Value Reading'''===
'''Connection for UNO R3:'''
<br>[[File:Pro14-1.png|500px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro14-2.png|500px|frameless|thumb]]<br>


'''Introduction:'''
<br>
In this experiment, we will begin the learning of analog I/O interfaces. On an Arduino, there are 6 analog interfaces numbered from 0 to 5. These 6 interfaces can also be used as digital ones numbered as 14-19. After a brief introduction, let's begin our project. Potentiometer used here is a typical output component of analog value that is familiar to us. <br>
'''Experimental Principle:'''<br>
If you want to decode the code from a remote controller, you must first know how it's coded. The coding method we use here is NEC protocol. Below is a brief introduction. <br>


'''Hardware Required:'''<br>
'''NEC protocol:''' <br>
1.Potentiometer *1 <br>
'''Features:''' <br>
2.Breadboard*1<br>
(1) 8 bit address and 8 bit command length <br>
3.Breadboard jumper wires * several<br>
(2) address and command are transmitted twice for reliability <br>
(3) pulse distance modulation <br>
(4) carrier frequency of 38 KHZ <br>
(5) bit time of 1.125ms or 2.25ms<br>


'''Connection:'''<br>
Protocol is as below: <br>
In this experiment, we will convert the resistance value of the potentiometer to analog ones and display it on the screen. This is an application we need to master well for our future experiments.<br>  
*Definition of logical 0 and 1 is as below
Connection circuit as below:<br>
<br>[[File:6938.png|700px|frameless|thumb]]<br>


'''Connection for UNO R3:'''
*Pulse transmitted when button is pressed and immediately released.
<br>[[File:6943.png|500px|frameless|thumb]]<br>
<br>[[File:6939.png|700px|frameless|thumb]]<br>


'''Connection for 2560 R3:'''
The picture above shows a typical pulse train of the NEC protocol. With this protocol the LSB is transmitted first. In this case Address $59 and Command $16 is transmitted. A message is started by a 9ms AGC burst, which was used to set the gain of the earlier IR receivers. <br>
<br>[[File:6944.png|500px|frameless|thumb]]<br>
This AGC burst is then followed by a 4.5ms space, which is then followed by the address and command. Address and Command are transmitted twice.
The second time all bits are inverted and can be used for verification of the received message. The total transmission time is constant because every bit is repeated with its inverted length. If you are not interested in this reliability, you can ignore the inverted values, or you can expand the Address and Command to 16 bits each!  <br>


We use the analog interface 0.
* Pulse transmitted when button is pressed and released after a period of time
The analog interface we use here is interface 0.
<br>[[File:6940.png|600px|frameless|thumb]]<br>


'''Sample Code:'''<br>
A command is transmitted only once, even when the key on the remote control remains pressed. Every 110ms a repeat code is transmitted for as long as the key remains down. This repeat code is simply a 9ms AGC pulse followed by a 2.25ms space and a 560µs burst.


The program compiling is simple. An analogRead () Statement can read the value of the interface. The A/D acquisition of Arduino 328 is in 10 bits, so the value it reads is among 0 to 1023. One difficulty in this project is to display the value on the screen, which is actually easy to learn. First, we need to set the baud rate in voidsetup (). Displaying the value is a communication between Arduino and PC, so the baud rate of the Arduino should match the the one in the PC's software set up. Otherwise, the display will be messy codes or no display at all. In the lower right corner of the Arduino software monitor window, there is a button for baud rate set up. The set up here needs to match the one in the program. The statement in the program is Serial.begin(); enclosed is the baud rate value, followed by statement for displaying. You can either use Serial.print() or Serial.println() statement. 
*Repeat pulse
<pre>
<br>[[File:6941.png|700px|frameless|thumb]]<br>
int potpin=0;// initialize analog pin 0
int ledpin=13;// initialize digital pin 13
int val=0;// define val, assign initial value 0
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin as “output”
Serial.begin(9600);// set baud rate at 9600
 
}
void loop()
{
digitalWrite(ledpin,HIGH);// turn on the LED on pin 13
delay(50);// wait for 0.05 second
digitalWrite(ledpin,LOW);// turn off the LED on pin 13
delay(50);// wait for 0.05 second
val=analogRead(potpin);// read the analog value of analog pin 0, and assign it to val
Serial.println(val);// display val’s value
}</pre>


'''Result:'''<br>
<span style=color:red>'''Note:''' when the pulse enters the integrated receiver, there will be decoding, signal amplifying and wave shaping process. So you need to make sure the level of the output is just the opposite from that of the signal sending end. That is when there is no infrared signal, the output end is in high level; when there is infrared signal, the output end is in low level.  
The sample program uses the built-in LED connected to pin 13. Each time the device reads a value, the LED blinks.<br>
You can see the pulse of the receiving end in the oscilloscope. Try to better understand the program base on what you see. </span><br>
Below is the analog value it reads.<br>
<br>[[File:6945.png|500px|frameless|thumb]]<br>


When you rotate the potentiometer knob, you can see the displayed value changes. The reading of analog value is a very common function since most sensors output analog value. After calculation, we can have the corresponding value we need.
<br>
The experiment is now completed, thank you.
'''Sample Code:'''<br>
<span style=color:red>'''Note:''' add IRremote folder into installation directory \Arduino\compiler libraries, or it will fail to compile. <br>
For example:C:\Program Files\Arduino\libraries


<pre>
#include <IRremote.h>
int RECV_PIN = 11;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
long on1  = 0x00FF6897;
long off1 = 0x00FF9867;
long on2 = 0x00FFB04F;
long off2 = 0x00FF30CF;
long on3 = 0x00FF18E7;
long off3 = 0x00FF7A85;
long on4 = 0x00FF10EF;
long off4 = 0x00FF38C7;
long on5 = 0x00FF5AA5;
long off5 = 0x00FF42BD;
long on6 = 0x00FF4AB5;
long off6 = 0x00FF52AD;
IRrecv irrecv(RECV_PIN);
decode_results results;
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN)
    {
    Serial.println("Could not decode message");
    }
  else
  {
    if (results->decode_type == NEC)
      {
      Serial.print("Decoded NEC: ");
      }
    else if (results->decode_type == SONY)
      {
      Serial.print("Decoded SONY: ");
      }
    else if (results->decode_type == RC5)
      {
      Serial.print("Decoded RC5: ");
      }
    else if (results->decode_type == RC6)
      {
      Serial.print("Decoded RC6: ");
      }
    Serial.print(results->value, HEX);
    Serial.print(" (");
    Serial.print(results->bits, DEC);
    Serial.println(" bits)");
  }
    Serial.print("Raw (");
    Serial.print(count, DEC);
    Serial.print("): ");
for (int i = 0; i < count; i++)
    {
      if ((i % 2) == 1) {
      Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
    }
    else 
    {
      Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
    }
    Serial.print(" ");
    }
      Serial.println("");
    }
void setup()
{
  pinMode(RECV_PIN, INPUT); 
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT); 
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}
int on = 0;
unsigned long last = millis();


=== '''Project 16: 74HC595'''===
void loop()  
 
'''Introduction:'''
To put it simply, 74HC595 is a combination of 8-digit shifting register, memorizer and equipped with tri-state output. Here, we use it to control 8 LEDs. You may wonder why use a 74HC595 to control LED? Well, think about how many I/O it takes for an Arduino to control 8 LEDs? Yes, 8. For an Arduino 168, it has only 20 I/O including analog ports. So, to save port resources, we use 74HC595 to reduce the number of ports it needs. Using 74HC595 enables us to use 3 digital I/O port to control 8 LEDs! <br>
 
'''Hardware Required:'''<br>
1.74HC595 chip*1<br>
2.Red M5 LED*4 <br>
3.Green M5 LED*4<br>
4.220Ω resistor*8<br>
5.Breadboard*1 <br>
6.Breadboard jumper wires*several<br>
Note: for pin 13 OE port of 74HC595, it needs to connect to GND<br>
 
'''Connection for UNO R3:'''
<br>[[File:6946.png|500px|frameless|thumb]]<br>
 
'''Connection for 2560 R3:'''
<br>[[File:6947.png|500px|frameless|thumb]]<br>
 
The circuit may seem complicated, but once you give it a good look, you will find it easy!
 
'''Sample Code:'''<br>
 
<pre>
int data = 2;// set pin 14 of 74HC595as data input pin SI
int clock = 5;// set pin 11 of 74hc595 as clock pin SCK
int latch = 4;// set pin 12 of 74hc595 as output latch RCK
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
void loop()
{
for(int i = 0; i < 256; i++)
{
updateLEDs(i);
delay(500);
}
}
void updateLEDs(int value)
{
digitalWrite(latch, LOW);//
shiftOut(data, clock, MSBFIRST, ~value);// serial data “output”, high level first
digitalWrite(latch, HIGH);// latch
}
</pre>
 
'''Result:'''<br>
After downloading the program, you can see 8 LEDs displaying 8-bit binary number.
 
 
=== '''Project 17: 1-digit LED Segment Display'''===
 
'''Introduction:'''
LED segment displays are common for displaying numerical information. It's widely applied on displays of electromagnetic oven, full automatic washing machine, water temperature display, electronic clock etc. It is necessary that we learn how it works.<br>
LED segment display is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED). LED segment display can be divided into 7-segment display and 8-segment display according to the number of segments. 8-segment display has one more LED unit ( for decimal point display) than 7-segment one. In this experiment, we use a 8-segment display. According to the wiring method of LED units, LED segment displays can be divided into display with common anode and display with common cathode. Common anode display refers to the one that combine all the anodes of LED units into one common anode (COM).<br>
For the common anode display, connect the common anode (COM) to +5V. When the cathode level of a certain segment is low, the segment is on; when the cathode level of a certain segment is high, the segment is off. For the common cathode display, connect the common cathode (COM) to GND. When the anode level of a certain segment is high, the segment is on; when the anode level of a certain segment is low, the segment is off.<br>
 
<br>[[File:1-digit 7-seg 英.png|500px|frameless|thumb]]<br>
 
 
Each segment of the display consists of an LED.  So when you use it, you also need use a current-limiting resistor. Otherwise, LED will be burnt out. In this experiment, we use a common cathode display. As we mentioned above, for common cathode display, connect the common cathode (COM) to GND. When the anode level of a certain segment is high, the segment is on; when the anode level of a certain segment is low, the segment is off.
 
'''Hardware Required:'''<br>
1.Eight-segment display*1 <br>
2.220Ω resistor*8<br>
3.Breadboard*1 <br>
4.Breadboard jumper wires*several<br>
 
'''Connection :'''<br>
Refer to below connection diagram for circuit connection
 
'''Connection for UNO R3:'''
<br>[[File:6949.png|500px|frameless|thumb]]<br>
 
'''Connection for 2560 R3:'''
<br>[[File:6950.png|500px|frameless|thumb]]<br>
 
'''Sample Code:'''<br>
There are seven segments for numerical display, one for decimal point display. Corresponding segments will be turned on when displaying certain numbers. For example, when displaying number 1, b and c segments will be turned on. We compile a subprogram for each number, and compile the main program to display one number every 2 seconds, cycling display number 0 ~ 9. The displaying time for each number is subject to the delay time, the longer the delay time, the longer the displaying time. 
<pre>
// set the IO pin for each segment
int a=7;// set digital pin 7 for segment a
int b=6;// set digital pin 6 for segment b
int c=5;// set digital pin 5 for segment c
int d=10;// set digital pin 10 for segment d
int e=11;// set digital pin 11 for segment e
int f=8;// set digital pin 8 for segment f
int g=9;// set digital pin 9 for segment g
int dp=4;// set digital pin 4 for segment dp
 
void digital_0(void) // display number 5
 
{
{
unsigned char j;
  if (irrecv.decode(&results))
digitalWrite(a,HIGH);
  {
digitalWrite(b,HIGH);
    // If it's been at least 1/4 second since the last
digitalWrite(c,HIGH);
    // IR received, toggle the relay
digitalWrite(d,HIGH);
    if (millis() - last > 250)
digitalWrite(e,HIGH);
      {
digitalWrite(f,HIGH);
      on = !on;
digitalWrite(g,LOW);
//      digitalWrite(8, on ? HIGH : LOW);
digitalWrite(dp,LOW);
      digitalWrite(13, on ? HIGH : LOW);
}
      dump(&results);
void digital_1(void) // display number 1
      }
{
    if (results.value == on1 )
unsigned char j;
      digitalWrite(LED1, HIGH);
digitalWrite(c,HIGH);// set level as “high” for pin 5, turn on segment c
    if (results.value == off1 )
digitalWrite(b,HIGH);// turn on segment b
      digitalWrite(LED1, LOW);  
for(j=7;j<=11;j++)// turn off other segments
    if (results.value == on2 )
digitalWrite(j,LOW);
      digitalWrite(LED2, HIGH);
digitalWrite(dp,LOW);// turn off segment dp
    if (results.value == off2 )
      digitalWrite(LED2, LOW);  
    if (results.value == on3 )
      digitalWrite(LED3, HIGH);
    if (results.value == off3 )
      digitalWrite(LED3, LOW);
    if (results.value == on4 )
      digitalWrite(LED4, HIGH);
    if (results.value == off4 )
      digitalWrite(LED4, LOW);  
    if (results.value == on5 )
      digitalWrite(LED5, HIGH);
    if (results.value == off5 )
      digitalWrite(LED5, LOW);
    if (results.value == on6 )
      digitalWrite(LED6, HIGH);
    if (results.value == off6 )
      digitalWrite(LED6, LOW);      
    last = millis();     
irrecv.resume(); // Receive the next value
  }
}
}
void digital_2(void) // display number 2
</pre>
{
 
unsigned char j;
<br>
digitalWrite(b,HIGH);
'''Program Function:'''<br>
digitalWrite(a,HIGH);
Decode the coded pulse signal emitted by the remote controller, then execute corresponding action according to the results of the decoding. <br>
for(j=9;j<=11;j++)
In this way, you are able to control your device with remote control.
digitalWrite(j,HIGH);
 
digitalWrite(dp,LOW);
<br>
digitalWrite(c,LOW);
'''Test Result:'''
digitalWrite(f,LOW);
<br>[[File:Pro14-3.png|600px|frameless|thumb]][[File:Ks0402 14-1.png|600px|frameless|thumb]]<br>
}
 
void digital_3(void) // display number 3
<br>
{
digitalWrite(g,HIGH);
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);


digitalWrite(dp,LOW);
=== Project 15: Analog Value Reading===
digitalWrite(f,LOW);
 
digitalWrite(e,LOW);
'''Introduction:''' <br>
}
In this experiment, we will begin the learning of analog I/O interfaces. <br>
void digital_4(void) // display number 4
On an Arduino, there are 6 analog interfaces numbered from 0 to 5. These 6 interfaces can also be used as digital ones numbered as 14-19. <br>
After a brief introduction, let's begin our project. Potentiometer used here is a typical output component of analog value that is familiar to us.<br>
 
'''Hardware Required:'''<br>
* Potentiometer *1
* UNO board *1
* Breadboard*1
* USB cable *1
* Breadboard jumper wire * 3
 
'''Circuit Connection:'''<br>
In this experiment, we will convert the resistance value of the potentiometer to analog ones, and display the analog value on the screen. <br>
This is an application we need to master well for our future experiments. <br>
Connection circuit as below:<br>
 
'''Connection for UNO R3:'''
<br>[[File:Ks0402 15-1.png|600px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Ks0402 15-2.png|600px|frameless|thumb]]<br>
 
The analog interface we use here is interface A0. <br>
 
<br>
'''Sample Code:'''<br>
The program compiling is simple. An '''analogRead () '''Statement can read the value of the interface. The A/D acquisition of Arduino 328 is in 10 bits, so the value it reads is among 0 to 1023. <br>
One difficulty in this project is to display the value on the screen, which is actually easy to learn. <br>
First, we need to set the baud rate in '''voidsetup ()'''. Displaying the value is a communication between Arduino and PC, so the baud rate of the Arduino should match the one in the PC's software setup. Otherwise, the display will be messy codes or no display at all. <br>
In the lower right corner of the Arduino software monitor window, there is a button for baud rate set up. The setup here needs to match the one in the program. <br>
The statement in the program is '''Serial.begin()'''; enclosed is the baud rate value, followed by statement for displaying. You can either use '''Serial.print()''' or '''Serial.println()''' statement.  <br>
 
<pre>
int potpin=0;// initialize analog pin 0
int ledpin=13;// initialize digital pin 13
int val=0;// define val, assign initial value 0
void setup()
{
{
digitalWrite(c,HIGH);
pinMode(ledpin,OUTPUT);// set digital pin as “output”
digitalWrite(b,HIGH);
Serial.begin(9600);// set baud rate at 9600
digitalWrite(f,HIGH);
 
digitalWrite(g,HIGH);
}
digitalWrite(dp,LOW);
void loop()
digitalWrite(a,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}
void digital_5(void) // display number 5
{
{
unsigned char j;
digitalWrite(ledpin,HIGH);// turn on the LED on pin 13
digitalWrite(a,HIGH);
delay(50);// wait for 0.05 second
digitalWrite(b, LOW);
digitalWrite(ledpin,LOW);// turn off the LED on pin 13
digitalWrite(c,HIGH);
delay(50);// wait for 0.05 second
digitalWrite(d,HIGH);
val=analogRead(potpin);// read the analog value of analog pin 0, and assign it to val
digitalWrite(e, LOW);
Serial.println(val);// display val’s value
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
}
void digital_6(void) // display number 6
</pre>
{
 
unsigned char j;
'''Result:'''<br>
for(j=7;j<=11;j++)
The sample program uses the built-in LED connected to pin 13. Each time the device reads a value, the LED blinks once. <br>
digitalWrite(j,HIGH);
Below is the analog value it reads.
digitalWrite(c,HIGH);
<br>[[File:Ks0402 15-3.png|600px|frameless|thumb]]<br>
digitalWrite(dp,LOW);
 
digitalWrite(b,LOW);
<br>
}
When you rotate the potentiometer knob, you can see the displayed value change. The reading of analog value is a very common function since most sensors output analog value. After calculation, we can have the corresponding value we need.<br>
void digital_7(void) // display number 7
The experiment is now complete. Thank you.
{
 
unsigned char j;
<br>
for(j=5;j<=7;j++)
 
 
=== Project 16: 74HC595===
<br>[[File:74HC595.png|250px|frameless|thumb]]<br>
 
'''Introduction:'''<br>
To put it simply, 74HC595 is a combination of 8-digit shifting register, memorizer and equipped with tri-state output. Here, we use it to control 8 LEDs. <br>
You may wonder why use a 74HC595 to control LED? Well, think about how many I/O it takes for an Arduino to control 8 LEDs? Yes, 8. <br>
For an Arduino, it has only 20 I/O including analog ports. So, to save port resources, we use 74HC595 to reduce the number of ports it needs. Using 74HC595 enables us to use 3 digital I/O port to control 8 LEDs! <br>
 
The 74HC595 devices contain an 8-bit serial-in, parallel-out shift register that feeds an 8-bit D-type storage register. The storage register has parallel 3-state outputs. Separate clocks are provided for both the shift and storage register.  <br>
The shift register has a direct overriding clear (SRCLR) input, serial (SER) input, and serial outputs for cascading. When the output-enable (OE) input is high, the outputs are in the high-impedance state. Both the shift register clock (SRCLK) and storage register clock (RCLK) are positive-edge triggered. If both clocks are connected together, the shift register always is one clock pulse ahead of the storage register.  <br>


digitalWrite(j,HIGH);
<br>
digitalWrite(dp,LOW);
'''Hardware Required:'''<br>
for(j=8;j<=11;j++)
* 74HC595 chip*1
digitalWrite(j,LOW);
* UNO board *1
}
* Red M5 LED*4
void digital_8(void) // display number 8
* Green M5 LED*4
{
* 220Ω resistor*8
unsigned char j;
* Breadboard*1
for(j=5;j<=11;j++)
* USB cable *1
digitalWrite(j,HIGH);
* Breadboard jumper wires*several
digitalWrite(dp,LOW);
 
<br>
'''Circuit Connection''' <br>
<span style=color:red> Note: for pin 13 OE port of 74HC595, it needs to connect to GND. </span><br>
 
'''Connection for UNO R3:'''
<br>[[File:Pro16-1.png|600px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro16-2.png|600px|frameless|thumb]]<br>
 
The circuit may seem complicated, but soon you will find it easy!  <br>
 
<br>
'''Sample Code:'''<br>
<pre>
int data = 2;// set pin 14 of 74HC595as data input pin SI
int clock = 5;// set pin 11 of 74hc595 as clock pin SCK
int latch = 4;// set pin 12 of 74hc595 as output latch RCK
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
}
void digital_9(void) // display number 5
void loop()
{
{
unsigned char j;
for(int i = 0; i < 256; i++)
digitalWrite(a,HIGH);
{
digitalWrite(b,HIGH);
updateLEDs(i);
digitalWrite(c,HIGH);
delay(500);
digitalWrite(d,HIGH);
}
digitalWrite(e, LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
}
void setup()
void updateLEDs(int value)
{
{
int i;// set variable
digitalWrite(latch, LOW);//
for(i=4;i<=11;i++)
shiftOut(data, clock, MSBFIRST, ~value);// serial data “output”, high level first
pinMode(i,OUTPUT);// set pin 4-11as “output”
digitalWrite(latch, HIGH);// latch
}
}
void loop()
</pre>
{
while(1)
{
digital_0();// display number 0
delay(1000);// wait for 1s
digital_1();// display number 1
delay(1000);// wait for 1s
digital_2();// display number 2
delay(1000); // wait for 1s
 
digital_3();// display number 3
delay(1000); // wait for 1s
digital_4();// display number 4
delay(1000); // wait for 1s
digital_5();// display number 5
delay(1000); // wait for 1s
digital_6();// display number 6
delay(1000); // wait for 1s
digital_7();// display number 7
delay(1000); // wait for 1s
digital_8();// display number 8
delay(1000); // wait for 1s
digital_9();// display number 9
delay(1000); // wait for 1s
}
}
</pre>


'''Result:'''<br>
'''Result:'''<br>
LED segment display displays number 0 to 9
After downloading the program, you can see 8 LEDs display 8-bit binary number.
<br>[[File:Pro16-3.png|500px|frameless|thumb]]<br>
<br>


=== '''Project 18: 4-digit LED Segment Display'''===
<br>


'''Introduction:'''
=== Project 17: 1-digit LED Segment Display===
In this experiment, we use an Arduino to drive a common anode, 4-digit, 7-segment LED display. For LED display, current-limiting resistors are indispensable. There are two wiring method for Current-limiting resistor. One is to connect one resistor for each anode, 4 in totals for d1-d4 anode. An advantage for this method is that it requires fewer resistors, only 4. But it cannot maintain consistent brightness, 1 the brightest, 8, the least bright. Another method is to connect one resistor to each pin. It guarantees consistent brightness, but requires more resistors. In this experiment, we use 8 220Ω resistors (we use 220Ω resistors because no 100Ω resistor available. If you use 100Ω, the displaying will be brighter).


<br>[[File:4-LED Display.png|500px|frameless|thumb]]<br>
<br>[[File:1-digit LED.png|250px|frameless|thumb]]<br>


For 4-digit displays, there are 12 pins in total. When you place the decimal point downward (see below photo position), the pin on the lower left part is refer to as 1, the upper left part 12. 
'''Introduction:'''<br>
<br>[[File:17 (1).png|500px|frameless|thumb]]<br>
LED segment displays are common for displaying numerical information. <br>
They are widely applied on displays of electromagnetic oven, full automatic washing machine, water temperature display, electronic clock etc.  
So it is necessary that we learn how it works. <br>


Manual for LED segment display:
LED segment display is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED).  <br>
<br>[[File:6953.png|500px|frameless|thumb]]<br>
LED segment display can be divided into 7-segment display and 8-segment display according to the number of segments.
8-segment display has one more LED unit (for decimal point display) than 7-segment one. <br>


'''Connection for UNO R3:'''
In this experiment, we use a 8-segment display. According to the wiring method of LED units, LED segment displays can be divided into display with common anode and display with common cathode.  <br>
<br>[[File:6954.png|500px|frameless|thumb]]<br>
Common anode display refers to the one that combine all the anodes of LED units into one common anode (COM).<br>
For the common anode display, connect the common anode (COM) to +5V. When the cathode level of a certain segment is low, the segment is on; when the cathode level of a certain segment is high, the segment is off.  <br>
 
For the common cathode display, connect the common cathode (COM) to GND. When the anode level of a certain segment is high, the segment is on; when the anode level of a certain segment is low, the segment is off. <br>
<br>[[File:Ks0402 17-1.png|500px|frameless|thumb]]<br>
 
Each segment of the display consists of an LED. So when you use it, you also need use a current-limiting resistor. Otherwise, LED will be burnt out. <br>
 
In this experiment, we use a common cathode display. As we mentioned above, for common cathode display, connect the common cathode (COM) to GND.
When the anode level of a certain segment is high, the segment is on; when the anode level of a certain segment is low, the segment is off.<br>
 
<br>
'''Hardware Required:'''<br>
* 1-digit 8-segment display *1
* 220Ω resistor *8
* Breadboard *1
* UNO board *1
* USB cable *1
* Breadboard jumper wire * 12
 
<br>
'''Connection:'''<br>
Refer to following connection diagram  <br>
 
'''Connection for UNO R3:'''
<br>[[File:Pro17-1.png|600px|frameless|thumb]]<br>


'''Connection for 2560 R3:'''
'''Connection for 2560 R3:'''
<br>[[File:6955.png|500px|frameless|thumb]]<br>
<br>[[File:Pro17-2.png|600px|frameless|thumb]]<br>


<br>
'''Sample Code:'''<br>
There are seven segments for numerical display, one for decimal point display. Corresponding segments will be turned on when displaying certain numbers. <br>
For example, when displaying number 1, b and c segments will be turned on. <br>
We compile a subprogram for each number, and compile the main program to display one number every 2 seconds, cycling display number 0 ~ 9. <br>
The displaying time for each number is subject to the delay time, the longer the delay time, the longer the displaying time. <br>


'''Sample Code:'''<br>
<pre>
<pre>
// display 1234
// set the IO pin for each segment
// select pin for cathode
int a=7;// set digital pin 7 for segment a
int a = 1;
int b=6;// set digital pin 6 for segment b
int b = 2;
int c=5;// set digital pin 5 for segment c
int c = 3;
int d=10;// set digital pin 10 for segment d
int d = 4;
int e=11;// set digital pin 11 for segment e
int e = 5;
int f=8;// set digital pin 8 for segment f
int f = 6;
int g=9;// set digital pin 9 for segment g
int g = 7;
int dp=4;// set digital pin 4 for segment dp
int dp = 8;
void digital_0(void) // display number 5
// select pin for anode
{
int d4 = 9;
unsigned char j;
int d3 = 10;
digitalWrite(a,HIGH);
int d2 = 11;
digitalWrite(b,HIGH);
int d1 = 12;
digitalWrite(c,HIGH);
// set variable
digitalWrite(d,HIGH);
long n = 1230;
digitalWrite(e,HIGH);
int x = 100;
digitalWrite(f,HIGH);
int del = 55;  // fine adjustment for clock
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
void setup()
}
{
void digital_1(void) // display number 1
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);
}
/////////////////////////////////////////////////////////////
void loop()
 
{
{
Display(1, 1);
unsigned char j;
Display(2, 2);
digitalWrite(c,HIGH);// set level as “high” for pin 5, turn on segment c
Display(3, 3);
digitalWrite(b,HIGH);// turn on segment b
Display(4, 4);
for(j=7;j<=11;j++)// turn off other segments
 
digitalWrite(j,LOW);
digitalWrite(dp,LOW);// turn off segment dp
}
}
///////////////////////////////////////////////////////////////
void digital_2(void) // display number 2
void WeiXuan(unsigned char n)//
{
{
    switch(n)
unsigned char j;
    {
digitalWrite(b,HIGH);
case 1:
digitalWrite(a,HIGH);
  digitalWrite(d1,LOW);
for(j=9;j<=11;j++)
  digitalWrite(d2, HIGH);
digitalWrite(j,HIGH);
  digitalWrite(d3, HIGH);
digitalWrite(dp,LOW);
  digitalWrite(d4, HIGH);  
digitalWrite(c,LOW);
break;
digitalWrite(f,LOW);
case 2:
}
  digitalWrite(d1, HIGH);
void digital_3(void) // display number 3
  digitalWrite(d2, LOW);
{digitalWrite(g,HIGH);
  digitalWrite(d3, HIGH);
digitalWrite(a,HIGH);
  digitalWrite(d4, HIGH);  
digitalWrite(b,HIGH);
    break;
digitalWrite(c,HIGH);
  case 3:
digitalWrite(d,HIGH);
    digitalWrite(d1,HIGH);
digitalWrite(dp,LOW);
  digitalWrite(d2, HIGH);
digitalWrite(f,LOW);
  digitalWrite(d3, LOW);
digitalWrite(e,LOW);
  digitalWrite(d4, HIGH);  
    break;
  case 4:
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, LOW);  
    break;
        default :
          digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
 
  digitalWrite(d4, HIGH);
        break;
  }
}
}
void Num_0()
void digital_4(void) // display number 4
{
{digitalWrite(c,HIGH);
  digitalWrite(a, HIGH);
digitalWrite(b,HIGH);
  digitalWrite(b, HIGH);
digitalWrite(f,HIGH);
  digitalWrite(c, HIGH);
digitalWrite(g,HIGH);
  digitalWrite(d, HIGH);
digitalWrite(dp,LOW);
  digitalWrite(e, HIGH);
digitalWrite(a,LOW);
  digitalWrite(f, HIGH);
digitalWrite(e,LOW);
  digitalWrite(g, LOW);
digitalWrite(d,LOW);
  digitalWrite(dp,LOW);
}
}
void Num_1()
void digital_5(void) // display number 5
{
{
  digitalWrite(a, LOW);
unsigned char j;
  digitalWrite(b, HIGH);
digitalWrite(a,HIGH);
  digitalWrite(c, HIGH);
digitalWrite(b, LOW);
  digitalWrite(d, LOW);
digitalWrite(c,HIGH);
  digitalWrite(e, LOW);
digitalWrite(d,HIGH);
  digitalWrite(f, LOW);
digitalWrite(e, LOW);
  digitalWrite(g, LOW);
digitalWrite(f,HIGH);
  digitalWrite(dp,LOW);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
}
void Num_2()
void digital_6(void) // display number 6
{
{
  digitalWrite(a, HIGH);
unsigned char j;
  digitalWrite(b, HIGH);
for(j=7;j<=11;j++)
  digitalWrite(c, LOW);
digitalWrite(j,HIGH);
  digitalWrite(d, HIGH);
digitalWrite(c,HIGH);
  digitalWrite(e, HIGH);
digitalWrite(dp,LOW);
  digitalWrite(f, LOW);
digitalWrite(b,LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
}
void Num_3()
void digital_7(void) // display number 7
{
{
  digitalWrite(a, HIGH);
unsigned char j;
 
for(j=5;j<=7;j++)
  digitalWrite(b, HIGH);
digitalWrite(j,HIGH);
  digitalWrite(c, HIGH);
digitalWrite(dp,LOW);
  digitalWrite(d, HIGH);
for(j=8;j<=11;j++)
  digitalWrite(e, LOW);
digitalWrite(j,LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
}
void Num_4()
void digital_8(void) // display number 8
{
{
  digitalWrite(a, LOW);
unsigned char j;
  digitalWrite(b, HIGH);
for(j=5;j<=11;j++)
  digitalWrite(c, HIGH);
digitalWrite(j,HIGH);
  digitalWrite(d, LOW);
digitalWrite(dp,LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
}
void Num_5()
void digital_9(void) // display number 5
{
{
  digitalWrite(a, HIGH);
unsigned char j;
  digitalWrite(b, LOW);
digitalWrite(a,HIGH);
  digitalWrite(c, HIGH);
digitalWrite(b,HIGH);
  digitalWrite(d, HIGH);
digitalWrite(c,HIGH);
  digitalWrite(e, LOW);
digitalWrite(d,HIGH);
  digitalWrite(f, HIGH);
digitalWrite(e, LOW);
  digitalWrite(g, HIGH);
digitalWrite(f,HIGH);
  digitalWrite(dp,LOW);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
}
void Num_6()
void setup()
{
{
  digitalWrite(a, HIGH);
int i;// set variable
  digitalWrite(b, LOW);
for(i=4;i<=11;i++)
  digitalWrite(c, HIGH);
pinMode(i,OUTPUT);// set pin 4-11as “output”
  digitalWrite(d, HIGH);
}
  digitalWrite(e, HIGH);
void loop()
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
 
}
void Num_7()
{
{
  digitalWrite(a, HIGH);
while(1)
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_8()
{
{
  digitalWrite(a, HIGH);
digital_0();// display number 0
  digitalWrite(b, HIGH);
delay(1000);// wait for 1s
  digitalWrite(c, HIGH);
digital_1();// display number 1
  digitalWrite(d, HIGH);
delay(1000);// wait for 1s
  digitalWrite(e, HIGH);
digital_2();// display number 2
  digitalWrite(f, HIGH);
delay(1000); // wait for 1s
  digitalWrite(g, HIGH);
digital_3();// display number 3
  digitalWrite(dp,LOW);
delay(1000); // wait for 1s
digital_4();// display number 4
delay(1000); // wait for 1s
digital_5();// display number 5
delay(1000); // wait for 1s
digital_6();// display number 6
delay(1000); // wait for 1s
digital_7();// display number 7
delay(1000); // wait for 1s
digital_8();// display number 8
delay(1000); // wait for 1s
digital_9();// display number 9
delay(1000); // wait for 1s
}
}
}
void Num_9()
</pre>
{
 
  digitalWrite(a, HIGH);
'''Result:'''<br>
  digitalWrite(b, HIGH);
LED segment display will display the number from 0 to 9.
  digitalWrite(c, HIGH);
<br>[[File:Pro17-3.png|500px|frameless|thumb]]<br>
  digitalWrite(d, HIGH);
 
  digitalWrite(e, LOW);
 
  digitalWrite(f, HIGH);
<br>
  digitalWrite(g, HIGH);
 
  digitalWrite(dp,LOW);
=== Project 18: 4-digit LED Segment Display===
}
<br>[[File:4-LED Display.png|300px|frameless|thumb]]<br>
void Clear()  // clear the screen
 
{
'''Introduction:'''<br>
  digitalWrite(a, LOW);
In this experiment, we use an Arduino to drive a common anode, 4-digit, 7-segment LED display. <br>
  digitalWrite(b, LOW);
For LED display, current-limiting resistors are indispensable. There are two wiring methods for Current-limiting resistor. <br>
  digitalWrite(c, LOW);
One is to connect one resistor for each anode, 4 in totals for d1-d4 anode. An advantage for this method is that it requires fewer resistors, only 4. But it cannot maintain consistent brightness.  <br>
  digitalWrite(d, LOW);
Another method is to connect one resistor to each pin. It guarantees consistent brightness, but requires more resistors.  <br>
 
In this experiment, we use 8 220Ω resistors (we use 220Ω resistors because no 100Ω resistor available. If you use 100Ω, the displaying will be more brighter).<br>
 
For 4-digit displays, there are 12 pins in total. <br>
When you place the decimal point downward (see below photo position), the pin on the lower left part is referred to 1, the upper left part 12.
<br>[[File:17 (1).png|500px|frameless|thumb]]<br>


  digitalWrite(e, LOW);
<br>
  digitalWrite(f, LOW);
'''Manual for LED segment display:'''
  digitalWrite(g, LOW);
<br>[[File:Pro17-4.png|600px|frameless|thumb]]<br>
  digitalWrite(dp,LOW);
 
}
 
void pickNumber(unsigned char n)// select number
'''Connection for UNO R3:'''
{
<br>[[File:Pro18-1.png|600px|frameless|thumb]]<br>
  switch(n)
<br>
  {
'''Connection for 2560 R3:'''
  case 0:Num_0();
<br>[[File:Pro18-2.png|600px|frameless|thumb]]<br>
  break;
 
  case 1:Num_1();
<br>
  break;
'''Sample Code:'''<br>
  case 2:Num_2();
<pre>
  break;
// display 1234
  case 3:Num_3();
// select pin for cathode
  break;
int a = 1;
  case 4:Num_4();
int b = 2;
  break;
int c = 3;
  case 5:Num_5();
int d = 4;
  break;
int e = 5;
  case 6:Num_6();
int f = 6;
  break;
int g = 7;
  case 7:Num_7();
int dp = 8;
  break;
// select pin for anode
  case 8:Num_8();
int d4 = 9;
  break;
int d3 = 10;
  case 9:Num_9();
int d2 = 11;
  break;
int d1 = 12;
  default:Clear();
// set variable
  break;
long n = 1230;
  }
int x = 100;
}
int del = 55; // fine adjustment for clock
void Display(unsigned char x, unsigned char Number)//  take x as coordinate and display number
{
void setup()
  WeiXuan(x);
{
  pickNumber(Number);
  pinMode(d1, OUTPUT);
delay(1);
  pinMode(d2, OUTPUT);
Clear() ; // clear the screen
  pinMode(d3, OUTPUT);
}</pre>
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(dp, OUTPUT);
}
/////////////////////////////////////////////////////////////
void loop()


'''Result:'''<br>
{
Download the above code to the controller board and see the result. <br>
Display(1, 1);
The experiment result displays 1234 on the display.<br>
Display(2, 2);
Note: if it’s not displaying correctly, check the wiring.<br>
Display(3, 3);
Thank you.
Display(4, 4);
 
=== '''Project 19: 8*8 LED Matrix'''===
<br>[[File:6956.png|500px|frameless|thumb]]<br>
'''Introduction:'''
 
With low-voltage scanning, LED dot-matrix displays have advantages such as power saving, long service life, low cost, high brightness, wide angle of view, long visual range, waterproof, and numerous specifications. LED dot-matrix displays can meet the needs of different applications and thus have a broad development prospect. This time, we will conduct an LED dot-matrix experiment to experience its charm firsthand.
 
'''Hardware required:'''<br>
*1 * Uno board
*1 * 8*8 dot-matrix
*8 * Resistor (220Ω)
*1 * Breadboard
*2 * 74HC595
*1 * USB cable
*Jumper wires
 
'''Connection :'''<br>
 
The external view of a dot-matrix is shown as follows:
<br>[[File:6957.png|500px|frameless|thumb]]<br>
 
The display principle of the 8*8 dot-matrix:<br>
The 8*8 dot-matrix is made up of sixty-four LEDs, and each LED is placed at the cross point of a row and a column. When the electrical level of a certain row is 1 and the electrical level of a certain column is 0, the corresponding LED will light up. If you want to light the LED on the first dot, you should set pin 9 to high level and pin 13 to low level. If you want to light LEDs on the first row, you should set pin 9 to high level and pins 13, 3, 4, 10, 6, 11, 15 and 16 to low level. If you want to light the LEDs on the first column, set pin 13 to low level and pins 9, 14, 8, 12, 1, 7, 2 and 5 to high level.<br>
The internal view of a dot-matrix is shown as follows:<br>
<br>[[File:6958.png|500px|frameless|thumb]]<br>
 
The principle of 74HC595 has been previously illustrated. One chip is used to control the rows of the dot-matrix while the other chip is used to control the columns.
 
Connect circuit as shown in the following diagram:
 
'''Connection for UNO R3:'''
<br>[[File:6959.png|500px|frameless|thumb]]<br>
 
'''Connection for 2560 R3:'''
<br>[[File:6960.png|500px|frameless|thumb]]<br>


'''Sample Code for Displaying “0”:'''<br>
<pre>
// set an array to store character of “0”
unsigned char Text[]={0x00,0x1c,0x22,0x22,0x22,0x22,0x22,0x1c};
void Draw_point(unsigned char x,unsigned char y)// point drawing function
{ clear_();
  digitalWrite(x+2, HIGH);
  digitalWrite(y+10, LOW);
  delay(1);
}
}
void show_num(void)// display function, call point drawing function
///////////////////////////////////////////////////////////////
void WeiXuan(unsigned char n)//
{
{
  unsigned char i,j,data;
    switch(n)
  for(i=0;i<8;i++)
    {
  {
case 1:
    data=Text[i];
  digitalWrite(d1,LOW);
    for(j=0;j<8;j++)
  digitalWrite(d2, HIGH);
    {
  digitalWrite(d3, HIGH);
      if(data & 0x01)Draw_point(j,i);
  digitalWrite(d4, HIGH);  
      data>>=1;
break;
 
case 2:
    } 
  digitalWrite(d1, HIGH);
  }
  digitalWrite(d2, LOW);
}
  digitalWrite(d3, HIGH);
void setup(){
  digitalWrite(d4, HIGH);  
int i = 0 ;  
    break;
for(i=2;i<18;i++)  
  case 3:
{
    digitalWrite(d1,HIGH);
  pinMode(i, OUTPUT);  
  digitalWrite(d2, HIGH);
  } 
  digitalWrite(d3, LOW);
  clear_();  
  digitalWrite(d4, HIGH);
    break;
  case 4:
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, LOW);  
    break;
        default :
          digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
 
  digitalWrite(d4, HIGH);
        break;
  }
}
}
void loop()
void Num_0()
{ show_num();  
{
}
  digitalWrite(a, HIGH);
void clear_(void)// clear screen
  digitalWrite(b, HIGH);
{for(int i=2;i<10;i++)
  digitalWrite(c, HIGH);
   digitalWrite(i, LOW);
  digitalWrite(d, HIGH);
   for(int i=0;i<8;i++)
  digitalWrite(e, HIGH);
   digitalWrite(i+10, HIGH);
   digitalWrite(f, HIGH);
   digitalWrite(g, LOW);
   digitalWrite(dp,LOW);
}
}
</pre>
void Num_1()
 
{
'''Result:'''<br>
  digitalWrite(a, LOW);
Burn the program into Uno board The dot-matrix will display 0.
  digitalWrite(b, HIGH);
 
  digitalWrite(c, HIGH);
 
  digitalWrite(d, LOW);
=== '''Project 20: 1602 LCD'''===
  digitalWrite(e, LOW);
 
  digitalWrite(f, LOW);
'''Introduction:'''
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_2()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_3()
{
  digitalWrite(a, HIGH);


In this experiment, we use an Arduino to drive the 1602 LCD.<br>
  digitalWrite(b, HIGH);
1602 LCD has wide applications. In the beginning, 1602 LCD uses a HD44780 controller. Now, almost all 1602 LCD module uses a compatible IC, so their features are basically the same. 
  digitalWrite(c, HIGH);
 
  digitalWrite(d, HIGH);
'''1602LCD main parameters:'''
  digitalWrite(e, LOW);
*Display Capacity: 16 × 2 characters.
  digitalWrite(f, LOW);
*Chip Operating Voltage: 4.5 ~ 5.5V.
  digitalWrite(g, HIGH);
*Working Current: 2.0mA (5.0V).
  digitalWrite(dp,LOW);
*Optimum working voltage of the module is 5.0V.
}
*Character Size: 2.95 * 4.35 (W * H) mm.
void Num_4()
 
{
'''Pin description of 1602 LCD:'''
  digitalWrite(a, LOW);
 
  digitalWrite(b, HIGH);
{| width="80%" cellspacing="0" border="1"
  digitalWrite(c, HIGH);
|-
  digitalWrite(d, LOW);
 
  digitalWrite(e, LOW);
! align="center" scope="col" | No.
  digitalWrite(f, HIGH);
! align="center" scope="col" |Mark
  digitalWrite(g, HIGH);
! align="center" scope="col" |Pin Description 
  digitalWrite(dp,LOW);
! align="center" scope="col" | No.
}
! align="center" scope="col" |Mark
void Num_5()
! align="center" scope="col" |Pin Description
{
|-
  digitalWrite(a, HIGH);
 
  digitalWrite(b, LOW);
| align="center" | 1
  digitalWrite(c, HIGH);
| align="center" | VSS
  digitalWrite(d, HIGH);
| align="center" | Power GND
  digitalWrite(e, LOW);
| align="center" | 9
  digitalWrite(f, HIGH);
| align="center" | D2
  digitalWrite(g, HIGH);
| align="center" | Date I/O
  digitalWrite(dp,LOW);
|-
}
void Num_6()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);


| align="center" | 2 
}
| align="center" | VDD
void Num_7()
| align="center" | Power Positive
{
| align="center" | 10 
  digitalWrite(a, HIGH);
| align="center" | D3
  digitalWrite(b, HIGH);
| align="center" | Date I/O
  digitalWrite(c, HIGH);
|-
  digitalWrite(d, LOW);
 
  digitalWrite(e, LOW);
| align="center" |  3                                     
  digitalWrite(f, LOW);
| align="center" | VL
  digitalWrite(g, LOW);
| align="center" | LCD Voltage Bias Signal
  digitalWrite(dp,LOW);
| align="center" | 11  
}
| align="center" | D4
void Num_8()
| align="center" | Date I/O
{
|-
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_9()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Clear() // clear the screen
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);


| align="center" | 4                                   
  digitalWrite(e, LOW);
| align="center" | RS
  digitalWrite(f, LOW);
| align="center" | Select data/command(V/L)
  digitalWrite(g, LOW);
| align="center" | 12 
  digitalWrite(dp,LOW);
| align="center" | D5
}
| align="center" | Date I/O
void pickNumber(unsigned char n)// select number
|-
{
 
  switch(n)
| align="center" | 5                                   
  {
| align="center" | R/W
  case 0:Num_0();
| align="center" | Select read/write(H/L)
  break;
| align="center" | 13 
  case 1:Num_1();
| align="center" | D6
  break;
| align="center" | Date I/O
  case 2:Num_2();
|-
  break;
 
  case 3:Num_3();
| align="center" | 6                                
  break;
| align="center" | E
  case 4:Num_4();
| align="center" | Enable Signal
  break;
| align="center" | 14 
  case 5:Num_5();
| align="center" | D7
  break;
| align="center" | Date I/O
  case 6:Num_6();
|-
  break;
 
  case 7:Num_7();
| align="center" | 7                               
  break;
| align="center" | D0
  case 8:Num_8();
| align="center" | Date I/O
  break;
| align="center" | 15  
  case 9:Num_9();
| align="center" | BLA
  break;
| align="center" | Back Light Power Positive
  default:Clear();
|-
  break;
  }
}
void Display(unsigned char x, unsigned char Number)//  take x as coordinate and display number
{
  WeiXuan(x);
  pickNumber(Number);
  delay(1);
Clear() ; // clear the screen
}
</pre>


| align="center" | 8                               
| align="center" | D1
| align="center" | Date I/O
| align="center" | 16 
| align="center" | BLK
| align="center" | Back Light Power Negative
|-
|}


'''Interface Description:'''<br>
'''Result:'''<br>
1. two power sources, one for module power, another one for back light, generally use 5V. In this project, we use 3.3V for back light.<br>
Download the above code to the controller board and see the result. <br>
2. VL is the pin for adjusting contrast ratio; it usually connects a potentiometer(no more than 5KΩ) in series for its adjustment. In this experiment, we use a 1KΩ resistor. For its connection, it has 2 methods, namely high potential and low potential. Here, we use low potential method; connect the resistor and then the GND.<br>
The experiment result displays 1234 on the LED display.
3. RS is a very common pin in LCD. It's a selecting pin for command/data. When the pin is in high level, it's in data mode; when it's in low level, it's in command mode.<br>
<br>[[File:Pro18-3.png|500px|frameless|thumb]]<br>
4. RW pin is also very common in LCD.  It's a selecting pin for read/write. When the    pin is in high level, it's in read operation; when it's in low level, it's in write operation.<br>
5. E pin is also very common in LCD. Usually, when the signal in the bus is stabilized, it sends out a positive pulse requiring read operation. When this pin is in high level, the bus is not allowed to have any change.<br>
6. D0-D7 is 8-bit bidirectional parallel bus, used for command and data transmission.<br>
7. BLA is anode for back light; BLK, cathode for back light.<br>


'''4 Basic Operations of 1602LCD:'''<br>
<span style=color:red> Note: if it’s not displaying correctly, check the wiring.</span><br>
{| width="80%" cellspacing="0" border="1"
Thank you!
|-


| align="center" | Read status                     
<br>
| align="center" |input
| align="center" |RS=L, R/W=H, E=H
| align="center" | output
| align="center" |D0-D7=status word
|-


| align="center" | Write command
=== Project 19: 8*8 LED Matrix===  
| align="center" | input
<br>[[File:6956.png|500px|frameless|thumb]]<br>
| align="center" | RS=L, R/W=H, D0-D7=command code, E=high pulse
| align="center" | output
| align="center" | none
|-


| align="center" | Read data 
'''Introduction:'''<br>
| align="center" | input
With low-voltage scanning, LED dot-matrix displays have some advantages such as power saving, long service life, low cost, high brightness, wide angle of view, long visual range, waterproof, and numerous specifications. <br>
| align="center" | RS=H, R/W=H, E=H
LED dot-matrix displays can meet the needs of different applications and thus have a broad development prospect. <br>
| align="center" | output 
This time, we will conduct an LED dot-matrix experiment to experience its charm firsthand.<br>
| align="center" | D0-D7=data
|-
 
| align="center" | Write data                                     
| align="center" | input
| align="center" | RS=H, R/W=L, D0-D7=data, E=high pulse
| align="center" | output 
| align="center" | none
|-
|}


'''Hardware Required:'''<br>
'''Hardware Required:'''<br>
*1 * Controller Board
* 1 * Uno board
*1 * 1602 LCD
* 1 * 8x8 dot-matrix
*1 * Potentiometer
* 8 * Resistor (220Ω)
*1 * Breadboard
* 1 * Breadboard
*1 * USB Cable
* 1 * USB cable
*Jumper Wires
* 16 * Jumper wires


'''Connection & Sample Program:'''


1602 can directly communicate with Arduino. According to the product manual, it has two connection methods, namely 8-bit connection and 4-bit connection.
'''Connection:'''<br>
The external view of a dot-matrix is shown as follows:
<br>[[File:6957.png|700px|frameless|thumb]]<br>
 
<br>
'''The display principle of the 8*8 dot-matrix:''' <br>
The 8*8 dot-matrix is made up of sixty-four LEDs, and each LED is placed at the cross point of a row and a column. <br>
When the electrical level of a certain row is 1 and the electrical level of a certain column is 0, the corresponding LED will light up. <br>
 
If you want to light up the LED on the first dot, you should set pin 9 to high level and pin 13 to low level. <br>
If you want to light LEDs on the first row, you should set pin 9 to high level and pins 13, 3, 4, 10, 6, 11, 15 and 16 to low level. <br>
If you want to light the LEDs on the first column, set pin 13 to low level and pins 9, 14, 8, 12, 1, 7, 2 and 5 to high level.<br>
 
The internal view of a dot-matrix is shown as follows:<br>
<br>[[File:6958.png|700px|frameless|thumb]]<br>


8-bit connection method:
The principle of 74HC595 has been previously illustrated. One chip is used to control the rows of the dot-matrix while the other chip is used to control the columns.<br>


<br>[[File:6961.png|700px|frameless|thumb]]<br>
Connect circuit as shown in the following diagram:<br>


'''Connection for UNO R3:'''
'''Connection for UNO R3:'''
<br>[[File:6962.png|700px|frameless|thumb]]<br>
<br>[[File:Pro19-1.png|600px|frameless|thumb]]<br>
 
<br>
'''Connection for 2560 R3:'''
'''Connection for 2560 R3:'''
<br>[[File:6963.png|700px|frameless|thumb]]<br>
<br>[[File:Pro19-1.png|600px|frameless|thumb]]<br>


 
<br>
'''Sample Code A :'''<br>
'''Sample Code for Displaying “0”:'''<br>
<pre>
<pre>
int DI = 12;
// set an array to store character of “0”
int RW = 11;
unsigned char Text[]={0x00,0x1c,0x22,0x22,0x22,0x22,0x22,0x1c};
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};// use array to select pin for bus
void Draw_point(unsigned char x,unsigned char y)// point drawing function
int Enable = 2;
{ clear_();
 
  digitalWrite(x+2, HIGH);
void LcdCommandWrite(int value) {
  digitalWrite(y+10, LOW);
// define all pins
  delay(1);
int i = 0;
}
for (i=DB[0]; i <= DI; i++) // assign value for bus
void show_num(void)// display function, call point drawing function
{
{
   digitalWrite(i,value & 01);// for 1602 LCD, it uses D7-D0( not D0-D7) for signal identification; here, it’s used for signal inversion.
  unsigned char i,j,data;
   value >>= 1;
  for(i=0;i<8;i++)
  {
    data=Text[i];
    for(j=0;j<8;j++)
    {
      if(data & 0x01)Draw_point(j,i);
      data>>=1;
 
    } 
  }
}
}
digitalWrite(Enable,LOW);
void setup(){
delayMicroseconds(1);
int i = 0 ;  
digitalWrite(Enable,HIGH);
for(i=2;i<18;i++)  
delayMicroseconds(1);  // wait for 1ms
{
digitalWrite(Enable,LOW);
  pinMode(i, OUTPUT);  
delayMicroseconds(1);  // wait for 1ms
  } 
  clear_();  
}
}
 
void loop()
void LcdDataWrite(int value) {
{ show_num();   
// initialize all pins
}
int i = 0;
void clear_(void)// clear screen
digitalWrite(DI, HIGH);
{for(int i=2;i<10;i++)
digitalWrite(RW, LOW);
  digitalWrite(i, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
  for(int i=0;i<8;i++)
   digitalWrite(i,value & 01);
  digitalWrite(i+10, HIGH);
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1);  // wait for 1ms
}
}
</pre>


void setup (void) {
'''Result:'''<br>
int i = 0;
Burn the program into Uno board, the dot-matrix will display 0.
for (i=Enable; i <= DI; i++) {
<br>[[File:Pro19-3.png|600px|frameless|thumb]]<br>
   pinMode(i,OUTPUT);
}
delay(100);
// initialize LCD after a brief pause
// for LCD control
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size
delay(64);                     
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size
delay(50);                     
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size             
delay(20);                     
LcdCommandWrite(0x06);  // set input mode
                         // auto-increment, no display of shifting
delay(20);                     
LcdCommandWrite(0x0E);  // display setup
                         // turn on the monitor, cursor on, no flickering
delay(20);                     
LcdCommandWrite(0x01);  // clear the scree, cursor position returns to 0
delay(100);                     
LcdCommandWrite(0x80);  //  display setup
                         //  turn on the monitor, cursor on, no flickering


delay(20);                     
<span style=color:red> Note: if it’s not displaying correctly, check the wiring.</span><br>
}
<br>


void loop (void) {
=== Project 20: 1602 LCD===
  LcdCommandWrite(0x01);  // clear the scree, cursor position returns to 0  
<br>[[File:1602LCD.png|500px|frameless|thumb]]<br>
  delay(10);
  LcdCommandWrite(0x80+3);
  delay(10);                     
  // write in welcome message
  LcdDataWrite('W');
  LcdDataWrite('e');
  LcdDataWrite('l');
  LcdDataWrite('c');
  LcdDataWrite('o');
  LcdDataWrite('m');


  LcdDataWrite('e');
'''Introduction:'''<br>
  LcdDataWrite(' ');
In this experiment, we use an Arduino to drive the 1602 LCD.<br>
  LcdDataWrite('t');
1602 LCD has wide applications. In the beginning,1602 LCD uses a HD44780 controller. Now, almost all 1602 LCD module uses a compatible IC, but their features are basically the same.
  LcdDataWrite('o');
  delay(10);
  LcdCommandWrite(0xc0+1);  // set cursor position at second line, second position
  delay(10);
  LcdDataWrite('g');
  LcdDataWrite('e');
  LcdDataWrite('e');
  LcdDataWrite('k');
  LcdDataWrite('-');
  LcdDataWrite('w');
  LcdDataWrite('o');
  LcdDataWrite('r');
  LcdDataWrite('k');
  LcdDataWrite('s');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('p');
  delay(5000);
  LcdCommandWrite(0x01);  // clear the screen, cursor returns to 0  
  delay(10);
  LcdDataWrite('I');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('m');
  LcdDataWrite(' ');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('n');
  LcdDataWrite('g');
  LcdDataWrite('y');
  LcdDataWrite('i');
  delay(3000);
  LcdCommandWrite(0x02); // set mode as new characters replay old ones, where there is no new ones remain the same
  delay(10);
  LcdCommandWrite(0x80+5); // set cursor position at first line, sixth position
  delay(10);  


  LcdDataWrite('t');
<br>
  LcdDataWrite('h');
'''1602LCD Parameters:'''
  LcdDataWrite('e');
*Display Capacity: 16 × 2 characters
  LcdDataWrite(' ');
*Chip Operating Voltage: 4.5 ~ 5.5V
  LcdDataWrite('a');
*Working Current: 2.0mA (5.0V)
  LcdDataWrite('d');
*Optimum working voltage of the module is 5.0V
  LcdDataWrite('m');
*Character Size: 2.95 * 4.35 (W * H) mm
  LcdDataWrite('i');
  LcdDataWrite('n');
  delay(5000);
}
</pre>


'''4-bit connection method:'''<br>
<br>
When using this module, 8-bit connection uses all the digital pins of the Arduino, leaving no pin for sensors. What then? We can use 4-bit connection.
'''Pin Description of 1602 LCD:'''<br>


'''Connection circuit:'''<br>
{| width="80%" cellspacing="0" border="1"
'''Connection for UNO R3:'''<br>
|-
<br>[[File:6964.png|700px|frameless|thumb]]<br>


'''Connection for 2560 R3:'''
! align="center" scope="col" | No.
<br>[[File:6965.png|700px|frameless|thumb]]<br>
! align="center" scope="col" |Mark
After the connection, upload below code to the controller board and see how it goes.
! align="center" scope="col" |Pin Description 
! align="center" scope="col" | No.
! align="center" scope="col" |Mark
! align="center" scope="col" |Pin Description
|-


'''Sample Code B :'''<br>
| align="center" | 1
<pre>
| align="center" | VSS
int LCD1602_RS=12;   
| align="center" | Power GND
int LCD1602_RW=11;   
| align="center" | 9
int LCD1602_EN=10;   
| align="center" | D2
int DB[] = { 6, 7, 8, 9};
| align="center" | Date I/O
char str1[]="Welcome to";
|-
char str2[]="geek-workshop";
char str3[]="this is the";
char str4[]="4-bit interface";


void LCD_Command_Write(int command)
| align="center" | 2 
{
| align="center" | VDD
int i,temp;
| align="center" | Power Positive
digitalWrite( LCD1602_RS,LOW);
| align="center" | 10 
digitalWrite( LCD1602_RW,LOW);
| align="center" | D3
digitalWrite( LCD1602_EN,LOW);
| align="center" | Date I/O
|-


temp=command & 0xf0;
| align="center" |  3                                     
for (i=DB[0]; i <= 9; i++)
| align="center" | VL
{
| align="center" | LCD Voltage Bias Signal
   digitalWrite(i,temp & 0x80);
| align="center" | 11 
   temp <<= 1;
| align="center" | D4
}
| align="center" | Date I/O
|-


| align="center" | 4                                   
| align="center" | RS
| align="center" | Select data/command(V/L)
| align="center" | 12 
| align="center" | D5
| align="center" | Date I/O
|-


digitalWrite( LCD1602_EN,HIGH);
| align="center" | 5                                   
delayMicroseconds(1);
| align="center" | R/W
digitalWrite( LCD1602_EN,LOW);
| align="center" | Select read/write(H/L)
| align="center" | 13 
| align="center" | D6
| align="center" | Date I/O
|-


temp=(command & 0x0f)<<4;
| align="center" | 6                               
for (i=DB[0]; i <= 10; i++)
| align="center" | E
{
| align="center" | Enable Signal
   digitalWrite(i,temp & 0x80);
| align="center" | 14 
   temp <<= 1;
| align="center" | D7
}
| align="center" | Date I/O
|-


digitalWrite( LCD1602_EN,HIGH);
| align="center" | 7                               
delayMicroseconds(1);
| align="center" | D0
digitalWrite( LCD1602_EN,LOW);
| align="center" | Date I/O
}
| align="center" | 15 
| align="center" | BLA
| align="center" | Back Light Power Positive
|-


void LCD_Data_Write(int dat)
| align="center" | 8                               
{
| align="center" | D1
int i=0,temp;
| align="center" | Date I/O
digitalWrite( LCD1602_RS,HIGH);
| align="center" | 16 
digitalWrite( LCD1602_RW,LOW);
| align="center" | BLK
digitalWrite( LCD1602_EN,LOW);
| align="center" | Back Light Power Negative
|-
|}


temp=dat & 0xf0;
<br>
for (i=DB[0]; i <= 9; i++)
'''Interface Description:'''<br>
{
1. two power sources, one for module power, another one for backlight, generally use 5V. In this project, we use 3.3V for backlight.<br>
   digitalWrite(i,temp & 0x80);
2. '''VL''' is the pin for adjusting contrast ratio; it usually connects a potentiometer(no more than 5KΩ) in series for its adjustment. <br> In this experiment, we use a 1KΩ resistor. For the connection, it has 2 methods, namely high potential and low potential. Here, we use low potential method; connect the resistor and then the GND.<br>
   temp <<= 1;
3. '''RS''' is a very common pin in LCD. It's a selecting pin for command/data. When the pin is in high level, it's in data mode; when it's in low level, it's in command mode.<br>
}
4. '''RW''' pin is also very common in LCD. It's a selecting pin for read/write. When the pin is in high level, it's in read operation; when it's in low level, it's in write operation.<br>
5. '''E''' pin is also very common in LCD. Usually, when the signal in the bus is stabilized, it sends out a positive pulse requiring read operation. When this pin is in high level, the bus is not allowed to have any change.<br>
6. '''D0-D7''' is 8-bit bidirectional parallel bus, used for command and data transmission.<br>
7. '''BLA''' is anode for back light; BLK, cathode for back light.<br>


digitalWrite( LCD1602_EN,HIGH);
<br>
delayMicroseconds(1);
'''4 Basic Operations of 1602LCD:'''<br>
digitalWrite( LCD1602_EN,LOW);
{| width="80%" cellspacing="0" border="1"
|-


temp=(dat & 0x0f)<<4;
| align="center" | Read status                     
for (i=DB[0]; i <= 10; i++)
| align="center" |input
{
| align="center" |RS=L, R/W=H, E=H
   digitalWrite(i,temp & 0x80);
| align="center" | output
   temp <<= 1;
| align="center" |D0-D7=status word
}
|-


digitalWrite( LCD1602_EN,HIGH);
| align="center" | Write command
delayMicroseconds(1);
| align="center" | input
digitalWrite( LCD1602_EN,LOW);
| align="center" | RS=L, R/W=H, D0-D7=command code, E=high pulse
}
| align="center" | output
| align="center" | none
|-


void LCD_SET_XY( int x, int y )
| align="center" | Read data 
{
| align="center" | input
  int address;
| align="center" | RS=H, R/W=H, E=H
  if (y ==0)    address = 0x80 + x;
| align="center" | output 
  else          address = 0xC0 + x;
| align="center" | D0-D7=data
  LCD_Command_Write(address);
|-
}


void LCD_Write_Char( int x,int y,int dat)
| align="center" | Write data                                     
{
| align="center" | input
  LCD_SET_XY( x, y );
| align="center" | RS=H, R/W=L, D0-D7=data, E=high pulse
  LCD_Data_Write(dat);
| align="center" | output 
}
| align="center" | none
|-
|}


void LCD_Write_String(int X,int Y,char *s)
<br>
{
'''Hardware Required:'''<br>
    LCD_SET_XY( X, Y );    // address setup
* 1 * Controller board
    while (*s)             // write character string
* 1 * 1602 LCD
    {
* 1 * Breadboard
      LCD_Data_Write(*s);   
* 1 * potentiometer
      s ++;
* 1 * USB cable
    }
* 16 *Jumper wires
}


void setup (void)
<br>
{
'''Connection & Sample Program:'''<br>
  int i = 0;
1602 can directly communicate with Arduino. According to the product manual, it has two connection methods, namely 8-bit connection and 4-bit connection.
  for (i=6; i <= 12; i++)
<br>
   {
'''8-bit connection method:'''
     pinMode(i,OUTPUT);
<br>[[File:Ks0402 20-1.png|700px|frameless|thumb]]<br>
   }
 
  delay(100);
'''Connection for UNO R3:'''
  LCD_Command_Write(0x28);// 4 wires, 2 lines 5x7
<br>[[File:Pro20-1.png|600px|frameless|thumb]]<br>
  delay(50);
 
  LCD_Command_Write(0x06);
'''Connection for 2560 R3:'''
 
<br>[[File:Pro20-2.png|600px|frameless|thumb]]<br>
 delay(50);
 
  LCD_Command_Write(0x0c);
<br>
  delay(50);
'''Sample Code A:'''<br>
  LCD_Command_Write(0x80);
<pre>
  delay(50);
int DI = 12;
  LCD_Command_Write(0x01);
int RW = 11;
  delay(50);
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};// use array to select pin for bus
 
int Enable = 2;
}
void LcdCommandWrite(int value) {
 
// define all pins
void loop (void)
int i = 0;
{
for (i=DB[0]; i <= DI; i++) // assign value for bus
   LCD_Command_Write(0x01);
{
   delay(50);
  digitalWrite(i,value & 01);// for 1602 LCD, it uses D7-D0( not D0-D7) for signal identification; here, it’s used for signal inversion.
   LCD_Write_String(3,0,str1);// line 1, start at the fourth address
  value >>= 1;
   delay(50);
}
   LCD_Write_String(1,1,str2);// line 2, start at the second address
digitalWrite(Enable,LOW);
   delay(5000);
delayMicroseconds(1);
   LCD_Command_Write(0x01);
digitalWrite(Enable,HIGH);
   delay(50);
delayMicroseconds(1);  // wait for 1ms
   LCD_Write_String(0,0,str3);
digitalWrite(Enable,LOW);
   delay(50);
delayMicroseconds(1);  // wait for 1ms
   LCD_Write_String(0,1,str4);
}
   delay(5000);
void LcdDataWrite(int value) {
}</pre>
// initialize all pins
 
int i = 0;
=== '''Project 21: 9g Servo Control'''===
digitalWrite(DI, HIGH);
 
digitalWrite(RW, LOW);
'''Introduction:'''
for (i=DB[0]; i <= DB[7]; i++) {
Servomotor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor. The receiver or MCU outputs a signal to the servomotor. The motor has a  built-in reference circuit that gives out reference signal, cycle of 20ms and width of 1.5ms. The motor compares the acquired DC bias voltage to the voltage of the potentiometer and outputs a voltage difference. The IC on the circuit board will decide the rotate direction accordingly and drive the core-less motor. The gear then pass the force to the shaft. The sensor will determine if it has reached the commanded position according to the feedback signal. Servomotors are used in control systems that requires to have and maintain different angles. When the motor speed is definite, the gear will  cause the potentiometer to  rotate. When the voltage difference reduces to zero, the motor stops. Normally, the rotation angle range is among 0-180 degrees. 
  digitalWrite(i,value & 01);
 
  value >>= 1;
<br>[[File:6966.png|500px|frameless|thumb]]<br>
}
 
digitalWrite(Enable,LOW);
Servomotor comes with many specifications. But all of them have three connection wires, distinguished by brown, red, orange colors(different brand may have different color). Brown one is for GND, red one for power positive, orange one for signal line.
delayMicroseconds(1);
 
digitalWrite(Enable,HIGH);
<br>[[File:6967.png|500px|frameless|thumb]]<br>
delayMicroseconds(1);
 
digitalWrite(Enable,LOW);
The rotate angle of the servo motor is controlled by regulating the duty cycle of the PWM(Pulse-Width Modulation) signal. The standard cycle of the PWM signal is 20ms(50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it's between 0.5ms-2.5ms. The width corresponds the rotate angle from 0° to 180°. But note that for different brand motor, the same signal may have different rotate angle. 
delayMicroseconds(1);  // wait for 1ms
 
}
<br>[[File:6968.png|500px|frameless|thumb]]<br>
void setup (void) {
After some basic knowledge, let's learn how to control a servomotor. For this experiment, you only need a servomotor and several jumper wires. 
int i = 0;
 
for (i=Enable; i <= DI; i++) {
'''Hardware Required:'''<br>
  pinMode(i,OUTPUT);
*RB—412 servomotor*1
}
*Breadboard jumper wire*several
delay(100);
 
// initialize LCD after a brief pause
'''Connection & Sample Program:'''
// for LCD control
 
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size
There are two ways to control a servomotor with Arduino. One is to use a common digital sensor port of Arduino to produce square wave with different duty cycle to simulate PWM signal and use that signal to control the positioning of the motor. Another way is to directly use the Servo function of the Arduino to control the motor. In this way, the program will be easier but it can only control two-contact motor because for the servo function, only digital pin 9 ang 10 can be used. The Arduino
delay(64);                     
 
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size
drive capacity is limited. So if you need to control more than one motor, you will need external power.
delay(50);                     
 
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size           
'''Method 1:'''
delay(20);                     
 
LcdCommandWrite(0x06);  // set input mode
'''Connection for UNO R3:'''
                        // auto-increment, no display of shifting
<br>[[File:6969.png|500px|frameless|thumb]]<br>
delay(20);                     
 
LcdCommandWrite(0x0E);  // display setup
'''Connection for 2560 R3:'''
                        // turn on the monitor, cursor on, no flickering
<br>[[File:6970.png|500px|frameless|thumb]]<br>
delay(20);                     
 
LcdCommandWrite(0x01);  // clear the screen, cursor position returns to 0
Connect the motor to digital pin 9.
delay(100);                     
Compile a program to control the motor to rotate to the commanded angle input by the user and display the angle on the screen.
LcdCommandWrite(0x80);  //  display setup
 
                        //  turn on the monitor, cursor on, no flickering
 
 
'''Sample Code A :'''<br>
delay(20);                     
<pre>
}
int servopin=9;// select digital pin 9 for servomotor signal line
 
int myangle;// initialize angle variable
void loop (void) {
int pulsewidth;// initialize width variable
  LcdCommandWrite(0x01);  // clear the screen, cursor position returns to 0
int val;
  delay(10);
void servopulse(int servopin,int myangle)// define a servo pulse function
  LcdCommandWrite(0x80+3);
{
  delay(10);                   
  // write in welcome message
  LcdDataWrite('W');
  LcdDataWrite('e');
  LcdDataWrite('l');
  LcdDataWrite('c');
  LcdDataWrite('o');
  LcdDataWrite('m');
 
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('t');
  LcdDataWrite('o');
  delay(10);
  LcdCommandWrite(0xc0+1);  // set cursor position at second line, second position
  delay(10);
  LcdDataWrite('g');
  LcdDataWrite('e');
  LcdDataWrite('e');
  LcdDataWrite('k');
  LcdDataWrite('-');
  LcdDataWrite('w');
  LcdDataWrite('o');
  LcdDataWrite('r');
  LcdDataWrite('k');
  LcdDataWrite('s');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('p');
  delay(5000);
  LcdCommandWrite(0x01);  // clear the screen, cursor returns to 0 
  delay(10);
  LcdDataWrite('I');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('m');
  LcdDataWrite(' ');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('n');
  LcdDataWrite('g');
  LcdDataWrite('y');
  LcdDataWrite('i');
  delay(3000);
  LcdCommandWrite(0x02); // set mode as new characters replay old ones, where there is no new ones remain the same.
  delay(10);
  LcdCommandWrite(0x80+5); // set cursor position at first line, sixth position
  delay(10); 
 
  LcdDataWrite('t');
  LcdDataWrite('h');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('d');
  LcdDataWrite('m');
  LcdDataWrite('i');
  LcdDataWrite('n');
  delay(5000);
}
</pre>
 
<br>[[File:Pro20-3.png|600px|frameless|thumb]]<br>
 
<br>
'''4-bit connection method:'''<br>
When using this module, 8-bit connection uses all the digital pins of the Arduino, leaving no pin for sensors. What then? you can use 4-bit connection.<br>
 
'''Connection Circuit:'''<br>
'''Connection for UNO R3:'''<br>
<br>[[File:Pro20-4.png|600px|frameless|thumb]]<br>
<br>
'''Connection for 2560 R3:'''
<br>[[File:Pro20-5.png|600px|frameless|thumb]]<br>
 
After the connection, upload below code to the controller board and see how it goes.
 
<br>
'''Sample Code B:'''<br>
<pre>
int LCD1602_RS=12; 
int LCD1602_RW=11; 
int LCD1602_EN=10; 
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="geek-workshop";
char str3[]="this is the";
char str4[]="4-bit interface";
 
void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
 
temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
  digitalWrite(i,temp & 0x80);
  temp <<= 1;
}
 
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
 
temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
  digitalWrite(i,temp & 0x80);
  temp <<= 1;
}
 
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
 
void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);
 
temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
  digitalWrite(i,temp & 0x80);
  temp <<= 1;
}
 
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
 
temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
  digitalWrite(i,temp & 0x80);
  temp <<= 1;
}
 
digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);
}
 
void LCD_SET_XY( int x, int y )
{
  int address;
  if (y ==0)    address = 0x80 + x;
  else          address = 0xC0 + x;
  LCD_Command_Write(address);
}
 
void LCD_Write_Char( int x,int y,int dat)
{
  LCD_SET_XY( x, y );
  LCD_Data_Write(dat);
}
 
void LCD_Write_String(int X,int Y,char *s)
{
    LCD_SET_XY( X, Y );    // address setup
    while (*s)            // write character string
    {
      LCD_Data_Write(*s); 
      s ++;
    }
}
 
void setup (void)
{
  int i = 0;
  for (i=6; i <= 12; i++)
  {
    pinMode(i,OUTPUT);
  }
  delay(100);
  LCD_Command_Write(0x28);// 4 wires, 2 lines 5x7
  delay(50);
  LCD_Command_Write(0x06);
delay(50);
  LCD_Command_Write(0x0c);
  delay(50);
  LCD_Command_Write(0x80);
  delay(50);
  LCD_Command_Write(0x01);
  delay(50);
 
}
 
void loop (void)
{
  LCD_Command_Write(0x01);
  delay(50);
  LCD_Write_String(3,0,str1);// line 1, start at the fourth address
  delay(50);
  LCD_Write_String(1,1,str2);// line 2, start at the second address
  delay(5000);
  LCD_Command_Write(0x01);
  delay(50);
  LCD_Write_String(0,0,str3);
  delay(50);
  LCD_Write_String(0,1,str4);
  delay(5000);
}
</pre>
 
<br>
 
=== Project 21: Servo Control===
 
'''Introduction:''' <br>
Servo motor is a position control rotary actuator.
It mainly consists of housing, circuit board, core-less motor, gear and position sensor. <br>
 
'''Working principle:'''<br>
The receiver or MCU outputs a signal to the servo motor. The motor has a  built-in reference circuit that gives out reference signal, cycle of 20ms and width of 1.5ms. The motor compares the acquired DC bias voltage to the voltage of the potentiometer and outputs a voltage difference. <br>
The IC on the circuit board will decide the rotating direction accordingly and drive the core-less motor. The gear then pass the force to the shaft. The sensor will determine if it has reached the commanded position according to the feedback signal. <br>
<br>
Servomotors are used in control systems that requires to have and maintain different angles. When the motor speed is definite, the gear will cause the potentiometer to rotate. When the voltage difference reduces to zero, the motor stops. Normally, the rotation angle range is among 0-90 degrees. <br>
<br>[[File:Ks0402 21-2.png|400px|frameless|thumb]]<br>
 
Servomotor comes with many specifications. But all of them have three connection wires, distinguished by brown, red, orange colors (different brand may have different color). Brown one is for GND, red one for power positive, orange one for signal line. <br>
<br>[[File:Ks0402 21-1.png|600px|frameless|thumb]]<br>
 
The rotation angle of the servo motor is controlled by regulating the duty cycle of the PWM(Pulse-Width Modulation) signal. <br>
The standard cycle of the PWM signal is fixed at 20ms (50 Hz), and the pulse width is distributed between 1ms-2ms. The width corresponds the rotation angle from 0° to 90°. <br>
But note that for different brand motor, the same signal may have different rotation angle.
<br>[[File:舵机90度.png|600px|frameless|thumb]]<br>
 
<br>
After some basic knowledge, let's learn how to control a servomotor. <br>
In this experiment, you only need a servomotor and several jumper wires. <br>
 
<br>
'''Hardware Required:'''<br>
* Servo motor*1
* Breadboard jumper wire* 3
 
<br>
'''Connection & Sample Program:'''<br>
There are two ways to control a servomotor with Arduino. <br>
One is to use a common digital sensor port of Arduino to produce square wave with different duty cycle to simulate PWM signal and use that signal to control the position of the motor. <br>
Another way is to directly use the Servo function of the Arduino to control the motor. In this way, the program will be easier but it can only control two-contact motor because of the servo function, only digital pin 9 and 10 can be used. <br>
The Arduino drive capacity is limited. So if you need to control more than one motor, you will need external power.<br>
 
<br>
'''Method 1:'''<br>
Connect the signal line of motor to digital pin 9.<br>
'''Connection for UNO R3:'''
<br>[[File:KS0209-2.png|500px|frameless|thumb]]<br>
<br>
 
'''Connection for 2560 R3:'''
<br>[[File:KS0209-3.png|500px|frameless|thumb]]<br>
<br>
 
Compile a program to control the motor to rotate to the commanded angle, and display the angle on the monitor.<br>
 
<br>
'''Sample Code A:'''<br>
<pre>
int servopin=9;// select digital pin 9 for servomotor signal line
int myangle;// initialize angle variable
int pulsewidth;// initialize width variable
int val;
void servopulse(int servopin,int myangle)// define a servo pulse function
{
pulsewidth=(myangle*11)+500;// convert angle to 500-2480 pulse width
pulsewidth=(myangle*11)+500;// convert angle to 500-2480 pulse width
digitalWrite(servopin,HIGH);// set the level of servo pin as “high”
digitalWrite(servopin,HIGH);// set the level of servo pin as “high”
Line 2,183: Line 2,597:
if(val>='0'&&val<='9')
if(val>='0'&&val<='9')
{
{
val=val-'0';// convert characteristic quantity to numerical variable
val=val-'0';// convert characteristic quantity to numerical variable
val=val*(180/9);// convert number to angle
val=val*(180/9);// convert number to angle
Serial.print("moving servo to ");
Serial.print("moving servo to ");
Line 2,196: Line 2,610:
</pre>
</pre>


'''Method 2::'''
<br>
'''Method 2:'''<br>
Let's first take a look at the Arduino built-in servo function and some of its common statements.<br>
* 1. '''attach(interface''')——select pin for servo, can only use pin 9 or 10.<br>
* 2. '''write(angle)'''——used to control the rotation angle of the servo, can set the angle among 0 degree to 180 degree.<br>
* 3. '''read()'''——used to read the angle of the servo, consider it a function to read the value in the write() function.<br>
* 4. '''attached()'''——determine whether the parameter of the servo is sent to the servo pin.<br>
* 5. '''detach()'''—— disconnect the servo and the pin, and the pin(digital pin 9 or 10) can be used for PWM port. <br>
 
<span style=color:red> Note: the written form of the above statements are "servo variable name. specific statement ()", e.g. myservo. Attach (9).</span><br>


Let's first take a look at the Arduino built-in servo function and some of its common statements.<br>
Still, connect the signal line of servo to pin 9. Please refer to the connection diagram mentioned above.<br>
1. attach(interface)——select pin for servo, can only use pin 9 or 10.<br>
2. write(angle)——used to control the rotate angle of the servo, can set the angle among 0 degree to 180 degree.<br>
3. read()——used to read the angle of the servo, consider it a function to read the value in the write() function.<br>
4、attached()——determine whether the parameter of the servo is sent to the servo pin.<br>
5、detach()—— disconnect the servo and the pin, and the pin(digital pin 9 or 10) can be used for PWM port. <br>
Note: Note: the written form of the above statements are " servo variable name. specific statement ()", e.g. myservo. Attach (9).<br>
Still, connect the servo to pin 9.


<br>
'''Sample program B:'''<br>
<span style=color:red> Please remember to place the Servo.h library into your arduino libraries directory.</span><br>


'''Sample Code B :'''<br>
<pre>
<pre>
#include <Servo.h>
#include <Servo.h>
Line 2,220: Line 2,638:
void loop()
void loop()
{
{
myservo.write(90);// set rotate angle of the motor
myservo.write(90);// set rotation angle of the motor
}
}
</pre>
</pre>


Above are the two methods to control the servo. You can choose either one according to your liking or actual need. 
<br>
Above are the two methods to control the servo. You can choose either one according to your liking or actual need.  
<br>[[File:Pro21-3.png|500px|frameless|thumb]]<br>
 
<br>


==Resources ==
==Resources ==

Revision as of 15:37, 23 May 2019

keyestudio Basic Starter Kit for UNO and Mega


thumb


Kit Introduction

This is the basic Starter Kit, developed specially for those beginners who are interested in Arduino.
You will have a set of Arduino's most common and useful electronic components. What's more, we will offer you a detailed tutorials including project introduction and their source codes.
You may learn about Arduino through using these basic projects. This kit will help you control the physical world with sensors.
thumb


Kit Contents

Kit A for unoR3 Kit B for 2560 R3 Kit C Without board
UNO R3 Mega 2560 No controller board
5x LED - Blue 5x LED - Blue 5x LED - Blue
5x LED - Red 5x LED - Red 5x LED - Red
5x LED - Yellow 5x LED - Yellow 5x LED - Yellow
1x LED - RGB 1x LED - RGB 1x LED - RGB
5x 10K Ω resistor 5x 10K Ω resistor 5x 10K Ω resistor
5x 1K Ω resistor 5x 1K Ω resistor 5x 1K Ω resistor
8x 220 Ω resistor 8x 220 Ω resistor 8x 220 Ω resistor
1x 10K Ω Pot 1x 10K Ω Pot 1x 10K Ω Pot
1x 7-seg LED 1x module 1x 7-seg LED 1x module 1x 7-seg LED 1x module
1x 7-seg LED 4x module 1x 7-seg LED 4x module 1x 7-seg LED 4x module
1x 8x8 LED Matrix 1x 8x8 dot LED array 1x 8x8 dot LED array
1x Buzzer (active) 1x Buzzer (active) 1x Buzzer (active)
1x Buzzer (passive) 1x Buzzer (passive) 1x Buzzer (passive)
1x Flame sensor 1x Flame sensor 1x Flame sensor
1x IR receiver 1x IR receiver 1x IR receiver
1x IR remote control 1x IR remote control 1x IR remote control
1x LM35 Temp Sensor 1x LM35 Temp Sensor 1x LM35 Temp Sensor
2x Ball tilt sensor 2x Ball tilt sensor 2x Ball tilt sensor
3x Photo Resistor 3x Photo Resistor 3x Photo Resistor
4x Small button switch 4x Small button switch 4x Small button switch
1x IC 74HC595N 16-pin DIP 1x IC 74HC595N 16-pin DIP 1x IC 74HC595N 16-pin DIP
1x LCD1602 1x LCD1602 1x LCD1602
1x 9g servo 1x 9g servo 1x 9g servo
830-pin Breadboard 830-pin Breadboard 830-pin Breadboard
Dupont connector wires Dupont connector wires Dupont connector wires
1x 6-cell AA Battery pack 1x 6-cell AA Battery pack 1x 6-cell AA Battery pack
1x USB cable 1x USB cable 1x USB cable



Getting Started with Arduino

Installing Arduino Software

When you get the UNO development board, first you should install the Arduino software and driver.
We usually use the Windows software Arduino 1.5.6 version. You can download it from the link below:
https://www.arduino.cc/en/Main/OldSoftwareReleases#1.5.x
Or you can browse the ARDUINO website to download the latest version from this link, https://www.arduino.cc, pop up the following interface.
KS0313-1.png

Then click the SOFTWARE on the browse bar, you will have two options ONLINE TOOLS and DOWNLOADS.
KS0313-2.png

Click DOWNLOADS, it will appear the latest software version of ARDUINO 1.8.5 shown as below.
KS0313-3.png

In this software page, on the right side you can see the version of development software for different operating systems. ARDUINO has a powerful compatibility. You should download the software that is compatible with the operating system of your computer.
We will take WINDOWS system as an example here. There are also two options under Windows system, one is installed version, the other is non-installed version. For simple installed version, first click Windows Installer, you will get the following page.

KS0313-4.png

KS0313-5.png

This way you just need to click JUST DOWNLOAD, then click the downloaded file to install it.
For non-installed version, first click Windows ZIP file, you will also get the pop-up interface as the above figure.
Click JUST DOWNLOAD, and when the ZIP file is downloaded well to your computer, you can directly unzip the file and click the icon of ARDUINO software to start it.


Installing Arduino (Windows)

Install Arduino with the exe. Installation package. Here we provide you with Arduino-1.5.6-r2-windows package, you can directly click the icon to install it.
thumb

Click“I Agree”to see the following interface.
thumb

Click “Next”. Pop up the interface below.
thumb

You can press Browse… to choose an installation path or directly type in the directory you want.
Then click “Install” to initiate installation.
thumb

Wait for the installing process, if appear the interface of Window Security, just continue to click Install to finish the installation.
thumb

All right, up to now, you have completed the Arduino setup! The following icon will appear on your PC desktop.
Ks0313图片1.png
Double-click the icon of Arduino to enter the desired development environment shown as below.
717.png

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


Installing Driver

Next, we will introduce the driver installation for development board. The driver installation may have slight differences in different computer systems. So in the following let’s move on to the driver installation in the WIN 7 system.
The Arduino folder contains both the Arduino program itself and the drivers that allow the Arduino to be connected to your computer by a USB cable. Before we launch the Arduino software, you are going to install the USB drivers.

Plug one end of your USB cable into the Arduino and the other into a USB socket on your computer.
When you connect the UNO board to your computer at the first time, right click the icon of your “Computer” —>for “Properties”—> click “Device manager”, under “Other Devices”, you should see an icon for “Unknown device” with a little yellow warning triangle next to it. This is your Arduino.

Driver 1.png
Then right-click on the device and select the top menu option (Update Driver Software...) shown as the figure below..
Driver 2.png

It will then be prompted to either “Search Automatically for updated driversoftware” or “Browse my computer for driver software”. Shown as below.
In this page, select “Browse my computer for driver software”.
Driver 3.png

After that, select the option to browseand navigate to the “drivers” folder of Arduino installation.
KS0286-4.png

Click “Next” and you may get a security warning, if so, allow the software to be installed. Shown as below.
Driver 5.png

Once the software has been installed, you will get a confirmation message. Installation completed, click “Close”.
Driver 6.png

Up to now, the driver is installed well. Then you can right click “Computer” —>“Properties”—>“Device manager”, you should see the device as the figure shown below.
Driver 7.png


Example Use: Displaying Hello World

Overview
It is very simple. You can use only a main board and a USB cable to display the “Hello World!”. It is a communication experiment between the control board and PC. This is an entry experiment for you to enter the Arduino programming world.
Note that need to use a serial communication software, Arduino IDE.
In the above part, you can check the detailed use of Arduino IDE.

Component Required

  • UNO R3 control board*1
  • USB cable*1

Component Introduction

Keyestudio UNO R3 Board

Keyestudio UNO R3 development board is a microcontroller board based on the ATmega328P (datasheet), fully compatible with ARDUINO UNO REV3. 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 with a AC-to-DC adapter or battery to get started.

Connect It Up
Connect the UNO board to your computer using the USB cable. The green power LED should go on.
Step2.jpg

Upload the Code
Below is an example code for displaying the Hello World!

int val;
int ledpin=13;
void setup()
{
Serial.begin(9600);
pinMode(ledpin,OUTPUT);
}
void loop()
{
val=Serial.read();
if(val=='R')
{
digitalWrite(ledpin,HIGH);
delay(500);
digitalWrite(ledpin,LOW);
delay(500);
Serial.println("Hello World!");
}
}

Select the Arduino Board
Open the Arduino IDE, you’ll need to click the “Tools”, then select the Board that corresponds to your Arduino.
Board.jpg


Select your serial port
Select the serial device of the Arduino board from the Tools | Serial Port menu.
Note: to avoid errors, the COM Port should keep the same as the Ports shown on Device Manager.
Manager.jpg

Port.jpg

Then click verify button to check the errors. If compiling successfully, the message "Done compiling." will appear in the status bar.
Compile 2.jpg

After that, click the “Upload” button to upload the code. Wait a few seconds - you should see the RX and TX leds on the board flashing. If the upload is successful, the message "Done uploading." will appear in the status bar.

(Note: If you have an Arduino Mini, NG, or other board, you'll need to physically present the reset button on the board immediately before pressing the upload button.)

Upload 2.jpg


Open the Serial Monitor
After that, click the serial monitor button to open the serial monitor.
UNO monitor.png
Then set the baud rate as 9600, enter an “R” and click Send, you should see the RX led on the board blink once, and then D13 led blink once, finally "Hello World!" is showed on the monitor, and TX led blink once.
Congrats! Your first simple program is complete.
Ks0248 monitor 2.png



Project Details


Project 1: Hello World

Introduction
As for starters, we will begin with something simple. In this project, you only need an Arduino and a USB cable to start the "Hello World!" experiment.
This is a communication test of your Arduino and PC, also a primer project for you to have your first try of the Arduino world!

Hardware required

  • Arduino board x1
  • USB cable x1

Sample program
After installing driver for Arduino, let's open Arduino software and compile code that enables Arduino to print "Hello World!" under your instruction.
Of course, you can compile code for Arduino to continuously echo "Hello World!" without instruction.
A simple If () statement will do the instruction trick. With the onboard LED connected to pin 13, we can instruct the LED to blink first when Arduino gets an instruction and then prints "Hello World!”.

int val;//define variable val
int ledpin=13;// define digital interface 13
void setup()
{
  Serial.begin(9600);// set the baud rate at 9600 to match the software set up. When connected to a specific device, (e.g. bluetooth), the baud rate needs to be the same with it.
  pinMode(ledpin,OUTPUT);// initialize digital pin 13 as output. When using I/O ports on an Arduino, this kind of set up is always needed.
}
void loop()
{
  val=Serial.read();// read the instruction or character from PC to Arduino, and assign them to Val.
  if(val=='R')// determine if the instruction or character received is “R”.
  {  // if it’s “R”,    
    digitalWrite(ledpin,HIGH);// set the LED on digital pin 13 on. 
    delay(500);
digitalWrite(ledpin,LOW);// set the LED on digital pin 13 off.    delay(500);

    Serial.println("Hello World!");// display“Hello World!”string.
  }
}


Result Show
Ks0402 1-1.png

Ks0402 1-2.png

Done compiling and uploading the code, click to open serial port monitor and set the baud rate to 9600. Enter an “R” and click Send. LED 13 will blink once, and your PC will receive information from Arduino: Hello World.
Ks0402 1-3.png
Now, the experiment is complete, so easy!


Note: if upload failed?
Check whether you select the correct Board and Port in Tools.
Ks0402 1-4.png



Project 2: LED blinking

Introduction
Blinking LED experiment is quite simple. In the "Hello World!" program, we have come across LED. This time, we are going to connect an LED to one of the digital pins rather than using the built-in LED13 of UNO board. Except an Arduino and an USB cable, we will need extra parts below.

Hardware required

  • Arduino board *1
  • Red M5 LED*1
  • 220Ω resistor*1
  • Breadboard*1
  • USB cable *1
  • Jumper wire* 2

Ks0402 2-2.png

We follow the connection diagram below to connect the components.
Here we use digital pin 10. We connect LED to a 220ohm resistor to avoid high current damaging the LED.

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample program

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
}


Test Result
After downloading this program, in the experiment, you will see the LED connected to pin 10 turning on and off, with an interval approximately one second.
The blinking LED experiment is now completed. Thank you!
thumb



Project 3: PWM

Introduction
PWM, short for Pulse Width Modulation, is a technique used to encode analog signal level into digital ones.
A computer cannot output analog voltage but only digital voltage values such as 0V or 5V. So we use a high resolution counter to encode a specific analog signal level by modulating the duty cycle of PMW.

The PWM signal is also digitalized because in any given moment, fully on DC power supply is either 5V (ON), or 0V (OFF).
The voltage or current is fed to the analog load (the device that uses the power) by repeated pulse sequence being ON or OFF. Being on, the current is fed to the load; being off, it's not.

With adequate bandwidth, any analog value can be encoded using PWM. The output voltage value is calculated via the on and off time.
Output voltage = (turn on time/pulse time) * maximum voltage value


thumb

PWM has many applications: lamp brightness regulating, motor speed regulating, sound making, etc.

The following are the three basic parameters of PMW:
thumb

  • 1. The amplitude of pulse width (minimum / maximum)
  • 2. The pulse period (The reciprocal of pulse frequency in 1 second)
  • 3. The voltage level (such as 0V-5V )

There are 6 PMW pins on Arduino board, namely digital pin 3, 5, 6, 9, 10, and 11.
In previous experiments, we have done "button-controlled LED", using digital signal to control digital pin.
This time, we will use a potentiometer to control the brightness of LED.

potentiometer


Hardware required

  • Arduino board *1
  • Potentiometer *1
  • Red M5 LED *1
  • 220Ω resistor *1
  • Breadboard *1
  • USB cable *1
  • Breadboard jumper wire * 6

The input of potentiometer is analog, so we connect it to analog port, and LED to PWM port. Different PWM signal can regulate the brightness of the LED.


Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code:
In the program compiling process, we will use the analogWrite (PWM interface, analog value) function.
In this experiment, we will read the analog value of the potentiometer and assign the value to PWM port, so there will be corresponding change to the brightness of the LED.
One final part will be displaying the analog value on the monitor window.
You can consider this as the "analog value reading" project adding the PWM analog value assigning part.
Below is a sample program for your reference.

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
}



thumb

Test Result
After uploading the program to the board, rotate the potentiometer knob, you can see the changes of displaying value, and also obvious change of LED brightness.
thumb


Project 4: Traffic light

Introduction
In the previous program, we have done the LED blinking experiment with one LED. Now, it’s time to up the stakes and do a bit more complicated experiment-traffic lights. Actually, these two experiments are similar. While in this traffic lights experiment, we use 3 LEDs with different colors rather than 1 LED.

Hardware required

  • Arduino board *1
  • USB cable *1
  • Red M5 LED*1
  • Yellow M5 LED*1
  • Green M5 LED*1
  • 220Ω resistor *3
  • Breadboard*1
  • Breadboard jumper wires* several

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample program
Since it is a simulation of traffic lights, the blinking time of each LED should be the same with those in traffic lights system.
In this program, we use Arduino delay () function to control delay time, which is much simpler than C language.

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 seconds
digitalWrite(redled, LOW);// turn off red LED
}


Test Result
When the uploading process is completed, you can see your own traffic light design.
The green light will be on for 5 seconds, and then off, followed by the yellow light blinking for 3 times, and then the red light on for 5 seconds, forming a cycle. Cycle then repeats.
Experiment is now completed. Thank you.
Note: this circuit design is very similar with the following LED chasing effect.
thumb


Project 5: LED chasing

LED

Introduction
We often see billboards composed of colorful LEDs. They are constantly changing to form various light effects. In this experiment, we compile a program to simulate LED chasing effect.
The long lead of LED is the positive side; short lead is negative.

Hardware required

  • Led *6
  • Arduino board *1
  • 220Ω resistor *6
  • Breadboard *1
  • USB cable*1
  • Breadboard wire *13

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code:

int BASE = 2 ;  // the I/O pin for the first LED
int NUM = 6;   // 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
   }  
}


Result
You can see the LEDs blink by sequence.
thumb


Project 6: Button-controlled LED

Introduction
I/O port means interface for INPUT and OUTPUT. Up until now, we have only used the OUTPUT function.
In this experiment, we will try to use the input function, which is to read the output value of device. We use 1 button and 1 LED using both input and output to give you a better understanding of the I/O function.
Button switches, familiar to most of us, are a switch value (digital value) component. When it's pressed, the circuit is in closed (conducting) state.

Ks0402 6-1.png

Hardware required

  • Button switch *1
  • Red M5 LED *1
  • 220Ω resistor *1
  • 10KΩ resistor *1
  • Breadboard *1
  • USB cable *1
  • Breadboard jumper wire * 6

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample program
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);}
}


Result
When the button is pressed, LED is on; otherwise, LED remains off. Congrats! the button controlled LED experiment is completed.
The simple principle of this experiment is widely used in a variety of circuit and electric appliances. You can easily come across it in your every day life.
One typical example is when you press a certain key of your phone, the backlight will be on.

thumb


Project 7: Active buzzer


thumb

Introduction
Active buzzer is a sound making element, widely used on computer, printer, alarm, electronic toy, telephone, timer, etc.
It has an inner vibration source. Simply connect it with 5V power supply, it can buzz continuously.

Hardware required

  • Buzzer *1
  • Arduino board *1
  • Breadboard *1
  • USB cable *1
  • Breadboard jumper wires * 2

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb

When connecting the circuit, pay attention to the positive and negative poles of the buzzer. In the photo, you can see there are red and black lines. When the circuit is finished, you can begin programming.

Sample program
Program is simple. You control the buzzer by outputting high/low level.

int buzzer=8;// 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
}


Result
After downloading the program, the buzzer experiment is completed.
You can hear the buzzer is ringing.
thumb


Project 8: Passive buzzer


thumb

Introduction
We can use Arduino to make many interactive works of which the most commonly used is acoustic-optic display.
All the previous experiment has something to do with LED. However, the circuit in this experiment can produce sound. Normally, the experiment is done with a buzzer or a speaker while buzzer is more simpler and easier to use.
The buzzer we introduced here is a passive buzzer. It cannot be actuated by itself, but by external pulse frequencies. Different frequencies produce different sounds. We can use Arduino to code the melody of a song, which is quite fun and simple.

Ks0402 8-1.png

Hardware required

  • Passive buzzer*1
  • Arduino board *1
  • Breadboard*1
  • USB cable *1
  • Breadboard jumper wire * 2

Connection for UNO R3:
Here we connect the passive buzzer to digital pin 8.
thumb

Connection for 2560 R3:
thumb


Sample Code:

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 
}
} 
} 

Result
After downloading the program, buzzer experiment is finished. You can hear the buzzer beep.
thumb

Project 9: RGB LED


thumb

Introduction:
Tricolor principle to display various colors;
PWM controlling ports to display full color;
Can be driven directly by Arduino PWM interfaces.

RGB LED

Hardware Required:

  • Arduino controller × 1
  • USB cable × 1
  • Full-color LED module × 1

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code:

int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED

int val;

void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  Serial.begin(9600);
}

void loop() 
{
for(val=255; val>0; val--)
  {
   analogWrite(11, val);
   analogWrite(10, 255-val);
   analogWrite(9, 128-val);
   delay(1); 
  }
for(val=0; val<255; val++)
  {
   analogWrite(11, val);
   analogWrite(10, 255-val);
   analogWrite(9, 128-val);
   delay(1); 
  }
 Serial.println(val, DEC);
}


Result:
Directly copy the above code into arduino IDE, and click upload thumb, wait for a few seconds, you can see a full-color LED.
thumb

Project 10: Photo Resistor


thumb

Introduction:
After completing all the previous experiments, you may acquire some basic understanding and knowledge about Arduino application. We have introduced digital input and output, analog input and PWM. Now, let's begin the learning of sensors applications.
Photo resistor (Photovaristor) is a resistor whose resistance varies from the different incident light strength. 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.
Photovaristor is commonly applied in the measurement of light, light control and photovoltaic conversion (convert the change of light into the change of electricity).
Photo resistor is also widely applied to various light control circuit, such as light control and adjustment, optical switches, etc.
We will start with a relatively simple experiment regarding to photovaristor application. Photovaristor is an element that will change its resistance as light strength changes. So need to read the analog values.
You can refer to the PWM experiment, replacing the potentiometer with photovaristor. When there is change in light strength, it will cause corresponding change on the LED.

Hardware Required:

  • Photo resistor*1
  • Red M5 LED*1
  • 10KΩresistor*1
  • 220Ωresistor*1
  • Breadboard*1
  • Breadboard jumper wires*several

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb

Sample Code:
After the connection, let's begin the program compiling. The program is similar to the one of PWM.
For change detail, please refer to the sample program 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 va
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin 11 as “output”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(potpin);// read the analog value of the sensor and assign it to val
Serial.println(val);// display the value of val
analogWrite(ledpin,val/4);// turn on the LED and set up brightness(maximum output value 255)
delay(10);// wait for 0.01
}

Result:
After downloading the program, you can change the light strength around the photovaristor and see corresponding brightness change of the LED.
Photovaristors has various applications in our everyday life. You can make other interesting interactive projects based on this one.
thumb


Project 11: Flame Sensor

Introduction:
Flame sensor (Infrared receiving triode) is specially used on robots to find the fire source. This sensor is of high sensitivity to flame.

Pro11-1.png

Working Principle:
Flame sensor is based on the principle that infrared ray is highly sensitive to flame. It has an infrared receiving tube specially designed to detect fire, and then convert the flame brightness into fluctuating level signal. The signals are then input into the central processor and be dealt with accordingly.

Sensor Connection:
The shorter lead of the receiving triode is for negative, the other one for positive. Connect negative to 5V pin, positive to resistor; connect the other end of the resistor to GND, connect one end of a jumper wire to a clip which is electrically connected to sensor positive, the other end to analog pin. As shown below:
thumb

Hardware Required:

  • Flame sensor *1
  • Control board *1
  • Buzzer *1
  • 10K resistor *1
  • USB cable *1
  • Breadboard jumper wire * 6

Experiment Connection:
1)Connecting buzzer:
Connect the controller board, prototype board, breadboard and USB cable according to the Arduino tutorial. Connect the buzzer to digital pin 8.

2)Connecting flame sensor:
Connect the sensor to analog pin 0.

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Experiment Principle:
When it's approaching a fire, the voltage value the analog port reads differs. If you use a multimeter, when there is no fire approaching, the voltage it reads is around 0.3V; when there is fire approaching, the voltage it reads is around 1.0V. The nearer the fire is, the higher the voltage is.
So in the beginning of the program, you can initialize voltage value i (no fire value); Then, continuously read the analog voltage value j and obtain difference value k=j-i; compare k with 0.6V (123 in binary) to determine whether there is a fire approaching or not; if yes, the buzzer will buzz.


Sample Code:

int flame=0;// select analog pin 0 for the sensor
 int Beep=9;// select digital pin 9 for the buzzer
 int val=0;// initialize variable
 void setup() 
{
  pinMode(Beep,OUTPUT);// set LED pin as “output”
 pinMode(flame,INPUT);// set buzzer pin as “input”
 Serial.begin(9600);// set baud rate at “9600”
 } 
void loop() 
{ 
  val=analogRead(flame);// read the analog value of the sensor 
  Serial.println(val);// output and display the analog value
  if(val>=600)// when the analog value is larger than 600, the buzzer will buzz
  {  
   digitalWrite(Beep,HIGH); 
   }else 
   {  
     digitalWrite(Beep,LOW); 
    }
   delay(500); 
}


Result:
This program can simulate an alarm when there is a fire. Everything is normal when there is no fire; when there is fire, the alarm will be set off immediately.
thumb


Project 12: LM35 Temperature Sensor

Ks0402 12-1.png

Introduction:
LM35 is a common and easy-to-use temperature sensor. It does not require other hardware. You just need an analog port to make it work. The difficulty lies in compiling the code to convert the analog value it reads to celsius temperature.
thumb

Hardware Required:
1.LM35*1
2.Control board *1
3.Breadboard*1
4.USB cable *1
5.Breadboard jumper wire *5

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code:

int potPin = 0; // initialize analog pin 0 for LM35 temperature sensor
void setup()
{
Serial.begin(9600);// set baud rate at”9600”
}
void loop()
{
int val;// define variable
int dat;// define variable
val=analogRead(0);// read the analog value of the sensor and assign it to val
dat=(125*val)>>8;// temperature calculation formula
Serial.print("Tep:");// output and display characters beginning with Tep
Serial.print(dat);// output and display value of dat
Serial.println("C");// display “C” characters
delay(500);// wait for 0.5 second
}

Result:
After downloading the program, you can open the monitoring window to see the current temperature.
thumb



Project 13: Tilt Switch

Introduction:
Tilt switch controlling the LED ON and OFF.

Ball Tilt Switch

Hardware Required:
1.Ball switch*1
2.Control board *1
3.Led *1
4.220Ω resistor*1
5.10KΩ resistor*1
6.USB cable *1
7.Breadboard jumper wire *5

Connection for UNO R3:
Connect the ball tilt switch, LED and resistors to control board. Connect the LED to digital pin 8, ball switch to analog pin 5.
thumb

Connection for 2560 R3:
thumb


Experiment Principle:
When one end of the switch is below horizontal position, the switch is on. The voltage of the analog port is about 5V (1023 in binary). The LED will be on.
When the other end of the switch is below horizontal position, the switch is off. The voltage of the analog port is about 0V (0 in binary). The LED will be off.
In the program, we determine whether the switch is on or off according to the voltage value of the analog port, whether it's above 2.5V (512 in binary) or not.

Sample Code:

void setup() 
{ 
  pinMode(8,OUTPUT);// set digital pin 8 as “output” 
} 
void loop() 
{ 
int i;// define variable i 
while(1) 
{ 
  i=analogRead(5);// read the voltage value of analog pin 5 
  if(i>512)// if larger that 512(2.5V) 
  { 
    digitalWrite(8,LOW);// turn on LED 
  } 
  else// otherwise 
  { 
    digitalWrite(8,HIGH);// turn off LED 
  } 
 } 
}


Result:
Hold the breadboard with your hand. Tilt it to a certain extent, the LED will be on. If there is no tilt, the LED will be off.
The principle of this experiment can be applied to relay control.
Experiment is completed.Thank you!
thumb

Project 14: IR Remote Control


thumb

Introduction:
What is an infrared receiver?
The signal from the infrared remote controller is a series of binary pulse code. To avoid interference from other infrared signals during the wireless transmission, the signal is pre-modulate at a specific carrier frequency and then send out by an infrared emission diode. The infrared receiving device needs to filter out other wave and receive signal at that specific frequency and modulate it back to binary pulse code, known as demodulation.

Working Principle:
The built-in receiver converts the light signal it received from the sender into feeble electrical signal. The signal will be amplified by the IC amplifier. After automatic gain control, band-pass filtering, demodulation, wave shaping, it returns to the original code. The code is then input to the code identification circuit by the receiver's signal output pin.
The pin and the connection of the infrared receiving head.

Pin and wiring of infrared receiver:
thumb

Infrared receiver has 3 pins.
When you use it, connect VOUT to analog pin, GND to GND, VCC to +5V.
thumb


Hardware Required:

  • Infrared remote controller x1
  • Infrared receiver x1
  • LED x6
  • 220Ω resistor x6
  • Breadboard wire x 10


Connection Diagram:
First, connect the controller board; then connect the infrared receiver as the above mentioned, connect VOUT to digital pin 11, connect the LEDs with resistors and connect the resistors to pin2,3,4,5,6,7.

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Experimental Principle:
If you want to decode the code from a remote controller, you must first know how it's coded. The coding method we use here is NEC protocol. Below is a brief introduction.

NEC protocol:
Features:
(1) 8 bit address and 8 bit command length
(2) address and command are transmitted twice for reliability
(3) pulse distance modulation
(4) carrier frequency of 38 KHZ
(5) bit time of 1.125ms or 2.25ms

Protocol is as below:

  • Definition of logical 0 and 1 is as below


thumb

  • Pulse transmitted when button is pressed and immediately released.


thumb

The picture above shows a typical pulse train of the NEC protocol. With this protocol the LSB is transmitted first. In this case Address $59 and Command $16 is transmitted. A message is started by a 9ms AGC burst, which was used to set the gain of the earlier IR receivers.
This AGC burst is then followed by a 4.5ms space, which is then followed by the address and command. Address and Command are transmitted twice. The second time all bits are inverted and can be used for verification of the received message. The total transmission time is constant because every bit is repeated with its inverted length. If you are not interested in this reliability, you can ignore the inverted values, or you can expand the Address and Command to 16 bits each!

  • Pulse transmitted when button is pressed and released after a period of time


thumb

A command is transmitted only once, even when the key on the remote control remains pressed. Every 110ms a repeat code is transmitted for as long as the key remains down. This repeat code is simply a 9ms AGC pulse followed by a 2.25ms space and a 560µs burst.

  • Repeat pulse


thumb

Note: when the pulse enters the integrated receiver, there will be decoding, signal amplifying and wave shaping process. So you need to make sure the level of the output is just the opposite from that of the signal sending end. That is when there is no infrared signal, the output end is in high level; when there is infrared signal, the output end is in low level. You can see the pulse of the receiving end in the oscilloscope. Try to better understand the program base on what you see.


Sample Code:
Note: add IRremote folder into installation directory \Arduino\compiler libraries, or it will fail to compile.
For example:C:\Program Files\Arduino\libraries

#include <IRremote.h>
int RECV_PIN = 11;
int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7;
long on1  = 0x00FF6897;
long off1 = 0x00FF9867;
long on2 = 0x00FFB04F;
long off2 = 0x00FF30CF;
long on3 = 0x00FF18E7;
long off3 = 0x00FF7A85;
long on4 = 0x00FF10EF;
long off4 = 0x00FF38C7;
long on5 = 0x00FF5AA5;
long off5 = 0x00FF42BD;
long on6 = 0x00FF4AB5;
long off6 = 0x00FF52AD;
IRrecv irrecv(RECV_PIN);
decode_results results;
// Dumps out the decode_results structure.
// Call this after IRrecv::decode()
// void * to work around compiler issue
//void dump(void *v) {
//  decode_results *results = (decode_results *)v
void dump(decode_results *results) {
  int count = results->rawlen;
  if (results->decode_type == UNKNOWN) 
    {
     Serial.println("Could not decode message");
    } 
  else 
   {
    if (results->decode_type == NEC) 
      {
       Serial.print("Decoded NEC: ");
      } 
    else if (results->decode_type == SONY) 
      {
       Serial.print("Decoded SONY: ");
      } 
    else if (results->decode_type == RC5) 
      {
       Serial.print("Decoded RC5: ");
      } 
    else if (results->decode_type == RC6) 
      {
       Serial.print("Decoded RC6: ");
      }
     Serial.print(results->value, HEX);
     Serial.print(" (");
     Serial.print(results->bits, DEC);
     Serial.println(" bits)");
   }
     Serial.print("Raw (");
     Serial.print(count, DEC);
     Serial.print("): ");
 for (int i = 0; i < count; i++) 
     {
      if ((i % 2) == 1) {
      Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
     } 
    else  
     {
      Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
     }
    Serial.print(" ");
     }
      Serial.println("");
     }
void setup()
 {
  pinMode(RECV_PIN, INPUT);   
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);  
  pinMode(13, OUTPUT);
  Serial.begin(9600);
   irrecv.enableIRIn(); // Start the receiver
 }
int on = 0;
unsigned long last = millis();

void loop() 
{
  if (irrecv.decode(&results)) 
   {
    // If it's been at least 1/4 second since the last
    // IR received, toggle the relay
    if (millis() - last > 250) 
      {
       on = !on;
//       digitalWrite(8, on ? HIGH : LOW);
       digitalWrite(13, on ? HIGH : LOW);
       dump(&results);
      }
    if (results.value == on1 )
       digitalWrite(LED1, HIGH);
    if (results.value == off1 )
       digitalWrite(LED1, LOW); 
    if (results.value == on2 )
       digitalWrite(LED2, HIGH);
    if (results.value == off2 )
       digitalWrite(LED2, LOW); 
    if (results.value == on3 )
       digitalWrite(LED3, HIGH);
    if (results.value == off3 )
       digitalWrite(LED3, LOW);
    if (results.value == on4 )
       digitalWrite(LED4, HIGH);
    if (results.value == off4 )
       digitalWrite(LED4, LOW); 
    if (results.value == on5 )
       digitalWrite(LED5, HIGH);
    if (results.value == off5 )
       digitalWrite(LED5, LOW); 
    if (results.value == on6 )
       digitalWrite(LED6, HIGH);
    if (results.value == off6 )
       digitalWrite(LED6, LOW);        
    last = millis();      
irrecv.resume(); // Receive the next value
  }
}


Program Function:
Decode the coded pulse signal emitted by the remote controller, then execute corresponding action according to the results of the decoding.
In this way, you are able to control your device with remote control.


Test Result:
thumbthumb


Project 15: Analog Value Reading

Introduction:
In this experiment, we will begin the learning of analog I/O interfaces.
On an Arduino, there are 6 analog interfaces numbered from 0 to 5. These 6 interfaces can also be used as digital ones numbered as 14-19.
After a brief introduction, let's begin our project. Potentiometer used here is a typical output component of analog value that is familiar to us.

Hardware Required:

  • Potentiometer *1
  • UNO board *1
  • Breadboard*1
  • USB cable *1
  • Breadboard jumper wire * 3

Circuit Connection:
In this experiment, we will convert the resistance value of the potentiometer to analog ones, and display the analog value on the screen.
This is an application we need to master well for our future experiments.
Connection circuit as below:

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb

The analog interface we use here is interface A0.


Sample Code:
The program compiling is simple. An analogRead () Statement can read the value of the interface. The A/D acquisition of Arduino 328 is in 10 bits, so the value it reads is among 0 to 1023.
One difficulty in this project is to display the value on the screen, which is actually easy to learn.
First, we need to set the baud rate in voidsetup (). Displaying the value is a communication between Arduino and PC, so the baud rate of the Arduino should match the one in the PC's software setup. Otherwise, the display will be messy codes or no display at all.
In the lower right corner of the Arduino software monitor window, there is a button for baud rate set up. The setup here needs to match the one in the program.
The statement in the program is Serial.begin(); enclosed is the baud rate value, followed by statement for displaying. You can either use Serial.print() or Serial.println() statement.

int potpin=0;// initialize analog pin 0
int ledpin=13;// initialize digital pin 13
int val=0;// define val, assign initial value 0
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin as “output”
Serial.begin(9600);// set baud rate at 9600

}
void loop()
{
digitalWrite(ledpin,HIGH);// turn on the LED on pin 13
delay(50);// wait for 0.05 second
digitalWrite(ledpin,LOW);// turn off the LED on pin 13
delay(50);// wait for 0.05 second
val=analogRead(potpin);// read the analog value of analog pin 0, and assign it to val 
Serial.println(val);// display val’s value
}

Result:
The sample program uses the built-in LED connected to pin 13. Each time the device reads a value, the LED blinks once.
Below is the analog value it reads.
thumb


When you rotate the potentiometer knob, you can see the displayed value change. The reading of analog value is a very common function since most sensors output analog value. After calculation, we can have the corresponding value we need.
The experiment is now complete. Thank you.



Project 16: 74HC595


thumb

Introduction:
To put it simply, 74HC595 is a combination of 8-digit shifting register, memorizer and equipped with tri-state output. Here, we use it to control 8 LEDs.
You may wonder why use a 74HC595 to control LED? Well, think about how many I/O it takes for an Arduino to control 8 LEDs? Yes, 8.
For an Arduino, it has only 20 I/O including analog ports. So, to save port resources, we use 74HC595 to reduce the number of ports it needs. Using 74HC595 enables us to use 3 digital I/O port to control 8 LEDs!

The 74HC595 devices contain an 8-bit serial-in, parallel-out shift register that feeds an 8-bit D-type storage register. The storage register has parallel 3-state outputs. Separate clocks are provided for both the shift and storage register.
The shift register has a direct overriding clear (SRCLR) input, serial (SER) input, and serial outputs for cascading. When the output-enable (OE) input is high, the outputs are in the high-impedance state. Both the shift register clock (SRCLK) and storage register clock (RCLK) are positive-edge triggered. If both clocks are connected together, the shift register always is one clock pulse ahead of the storage register.


Hardware Required:

  • 74HC595 chip*1
  • UNO board *1
  • Red M5 LED*4
  • Green M5 LED*4
  • 220Ω resistor*8
  • Breadboard*1
  • USB cable *1
  • Breadboard jumper wires*several


Circuit Connection
Note: for pin 13 OE port of 74HC595, it needs to connect to GND.

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb

The circuit may seem complicated, but soon you will find it easy!


Sample Code:

int data = 2;// set pin 14 of 74HC595as data input pin SI 
int clock = 5;// set pin 11 of 74hc595 as clock pin SCK
int latch = 4;// set pin 12 of 74hc595 as output latch RCK 
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
void loop()
{
for(int i = 0; i < 256; i++)
{
updateLEDs(i);
delay(500);
}
}
void updateLEDs(int value)
{
digitalWrite(latch, LOW);//
shiftOut(data, clock, MSBFIRST, ~value);// serial data “output”, high level first
digitalWrite(latch, HIGH);// latch
}

Result:
After downloading the program, you can see 8 LEDs display 8-bit binary number.
thumb


Project 17: 1-digit LED Segment Display


thumb

Introduction:
LED segment displays are common for displaying numerical information.
They are widely applied on displays of electromagnetic oven, full automatic washing machine, water temperature display, electronic clock etc. So it is necessary that we learn how it works.

LED segment display is a semiconductor light-emitting device. Its basic unit is a light-emitting diode (LED).
LED segment display can be divided into 7-segment display and 8-segment display according to the number of segments. 8-segment display has one more LED unit (for decimal point display) than 7-segment one.

In this experiment, we use a 8-segment display. According to the wiring method of LED units, LED segment displays can be divided into display with common anode and display with common cathode.
Common anode display refers to the one that combine all the anodes of LED units into one common anode (COM).
For the common anode display, connect the common anode (COM) to +5V. When the cathode level of a certain segment is low, the segment is on; when the cathode level of a certain segment is high, the segment is off.

For the common cathode display, connect the common cathode (COM) to GND. When the anode level of a certain segment is high, the segment is on; when the anode level of a certain segment is low, the segment is off.

thumb

Each segment of the display consists of an LED. So when you use it, you also need use a current-limiting resistor. Otherwise, LED will be burnt out.

In this experiment, we use a common cathode display. As we mentioned above, for common cathode display, connect the common cathode (COM) to GND. When the anode level of a certain segment is high, the segment is on; when the anode level of a certain segment is low, the segment is off.


Hardware Required:

  • 1-digit 8-segment display *1
  • 220Ω resistor *8
  • Breadboard *1
  • UNO board *1
  • USB cable *1
  • Breadboard jumper wire * 12


Connection:
Refer to following connection diagram

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code:
There are seven segments for numerical display, one for decimal point display. Corresponding segments will be turned on when displaying certain numbers.
For example, when displaying number 1, b and c segments will be turned on.
We compile a subprogram for each number, and compile the main program to display one number every 2 seconds, cycling display number 0 ~ 9.
The displaying time for each number is subject to the delay time, the longer the delay time, the longer the displaying time.

// set the IO pin for each segment
int a=7;// set digital pin 7 for segment a
int b=6;// set digital pin 6 for segment b
int c=5;// set digital pin 5 for segment c
int d=10;// set digital pin 10 for segment d
int e=11;// set digital pin 11 for segment e
int f=8;// set digital pin 8 for segment f
int g=9;// set digital pin 9 for segment g
int dp=4;// set digital pin 4 for segment dp
void digital_0(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,LOW);
digitalWrite(dp,LOW);
}
void digital_1(void) // display number 1
{
unsigned char j;
digitalWrite(c,HIGH);// set level as “high” for pin 5, turn on segment c
digitalWrite(b,HIGH);// turn on segment b
for(j=7;j<=11;j++)// turn off other segments
digitalWrite(j,LOW);
digitalWrite(dp,LOW);// turn off segment dp
}
void digital_2(void) // display number 2
{
unsigned char j;
digitalWrite(b,HIGH);
digitalWrite(a,HIGH);
for(j=9;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
digitalWrite(c,LOW);
digitalWrite(f,LOW);
}
void digital_3(void) // display number 3
{digitalWrite(g,HIGH);
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(dp,LOW);
digitalWrite(f,LOW);
digitalWrite(e,LOW);
}
void digital_4(void) // display number 4
{digitalWrite(c,HIGH);
digitalWrite(b,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
digitalWrite(a,LOW);
digitalWrite(e,LOW);
digitalWrite(d,LOW);
}
void digital_5(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b, LOW);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e, LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void digital_6(void) // display number 6
{
unsigned char j;
for(j=7;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(c,HIGH);
digitalWrite(dp,LOW);
digitalWrite(b,LOW);
}
void digital_7(void) // display number 7
{
unsigned char j;
for(j=5;j<=7;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
for(j=8;j<=11;j++)
digitalWrite(j,LOW);
}
void digital_8(void) // display number 8
{
unsigned char j;
for(j=5;j<=11;j++)
digitalWrite(j,HIGH);
digitalWrite(dp,LOW);
}
void digital_9(void) // display number 5
{
unsigned char j;
digitalWrite(a,HIGH);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(d,HIGH);
digitalWrite(e, LOW);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
digitalWrite(dp,LOW);
}
void setup()
{
int i;// set variable
for(i=4;i<=11;i++)
pinMode(i,OUTPUT);// set pin 4-11as “output”
}
void loop()
{
while(1)
{
digital_0();// display number 0
delay(1000);// wait for 1s
digital_1();// display number 1
delay(1000);// wait for 1s
digital_2();// display number 2
delay(1000); // wait for 1s
digital_3();// display number 3
delay(1000); // wait for 1s
digital_4();// display number 4
delay(1000); // wait for 1s
digital_5();// display number 5
delay(1000); // wait for 1s
digital_6();// display number 6
delay(1000); // wait for 1s
digital_7();// display number 7
delay(1000); // wait for 1s
digital_8();// display number 8
delay(1000); // wait for 1s
digital_9();// display number 9
delay(1000); // wait for 1s
}
}

Result:
LED segment display will display the number from 0 to 9.
thumb



Project 18: 4-digit LED Segment Display


thumb

Introduction:
In this experiment, we use an Arduino to drive a common anode, 4-digit, 7-segment LED display.
For LED display, current-limiting resistors are indispensable. There are two wiring methods for Current-limiting resistor.
One is to connect one resistor for each anode, 4 in totals for d1-d4 anode. An advantage for this method is that it requires fewer resistors, only 4. But it cannot maintain consistent brightness.
Another method is to connect one resistor to each pin. It guarantees consistent brightness, but requires more resistors.

In this experiment, we use 8 220Ω resistors (we use 220Ω resistors because no 100Ω resistor available. If you use 100Ω, the displaying will be more brighter).

For 4-digit displays, there are 12 pins in total.
When you place the decimal point downward (see below photo position), the pin on the lower left part is referred to 1, the upper left part 12.
thumb


Manual for LED segment display:
thumb


Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code:

// display 1234 
	// select pin for cathode
	int a = 1;
	int b = 2;
	int c = 3;
	int d = 4;
	int e = 5;
	int f = 6;
	int g = 7;
	int dp = 8;
	// select pin for anode
	int d4 = 9;
	int d3 = 10;
	int d2 = 11;
	int d1 = 12;
	// set variable
	long n = 1230;
	int x = 100;
	int del = 55;  // fine adjustment for clock
	 
	void setup()
	{
	  pinMode(d1, OUTPUT);
	  pinMode(d2, OUTPUT);
	  pinMode(d3, OUTPUT);
	  pinMode(d4, OUTPUT);
	  pinMode(a, OUTPUT);
	  pinMode(b, OUTPUT);
	  pinMode(c, OUTPUT);
	  pinMode(d, OUTPUT);
	  pinMode(e, OUTPUT);
	  pinMode(f, OUTPUT);
	  pinMode(g, OUTPUT);
	  pinMode(dp, OUTPUT);
	}
/////////////////////////////////////////////////////////////
void loop()

{
 Display(1, 1);
 Display(2, 2);
 Display(3, 3);
 Display(4, 4);

}
///////////////////////////////////////////////////////////////
void WeiXuan(unsigned char n)//
{
    switch(n)
     {
	case 1: 
	  digitalWrite(d1,LOW);
 	  digitalWrite(d2, HIGH);
	  digitalWrite(d3, HIGH);
	  digitalWrite(d4, HIGH);   
	 break;
	 case 2: 
	  digitalWrite(d1, HIGH);
 	  digitalWrite(d2, LOW);
	  digitalWrite(d3, HIGH);
	  digitalWrite(d4, HIGH); 
	    break;
	  case 3: 
	    digitalWrite(d1,HIGH);
 	   digitalWrite(d2, HIGH);
	   digitalWrite(d3, LOW);
	   digitalWrite(d4, HIGH); 
	    break;
	  case 4: 
	   digitalWrite(d1, HIGH);
 	   digitalWrite(d2, HIGH);
	   digitalWrite(d3, HIGH);
	   digitalWrite(d4, LOW); 
	    break;
        default :
           digitalWrite(d1, HIGH);
	   digitalWrite(d2, HIGH);
	   digitalWrite(d3, HIGH);

	   digitalWrite(d4, HIGH);
        break;
	  }
}
void Num_0()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_1()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_2()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_3()
{
  digitalWrite(a, HIGH);

  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_4()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_5()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_6()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);

}
void Num_7()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void Num_8()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Num_9()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(dp,LOW);
}
void Clear()  // clear the screen
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);

  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(dp,LOW);
}
void pickNumber(unsigned char n)// select number
{
  switch(n)
  {
   case 0:Num_0();
   break;
   case 1:Num_1();
   break;
   case 2:Num_2();
   break;
   case 3:Num_3();
   break;
   case 4:Num_4();
   break;
   case 5:Num_5();
   break;
   case 6:Num_6();
   break;
   case 7:Num_7();
   break;
   case 8:Num_8();
   break;
   case 9:Num_9();
   break;
   default:Clear();
   break; 
  }
}
void Display(unsigned char x, unsigned char Number)//  take x as coordinate and display number
{
  WeiXuan(x);
  pickNumber(Number);
 delay(1);
 Clear() ; // clear the screen
}


Result:
Download the above code to the controller board and see the result.
The experiment result displays 1234 on the LED display.
thumb

Note: if it’s not displaying correctly, check the wiring.
Thank you!


Project 19: 8*8 LED Matrix


thumb

Introduction:
With low-voltage scanning, LED dot-matrix displays have some advantages such as power saving, long service life, low cost, high brightness, wide angle of view, long visual range, waterproof, and numerous specifications.
LED dot-matrix displays can meet the needs of different applications and thus have a broad development prospect.
This time, we will conduct an LED dot-matrix experiment to experience its charm firsthand.

Hardware Required:

  • 1 * Uno board
  • 1 * 8x8 dot-matrix
  • 8 * Resistor (220Ω)
  • 1 * Breadboard
  • 1 * USB cable
  • 16 * Jumper wires


Connection:
The external view of a dot-matrix is shown as follows:
thumb


The display principle of the 8*8 dot-matrix:
The 8*8 dot-matrix is made up of sixty-four LEDs, and each LED is placed at the cross point of a row and a column.
When the electrical level of a certain row is 1 and the electrical level of a certain column is 0, the corresponding LED will light up.

If you want to light up the LED on the first dot, you should set pin 9 to high level and pin 13 to low level.
If you want to light LEDs on the first row, you should set pin 9 to high level and pins 13, 3, 4, 10, 6, 11, 15 and 16 to low level.
If you want to light the LEDs on the first column, set pin 13 to low level and pins 9, 14, 8, 12, 1, 7, 2 and 5 to high level.

The internal view of a dot-matrix is shown as follows:

thumb

The principle of 74HC595 has been previously illustrated. One chip is used to control the rows of the dot-matrix while the other chip is used to control the columns.

Connect circuit as shown in the following diagram:

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code for Displaying “0”:

// set an array to store character of “0”
unsigned char Text[]={0x00,0x1c,0x22,0x22,0x22,0x22,0x22,0x1c};
void Draw_point(unsigned char x,unsigned char y)// point drawing function
{ clear_();
   digitalWrite(x+2, HIGH);
   digitalWrite(y+10, LOW);
   delay(1);
}
void show_num(void)// display function, call point drawing function
{
  unsigned char i,j,data;
  for(i=0;i<8;i++)
  {
    data=Text[i];
    for(j=0;j<8;j++)
    {
      if(data & 0x01)Draw_point(j,i);
      data>>=1;

    }  
  }
}
void setup(){ 
int i = 0 ; 
for(i=2;i<18;i++) 
 { 
   pinMode(i, OUTPUT); 
  }  
  clear_(); 
}
void loop()
{ show_num();    
} 
void clear_(void)// clear screen
{for(int i=2;i<10;i++)
  digitalWrite(i, LOW);
  for(int i=0;i<8;i++)
  digitalWrite(i+10, HIGH);
}

Result:
Burn the program into Uno board, the dot-matrix will display 0.
thumb

Note: if it’s not displaying correctly, check the wiring.

Project 20: 1602 LCD


thumb

Introduction:
In this experiment, we use an Arduino to drive the 1602 LCD.
1602 LCD has wide applications. In the beginning,1602 LCD uses a HD44780 controller. Now, almost all 1602 LCD module uses a compatible IC, but their features are basically the same.


1602LCD Parameters:

  • Display Capacity: 16 × 2 characters
  • Chip Operating Voltage: 4.5 ~ 5.5V
  • Working Current: 2.0mA (5.0V)
  • Optimum working voltage of the module is 5.0V
  • Character Size: 2.95 * 4.35 (W * H) mm


Pin Description of 1602 LCD:

No. Mark Pin Description No. Mark Pin Description
1 VSS Power GND 9 D2 Date I/O
2 VDD Power Positive 10 D3 Date I/O
3 VL LCD Voltage Bias Signal 11 D4 Date I/O
4 RS Select data/command(V/L) 12 D5 Date I/O
5 R/W Select read/write(H/L) 13 D6 Date I/O
6 E Enable Signal 14 D7 Date I/O
7 D0 Date I/O 15 BLA Back Light Power Positive
8 D1 Date I/O 16 BLK Back Light Power Negative


Interface Description:
1. two power sources, one for module power, another one for backlight, generally use 5V. In this project, we use 3.3V for backlight.
2. VL is the pin for adjusting contrast ratio; it usually connects a potentiometer(no more than 5KΩ) in series for its adjustment.
In this experiment, we use a 1KΩ resistor. For the connection, it has 2 methods, namely high potential and low potential. Here, we use low potential method; connect the resistor and then the GND.
3. RS is a very common pin in LCD. It's a selecting pin for command/data. When the pin is in high level, it's in data mode; when it's in low level, it's in command mode.
4. RW pin is also very common in LCD. It's a selecting pin for read/write. When the pin is in high level, it's in read operation; when it's in low level, it's in write operation.
5. E pin is also very common in LCD. Usually, when the signal in the bus is stabilized, it sends out a positive pulse requiring read operation. When this pin is in high level, the bus is not allowed to have any change.
6. D0-D7 is 8-bit bidirectional parallel bus, used for command and data transmission.
7. BLA is anode for back light; BLK, cathode for back light.


4 Basic Operations of 1602LCD:

Read status input RS=L, R/W=H, E=H output D0-D7=status word
Write command input RS=L, R/W=H, D0-D7=command code, E=high pulse output none
Read data input RS=H, R/W=H, E=H output D0-D7=data
Write data input RS=H, R/W=L, D0-D7=data, E=high pulse output none


Hardware Required:

  • 1 * Controller board
  • 1 * 1602 LCD
  • 1 * Breadboard
  • 1 * potentiometer
  • 1 * USB cable
  • 16 *Jumper wires


Connection & Sample Program:
1602 can directly communicate with Arduino. According to the product manual, it has two connection methods, namely 8-bit connection and 4-bit connection.
8-bit connection method:
thumb

Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb


Sample Code A:

int DI = 12;
int RW = 11;
int DB[] = {3, 4, 5, 6, 7, 8, 9, 10};// use array to select pin for bus
int Enable = 2;
void LcdCommandWrite(int value) {
// define all pins
int i = 0;
for (i=DB[0]; i <= DI; i++) // assign value for bus
{
   digitalWrite(i,value & 01);// for 1602 LCD, it uses D7-D0( not D0-D7) for signal identification; here, it’s used for signal inversion. 
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);  // wait for 1ms
digitalWrite(Enable,LOW);
delayMicroseconds(1);  // wait for 1ms
}
void LcdDataWrite(int value) {
// initialize all pins
int i = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[7]; i++) {
   digitalWrite(i,value & 01);
   value >>= 1;
}
digitalWrite(Enable,LOW);
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1);  // wait for 1ms
}
void setup (void) {
int i = 0;
for (i=Enable; i <= DI; i++) {
   pinMode(i,OUTPUT);
}
delay(100);
// initialize LCD after a brief pause
// for LCD control
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size 
delay(64);                      
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size 
delay(50);                      
LcdCommandWrite(0x38);  // select as 8-bit interface, 2-line display, 5x7 character size             
delay(20);                      
LcdCommandWrite(0x06);  // set input mode
                         // auto-increment, no display of shifting
delay(20);                      
LcdCommandWrite(0x0E);  // display setup
                         // turn on the monitor, cursor on, no flickering
delay(20);                      
LcdCommandWrite(0x01);  // clear the screen, cursor position returns to 0
delay(100);                      
LcdCommandWrite(0x80);  //  display setup
                         //  turn on the monitor, cursor on, no flickering

delay(20);                      
}

void loop (void) {
  LcdCommandWrite(0x01);  // clear the screen, cursor position returns to 0  
  delay(10); 
  LcdCommandWrite(0x80+3); 
  delay(10);                     
  // write in welcome message 
  LcdDataWrite('W');
  LcdDataWrite('e');
  LcdDataWrite('l');
  LcdDataWrite('c');
  LcdDataWrite('o');
  LcdDataWrite('m');

  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('t');
  LcdDataWrite('o');
  delay(10);
  LcdCommandWrite(0xc0+1);  // set cursor position at second line, second position
  delay(10); 
  LcdDataWrite('g');
  LcdDataWrite('e');
  LcdDataWrite('e');
  LcdDataWrite('k');
  LcdDataWrite('-');
  LcdDataWrite('w');
  LcdDataWrite('o');
  LcdDataWrite('r');
  LcdDataWrite('k');
  LcdDataWrite('s');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('p');
  delay(5000);
  LcdCommandWrite(0x01);  // clear the screen, cursor returns to 0  
  delay(10);
  LcdDataWrite('I');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('m');
  LcdDataWrite(' ');
  LcdDataWrite('h');
  LcdDataWrite('o');
  LcdDataWrite('n');
  LcdDataWrite('g');
  LcdDataWrite('y');
  LcdDataWrite('i');
  delay(3000);
  LcdCommandWrite(0x02); // set mode as new characters replay old ones, where there is no new ones remain the same.
  delay(10);
  LcdCommandWrite(0x80+5); // set cursor position at first line, sixth position
  delay(10);  

  LcdDataWrite('t');
  LcdDataWrite('h');
  LcdDataWrite('e');
  LcdDataWrite(' ');
  LcdDataWrite('a');
  LcdDataWrite('d');
  LcdDataWrite('m');
  LcdDataWrite('i');
  LcdDataWrite('n');
  delay(5000);
}


thumb


4-bit connection method:
When using this module, 8-bit connection uses all the digital pins of the Arduino, leaving no pin for sensors. What then? you can use 4-bit connection.

Connection Circuit:
Connection for UNO R3:

thumb

Connection for 2560 R3:
thumb

After the connection, upload below code to the controller board and see how it goes.


Sample Code B:

int LCD1602_RS=12;   
int LCD1602_RW=11;   
int LCD1602_EN=10;   
int DB[] = { 6, 7, 8, 9};
char str1[]="Welcome to";
char str2[]="geek-workshop";
char str3[]="this is the";
char str4[]="4-bit interface";

void LCD_Command_Write(int command)
{
int i,temp;
digitalWrite( LCD1602_RS,LOW);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=command & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(command & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1); 
digitalWrite( LCD1602_EN,LOW);
}

void LCD_Data_Write(int dat)
{
int i=0,temp;
digitalWrite( LCD1602_RS,HIGH);
digitalWrite( LCD1602_RW,LOW);
digitalWrite( LCD1602_EN,LOW);

temp=dat & 0xf0;
for (i=DB[0]; i <= 9; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1);
digitalWrite( LCD1602_EN,LOW);

temp=(dat & 0x0f)<<4;
for (i=DB[0]; i <= 10; i++)
{
   digitalWrite(i,temp & 0x80);
   temp <<= 1;
}

digitalWrite( LCD1602_EN,HIGH);
delayMicroseconds(1); 
digitalWrite( LCD1602_EN,LOW);
}

void LCD_SET_XY( int x, int y )
{
  int address;
  if (y ==0)    address = 0x80 + x;
  else          address = 0xC0 + x;
  LCD_Command_Write(address); 
}

void LCD_Write_Char( int x,int y,int dat)
{
  LCD_SET_XY( x, y ); 
  LCD_Data_Write(dat);
}

void LCD_Write_String(int X,int Y,char *s)
{
    LCD_SET_XY( X, Y );    // address setup 
    while (*s)             // write character string
    {
      LCD_Data_Write(*s);   
      s ++;
    }
}

void setup (void) 
{
  int i = 0;
  for (i=6; i <= 12; i++) 
   {
     pinMode(i,OUTPUT);
   }
  delay(100);
  LCD_Command_Write(0x28);// 4 wires, 2 lines 5x7
  delay(50); 
  LCD_Command_Write(0x06);
 
 delay(50); 
  LCD_Command_Write(0x0c);
  delay(50); 
  LCD_Command_Write(0x80);
  delay(50); 
  LCD_Command_Write(0x01);
  delay(50); 

}

void loop (void)
{
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(3,0,str1);// line 1, start at the fourth address
   delay(50);
   LCD_Write_String(1,1,str2);// line 2, start at the second address
   delay(5000);
   LCD_Command_Write(0x01);
   delay(50);
   LCD_Write_String(0,0,str3);
   delay(50);
   LCD_Write_String(0,1,str4);
   delay(5000);
}


Project 21: Servo Control

Introduction:
Servo motor is a position control rotary actuator. It mainly consists of housing, circuit board, core-less motor, gear and position sensor.

Working principle:
The receiver or MCU outputs a signal to the servo motor. The motor has a built-in reference circuit that gives out reference signal, cycle of 20ms and width of 1.5ms. The motor compares the acquired DC bias voltage to the voltage of the potentiometer and outputs a voltage difference.
The IC on the circuit board will decide the rotating direction accordingly and drive the core-less motor. The gear then pass the force to the shaft. The sensor will determine if it has reached the commanded position according to the feedback signal.

Servomotors are used in control systems that requires to have and maintain different angles. When the motor speed is definite, the gear will cause the potentiometer to rotate. When the voltage difference reduces to zero, the motor stops. Normally, the rotation angle range is among 0-90 degrees.

thumb

Servomotor comes with many specifications. But all of them have three connection wires, distinguished by brown, red, orange colors (different brand may have different color). Brown one is for GND, red one for power positive, orange one for signal line.

thumb

The rotation angle of the servo motor is controlled by regulating the duty cycle of the PWM(Pulse-Width Modulation) signal.
The standard cycle of the PWM signal is fixed at 20ms (50 Hz), and the pulse width is distributed between 1ms-2ms. The width corresponds the rotation angle from 0° to 90°.
But note that for different brand motor, the same signal may have different rotation angle.
thumb


After some basic knowledge, let's learn how to control a servomotor.
In this experiment, you only need a servomotor and several jumper wires.


Hardware Required:

  • Servo motor*1
  • Breadboard jumper wire* 3


Connection & Sample Program:
There are two ways to control a servomotor with Arduino.
One is to use a common digital sensor port of Arduino to produce square wave with different duty cycle to simulate PWM signal and use that signal to control the position of the motor.
Another way is to directly use the Servo function of the Arduino to control the motor. In this way, the program will be easier but it can only control two-contact motor because of the servo function, only digital pin 9 and 10 can be used.
The Arduino drive capacity is limited. So if you need to control more than one motor, you will need external power.


Method 1:
Connect the signal line of motor to digital pin 9.
Connection for UNO R3:
thumb

Connection for 2560 R3:
thumb

Compile a program to control the motor to rotate to the commanded angle, and display the angle on the monitor.


Sample Code A:

int servopin=9;// select digital pin 9 for servomotor signal line
int myangle;// initialize angle variable
int pulsewidth;// initialize width variable
int val;
void servopulse(int servopin,int myangle)// define a servo pulse function
{
pulsewidth=(myangle*11)+500;// convert angle to 500-2480 pulse width
digitalWrite(servopin,HIGH);// set the level of servo pin as “high”
delayMicroseconds(pulsewidth);// delay microsecond of pulse width
digitalWrite(servopin,LOW);// set the level of servo pin as “low”
delay(20-pulsewidth/1000);
}

void setup()
{
pinMode(servopin,OUTPUT);// set servo pin as “output”
Serial.begin(9600);// connect to serial port, set baud rate at “9600”
Serial.println("servo=o_seral_simple ready" ) ;
}
void loop()// convert number 0 to 9 to corresponding 0-180 degree angle, LED blinks corresponding number of time
{
val=Serial.read();// read serial port value
if(val>='0'&&val<='9')
{
val=val-'0';// convert characteristic quantity to numerical variable
val=val*(180/9);// convert number to angle
Serial.print("moving servo to ");
Serial.print(val,DEC);
Serial.println();
for(int i=0;i<=50;i++) // giving the servo time to rotate to commanded position
{
servopulse(servopin,val);// use the pulse function
}
}
}


Method 2:
Let's first take a look at the Arduino built-in servo function and some of its common statements.

  • 1. attach(interface)——select pin for servo, can only use pin 9 or 10.
  • 2. write(angle)——used to control the rotation angle of the servo, can set the angle among 0 degree to 180 degree.
  • 3. read()——used to read the angle of the servo, consider it a function to read the value in the write() function.
  • 4. attached()——determine whether the parameter of the servo is sent to the servo pin.
  • 5. detach()—— disconnect the servo and the pin, and the pin(digital pin 9 or 10) can be used for PWM port.

Note: the written form of the above statements are "servo variable name. specific statement ()", e.g. myservo. Attach (9).

Still, connect the signal line of servo to pin 9. Please refer to the connection diagram mentioned above.


Sample program B:
Please remember to place the Servo.h library into your arduino libraries directory.

#include <Servo.h>
/* define a header file. Special attention here, you can call the servo function directly from Arduino's software menu  bar Sketch>Importlibrary>Servo, or input 
#include <Servo.h>. Make sure there is a space between #include and  <Servo.h>. Otherwise, it will cause compile error.*/
Servo myservo;// define servo variable name
void setup()
{
myservo.attach(9);// select servo pin(9 or 10)
}
void loop()
{
myservo.write(90);// set rotation angle of the motor
}


Above are the two methods to control the servo. You can choose either one according to your liking or actual need.
thumb


Resources

  • Video:

http://video.keyestudio.com/KS0069/

  • Download all the datasheet:

https://drive.google.com/open?id=1rq0BzIjfrSJL9DOdKW8xjqtbqMkk5TlP

Buy from

Official Website

With UNO http://www.keyestudio.com/keyestudio-basic-starter-kit-for-arduino-starter-with-uno-r3.html

No Board http://www.keyestudio.com/keyestudio-basic-starter-kit-for-arduino-starter-no-board.html

With 2560 R3 http://www.keyestudio.com/keyestuio-basic-starter-kit-for-arduino-starter-with-mega2560-r3.html