Professional Documents
Culture Documents
Package Include:
1, 1 X Arduino Duemilnove ATmega 328 board
2, 1 X Prototyping Shield
3, 1 X Mini breadbord
4, 1 X bundle Breadboard Jump Wires (Approximately 70 pcs)
5, 1 X 9v power adapter
6, 1 X 40Pin Straight Male Headers
7, 1 X Universal board
8, 1 X IR Remote Control
9, 1 X Infrared Receiver
10, 1 X LM35 Temperature Sensor
11, 1 X Ambient Light sensor
12, 1 X Tilt switch sensor
13, 1 X 1602 LCD Dispaly ( default blue one, or you can choose the green one)
14, 1 X 7seg-4digit LED Display
15, 1 X Rotary Potentiometer
16, 1 X Buzzer
17, 5 X Mini Push Button
18, 1 X 2P Screw Terminal Block Connector 5mm
19, 4 X Red LED 3mm
20, 4 X Yellow LED 3mm
21, 4 X Green LED 3mm
22, 2 X Blue LED 3mm
23, 1 X color LED 5mm
24, 10 X 220Ω Resistors
25, 10 X 1 kΩ Resistors
26, 10 X 10 kΩ Resistors
27, 1x High quality electronic tool plastic Box
28, Email CD ROM information( Lessons and User Manuals )
The mini breadboard has 170 Tie points. It is Compatible with all kind of
ProtoShield. Completely reusable,Reusable for fast build a prototype of an electronic
circuit. With Twin adhesive back, it could be fix and remove to any position easily. Its
Dimension: 45mm (L) X 34.5mm (W) X 9.5mm (H).
This mini breadboard has 10 rows 17 columns point. And the top and bottom Five row
columns are separate columns, with no internal connections made between them inside the
breadboard. The only five internal connections inside the breadboard are to any five vertical
consecutive holes . So the five point which are in one column is connected internally.
Overview
What is Arduino?
Arduino is a tool for making computers that can sense and control more of the physical world
than your desktop computer. It's an open-source physical computing platform based on a simple
microcontroller board, and a development environment for writing software for the board.
Arduino can be used to develop interactive objects, taking inputs from a variety of switches or
sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects
can be stand-alone, or they can be communicate with software running on your computer (e.g.
Flash, Processing, MaxMSP.) The boards can be assembled by hand or purchased preassembled;
the open-source IDE can be downloaded for free.
With this kit you can make a lot of interesting projuct.
Features :
Language Reference
The Arduino language is based on C / C + + . Arduino programs can be divided in three
main parts: structure, values (variables and constants), and functions.
You can check them in the Arduino website : http://arduino.cc/en/Reference/HomePage
Control Structures
if
if...else
for
switch case
while
do... while
break
continue
return
goto
Further Syntax
; (semicolon)
{} (curly braces)
// (single line comment)
/* */ (multi-line comment)
#define
www.bestqshop.com Ebay store: b2cqshop , E-qstore
www.bestqshop.com Ebay store: b2cqshop , E-qstore
#include
Operators
= (assignment operator)
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulo)
== (equal to)
!= (not equal to)
< (less than)
> (greater than)
<= (less than or equal to)
>= (greater than or equal to)
&& (and)
|| (or)
! (not)
++ (increment)
-- (decrement)
+= (compound addition)
-= (compound subtraction)
*= (compound multiplication)
/= (compound division)
Constants ( Variables )
HIGH | LOW
It means a different meaning depending on whether a pin is set to INPUT or OUTPUT .
HIGH: means high potential (+).
INPUT | OUTPUT
INPUT : to be in a high-impedance state
OUTPUT : to be in a low-impedance state
true | false
false : is defined as 0 (zero).
true : is often said to be defined as 1, which is correct, but true has a wider definition.
Any integer which is non-zero is TRUE, in a Boolean sense. So -1, 2 and -200 are all defined
as true, too, in a Boolean sense.
/*******************************************************************/
Structure:
setup()
The setup() function is called when a sketch starts. Use it to initialize variables, pin
modes, start using libraries, etc. The setup function will only run once, after each powerup or reset
of the Arduino board.
loop()
After creating a setup() function, which initializes and sets the initial values, the loop()
function does precisely what its name suggests, and loops consecutively, allowing your program to
change and respond. Use it to actively control the Arduino board.
Functions
Digital I/O
pinMode()
Syntax : pinMode(pin, mode)
Configures the specified pin to behave either as an input or an output. Digital pins :
( 0 ~ 13 ). Mode: either INPUT or OUTPUT
digitalWrite()
Syntax : digitalWrite(pin, value)
Write a HIGH or a LOW value to a digital pin. Digital pins : ( 0 ~ 13 ). value:
HIGH or LOW . For example: light a LED.
digitalRead()
Syntax : digitalWrite(pin, value)
Reads the value from a specified digital pin, either HIGH or LOW. Digital pins :
www.bestqshop.com Ebay store: b2cqshop , E-qstore
www.bestqshop.com Ebay store: b2cqshop , E-qstore
( 0 ~ 13 ). value: HIGH or LOW . For example : read the digital sensor’s value.
Analog I/O
analogRead()
Syntax : int analogReference(pin)
Reads the value from the specified analog pin. pins : 0 ~ 5 (Arduino Diecimila:
0 ~ 5 ; Arduino nano : 0~7). The Arduino board contains a 6 channel (8 channels on the Mini
and Nano, 16 on the Mega)
For example : in the analog sensor (10-bit analog to digital converter. ) , this means
that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.
analogWrite() // PWM
Syntax : analogWrite(pin, value)
Writes an analog value (PWM wave) to a pin.
Pin : On most Arduino boards (those with the ATmega168 or ATmega328), this
function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2
through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins
9, 10, and 11.
value: the duty cycle: between 0 (always off) and 255 (always on).
For example : in the analog sensor (10-bit analog to digital converter. ) , this means
that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023.
Time
delay()
Pauses the program for the amount of time (in miliseconds) specified as parameter.
(There are 1000 milliseconds in a second.)
delayMicroseconds()
Pauses the program for the amount of time (in microseconds) specified as parameter.
Math
/*****************************************************************************/
Now, go ahead. Let.s begin with the actual operation of this kit.
1. Ready
( 1 ). Find out the Prototype shield and mini breadboard from your
tool plastic Box . The item like the following picture:
( 2 ) Find out the arduino controller (Duemilnove ATmega 328 board) from your tool box too.
2 . And then , plug the other side to your computer’s USB interface. ( such as the
following picture ).
3. At this time , the power LED of Atmage 328 board will be light. And your computer
will will appear A dialog box like this picture. ( I am sorry the the Operating System showing
Chinese.)
Now, you can connect your arduino board to the computer , and upload my programs to the
Arduino board.
3. Upload my programs
(1) Make sure you have the right item selected in the Tools > Board menu. In particular,
newer Arduino Duemilanove boards come with an ATmega328, while older ones have an
ATmega168. To check, read the text on the microcontroller (the larger chip) on your Arduino
board.
(2) Check that the proper port is selected in the "Tools > Serial Port" menu (if your port doesn't
appear, restart the IDE with the board connected to the computer).
For the “ Serial Port” , you can check it in your computer’s Device Manager
When it show “Done Uploading” , That is great , the Arduino experiment works well .
Experiment
Lesson 1 : Color LED Flashing
1. What is an LED ?
The longer one lead is Anode ( “+”), the another one is Cathode (“ --”) .
First Experiment:
Prepare: 1 X LED , 1 X 220-ohm resistor , some Jumper cable.
To build the circuit, grab an LED and attach its long, positive leg (called the anode) to pin 8
through a 220-ohm resistor . Connect the cathode (the shorter, negative leg) directly to ground.
In the program below, the first thing you do is to initialize pin 8 as an output pin with the line
pinMode(8, OUTPUT);
In the main loop, you turn the LED on with the line:
digitalWrite(8, HIGH);
This supplies 5 volts to pin 8. That creates a voltage difference across the pins of the LED, and
lights it up. Then you turn it off with the line:
digitalWrite(8, LOW);
That takes pin 8 back to 0 volts, and turns the LED off. In between the on and the off, you want
enough time for a person to see the change, so the delay() commands tell the Arduino to do
nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else
happens for that amount of time. Once you've understood the basic examples, check out the
BlinkWithoutDelay example to learn how to create a delay while doing other things.
From the code , We can know that arduino language start with setup() function, and Use loop()
function to actively control the Arduino board .
setup() function is used to initialize variables, pin modes, start using libraries, etc. The setup
function will only run once, after each powerup or reset of the Arduino board.
loop() : After creating a setup() function, the loop() function does precisely what its name
suggests, and loops consecutively, allowing your program to change and respond. Use it to
actively control the Arduino board.
In this First Experiment, you can find that the LED flashes per 1 second.
Second Experiment:
Prepare: 6 X LED , 6 X 220-ohm resistor , some Jumper cable.
To build the circuit, attach LED to digital IO pin 1, 2, 3, 4, 5, 6 through 220-ohm resistor .
void flash(void)
{
unsigned char j,k;
for(k=0;k<=1;k++)//闪烁两次
{
for(j=1;j<=6;j++)//点亮 1~6 引脚相连的 led 灯
digitalWrite(j,HIGH);//点亮与 j 引脚相连的 led 灯
delay(200);//延时 200ms
for(j=1;j<=6;j++)//熄灭 1~6 引脚相连的 led 灯
digitalWrite(j,LOW);//熄灭与 j 引脚相连的 led 灯
delay(200);//延时 200ms
}
}
//led 灯花样显示样式 2 子程序
void style_2(void)
{
unsigned char j,k;
k=1;//设置 k 的初值为 1
for(j=3;j>=1;j--)
{
digitalWrite(j,HIGH);//点亮灯
digitalWrite(j+k,HIGH);//点亮灯
delay(400);//延时 400ms
k +=2;//k 值加 2
}
k=5;//设置 k 值为 5
for(j=1;j<=3;j++)
{
digitalWrite(j,LOW);//熄灭灯
digitalWrite(j+k,LOW);//熄灭灯
delay(400);//延时 400ms
k -=2;//k 值减 2
}
}
//led 灯花样显示样式 3 子程序
void style_3(void)
{
unsigned char j,k;//led 灯花样显示样式 3 子程序
k=5;//设置 k 值为 5
for(j=1;j<=3;j++)
{
digitalWrite(j,HIGH);//点亮灯
digitalWrite(j+k,HIGH);//点亮灯
delay(400);//延时 400ms
digitalWrite(j,LOW);//熄灭灯
digitalWrite(j+k,LOW);//熄灭灯
k -=2;//k 值减 2
}
k=3;//设置 k 值为 3
for(j=2;j>=1;j--)
{
digitalWrite(j,HIGH);//点亮灯
digitalWrite(j+k,HIGH);//点亮灯
delay(400);//延时 400ms
digitalWrite(j,LOW);//熄灭灯
digitalWrite(j+k,LOW);//熄灭灯
k +=2;//k 值加 2
}
}
void setup()
{
unsigned char i;
for(i=1;i<=6;i++)//依次设置 1~6 个数字引脚为输出模式
pinMode(i,OUTPUT);//设置第 i 个引脚为输出模式
}
void loop()
{
style_1();//样式 1
flash();//闪烁
style_2();//样式 2
flash();//闪烁
style_3();//样式 3
flash();//闪烁
}
The Code :
for(i=1;i<=6;i++)// Set the digital IO pins(1, 2, 3, 4, 5, 6) configured as “OUTPUT”
pinMode(i,OUTPUT);// Set the digital pins(i) configured as “OUTPUT”
1. What is an buzzer?
Features:
1) High sound pressure with low power consumption
2) Compact, lightweight
3) No-contact design makes element highly reliable and eliminates noise problem
4) Easily mountable
5) A wide variety of tones can be generated depending on casing design
Applications:
1) Telephone ringers
2) Confirmation tones in various office automation equipment
3) Used in a variety of consumer products such as microwave ovens and refrigerators
4) Clocks, toys games
5) Automobiles
In this experiment, because of the internal buzzer with a drive circuit, it can be used directly.
To build the circuit, attach buzzer to digital IO pin 7 . Connect the cathode (the shorter,
negative leg) directly to ground.
int buzzer=7;//设置控制蜂鸣器的数字 IO 脚
void setup()
{
pinMode(buzzer,OUTPUT);//设置数字 IO 脚模式,OUTPUT 为输出
}
void loop()
{
unsigned char i,j;//定义变量
while(1)
{
for(i=0;i<80;i++)//输出一个频率的声音
{
digitalWrite(buzzer,HIGH);//发声音
delay(1);//延时 1ms
digitalWrite(buzzer,LOW);//不发声音
delay(1);//延时 ms
}
for(i=0;i<100;i++)//输出另一个频率的声音
{
digitalWrite(buzzer,HIGH);//发声音
delay(2);//延时 2ms
digitalWrite(buzzer,LOW);//不发声音
delay(2);//延时 2ms
}
}
}
while loops will loop continuously, and infinitely, until the expression inside the parenthesis,
() becomes false. Something must change the tested variable, or the while loop will never exit.
This could be in your code, such as an incremented variable, or an external condition, such as
testing a sensor.
Lesson 3 : Button
1. What is an Button ?
When the button was not pressed, it’s pin 4 and pin 1 is connecting in a circuit, and pin 3 and
pin 2 is connectiong.
When the button was pressed, all of it’s pins are connecting in the circuit.
First Experiment:
Prepare: 1 X button (momentary button or switch), 1 X LED , 1 X 220-ohm
resistor , some Jumper cable.
To build the circuit, grab an LED and attach its long, positive leg (called the anode) to pin 7
through a 220-ohm resistor . Connect the cathode (the shorter, negative leg) directly to ground.
The switch wire like the following picture, one point is to the 5 volt supply, the other connect to
Analog Pin 1
int key=7;//设置控制按键的数字 IO 脚
void setup()
{
pinMode(key,OUTPUT);//设置数字 IO 引脚为输出模式
}
void loop()
{
int i;//定义变量
while(1)
{
i=analogRead(1);//读取模拟 1 口电压值
if(i>512)//如果电压值大于 512(即 0.25V)
digitalWrite(key,HIGH);//设置第七引脚为高电平,点亮 led 灯
else
digitalWrite(key,LOW);//设置第七引脚为低电平,熄灭 led 灯
}
}
Experiment:
Prepare: 3 X key, 1 X buzzer , 1 X red LED , ! X green LED . 2 X 220-ohm resistor ,
some Jumper cable.
To build the circuit, attach each key’s pin 1 to the 5 Volt supply, pin 2 to the board’s Analog
Pin 1 (key 1), pin 2 (key 2), pin 3 (key 3).
The LED attach to Digital pin 8 (red LED), pin 7 (green LED) through 220-ohm resistor.
The buzzer connect to Digital pin 5.
int RedLed=8;//定义第八引脚连接红灯
int GreenLed=7;//定义第七引脚连接绿灯
int i;//定义变量 i
int j=0;//定义变量 j
void buzzer()//蜂鸣器发出“嘀”声音子程序
{
for(i=0;i<80;i++)
{
digitalWrite(5,HIGH);//发声音
delay(1);//延时 1ms
digitalWrite(5,LOW);//不发声音
delay(1);//延时 ms
}
}
void key_scan()//按键扫描子程序
{
int key_1,key_2,key_3;//定义变量
key_1=analogRead(1);//读取模拟第一引脚的电压值
key_2=analogRead(2);//读取模拟第二引脚的电压值
key_3=analogRead(3);//读取模拟第三引脚的电压值
}
else //否则认为是抖动干扰,不做任何动作
{
return;//跳出本子函数
}
}
if(key_3>818)//如果按键 3 的电压值都大于 818(即模拟值的 4V),则可以判断按键 3 被按下
{
delay(10);//由于有抖动,所以延时 100ms 再一次判断
if(key_3>818)//如果按键 3 的电压值都大于 818(即模拟值的 4V),则可以判断按键 3 确实
被按下
{
buzzer();//蜂鸣器发出声音
digitalWrite(RedLed,LOW);//红灯灭
digitalWrite(GreenLed,LOW);//绿灯灭
}
else //否则认为是抖动干扰,不做任何动作
{
return;//跳出本子函数
}
}
}
void setup()
{
for(i=5;i<=8;i++)
{
pinMode(i,OUTPUT);//将 5~8 引脚设置为输出模式
}
}
void loop()
{
while(1)
{
key_scan();//循环扫描按键
}
}
The internal structure Of LED display. This is the Common cathode 7seg-4digit LED
Display that we will use in this Experiment.
First Experiment:
Prepare: 1 X 7seg-4digit LED Display , 8 X 220-ohm resistor , some Jumper
cable.
To build the circuit, attach the LED display’s (through a 220-ohm resistor) Pin 11, 7, 4, 2, 1,
10, 5, 3 to your Arduino board’s digital Pin 12, 11, 10, 9, 8, 7, 6, 5 .
Attach the LED display’s 12, 9, 8, 6 to the Ardiuno board’s digital Pin 4, 3, 2, 1 .
//设置控制各段的数字 IO 脚
int a = 12;
int b = 11;
int c = 10;
int d = 9;
int e = 8;
int f = 7;
int g = 6;
int dp = 5;
int ls1 = 1; //最右边第一位数码管
int ls2 = 2; //第二位数码管
int ls3 = 3; //第三位数码管
int ls4 = 4; //第四位数码管
//显示数字 0 , 11111100
void digital_0(void)
{
unsigned char j;
digitalWrite(dp,LOW);//给数字 5 引脚低电平,熄灭小数点 DP 段
digitalWrite(g,LOW);//熄灭 g 段
for(j=7;j<=12;j++)//点亮其余段
digitalWrite(j,HIGH);
}
//显示数字 1 , 01100000
void digital_1(void)
{
unsigned char j;
digitalWrite(a,LOW);
for(j=5;j<=9;j++)
digitalWrite(j,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
}
//显示数字 2
void digital_2(void)
{
unsigned char j;
digitalWrite(c,LOW);
digitalWrite(f,LOW);
digitalWrite(dp,LOW);
for(j=11;j<=12;j++)
digitalWrite(j,HIGH);
for(j=8;j<=9;j++)
digitalWrite(j,HIGH);
digitalWrite(g,HIGH);
}
//显示数字 3 22
void digital_3(void)
{
unsigned char j;
digitalWrite(e,LOW);
digitalWrite(f,LOW);
digitalWrite(dp,LOW);
for(j=9;j<=13;j++)
digitalWrite(j,HIGH);
digitalWrite(g,HIGH);
}
//显示数字 4
void digital_4(void)
{
digitalWrite(a,LOW);
digitalWrite(d,LOW);
digitalWrite(e,LOW);
digitalWrite(dp,LOW);
digitalWrite(b,HIGH);
digitalWrite(c,HIGH);
digitalWrite(f,HIGH);
digitalWrite(g,HIGH);
}
//显示数字 5
void digital_5(void)
{
unsigned char j;
digitalWrite(b,LOW);
digitalWrite(e,LOW);
digitalWrite(dp,LOW);
digitalWrite(a,HIGH);
for(j=6;j<=7;j++)
digitalWrite(j,HIGH);
for(j=9;j<=10;j++)
digitalWrite(j,HIGH);
}
//显示数字 6
void digital_6(void)
{
unsigned char j;
digitalWrite(b,LOW);
digitalWrite(dp,LOW);
digitalWrite(a,HIGH);
for(j=6;j<=10;j++)
digitalWrite(j,HIGH);
}
//显示数字 7
void digital_7(void)
{
unsigned char j;
for(j=5;j<=9;j++)
digitalWrite(j,LOW);
for(j=10;j<=12;j++)
digitalWrite(j,HIGH);
}
//显示数字 8
void digital_8(void)
{
unsigned char j;
digitalWrite(dp,LOW);
for(j=6;j<=12;j++)
digitalWrite(j,HIGH);
}
//显示数字 9
void digital_9(void)
{
unsigned char j;
digitalWrite(e,LOW);
digitalWrite(dp,LOW);
for(j=9;j<=12;j++)
digitalWrite(j,HIGH);
for(j=6;j<=7;j++)
digitalWrite(j,HIGH);
//显示----------------------------
void Display(unsigned char mun)
{
switch (mun) {
case 0:
digital_0();//显示数字 0
break;
case 1:
digital_1();
break;
case 2:
digital_2();
break;
case 3:
digital_3();
break;
case 4:
digital_4();
break;
case 5:
digital_5();
break;
case 6:
digital_6();
break;
case 7:
digital_7();
break;
case 8:
digital_8();
break;
case 9:
digital_9();
break;
default: return;
// if nothing else matches, do the default
// default is optional
}
}
void setup()
{
int i;//定义变量
for(i=1;i<=12;i++)
pinMode(i,OUTPUT);//设置 1~12 引脚为输出模式
}
void loop()
{
unsigned char i, j, t;
while(1)
{
t = 1;
for(j=0; j<4 ; j++)
{
digitalWrite(ls1,LOW); //熄灭数码管使能端
}
digitalWrite(ls4,LOW);
}
Display(i);
delay(2000);//延时 2s
t++;
First Experiment:
Prepare: 1 X Tilt Switch , 1 X LED , 1 X 220-ohm resistor , some Jumper cable.
To build the circuit, grab an LED and attach its long, positive leg (called the anode) to pin 8
through a 220-ohm resistor . Connect the cathode (the shorter, negative leg) directly to ground.
Attach the Tilt Switch’s silver pin to the 5 volt supply, the other side to Analog Pin 5 .
void setup()
{
pinMode(8,OUTPUT);//设置数字 8 引脚为输出模式
}
void loop()
{
int i;//定义变量 i
while(1)
{
i=analogRead(5);//读取模拟 5 口电压值
if(i>200)//如果大于 512(2.5V)
{
digitalWrite(8,HIGH);//点亮 led 灯
}
else//否则
{
digitalWrite(8,LOW);//熄灭 led 灯
}
}
}
In the light intensity is strong , it’s resistance decreases; however , when the light is weak, this
sensor’s resistance increases.
First Experiment:
Prepare: 1 X Ambient Light sensor , 1 X buzzer , some Jumper cable.
To build the circuit, attach buzzer’s long, positive leg (called the anode) to pin 6 through a
Ambient Light sensor . Connect the cathode (the shorter, negative leg) directly to ground.
Attach the Tilt Switch’s silver pin to the 5 volt supply, the other side to Analog Pin 5 .
void setup()
{
pinMode(6,OUTPUT);
}
void loop()
{
while(1)
{
char i,j;
while(1)
{
for(i=0;i<80;i++) //输出一个频率的声音
{
digitalWrite(6,HIGH);
delay(1);
digitalWrite(6,LOW);
delay(1);
}
for(i=0;i<100;i++) //输出另一个频率的声音
{
digitalWrite(6,HIGH);
delay(2);
digitalWrite(6,LOW);
delay(2);
}
}
}
}
First Experiment:
Prepare: 1 X LCD Screen , 1 X 10k Potentiometer , some Jumper cable.
To build the circuit, To wire your LED screen to your Arduino, connect the following pins:
Additionally, wire a 10K pot to +5V and GND, with it's wiper (output) to LCD screen's
VO pin (pin3).
/*
* The circuit:
www.bestqshop.com Ebay store: b2cqshop , E-qstore
www.bestqshop.com Ebay store: b2cqshop , E-qstore
*/
void setup() {
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
}
void loop() {
lcd.setCursor(0,0); // 第一行第一个字符开始显示
lcd.print("OK: ");
lcd.setCursor(5,0); // 第一行第六个字符开始显示
lcd.print("Arduino!");
lcd.setCursor(0,1); // 第一行第六个字符开始显示
lcd.print("Yes,B2CQSHOP");
delay(500);
}
The LM35 series are precision integrated-circuit temperature sensors, whose output voltage
is linearly proportional to the Celsius (Centigrade) temperature.
Connection Diagrams
First Experiment:
Prepare: 1 X LM35 Temperature Sensor , 1 X 1602 LCD display , some Jumper cable.
Please check the Lesson 8 : 1602 LCD , to wire your LED screen to your Arduino .
Attach the LM35’s Pin Vout ( the middle pin) to your arduino’s Analog Pin 0 . Vcc to
+5v , Gnd to your arduino board’s gnd
/*
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
*/
void setup() {
Serial.begin(9600);
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
}
void loop() {
val=analogRead(0);
dat=(125*val)>>8; //计算温度值
lcd.setCursor(0,0);
lcd.print("Tep: ");
lcd.setCursor(6,0);
lcd.print(dat);
lcd.setCursor(10,0);
lcd.print("'C");
delay(10);
lcd.setCursor(0,1);
if(dat>18)
{
lcd.print("Warm! B2CQSHOP");
}
else
{
lcd.print("Cold! B2CQSHOP");
}
delay(500);
}
To know more about how to control IR Remote Control. You should have a knowledge of the
NEC Protocol
Modulation:
The NEC protocol uses pulse distance encoding of the bits. Each pulse
is a 560µs long 38kHz carrier burst (about 21 cycles). A logical "1" takes
2.25ms to transmit, while a logical "0" is only half of that, being 1.125ms.
The recommended carrier duty-cycle is 1/4 or 1/3.
Protocol :
The picture above shows a 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're not interested in this reliability you can ignore the
inverted values, or you can expand the Address and Command to 16 bits each!
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.
In this Experiment, we will use a little different IR Remote Control , which use WD6122
chip . ( Extended NEC protocol )
Pay attention: When there is no infrared signals, the receiver’s output is High ; while,
it receives an signal, its output is LOW . We can check the received pulse through the
Oscilloscope, analyse the program according to the waveform.
The Command Values of the IR Remote Control’s key like the following picture.
First Experiment:
Prepare: 1 X Infrared Receiver (IRM_3638) , 1 X IR Remote Control , 1 X 10 kΩ
Resistors , some Jumper cable.
Please check the Lesson 8 : 1602 LCD , to wire your LED screen to your Arduino .
Attach the Infrared Receiver’s Pin Vout to your arduino’s digital Pin 8 , Please check the
following picture.
#include <LiquidCrystal.h>
int Pulse_Width=0;//存储脉宽
int ir_code=0x00;// 用户编码值
char adrL_code=0x00;//命令码
char adrH_code=0x00;//命令码反码
void timer1_init(void)//定时器初始化函数
{
TCCR1A = 0X00;
TCCR1B = 0X05;//给定时器时钟源
TCCR1C = 0X00;
TCNT1 = 0X00;
TIMSK1 = 0X00; //禁止定时器溢出中断
}
void remote_deal(void)//执行译码结果函数
{
//数据显示
lcd.clear(); //清屏
delay(1);
lcd.setCursor(0,0);
lcd.print(ir_code,HEX);//16 进制显示
lcd.setCursor(0,1);
lcd.print(adrL_code,HEX);//16 进制显示
}
char logic_value()//判断逻辑值“0”和“1”子函数
{
TCNT1 = 0X00;
while(!(digitalRead(8))); //低等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=7&&Pulse_Width<=10)//低电平 560us
{
while(digitalRead(8));//是高就等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=7&&Pulse_Width<=10)//接着高电平 560us
return 0;
else if(Pulse_Width>=25&&Pulse_Width<=27) //接着高电平 1.7ms
return 1;
}
return -1;
}
void pulse_deal()//接收地址码和命令码脉冲函数
{
int i;
int j;
ir_code=0x00;// 清零
adrL_code=0x00;// 清零
adrH_code=0x00;// 清零
//解析遥控器编码中的用户编码值
for(i = 0 ; i < 16; i++)
{
if(logic_value() == 1) //是 1
ir_code |= (1<<i);//保存键值
}
//解析遥控器编码中的命令码
for(i = 0 ; i < 8; i++)
{
if(logic_value() == 1) //是 1
adrL_code |= (1<<i);//保存键值
}
//解析遥控器编码中的命令码反码
for(j = 0 ; j < 8; j++)
{
if(logic_value() == 1) //是 1
adrH_code |= (1<<j);//保存键值
}
}
void remote_decode(void)//译码函数
{
TCNT1=0X00;
while(digitalRead(8))//是高就等待
{
if(TCNT1>=1563) //当高电平持续时间超过 100ms,表明此时没有按键按下
{
ir_code=0x00ff;// 用户编码值
adrL_code=0x00;//键码前一个字节值
adrH_code=0x00;//键码后一个字节值
return;
}
}
//如果高电平持续时间不超过 100ms
TCNT1=0X00;
while(!(digitalRead(8))); //低等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=140&&Pulse_Width<=141)//9ms
{
while(digitalRead(8));//是高就等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=68&&Pulse_Width<=72)//4.5ms
{
pulse_deal();
return;
}
else if(Pulse_Width>=34&&Pulse_Width<=36)//2.25ms
{
while(!(digitalRead(8)));//低等待
Pulse_Width=TCNT1;
TCNT1=0;
if(Pulse_Width>=7&&Pulse_Width<=10)//560us
{
return;
}
}
}
}
void
setup()
{
unsigned char i;
pinMode(IR_IN,INPUT);//设置红外接收引脚为输入
lcd.begin(16, 2);
}
void loop()
{
timer1_init();//定时器初始化
while(1)
{
remote_decode(); //译码
remote_deal(); //执行译码结果
}
}