arduino tutorials en
TRANSCRIPT
-
8/16/2019 Arduino Tutorials En
1/100
Tutorials for Arduino
Service-Team
This version of our tutorials in english language is a new one (april 2016). Please contact us incase you notice any mistakes: infofun!uino.!e
"ave fun with our tutorials# $in! regar!s%
&ervice Team
mailto:[email protected]:[email protected]:[email protected]
-
8/16/2019 Arduino Tutorials En
2/100
Contents
Preface...................................................................................................................................21. Preface to the 'r!uino tutorials.....................................................................................2
2. "ar!ware an! &oftware.....................................................................................................2.1 "ar!ware.....................................................................................................................
2.1.2 escription of typical e*uipment..........................................................................+2.1.2.1 ,rea!-oar!........................................................................................................+2.1.2.2 / (light emitting !io!e)..................................................................................+
2.2 &oftware.......................................................................................................................2.2.1 nstallation............................................................................................................2.2.1.1 nstallation an! set up of the 'r!uino software.................................................2.2.1.2 nstallation of the &, !river.............................................................................3
. Programming......................................................................................................................4,asic structure of a sketch:................................................................................................5
1. ame varia-le...........................................................................................................5
2. &etup (a-solutely necessary for the program)..........................................................5Tutorials:...............................................................................................................................11,linking /.....................................................................................................................11 'lternately -linking /...................................................................................................17a!ing /......................................................................................................................13ight an! soun!...............................................................................................................20Push -utton an! /.......................................................................................................2289, /.........................................................................................................................2otion !etector................................................................................................................Photo resistor...................................................................................................................3Potentiometer...................................................................................................................+1
Temperature measurement..............................................................................................++easurement of !istance................................................................................................+5sage of an infrare! remote............................................................................................3;ontrol a servo.................................................................................................................62; isplay.....................................................................................................................68elay shiel!.....................................................................................................................63&tepper............................................................................................................................65oisture sensor...............................................................................................................32rop sensor.....................................................................................................................3687 $it...........................................................................................................................40
Tutorials with a!!itional /*uipment.....................................................................................50
$eypa! shiel!..................................................................................................................50
-
8/16/2019 Arduino Tutorials En
3/100
Preface
1. Preface to the Arduino tutorials
These tutorials are meant to -e an entry to the 'r!uino -asis. ,eginners shoul! get an
interesting lea!=in the worl! of 'r!uino. >ur tutorials are all -ase! on practical tasks withtheoretical intro!uctions at the -eginning. ?e really recommen! to rea! the theoretical part
to successfully complete the practical tasks.
These tutorials were create! in the conte@t of a teaching unit. They can -e use! for free to
learn a-out 'r!uino% -ut itAs not allowe! to copy an! use the tutorials without any permission.
These tutorials have -een create! carefully an! are continuously maintaine!% however we canAt
give any warranty a-out the accuracy an! completeness of the tutorials.
7or the practical tasks youAll nee! some technical e*uipment. >n our we-site
www.fu n!uinoshop.com you can -uy especially customiBe! 7un!uino kits for our tutorials.
What is actually Arduino?
'r!uino is an >pen=source=electronic=prototyping=-ase for simple use! har!ware an! software
in the fiel! of microcontrolling. t is suita-le to realiBe fascinating proCects in a short time.
any of them can -e foun! on Doutu-e un!er E'r!uinoF. t is mostly use! -y artists% !esigner or
tinkers to realiBe creative i!eas.
,ut 'r!uino is also increasingly use! -y universities an! schools to teach an interesting an!
simple -eginning to the worl! of microcontrolling.
3
http://www.funduinoshop.de/http://www.funduinoshop.de/http://www.funduinoshop.de/
-
8/16/2019 Arduino Tutorials En
4/100
2. Hardware and Software
The term E'r!uinoF ist mostly use! for -oth components. The har!ware ('r!uino ,oar!s)
an! the correspon!ing software ('r!uino).
2.1 Hardware
The 'r!uino har!ware is a so=calle! microcontrolling -oar! (7ollowing calle! E-oar!F).
,asically it is a circuit -oar! with many electronic parts aroun! the actual microcontroller.
>n the e!ge of the -oar! are many pins with whom it is possi-le to connect !ifferent
components. &ome of them are for e@ample: &witches% /As% ltrasonic sensors%temperature sensors% !isplays% stepper% etc..
There are !ifferent kin! of -oar!s% that can -e use! with the 'r!uino sofware. ifferent
siBe! Fofficial” -oar!s% with the official F'!uinoG name on it% -ut also many% mostly
cheaper% -ut e*uivalent 'r!uino “fitting” -oar!s. Typical official -oar!s are calle! 'r!uino
>% 'r!uino /9'% 'r!uino ini% etc. 'r!uino compati-le -oar!s are for e@ample
7un!uino >% 7un!uino /9'% 7ree!uino% &ee!uino% &ainsmart > etc..
-
8/16/2019 Arduino Tutorials En
5/100
2.1.2 !escri"tion of ty"ical e#ui"$ent
,esi!e sensors an! actuators you nee!% as a -ase for *uick an! fle@i-le e@perimental
setups% Cumper ca-le com-ine! with a -rea!-oar!. This way you wonAt nee! to sol!er.
7urthermore the /s are useful to check the signal output of the -oar!.
2.1.2.1 %read&oard
' ,rea!-oar! is a helpful tool to -uil! circuits without any sol!ering. ;ertain contacts are
connecte! with each other. Therefore it is possi-le to connect many ca-les with each other
without sol!ering or screwing them together.
This image -elow shows in color% which contacts are connecte!.
2.1.2.2 '(! )light e$itting diode*
?ith /s it is possi-le to check the results of proCects real *uick. ,ecause of that theyAre
useful for almost every 'r!uino proCect. >n the internet are many information a-out /s.
The most important information:
The electricity can only get through the / in one !irection. &o the / has to -e
connecte! the right way to work. There is a longer an! a shorter contact at the /.
The longer one is the positive (H) an! the shorter one is the negative (I) contact.
+
-
8/16/2019 Arduino Tutorials En
6/100
The / is only !esigne! for a specific voltage. f there isnAt enough voltage the /
wonAt shine as -right as it shoul!. f thereAs to much voltage for the /% it will get really
hot ('TT/T>) an! -urn out.
Typical voltage !ata for the !ifferent colors of /s: -lue: %1J% white: %J% green: %3J%yellow: 2%2J% re!: 2%1J. The voltage on the microcontroller-oar!s is J. &o the /
shoul!nAt -e connecte! to the -oar! !irectly% -ut with a resistor -etween it in the circuit.
on=committal recommen!ation for resistors at !ifferent /s (while connecting to the
J pins on the microcontroller -oar!s):
/: white re! yellow green -lue 8resistor: 100 >hm 200 >hm 200 >hm 100 >hm 100 >hm 100 >hm
2.2 Software
The software that is use! to program the microcontroller% is open=source=software an! can
-e !ownloa!e! for free on www.ar!uino.cc. ?ith this F'r!uino softwareG you can write little
programs witch the microcontroller shoul! perform. This programs are calle! F&ketchG.
n the en! the sketches are transferre! to the microcontroller -y &, ca-le.
ore on that later on the su-Cect FprogramingG.
2.2.1 ,nstallation
ow one after another the 'r!uino software an! the &, !river for the -oar! have to -e
installe!.
2.2.1.1 ,nstallation and set u" of the Arduino software
1. ownloa! the 'r!uino software on www.ar!uino.cc an! install it on the computer (The
microcontroller >T connecte! to the P;). 'fter that you open the software file an! start
-
http://www.arduino.cc/http://www.arduino.cc/http://www.arduino.cc/http://www.arduino.cc/
-
8/16/2019 Arduino Tutorials En
7/100
the program name! ar!uino.e@e.
Two set ups on the program are important an! shoul! -e consi!ere!.
a) The -oar! that you want to connect% has to -e selecte! on the ar!uino software.The F7un!uino noG is here known as F'r!uino K 9enuino noG.
-) Dou have to choose the right F&erial=PortG% to let the ;omputer know to which port the
-oar! has -een connecte!. That is only possi-le if the &, !river has -een installe!
correctly. t can -e checke! this way:
-
8/16/2019 Arduino Tutorials En
8/100
't the moment the 'r!uino isnAt connecte! to the P;. f you now choose FPortG% un!er the
fiel! FToolG% you will alrea!y see one or more ports here (;>1K ;>2K ;>L).
The *uantity of the shown ports !oesnAt !epen! on the *uantity of the &, ports on the
computer. ?hen the -oar! gets connecte! to the computer% D> ? 7 >/ >8/
P>8T.
2.2.1.2 ,nstallation of the /S% dri0er
"ow it shoul! -e:
1. Dou connect the -oar! to the computer.
2. The ;omputer recogniBes the -oar! an! suggests to install a !river automatically.
'TT/T>: ?ait a secon!# ost of the time the computer canAt fin! the !river
automatically to install it. Dou might choose the !river -y your own to install it. t can -e
foun! in the 'r!uino file un!er FriversG.
;ontrol: 't the control panel of the ;omputer you can fin! the Fevice managerG. f the
-oar! has -een installe! successfully% it shoul! appear here. ?hen the installation has
faile!% there is either nothing special to fin! or you will fin! an unknown &, !evice with a
-
8/16/2019 Arduino Tutorials En
9/100
yellow e@clamation mark. n this case: ;lick on the unknown !evice an! choose Fup!ate
&, !riverG. ow you can start over with the manual installation.
3. Progra$$ing
ow we can start properly. ?ithout to much theoretical information we start !irectly with
programming. earning -y !oing. >n the left si!e you can fin! the FsketchesG% on the right
the accompanying e@planation for the comman!s in grey. f you work trough the tutorials
with this system% you will soon un!erstan! the co!e an! -e a-le to use it -y yourself. ater
on you can familiariBe yourself with other features. These tutorials are only meant as first
steps to the 'r!uino worl!. 'll possi-le program features an! co!es are referre! on
www.ar!uino.cc un!er EreferenceF.
7irst of all a short e@planation for possi-le error reports that can appear while working with
the 'r!uino software. The two most common ones are:
1) The -oar! is not installe! right or the
wrong -oar! is selecte!. 'fter uploa!ing
the sketch% there will appear an error report
un!erneath the sketch. t looks like the
one in the picture on the right. The note
Fnot in syncG shows up in the error report.
http://www.arduino.cc/http://www.arduino.cc/
-
8/16/2019 Arduino Tutorials En
10/100
2.) There is a mistake in the sketch.
7or e@ample% a wor! is misspelle! or a
-racket is missing. n the e@ample on theleft the last semicolon in the sketch is
missing. n this ;ase the error report
often starts with Fe@cepte!..G. This means
that the program is still e@pecting
something that is missing.
%asic structure of a setch4
' sketch can -e !ivi!e! in three parts.
1. 5a$e 0aria&le
n the first part elements of the program are name! ( This will -e e@plaine! in program no.
). This part is not a-solutely necessary.
2. Setu" )a&solutely necessary for the "rogra$*
The setup will -e performe! only once. "ere you are telling the program for e@ample what
Pin (slot for ca-les) shoul! -e an input an! what shoul! -e an output on the -oar!s.
efine! as >utput: The pin shoul! put out a voltage. 7or e@ample: ?ith this pin a / is
meant to light up.
16
-
8/16/2019 Arduino Tutorials En
11/100
efine! as an nput: The -oar! shoul! rea! out a voltage. 7or e@ample: ' switch is
actuate!. The -oar! recogniBe! this% -ecause it gets a voltage on the nput pin.
3. 'oo" )a&solutely necessary for the "rogra$*
This loop part will -e continuously repeate! -y the -oar!. t assimilates the sketch from
-eginning to en! an! starts again from the -eginning an! so on.
etAs start#
11
-
8/16/2019 Arduino Tutorials En
12/100
7utorials4
%lining '(!
Task: 9et a / to -link.
8e*uire! e*uipment: Must the microcontroller -oar! with the &, ca-le.
>n the -oar! a / is alrea!y -uil! on the pin 1 (for test purpose). This / often
alrea!y -links% after connecting a new -oar! to the computer% -ecause !uring
manufacturing the -link program is uploa!e! for test purposes. ?e are going to program
this -linking -y ourself.
;ircuit:
The meant / is circle! in re! on the image a-ove.
Dou only have to connect the -oar! properly with the computer.
1.1 7irst part of the program: ame varia-les
I 7irst we !onAt !o anything here.
12
-
8/16/2019 Arduino Tutorials En
13/100
1.2 &econ! part of the program: &etup
?e only have one output I Pin 1 shoul! put out voltage (The / shoul! light up.).
?e start writing in the white area of the ar!uino software:
void setup() //The setup begins here
{ //opening curly bracket – A program part begins here
} //closing curly bracket – A program part is ending here
ow we are going to write the setup information -etween the curly -rackets.
n this case: Fpin 1 is suppose! to -e an outputG :
void setup() //The setup begins here
{ //A program part begins here
pinMode(1! "#T$#T)% //$in 1 is supposed to be an ouput&
} //A program part is ending here&
13
-
8/16/2019 Arduino Tutorials En
14/100
1. Thir! part of the program: oop (main part):
void setup() //The setup begins here
{ //A program part begins here
pinMode(1! "#T$#T)% //$in 1 is supposed to be an ouput&
} //A program part is ending here&
void loop() //The main part o' the program begins here
{ //A program part begins here
} //A program part is ending here&
ow we -ring in the loop part (main part).
T"& & T"/ ;>P/T/ &$/T;":
void setup() //The setup begins here
{ //A program part begins here
pinMode(1! "#T$#T)% //$in 1 is supposed to be an ouput&
} //A program part is ending here&
void loop() //The main part o' the program begins here
{ //program part begins here
digitalrite(1! *+)% //Turn on the voltage on pin 1 (,-. on)
delay(1)% //ait 'or 1 milliseconds (one second)
digitalrite(1! ,")% //Turn o'' the voltage on pin 1 (,-. o'')
delay(1)% //ait 'or 1 milliseconds (one second)
} //$rogram ends here
1
-
8/16/2019 Arduino Tutorials En
15/100
one# The sketch shoul! look Cust like
the one in the image on the right. ow
we have to uploa! it to the -oar!. ,y
clicking on the -utton with the arrow on
the upper left of the software% you will
uploa! the sketch to the -oar!.
1.+ ow you have the option to mo!ify the program. /@ample: Dou want the / to
-link really fast. Therefore we will shorten the waiting time ( 7rom 1000ms to 200ms)
void setup() //The setup begins here
{ //A program part begins here
pinMode(1! "#T$#T)% //$in 1 is supposed to be an ouput&
} //A program part is ending here&
void loop() //The main part o' the program begins here
{ //program part begins here
digitalrite(1! *+)% //Turn on the voltage on pin 1 (,-. on)
delay(0)% //ait 'or 0 milliseconds
digitalrite(1! ,")% //Turn o'' the voltage on pin 1 (,-. o'')
delay(0)% //ait 'or 0 milliseconds
} //program part ends&
The new mo!ifie! sketch has to -e uploa!e! to the -oar! again. ow if everything has
worke! properly the / shoul! -link faster.
1+
-
8/16/2019 Arduino Tutorials En
16/100
Alternately &lining '(!
Task: ?e want to let two /s -link alternately.
8e*uire! e*uipment: icrocontroller K two /s (-lue) K two resistors with 100 >hm K
,rea!-oar! K ca-les
&etup:
1-
-
8/16/2019 Arduino Tutorials En
17/100
;o!e:
void setup()
{ //e are starting ith the setup
pinMode(2! "#T$#T)% //$in 2 is de'ined as output
pinMode(3! "#T$#T)% //$in 3 is de'ined as output
}
void loop()
{ //The main part starts
digitalrite(2! *+)% //turn on the ,-. on pin 2
delay(1)% //ait 'or 1 milliseconds
digitalrite(2! ,")% //turn o'' the ,-. on pin 2
digitalrite(3! *+)% //turn on the ,-. on pin 3
delay(1)% //ait 'or 1 milliseconds
digitalrite(3! ,")% //turn o'' the ,-. on pin 3
} //ere at the end o' the loop the program starts again 'rom the beginning o'
//the loop part& 4o&&turn on ,-. on pin 2&&ait 'or 1
//milliseconds&&etc&&etc&&
1
-
8/16/2019 Arduino Tutorials En
18/100
8ading '(!
Task: ' / shoul! pulsating get -righter an! !arker (fa!e).
8e*uire! e*uipment: icrocontroller K one / (-lue) K resistor with 100 >hm K
,rea!-oar! K ca-les
&etup:
The 'r!uino is a !igital microcontroller. t only knows F Jolt onG an! F Jolt offG on itsoutputs. ,ut to vary the -rightness of an /% it ought -e vary the voltage on the outputs.
7or e@ample Jolts if the / shines -right% + Jolts if it shines a -it !arker an! so on.
T"& >/&AT ?>8$ > 9T' P&. ,ut there is an other option. t is calle! pulse
wi!th mo!ulation (short P?). P? lets the J voltage fa!e. The voltage is turne! on
an! off for millisecon!s. ?ith a really high P? the J signal nearly gets constant on the
pin. ?ith a low P? it is the other way aroun! an! the J signal is -arely there (This is
only a re!uce! summary% so you shoul! look it up on the internet% if you nee! more
information). ?ith this P? it is possi-le to get nearly the same effect on a /% as if the
1
-
8/16/2019 Arduino Tutorials En
19/100
voltage woul! get varie!. ot every pin on the -oar! has the P? function. The pins with
this function are specially la-elle!% for e@ample with a little wave in front of the pin num-er
(see image on the -ottom of page 22).
etAs go#
;o!e:
int ,-.56% //The ord 7,-.8 stands 'or the number 6&
int brightness5% //The ord 7brightness8 stands 'or the value that is emitted
//at the $M&The number 78 is only used as an initial value&
int 'ading59% //8'ading8 sets up the speed o' the 'ading&
void setup()
{ // The setup begins here&
pinMode(,-.! "#T$#T)% //The pin ith the ,-. is supposed to be an output&
}
void loop()
{ //The loop part begins here&
analogrite(,-.! brightness)% //The 'unction analogrite is used to activate
// the $M output on the pin ith the ,-.& The value o' the $M is saved under
//the ord 7brightness8& *n this case it is 78&
brightness5brightness : 'ading% // ;o e modi'y the value o' the $M output& e
//add the value o' the 'ading to the value o' the brightness& *n this case<
//brightness 5 :9& The ne value that is standing 'or brightness isn=t any
// longer but 9& hen the program has ran through the loop part once it ill
//start over again& The ne>t pass the value ill be 1& A'ter that it ill be
// 19&&& etc&
delay(09)% //The ,-. should only stay bright 'or a really short time like 09
//milliseconds& *' you reduce that time the 'ading ill also get 'aster&
1
-
8/16/2019 Arduino Tutorials En
20/100
i'(brightness55 ?? brightness55 099){
'ading5 @'ading%
} //Meaning o' this command< *' the alue o' brightness ill reach "B 099! the//value o' 'ading ill change 'rom negative to positive and the other ay
//around& The Beason hy is! that hile the program is running trough the loop
//part! the ,-. ill shine brighter and brighter& Cut at the point o' 099 as a
//value o' the $M! it ill reach it=s ma>imum& At this point the ,-. should get
//darker step by step& Cecause o' this the 'ading ill be negate at this point&
//This means the ne>t time the program runs through the loop the part
//7brightness5brightness:'ading%8 ill e''ect that the ,-. gets darker& ->ample<
//7brightness5099:(@9)8& The value o' brightness ill be 09 a'ter that& The
//ne>t time it ill be 0D9&&etc& As the value o' brightness ill reach the
//value o' 'ading ill get positive again&
} // ith this bracket the loop part ends&
26
-
8/16/2019 Arduino Tutorials En
21/100
'ight and sound
Task: ' / an! a pieBo speaker are suppose! to -link or -eep continuously.
8e*uire! e*uipment: icrocontroller K one / K resistor with 200 >hm K ,rea!-oar! K
pieBo speaker K ca-les
&etup:
21
-
8/16/2019 Arduino Tutorials En
22/100
;o!e:
int ,-.5D% //this time e also going to use the 'irst part o' the program& ere
//e are going to put in variables& This means that there ill be a letter or a
//ord standing 'or a number& *n this e>ample the ,-. is connected to pin D and
//the speaker to pin 9! so e rename pin D and pin 9! to avoid con'usion& The
//ord 7,-.8 no stands 'or the number D and the ord 7beep8 'or the number 9&
int beep59%
void setup()
{ //e are starting ith the setup
pinMode(,-.! "#T$#T)% //pin D (pin 7,-.8) is supposed to be an output
pinMode(beep! "#T$#T)% //$in 9 (pin 7beep8) is supposed to be an output
}
void loop()
{ //The main part starts
digitalrite(,-.! *+)% //turn on the ,-.
digitalrite(beep! *+)% //turn on the speaker
delay(1)% //ait 'or 1 milliseconds (sound and light)
digitalrite(,-.! ,")% //turn o'' the ,-.
digitalrite(beep! ,")% //turn o'' the speaker
delay(1)% //ait 'or 1 milliseconds (no sound and no light)
} //ere at the end o' the loop the program starts again 'rom the beginning o'
//the loop& 4o it ill beep and light up again& *' you change the break (delay)
//it ill be either beep and light up 'aster or sloer&
22
-
8/16/2019 Arduino Tutorials En
23/100
Push &utton and '(!
Task: 'fter pushing the -utton an / is suppose! to light up for secon!s.
8e*uire! e*uipment: 'r!uino K one / (-lue) K one resistor with 100>hm K one resistor
with 1$ >hm (1000 >hm) K ,rea!-oar! K ;a-les K Push -utton
The !igital pins of the microcontroller are not only a-le to put out voltage% they are also
a-le to rea! out voltage. ?e are going to try this with the following program. This time
there is something special in the setup. f we woul! simply connect the push -utton with
the microcontroller an! push the -utton% there woul! -e voltage on the pin. Dou can
imagine it like many electrons floating aroun! the pin. ?hen you now release the -utton%
there woul!nAt get any more electrons to the pin. ow the !ifficulty: The electrons that are
alrea!y floating aroun! the pin are only escaping e@tremely slow. &o the microcontroller
thinks that the -utton has -een pushe! longer than it actually has -een. The
microcontroller thinks that the -utton has -een pushe! until the electrons have escape!
completely from the pin. This pro-lem can -e fi@e! -y groun!ing the pin with a 1000 >hm
(1$ >hm) resistor. ow the electrons are a-le to escape from the pin faster an! the
microcontroller recogniBes that the -utton only has -een pushe! -riefly. The resistor iscalle! FP>?G= resistor% -ecause the resistors is always Fpulling !ownG the voltage
to 0J. 'TT/T>: f you are using a smaller value! resistor% you can get an electrical
short on the microcontroller while pushing the -utton.
23
-
8/16/2019 Arduino Tutorials En
24/100
&etup:
;o!e:
int ,-.blue5E% //The ord 7,-.blue8 stands 'or the value E&
int button52% //The ord 7button8 stands 'or the value 2&
int buttonstatus5% //The ord 7buttonstatus8 stands 'or the value & ,ater
//on there ill be sa'ed heter the button is pushed or not&
void setup()
{ //The setup starts here
pinMode(,-.blue! "#T$#T)% //The pin connected to the ,-. (pin E) is an output
pinMode(button! *;$#T)% //The pin connected to the button (pin 2) is an input&
2
-
8/16/2019 Arduino Tutorials En
25/100
}
void loop()
{ //ith this bracket the loop part starts
buttonstatus5digitalBead(button)% //The value on pin 2 is read out (command<
//digitalBead)& The result ill be sa'ed under 7buttonstatus8& (*+ means 9
//and ," means )
i' (buttonstatus 55 *+) //*' the button gets pushed (high voltage value)&&&
{ //open program part o' the *F@command
digitalrite(,-.blue! *+)% //&&&the ,-. should light up
delay(9)% //9 miliseconds (9 seconds) long
digitalrite(,-.blue! ,")% //a'ter 9seconds the ,-. should turn o''
} //close the program part o' the *F@command
else
{ //open the program part o' the else@command
digitalrite(,-.blue! ,")% //the ,-. shouldn=t light up
} //close the program part o' the else@command
} //ith this bracket the hole loop parts gets closed
2+
-
8/16/2019 Arduino Tutorials En
26/100
-
8/16/2019 Arduino Tutorials En
27/100
Jersion -: F;ommon ano!eG I The longest contact of the 89, / is FHG. This means that
the other contacts are suppose! to get negative gate Joltage (9) F=G.
t is possi-le to create much more colours if you temper the colours. 7or e@ampleO you willget FyellowG -y tempering F-lueG an! FgreenG.
There is a simple way to fin! out what 89, / version you have. Must switch FHG an! F=G
on the /. >nly if the / is connecte! the right way it will work an! shine.
2
-
8/16/2019 Arduino Tutorials En
28/100
&etup with version a:
&etup for version -:
2
-
8/16/2019 Arduino Tutorials En
29/100
The 'r!uino is a !igital icrocontroller. >n its >utputs it only can Fturn onG J or Fturn offG
J. ,ut to create the !ifferent colours% the three colours of the / have to -e actuate
more specific. That is the reason to use the Pulse ?i!th o!ulation. The P? can -e
use! on the pins with the little wave in front of the num-er.
The P? lets the voltage pulse from HJ to 0J. &o the voltage gets turne! off an! on for
only millisecon!s. ?ith a really high P? the J signal nearly gets constant on the pin.
?ith a low P? it is the other way aroun! an! the J signal is -arely there (This is only a
re!uce! summary% so you shoul! look it up on the internet% if you nee! more information).
?ith P? it is possi-le to get nearly the same effect as if the voltage woul! vary.
The following co!es are working for -oth 89, versions:
&ketch 1:
?ith this co!e you can turn on an! off the three !ifferent colours one -y one.
int ,-.blue5% //Clue colour on pin
int ,-.red59% //Bed colour on 9
int ,-.green5E% //+reen colour on pin E
int b51% //b de'ines a break o' 1ms (1 second)
2
-
8/16/2019 Arduino Tutorials En
30/100
int brightnessblue519% //alue beteen and 099 – de'ines the brightness o'
//the single colour
int brightnessred519% //alue beteen and 099 – de'ines the brightness o'
//the single colour
int brightnessgreen519% //alue beteen and 099 – de'ines the brightness o'
//the single colour
int dark5% //alue stands vor voltage – there'ore ,-. o''
void setup()
{
pinMode(,-.blue! "#T$#T)%
pinMode(,-.green! "#T$#T)%
pinMode(,-.red! "#T$#T)%
}
void loop()
{
analogrite(,-.blue! brightnessblue)% //Turn on blue
delay(b)% //Creak
analogrite(,-.blue! dark)% //Turn o'' blue
analogrite(,-.red! brightnessred)% //Turn on red
delay(b)% //Creak
analogrite(,-.red! dark)% // Turn o'' red
36
-
8/16/2019 Arduino Tutorials En
31/100
-
8/16/2019 Arduino Tutorials En
32/100
pinMode(,-.blue! "#T$#T)%
pinMode(,-.green! "#T$#T)%
pinMode(,-.red! "#T$#T)%
}
void loop()
{
analogrite(,-.green! brightnessgreen)% //+reen and red on 5 yello
analogrite(,-.red! brightnessred)%
delay(b)%
analogrite(,-.green! dark)% //+reen and red o'' 5 yello o''
analogrite(,-.red! dark)%
analogrite(,-.green! brightnessgreen)% //+reen and blue on 5 turGuoise
analogrite(,-.blue! brightnessblue)%
delay(b)%
analogrite(,-.green! dark)% //+reen and blue o'' 5 turGouise o''
analogrite(,-.blue! dark)%
analogrite(,-.red! brightnessred)% //Bed an blue on 5 purple
analogrite(,-.blue! brightnessblue)%
32
-
8/16/2019 Arduino Tutorials En
33/100
delay(b)%
analogrite(,-.red! dark)% //Bed and blue o'' 5 purple o''
analogrite(,-.blue! dark)%
}
33
-
8/16/2019 Arduino Tutorials En
34/100
nce it has !etecte!
a movement% it puts out J voltage on a pin.
ow the microcontroller Cust has to rea! this
voltage out an! processes it.
The !uration of the of the output signal an!
the sensi-ility (reach) of the motion !etector
can -e a!Custe! with the two kno-s on it
(see image on the right).
The plastic lens on top of the motion
!etector can easily -e remove!.
n!erneath of it% there is the 8=
!etector an! the lettering of the three
contacts. 9 ( = )% >T (&ignal output)%
J;; ( H ).
's shown in the image on the left.
3
-
8/16/2019 Arduino Tutorials En
35/100
7urthermore there is a Cumper on the
-ottom of the !etector. This Cumper
makes it possi-le to switch -etween
two !ifferent mo!es.
1) Mumper on the outermost: The signal
of the output will -e maintaine! for a
certain time% after the !etector has
recogniBe! a movement. ,ut after that
certain time the signal will -e !eactivate!%
even if movement coul! -e !etecte!. Thesignal will -e activate! again after some time.
2) The other mo!e gets activate! if
the Cumper is place! on the two inner
contacts. The output signal will stay
constantly active% as long as amovement is !etecte!.
This mo!e is recommen!e! for
proCects with 'r!uino.
3+
-
8/16/2019 Arduino Tutorials En
36/100
&etup:
;o!e:
int pieHo59% //The ord 7pieHo8 stands 'or the value 9&
int movement52% //The ord 7movement8 stands 'or the value 2&
int movementstatus5% //The ord 7movementstatus8 stands 'or the value & ,ater
//on there ill be saved i' a movement is detected or not
void setup()
{ //The setup starts here
pinMode(pieHo! "#T$#T)% //The pin connected to the pieHo speaker (pin 9) is
//de'ined as an output&
3-
-
8/16/2019 Arduino Tutorials En
37/100
pinMode(movement! *;$#T)% //The pin connected to the moition detector (pin 2)is
//de'ined as an input&
}
void loop()
{ //The loop part starts here
movementstatus5digitalBead(movement)% //The value on pin 2 is read out
//(command< digitalBead)& The result ill be sa'ed under 7movementstatus8& (*+
//means 9 and ," means )
i'(movementstatus55*+) //i' a movement is detected (voltage signal high) &&
{ //open program part o' the *F@command
digitalrite(pieHo!*+)% //&&the pieHo should beep
delay(9)% //9 seconds long
digitalrite(pieHo! ,")% //a'ter that the pieHo should be Guiet
} //close program part o' the *F@command
else
{ //open else@command
digitalrite(pieHo!,")% //the pieHo speaker should be turned o''
} //close else@command
} //close loop part
3
-
8/16/2019 Arduino Tutorials En
38/100
Photo resistor
ow it will get a little -it more complicate!#
Task: ' / shoul! light up if it gets !ark or rather the photo resistor is covere!.
8e*uire! e*uipment: 'r!uinoK >ne / K 8esistor with 200 >hm K 8esistor with 10$
>hm K ,rea!-oar! K ;a-les K Photo resistor
earning content: 8ea! out voltage an! show out the values via Fserial monitorG.
8ea! out voltage:
?e want the microcontroller to recogniBe how -right it is -y rea!ing out a photo resistor.
7or this purpose we will use a simple physical principle. f two consumers are connecte! in
one circuit (series connection)% they also FshareG the applie! voltage. /@ample: Two same
lamps are connecte! in series with 6J applie! voltage. ?ith a voltmeter we are now a-le
to measure only J on each lamp. f we woul! connect two !ifferent lamps (with !ifferent
values of resistance)% than we woul! -e a-le to measure two !ifferent voltage values on
each lamp% for e@ample: 1%J an! +%J.
' photo resistor changes its resistance !epen!ing on the luminous intensity. ?e will use
this effect to get a value for the -rightness or !arkness -y rea!ing out the applie! voltage.
To create a split voltage we nee! to connect the photo resistor with a resistor (1=10$ >hm%
!epen!ing on what kin! of photo resistor is use!. The resistor shoul! have nearly the
same value of resistance as the use! photo resistor.) in series. ow the circuit shoul! get
connecte! to groun! (9) an! J (see setup).
The microcontroller is a-le to rea! out analogue signals (voltage) an! work with the
values. 7or this function the analog nputs on the -oar! are use!. They are converting the
voltage into a num-er% with that we are a-le to work with. 0 Jolt correspon!s to the num-er
0 an! the highest measura-le value J correspon!s to the num-er 102 (0 to 102 are
102+ num-ers 10 ,it). /@ample: ' voltage of 2%J is measure!% so the microcontroller
woul! give out the value 12 (102+:2).
3
-
8/16/2019 Arduino Tutorials En
39/100
-
8/16/2019 Arduino Tutorials En
40/100
-
8/16/2019 Arduino Tutorials En
41/100
{ //loop part starts here
sensorvalue5analogBead(input)% //Bead out the voltage o' the photo resistor
//and save it under 7sensorvalue8
4erial&print(I4ensor value 5I)% //4ho 74ensor value58 on serial monitor
4erial&print(sensorvalue)% //4end the value o' the photo resistor as a number
//beteen and 10 to the serial monitor
i'(sensorvalue J 910) //*' the sensor value gets higher than 910K
{
digitalrite(,-.!*+)% //&&&the ,-. should light up&&&
}
else
{
digitalrite(,-.!,")% //else the ,-. should be turned o''
}
delay(9)% //short break here the ,-. is turned on or o''&
} //This last bracket closes the loop part
//*' the sensor value 'or e>ample at normal brightness only reaches 1 (this
//value depends on the used resistor! the brightness and the current
//direction)! it ould make sense to use a loer value than 910 ('or e>ample
//6)! to turn on the ,-.& Lou can look up the current sensor value on the
//7serial monitor8& Lou //can 'ind it in the Arduino so'tare at 7tools8&
1
-
8/16/2019 Arduino Tutorials En
42/100
2
-
8/16/2019 Arduino Tutorials En
43/100
Potentio$eter
Task: The spee! of a -linking / shoul! -e regulate! with a potentiometer.
8e*uire! e*uipment: 'r!uino K Potentiometer K ,rea!-oar! K ;a-les
earning content: 8ea!ing out a potentiometer an! working with sensor !ata on a
mathematical -asis (in this case for the !uration of a -reak).
There are three contacts on a potentiometer. The outer ones shoul! -e connecte! with H
an! I. The contact in the mi!!le has to -e connecte! with a analog nput on the
microcontroller. f the potentiometer gets turne! aroun!% the pin in the mi!!le will give out a
voltage -etween 0J an! J. Potentiometer on the most left position: 0J an! potentiometer
on the most right position: J% !epen!ing on the way itAs connecte! (H an! =).
?e will use the alrea!y fi@e! / with pin 1 on the microcontroller. ,ut itAs also possi-le
to connect an a!!itional / on the ,rea!-oar! like itAs shown here:
3
-
8/16/2019 Arduino Tutorials En
44/100
&ketch:
int input5A% //The ord 7input8 no stands 'or the value 7A8
int ,-.51% //8,-.8 no stand 'or the value 1
int sensorvalue5% //ariable 'or the sensorvalue ith as starting value
void setup()
{ //The setup begins here
pinMode (,-.!"#T$#T)% //The pin 1 connected to the ,-. is de'ined as an output
}
void loop()
{ //The loop part begins here
sensorvalue5 analogBead(input)% //The voltage at the potentiometer is read
//out and gets saved as a number beteen and 10 under 7sensorvalue8
digitalrite (,-.!*+)% //Turn on ,-.
delay(sensorvalue)% //The value! hich is saved under 7sensorvalue8 de'ines
//ho long the ,-. ill light up (milliseconds)
digitalrite(,-.! ,")% //Turn o'' ,-.
delay(sensorvalue)% //The value! hich is saved under 7sensorvalue8 de'ines
//ho long the ,-. is turned o'' (milliseconds)
} //-nd o' the loop part
//;o the loop part ill be restarted& *' the read out value o' the
//potentiometer has changed! the time beteen the on and o'' stage ill also
//change& The ,-. ill be blinking 'aster or sloer& The longest possible delay
//in this sketch can be 10ms (milliseconds) long& *' needed longer delays are
//also possible& There'ore you ould have to add a litte mathimatical 'unction
-
8/16/2019 Arduino Tutorials En
45/100
//into the sketch& ->ample< The line 7sensorvalue5analogBead(input)%8 has to be
//changed into 7sensorvalue5analogBead(input)0%8& The saved sensor value ill
//be increased by the 'actor o' 0& 4o the longest delay ould be 0DEms and so
//on &&
+
-
8/16/2019 Arduino Tutorials En
46/100
-
8/16/2019 Arduino Tutorials En
47/100
;o!e:
int TM$E 5 A% //The sensor should be connected to the analog $in A& e ill
//call the pin 7TM$E8 'rom no on
int temperature 5 % //#nder the variable 7temperature8 e ill later on save
//the temperature value
int tempN1O% //To get good values e have to read out some values to get the
//mean value out o' them& The sGuare brackets 7N1O8 create ten di''erent
//variables at one time
-
8/16/2019 Arduino Tutorials En
48/100
tempNDO 5 map(analogBead(TM$E)! ! D1! @9! 19)%
delay(time)%
tempN9O 5 map(analogBead(TM$E)! ! D1! @9! 19)%
delay(time)%
tempNEO 5 map(analogBead(TM$E)! ! D1! @9! 19)%
delay(time)%
tempN2O 5 map(analogBead(TM$E)! ! D1! @9! 19)%
delay(time)%
tempN3O 5 map(analogBead(TM$E)! ! D1! @9! 19)%
delay(time)%
tempN6O 5 map(analogBead(TM$E)! ! D1! @9! 19)% //#ntil here the
//temperature gets read out ten times& *n beteen there is alays a little
//break& e ill take a closer look on the used command<
//tempN1O5map(analogBead(TM$E)!!D1!@9!19)%
//tempN1O – The name o' the 'irst variable&
//7map(a!b!c!d!e)8 @ This is a 7Map command8& ith this command it is possible
//to change a read out value (a) 'rom one region beteen (b) and (c) into a
//region beteen (d) and (e)&
//*n our case this means< The sensor value gets read out right in the map
//command 7analogBead(TM$E)8& The value should be beteen and D1 (5 values
//beteen and 0 at the analog port)& The sensor outputs this voltage values
//i' it measures temperature beteen @9QR and 19QR& ith the 7map command8
//these values get converted into degree values beteen @9QR and 19QR&
temperature5(tempNO:tempN1O:tempN0O:tempNO:tempNDO:tempN9O:tempNEO:tempN2O:tem
pN3O:tempN6O)/1% //-verything in one roS *n this line! all ten values get
//summariHed and get divided by ten& This average value gets saved under
//7temperature8&
-
8/16/2019 Arduino Tutorials En
49/100
4erial&print(temperature)% //;o the temperature values gets send to the
//computer and can be looked up on the serial monitor&
4erial&println(IdegreeI)%
}
(=tension of the setch4
f the temperature reaches 0Q;% a noise from the pieBo speaker appears.
int TM$E 5 A%
int temperature 5 %
int tempN1O%
int time5 0%
int pieHo59% //$ieHo speaker on pin 9
void setup() {
4erial&begin(6E)%
pinMode (pieHo! "#T$#T)% //$in 9 is an output
}
void loop() {
tempNO 5 map(analogBead(TM$E)! ! D1! @9! 19)%
delay(time)%
tempN1O 5 map(analogBead(TM$E)! ! D1! @9! 19)%
-
8/16/2019 Arduino Tutorials En
50/100
//&&&
tempN6O 5 map(analogBead(TM$E)! ! D1! @9! 19)%
temperature5(tempNO:tempN1O:tempN0O:tempNO:tempNDO:tempN9O:tempNEO:tempN2O:tempN3O:tempN6O)/1% // -verything in one roS
4erial&print(temperature)%
4erial&println(IdegreesI)%
i' (temperatureJ5) //*' the temperature reaches QR or moreK&
{
digitalrite(pieHo!*+)% //&&&the pieHo speakers beeps&&
}
else
{
digitalrite(pieHo!,")% //&&&it is Guiet&
}
}
+6
-
8/16/2019 Arduino Tutorials En
51/100
-
8/16/2019 Arduino Tutorials En
52/100
;o!e:
int trigger52% //8trigger8 on pin 2&
int echo5E% //8echo8 on pin E&
long time5% //The value 7time8 ill sa'e the time beteen transmission and
//returning o' the soundave&
long dist5% //The value 7dist8 ill save the calculated distance& *t ill
//start ith 78& *nstead o' 7int8 e are using 7long8 'or this value! to save a
//bigger number&
void setup()
{
4erial&begin (6E)% //4tarting the serial communication& *t ill send the
//data 'rom the arduino board to the computer to sho it on the serial monitor&
pinMode(trigger! "#T$#T)% //8trigger8 ($in 2) is an output&
pinMode(echo! *;$#T)% //8echo8 ($in E) is an input&
}
void loop()
{
digitalrite(trigger! ,")% //,o voltage on the trigger pin to produce a
//clear signal&
delay(9)% //K&'or 9 milliseconds&
digitalrite(trigger! *+)% //Rreating the soundave&
delay(1)% //&&'or 1 milliseconds&
digitalrite(trigger! ,")% //4top creating the soundave&
+2
-
8/16/2019 Arduino Tutorials En
53/100
time 5 pulse*n(echo! *+)% //ith the command pulse*n (Rapital 7i8 in the
//'ront o' the 7n8) the arduino board measures the time beteen sending and
//receiving the soundave&
dist 5 (time/0) / 06&1% //This calculation trans'orms the measured time into
//the distance in centimeter& (The sound needs 06!1 seconds 'or one centimeter&//The time gets divided ith to! because e only ant to get one distance and
//not the to ays that the soundave has to take)&
i' (dist J5 9 ?? dist 5 ) //*' the distance gets over 9cm "B under cm!
//the measurement is no longer accurate&
{
4erial&println(I;o measurementI)% //4o the serial monitor displays 7;o
//measurement8
}
else //otherise
{
4erial&print(dist)% //The calculated distance is shon on the serial monitor&
4erial&println(IcmI)%
}
delay(1)% //This command causes a short break beteen the measurements&
}
/@tension of the sketch
f the !istance is less than 40cm% the pieBo speaker shoul! -eep.
+3
-
8/16/2019 Arduino Tutorials En
54/100
int trigger510%
int echo51%
long time5%
long dist5%
int pieHo59% //$ieHo speaker on pin 9&
void setup()
{
4erial&begin (6E)%
pinMode(trigger! "#T$#T)%
pinMode(echo! *;$#T)%
pinMode(pieHo! "#T$#T)% //The pin (9) connected to the pieHo is a output&
}
void loop()
{
digitalrite(trigger! ,")%
delay(9)%
digitalrite(trigger! *+)%
delay(1)%
digitalrite(trigger! ,")%
time 5 pulse*n(echo! *+)%
dist 5 (time/0) / 06&1%
+
-
8/16/2019 Arduino Tutorials En
55/100
i' (dist J5 9 ?? dist 5 )
{
4erial&println(I;o measurementI)%
}
else
{
4erial&print(dist)%
4erial&println(IcmI)%
}
i' (dist 5 3) //*' the measured distance gets 3 or shorter&&&
{
digitalrite(pieHo!*+)% //&&the pieHo should beep
}
else //*' not &&&
{
digitalrite(pieHo!,")% //&&the speaker should be Guiet&
}
delay(1)%
}
++
-
8/16/2019 Arduino Tutorials En
56/100
9e0erse warning syste$
?ith this co!e we are a-le to create a reverse warning system. '!!itional to the ultrasonic
sensor we are going to connect a / with pin 12.
're you a-le to construct a reverse warning system without any imagesN
int trigger52%
int echo5E%
long time5%
int ,-.510%
long dist5%
void setup()
{
4erial&begin (6E)%
pinMode(trigger! "#T$#T)%
pinMode(echo! *;$#T)%
pinMode(10! "#T$#T)%
}
void loop()
{
digitalrite(trigger! ,")%
delay(9)%
+-
-
8/16/2019 Arduino Tutorials En
57/100
digitalrite(trigger! *+)%
delay(1)%
digitalrite(trigger! ,")%
time 5 pulse*n(echo! *+)%
dist 5 (time/0) / 06&1%
i' (dist J5 9 ?? dist 5 )
{
4erial&println(I;o measurementI)%
}
else
{
4erial&print(dist)%
4erial&println(IcmI)%
}
i' (dist 5 D)
{
digitalrite(,-.! *+)%
delay(dist)%
digitalrite(,-.! ,")%
delay(dist)%
}
+
-
8/16/2019 Arduino Tutorials En
58/100
-
8/16/2019 Arduino Tutorials En
59/100
/sage of an infrared re$ote
Task: 8ea! out the signal from an infrare! remote
with an infrare! sensor.
8e*uire! e*uipment: 'r!uino K -rea!-oar! K ca-le K
infrare! sensor K infrare! remote control
?ith an infrare! receiver% the 'r!uino -oar! can
receive the comman!s of an infrare! remote control.The !ata are sent with infrare! light from remote control
to the receiver. &ince our eyes can not perceive this light%
we can not see this light. ?ith a little trick you can see
the light. Take your mo-ile=phone an! look with the
camera on the infrare! !io!e of the remote while pressing
a -utton on the remote. Dou will see the flashing infrare!
!io!e on the !isplay of the mo-ile phone.
&etup:
+
-
8/16/2019 Arduino Tutorials En
60/100
The sketch is a variation of the sketch F8recvemoG% an! can -e !ownloa!e! together
with the very important 8=8emote=i-rary on the following we-site:
https:KKgithu-.comKshirriffK'r!uino=8remote
ownloa! the Bip=package an! copy the files into your Fli-rariesG !irectory in the ar!uino
software. 8ename the !ownloa!e! !irectory to RrremoteR.
ow you can open the sketch in the sample=files in the ar!uino=software:
7ile =S /@amples =S 8remote =S rrecvemo
;o!e:
/
*Bremote< *Brecv.emo @ demonstrates
receiving *B codes ith *Brecv
An *B detector/demodulator must be
connected to the input B-RU$*;&
ersion &1 Vuly! 06
Ropyright 06 Wen 4hirri''
-6
https://github.com/shirriff/Arduino-IRremotehttps://github.com/shirriff/Arduino-IRremote
-
8/16/2019 Arduino Tutorials En
61/100
-
8/16/2019 Arduino Tutorials En
62/100
}
}
Pressing the R1R -utton on the infrare! remote control causes (in this case) the serial=
monitor to write the num-er R1632+13R. This is the !ecrypte! num-er co!e -ehin! this
-utton.
f you hol! the -utton permanently presse!% the num-er R+25+56325R appears. This is the
co!e that in!icates that a -utton is presse! continuously. This num-er !oes not !epen! on
which -utton is presse!.
There can also appear other num-ers if a key is presse! only very short or pulsating. n
that case the sensor may not rea! a uni*ue value.
/@tension of the sketch: &witch on a / -y pressing -utton1 an! switch it off with
-utton2.
Xinclude *rremote&hJ
int B-RU$*; 5 11%
*Brecv irrecv(B-RU$*;)%
decodeUresults results%
void setup()
{
4erial&begin(6E)%
pinMode (1! "#T$#T)% //$in 1 gets connected ith a ,-. ("utput)&
-2
-
8/16/2019 Arduino Tutorials En
63/100
digitalrite(1! ,")% //At 'irst the ,-. should be tuned o''&
irrecv&enable*B*n()%
}
void loop() {
i' (irrecv&decode(Zresults)) {
4erial&println(results&value! .-R)%
i' (results&value 55 1E20D129) //*' The *B receiver receives the number
//1E20D129 (button 1)&&&
{digitalrite (1! *+)%} //&&&the ,-. gets turned on&
i' (results&value 55 1E21399) //*' the *B receiver receives the number
//1E21399 (button 0)&&&
{digitalrite (1! ,")%} //K&the ,-. gets turned o''&
irrecv&resume()% // Beceive the ne>t value
}
}
-3
-
8/16/2019 Arduino Tutorials En
64/100
Control a ser0o
Task: ' servo has to turn to three !ifferent positions. ,etween the movements there shoul!
-e a short -reak.
8e*uire! e*uipment: 'r!uino K one servo K three ca-les
;o!e:
Xinclude 4ervo&hJ //*nclude the servo library
4ervo servoblue% //The servo gets the name 7servoblue8
void setup()
{
servoblue&attach(3)% //The signal line o' the servo is on pin 3
}
void loop()
{
-
-
8/16/2019 Arduino Tutorials En
65/100
servoblue&rite()% //$osition 1 ith an angle o' Q
delay()% //ait seconds
servoblue&rite(6)% //$osition 0 ith an angle o' 6Q
delay()% //ait seconds
servoblue&rite(13)% //$osition ith an angle o' 13Q
delay()% //ait seconds
servoblue&rite(0)% //$osition D ith an angle o' 0Q
delay()% //ait seconds
}
'C! !is"lay
Task: &how a te@t on an ; isplay.
8e*uire! e*uipment: 'r!uino K potentiometer K 1+ ca-les K -rea!-oar!
-+
-
8/16/2019 Arduino Tutorials En
66/100
's you may see on the image% the wiring for this proCect is not that easy. t is -ecause the
; has to -e connecte! with pretty many ca-les. 'nother !ifficulty is that there is no plug
strip alrea!y on the ;. Dou can either sol!er a plug strip to the ; or sol!er the ca-le
!irectly to the contacts of the ;. f you want to sol!er the ca-les !irectly with the ;
we recommen! to use flat ca-les (e.g. from an ol! har! !rive or an ol! ; !rive). ?ithoutany sol!ering it is *uite impossi-le to get goo! results.
The potentiometer is nee!e! to a!Cust the contrast. The -ack light of the ; is powere!
with J. ,ecause it is !ifficult to show the lettering on the ;% in the image un!erneath
you shoul! try to count the contacts of the ;. (/@ample: The first contact from the right
to the left gets connecte! to 9. The secon! contact from the right to the left gets
connecte! to J L.). nformation: t is easier to use an ; $eypa! shiel! or an 2; ;for more complicate! proCects -ecause you !onAt have to take so much time for the wiring
of the ;. ,ut these two other options are more e@pensive than the simple ; mo!ule.
&etup:
--
-
8/16/2019 Arduino Tutorials En
67/100
ow if the ; is successfully connecte! we are rea!y to start with the programming.
ike many other components the ; also nee!s to revert to a li-rary in the co!e. This
li-rary is alrea!y a component of the ar!uino software. &o you !onAt have to install
something on your own.
;o!e:
Xinclude ,iGuidRrystal&hJ //,oad the ,R. library
,iGuidRrystal lcd(10! 11! E! 9! D! )% //*n this line e de'ine hich pins on
//the microcontroller get connected to the ,R. (Cetter don=t change yet)&
void setup() {
lcd&begin(1E! 0)% //*n the setup e indicate ho many signs and ho many ros
//e are using& *n this case< 1E signs in to ros&
}
void loop() {
lcd&setRursor(! )% //4tart position o' the cursor on the ,R. (! 5 'irst
//character in the 'irst ro)&
lcd&print(I&'unduino&deI)% // rite the te>t 7&'unduino&de8
lcd&setRursor(! 1)% //4tart position o' the cursor on the ,R. (! 5 'irst
//character in the second ro)&
lcd&print(Igood luckSSSI)% // rite the te>t 7good luckSSS8&
}
/@tension of the sketch: ?e want to print alternately te@t on the first an! on the secon!
row. 7or this e@ample we will use the wor!s FupG an! F!ownG.
-
-
8/16/2019 Arduino Tutorials En
68/100
Xinclude ,iGuidRrystal&hJ
,iGuidRrystal lcd(10! 11! 9! D! ! 0)%
void setup() {
lcd&begin(1E! 0)%
}
void loop() {
lcd&setRursor(! )% //4tart position 'irst sign in the 'irst ro&&&
lcd&print(IupI)% //&&sho the ord 7up8
delay (0)% //ait 0 seconds
lcd&clear()% //Rlear the display
lcd&setRursor(9! 1)% //4tart position 'i'th sigh in the second ro&&&
lcd&print(IdonI)% //&&&print 7don8
delay (0)% //ait 0 seconds
lcd&clear()% //Rlear the display
}
The ; mo!ule is especially useful to show sensor values or other outputs from the
microcontroller. Dou can also fin! help in the ar!uino software at the e@ample sketches.
There you can fin! many !ifferent e@amples at Fi*ui!;rystalG.
-
-
8/16/2019 Arduino Tutorials En
69/100
9elay shield
Task: se a relay shiel!.
8e*uire! e*uipment: 8elay shiel! K 'r!uino K ca-les
' relay is a switch% which can -e activate! with a low current from the 'r!uino -oar!. &o
you can switch on an! off electrical things% that nee! much more power than a 'r!uino
-oar! can provi!e.
The relay nee!s a permanent power supply trough JH an! I (n the picture J;; an!
9). ?ith the FG=pin% the switch can -e activate! -y the 'r!uino -oar!. The three
terminals on the left si!e are meant to -e connecte! with the electrical component that
nee!s more power than the 'r!uino can provi!e. f the relay !oesnAt get any signal from
the 'r!uino on the FG contact% the contacts ' an! , are connecte!. >nce the relay gets a
signal from the 'r!uino the contacts , an! ; get connecte!.
'TT/T>: There are two types of relay shiel!s. They !iffer in the way you get them to
switch. /ither you have to connect the FG pin to JH on the 'r!uino% or you have to
connect the FG pin to 9 on the 'r!uino. Dou can easily fin! out which type of relay you
got. Try connecting the FsignalG pin with JH or with 9. The version where the relay
switches (you can hear a lou! crack) will -e the right version.
7or testing purpose% you can use the F-linkG=sketch. nstea! of the /% you connect the
output=pin from the 'r!uino -oar! with the FG pin from the relay. ?ith that sketch% the
relays will switch on an! off in a 1 secon! rhythm.
-
-
8/16/2019 Arduino Tutorials En
70/100
;o!e:
void setup()
{
pinMode(E! "#T$#T)%
}
void loop()
{
digitalrite(E! *+)% //At this point the relay turns on
delay(1)% //&&ait one second
digitalrite(E! ,")% //turn o'' the relay again
delay(1)% //&&&ait a second
}
6
-
8/16/2019 Arduino Tutorials En
71/100
Ste""er
Task: ' stepper shoul! turn aroun! -ack an! forth.
8e*uire! e*uipment: 'r!uino K stepper with control -oar! K 6 ca-les
This stepper is especially useful for small proCects with the ar!uino -oar!. The stepper can
operate without any e@ternal power supply. t can get a *uite high tor*ue. This is possi-le
-ecause of a gear% which is installe! insi!e of the metal case in front of the actual step
motor. >ne full rotation of the !rive shaft can -e !ivi!e! in 20+4 separate steps. >ne little
!isa!vantage of this can -e the slow ma@imum rotation spee!.
The stepper gets connecte! with a control -oar!. This control -oar! supplies the stepper
with enough power% to prevent the !igital pins of having to !o this. There are two versions
of the control -oar!. >ne with the outer pins on the top an! one with the outer pins on the
-ottom. The connection is the same on -oth versions.
1
-
8/16/2019 Arduino Tutorials En
72/100
Wiring
P2 on 'r!uino -oar! to + on control -oar! ### 2 with + ###
P on 'r!uino -oar! to 2 on control -oar! ### with 2 ###
P+ on 'r!uino -oar! to on control -oar! ### + with ###
P on 'r!uino -oar! to 1 on control -oar! ### with 1 ###
P F9G on 'r!uino -oar! to 9 on control -oar!
P FJG on 'r!uino -oar! to J;; on control -oar!
2
-
8/16/2019 Arduino Tutorials En
73/100
The following ;o!e is an e@ample an! lets the stepper !o one rotation (20+4 steps) -ack
an! forth.
Xinclude 4tepper&hJ //,oad stepper library (already including the arduinoso'tare)
int 4$M# 5 0%
4tepper my4tepper(4$M#! 0!!D!9)%
void setup()
{
my4tepper&set4peed(9)%
}
void loop() {
my4tepper&step(0D3)%
delay(9)%
my4tepper&step(@0D3)%
delay(9)%
}
3
-
8/16/2019 Arduino Tutorials En
74/100
ne e@ample of use can -e the moisture sensor use! to measure the
groun! humi!ity of a plant. f the groun! of the plant gets to !ry% an alarm coul! set off or
an automatic water pump system coul! water the plant. The moisture sensor is also
suita-le for measuring a water level% in the region of the sensor.
The way of functioning is *uite simple. There is a voltage on the two contacts of the
moisture sensor. The higher the level of moisture gets -etween the contacts% the -etter the
currant can flow from one contact to the other. This value gets electronically processe!
from the moisture sensor an! gets transmitte! as an analog signal to an analog input of
the -oar!. &ince the -oar!% as !escri-e! in previous tutorials% isnAt a-le to measureelectrical voltage as such% it converts the analog signal into a numerical value. 0J to 12J
-
8/16/2019 Arduino Tutorials En
75/100
correspon!s to a numerical value from 0 to 102 (That are 102+ num-ers% since the Bero
is counte! as the first numerical value).
,ut the upper level of the moisture sensor is aroun! 400% if the sensor gets completely
un!er water. The accurate cali-ration !epen!s on the sensor an! the type of li*ui!Kmoisture that is measure! (e.g. salt water has a -etter con!uctivity so the value woul! -e
higher).
&etup:
The programming of the moisture sensor isnAt that complicate! an! is very similar to theprogramming of the potentiometer% -ecause there is Cust an analog value rea! out.
int measurement5% //ariable 'or the measurement ith as starting value
void setup()
{ //The setup begins here
+
-
8/16/2019 Arduino Tutorials En
76/100
4erial&begin(6E)% //4tarting the serial communication to sho the sensor
//values on the serial monitor later on
}
void loop()
{ //The loop part starts here
measurement5analogBead(A)% //The voltage on the moisture sensor gets read out
//and gets saved under 7measurement8
4erial&print(IMoisture measurement
-
8/16/2019 Arduino Tutorials En
77/100
void loop()
{
measurement5analogBead(A)%
4erial&print(IMoisture measurement
-
8/16/2019 Arduino Tutorials En
78/100
-
8/16/2019 Arduino Tutorials En
79/100
are touching the surface of the sensor% the higher the value woul! get.
Setu"4
&ee image on page 4 (The two ca-les on the top (-lack an! re!) an! the pieBo speaker
on the -rea!-oar! will -e nee!e! later on).
The programming of the moisture sensor isnAt that complicate! an! is very similar to the
programming of the potentiometer or the moisture sensor% -ecause there is Cust an analog
value rea! out.
int measurement5% //ariable 'or the measurement ith as starting value
void setup()
{ //The setup begins here
4erial&begin(6E)% //4tarting the serial communication to sho the sensor
//values on the serial monitor later on
}
void loop()
{ //The loop part starts here
measurement5analogBead(A)% //The voltage on the drop sensor gets read out and
//gets saved under 7measurement8
4erial&print(IMoisture measurement
-
8/16/2019 Arduino Tutorials En
80/100
(=tension of the code4
ow we want a pieBo speaker to -eep as soon as a rain !rop touches the sensor. 's limit
we are going to use the sensor value +00% -ecause if a !rop touches the sensor we are
e@pecting a value at a-out +40.
&etup:
;o!e:
int measurement5%
int beep5E% //8beep8 stands 'or the pin E! hich gets connected ith the pieHo
void setup()
{
4erial&begin(6E)%
pinMode (E!"#T$#T)% //The pin E gets de'ined as an output
6
-
8/16/2019 Arduino Tutorials En
81/100
}
void loop()
{
measurement5analogBead(A)%
4erial&print(IMoisture measurement
-
8/16/2019 Arduino Tutorials En
82/100
98,! >it
Task: 8ea! out the of a 87 tag an! !isplay the as one contiguous !ecimal
num-er on the serial monitor.
8e*uire! e*uipment: 'r!uino K 87 $it K ca-les
The 87 (Fra!io fre*uency i!entificationG) rea!er is use! to rea! out a certain co!e% which
is sen! from a 87 transmitter (also calle! F87 tagG) -y ra!io. /ach 87 tag has only
one uni*ue co!e. The 87 $it is useful to realiBe proCects like for e@ample a locking
mechanism or other similar proCects in which a person shoul! -e i!entifie! with a tag.
87 tags may come in !ifferent shapes% like a key chain or a car! in cre!it car! format.
>n the following image you can see on the left si!e two 87 tags% on the right the 87
receiver 87 8;22 an! pin hea!er which have to -e sol!ere! to the receiver (There are
also versions with alrea!y sol!ere! pin hea!ers on the receiver).
2
-
8/16/2019 Arduino Tutorials En
83/100
"ow !oes it workN ' 87 receiver contains a small copper coil that generates a magnetic
fiel!. 'n 87 transmitter also inclu!es a copper coil that picks up the magnetic fiel! an!
generates an electrical voltage insin!e the transmitter. This voltage is use! -y a small
electronic chip to get it to emit an electrical co!e -y ra!io. The transmitter !irectly receivesthis co!e an! processes it% so that the microcontroller is a-le to process the receive! co!e.
t is also possi-le to co!ify a 87 tag. ue to the comple@ity it is not mentione! in this
tutorial. ,ut you can fin! several other tutorials for this on the we-.
9ead out and "rocess the data of 98,! tags with the Arduino
8e*uire! e*uipment: 'r!uino > or /9'% 87 rea!er% at least one 87 tag%
-rea!-oar!% ca-les% one /% one 200 >hm resistor
sing a 'r!uino microcontroller% we want to rea! out a 87 tag. f it is the right tag the
/ shoul! light up for secon!s.
?iring of the 87 rea!er with the 'r!uino -oar!:
,oar!: 'r!uino no 'r!uino ega 78;22=8/'/8
Pin: 10 &'
Pin: 1 2 &;$
Pin: 11 1 >&
Pin: 12 0 &>
Pin: nothing nothing 8
Pin: 9 9 9
Pin: 5 8&T
Pin: %J %J %J
>n the image un!erneath the 87 rea!er has sol!ere! pins -ent -y 50Q on it (like the
alrea!y sol!ere! version). This way it is possi-le to plug the rea!er vertical on the
-rea!-oar!.
3
-
8/16/2019 Arduino Tutorials En
84/100
-
8/16/2019 Arduino Tutorials En
85/100
Progra$$ing
8ea!ing out an! processing the !ata of an 87 receiver woul! re*uire%as well as other
comple@ tasks% a lot lines of co!e. Therefore we are going to use one of many e@isting
li-raries from the internet. The one we use! in this tutorial can -e foun! onhttps://github.com/miguelbalboa/rfid.To work with the li-rary you have to !ownloa! it an!
save it in the 'r!uino program fol!er. Dou Cust have to click on Fownloa! UPG an! save
the unBipe! !ata on your har! !rive.
Dou have to unpack the fol!er into the 'r!uino software fol!er an! save it un!er Fli-rariesG.
sually% the fol!er has -een save! un!er R;: Programmear!uinoli-rariesLR (f you have
save! it somewhere else you have to use the Fli-rariesG fol!er there).
'fter the !ata is unBippe! an! save! correctly% there shoul! appear a !ata with the name
Frfi!=masterG in the Fli-rariesG fol!er. ow you &T !elete the hyphen from the name.
&o you rename the !ata into Frfi!masterG. f you have followe! this steps correctly% the
li-rary is rea!y to -e use! in the 'r!uino software.
Setch 1
7irst of all we are going to rea! out the (Fni*ue !entification um-erG). t is the
in!ivi!ual sign of every 87 tag. ?e are going to use the sketch un!erneath ('ttention%
the sketch only works if the li-rary has -een a!!e! to the software the way it is e@plaine!
-efore). This program is only !estine! for > 8 microcontroller. f you want to use
/9'260 or other controllers% you have to a!Cust the pins.
Xinclude 4$*&hJ //*nclude 4$* library
Xinclude MFBR900&hJ //*nclude BF*. library
Xde'ine 44U$*; 1 //4.A on $in 1 (di''erent on M-+A)
Xde'ine B4TU$*; 6 //B4T on $in 6 (di''erent on M-+A)
+
https://github.com/miguelbalboa/rfidhttps://github.com/miguelbalboa/rfid
-
8/16/2019 Arduino Tutorials En
86/100
MFBR900 m'rc900(44U$*;! B4TU$*;)% //;ame BF*. receiver
void setup() //4tarting setup
{
4erial&begin(6E)% //4tarting serial connection
4$*&begin()% //set up 4$* connection
m'rc900&$R.U*nit()% //*nitialiHe BF*. receiver
}
void loop() //,oop part starts here
{
i' (S m'rc900&$*RRU*s;eRard$resent()) //*' a card is near by&&&
{
return% //move on&&
}
i' (S m'rc900&$*RRUBeadRard4erial()) //*' a BF*. tag has been chosen&&&
{
return% //move on&&&
}
4erial&print(IThe *. o' the BF*.@TA+ is
-
8/16/2019 Arduino Tutorials En
87/100
-
8/16/2019 Arduino Tutorials En
88/100
t is not very easy to work with these "/V num-ers one after another. &o we change the
line R &erial.print ( mfrc22.ui!.ui!,yte WiX % "/V ) O R to R &erial.print ( mfrc22.ui!.ui!,yte
WiX % /; O R . Then you will get the in!ivi!ual parts of the as a !ecimal num-er.
Setch 2
ow the co!e is shown as a !ecimal num-er% -ut it is still !ivi!e! into four -locks. ?e
are going to change the co!e in a mathematical way to effect that the is shown as asingle contiguous num-er (!ecimal num-er).
?hy are we !oing thisN f we want to use this sketch later on% for e@ample to let a /
light up or turn a stepper aroun! !epen!ing on the right recogniBe! 87 tag% it will -e
easier to use a 7 comman! with one contiguous num-er. /@ample:
Ff the 87 ;o!e is 101320% a / shoul! turn for secon!sG.
The comman! woul! -e more complicate! this way: Ff the first -lock is 15 an! the
secon! -lock is 36 an! the thir! -lock is 330 an! the fourth -lock is 2 a / shoul!
turn on for secon!sG.
' !isa!vantage of the !ecimal way is the fact that the sketch can -e a little -it unsafe
-ecause not all four -locks (ma@. 12 num-ers) can -e shown as a contiguous num-er.
f you want it to -e completely safe you woul! have to test every single -lock.
Xinclude 4$*&hJ
Xinclude MFBR900&hJ
Xde'ine 44U$*; 1
Xde'ine B4TU$*; 6
MFBR900 m'rc900(44U$*;! B4TU$*;)%
-
8/16/2019 Arduino Tutorials En
89/100
void setup()
{
4erial&begin(6E)%
4$*&begin()%
m'rc900&$R.U*nit()%
}
void loop()
{
i' ( S m'rc900&$*RRU*s;eRard$resent())
{
return%
}
i' ( S m'rc900&$*RRUBeadRard4erial())
{
return%
}
long code5% //e are using 7code8 as a ne variable to save the #*. as
//contiguous number later on& Cy using 7long8 instead o' 7int8 e are able to
//save a longer number&
'or (byte i 5 % i m'rc900&uid&siHe% i::)
{
code5((code:m'rc900&uid&uidCyteNiO)1)% //;o the 'our blocks are read out and
//the code gets 7stretched8 by the 'actor 1 every passing (Actually! e should
//use the 'actor 1! but the number ould become too large)&
}
4erial&print(IThe Rard number is
-
8/16/2019 Arduino Tutorials En
90/100
'n! nowN ?e want a / to turn on for secon!s if the wante! 87 Tag gets hol! in
front of the 87 8ea!er.
Setch 3
Xinclude 4$*&hJ
Xinclude MFBR900&hJ
Xde'ine 44U$*; 1
Xde'ine B4TU$*; 6
MFBR900 m'rc900(44U$*;! B4TU$*;)%
void setup()
{
4erial&begin(6E)%
4$*&begin()%
m'rc900&$R.U*nit()%
pinMode (0! "#T$#T)% //$in 0 gets de'ined as output (here e are going to
//connect the ,-.)
}
void loop()
{
i' ( S m'rc900&$*RRU*s;eRard$resent())
{
return%
}
i' ( S m'rc900&$*RRUBeadRard4erial())
{
return%
6
-
8/16/2019 Arduino Tutorials En
91/100
}
long code5%
'or (byte i 5 % i m'rc900&uid&siHe% i::)
{code5((code:m'rc900&uid&uidCyteNiO)1)%
}
4erial&print(IThe Rard number istension o' the sketch
i' (code551120) //*' the number code is 1120&&&
{ //"pen program part
digitalrite (0! *+)% //&&&the ,-. on $in 0 should light up &&&
delay (9)% //&&'or 9 seconds&&
digitalrite (0! ,")% // Kand than turn o''&
} //-nd o' the program part
} //-nd o' the sketch
1
-
8/16/2019 Arduino Tutorials En
92/100
7utorials with additional (#ui"$ent
>ey"ad shield
Task: se a keypa! shiel! with 'r!uino.
8e*uire! e*uipment: 'r!uino K $eypa! shiel!
The keypa! shiel! has some a!vantages over the simple ; mo!ule. t !oesnAt has to -e
wire! that complicate! an! has si@ push -utton% which can -e use!. These si@ -uttons can
-e rea! out trough the analog pins of the microcontroller. The -uttons are all connecte!%
trough !ifferent resistors% with a analog pin ('0). ThatAs why the analog pin '0 can only -e
use! limite! for !ifferent purposes. The shiel! !oesnAt even has the '0 pin.
2
The pins on this site are unchange!(from the right site: '%'+%'%'2%'1...etc.)
-
8/16/2019 Arduino Tutorials En
93/100
The keypa! shiel! can -e plugge! on the > -oar! or the /9' -oar! for e@ample.
The power supply pins of the keypa! shiel! shoul! -e plugge! into the power supply pins
of the microcontroller -oar! (on the mi!!le -ottom you can fin! the power supply pins of
the keypa! shiel!. The la-el J or J can also help you to fin! them). The pins on the topof the 'r!uino are also use! -y the keypa! shiel! (pin 0=1). &ome of them are use! for
the ; on the keypa! shiel!. The other free pins are com-ine! in a row of slots (see on
the top of the image).
f you want to use these slots% we recommen! to sol!er a connector strip onto them.
>ne e@ample of a sketch can -e the following:
Code:
//4ample using ,iGuidRrystal library
Xinclude ,iGuidRrystal&hJ
/
This program ill test the ,R. panel and the buttons
Mark Cramell! Vuly 01
/
// select the pins used on the ,R. panel
,iGuidRrystal lcd(3! 6! D! 9! E! 2)%
// de'ine some values used by the panel and buttonsint lcdUkey 5 %
int adcUkeyUin 5 %
Xde'ine btnB*+T
Xde'ine btn#$ 1
Xde'ine btn."; 0
Xde'ine btn,-FT
Xde'ine btn4-,-RT D
Xde'ine btn;";- 9
3
-
8/16/2019 Arduino Tutorials En
94/100
// read the buttons
int readU,R.Ubuttons()
{
adcUkeyUin 5 analogBead()% // read the value 'rom the sensor
// my buttons hen read are centered at these valies< ! 1DD! 06! 9D! 2D1
// e add appro> 9 to those values and check to see i' e are closei' (adcUkeyUin J 1) return btn;";-% // e make this the 1st option 'or speed
reasons since it ill be the most likely result
i' (adcUkeyUin 9) return btnB*+T%
i' (adcUkeyUin 169) return btn#$%
i' (adcUkeyUin 3) return btn.";%
i' (adcUkeyUin 999) return btn,-FT%
i' (adcUkeyUin 26) return btn4-,-RT%
return btn;";-% // hen all others 'ail! return this&&&
}
void setup()
{
lcd&begin(1E! 0)% // start the library
lcd&setRursor(!)%
lcd&print(IMessageI)% // print a simple message
pinMode (0! "#T$#T)%
}
void loop()
{
digitalrite (0! *+)%
lcd&setRursor(6!1)% // move cursor to second line I1I and 6 spaces over
lcd&print(millis()/1)% // display seconds elapsed since poer@up
lcd&setRursor(!1)% // move to the begining o' the second line
lcdUkey 5 readU,R.Ubuttons()% // read the buttons
sitch (lcdUkey) // depending on hich button as pushed! e per'orm an action
{
case btnB*+T<
{
lcd&print(IB*+T I)%
digitalrite (0! ,")%
-
8/16/2019 Arduino Tutorials En
95/100
break%
}
case btn,-FT<
{
lcd&print(I,-FT I)%
break%}
case btn#$<
{
lcd&print(I#$ I)%
break%
}
case btn.";<
{
lcd&print(I."; I)%
break%
}
case btn4-,-RT<
{
lcd&print(I4-,-RTI)%
break%
}
case btn;";-<
{
lcd&print(I;";- I)%
break%
}
}
}
+
-
8/16/2019 Arduino Tutorials En
96/100
,C !is"lay
7as4 &how a te@t on an
-
8/16/2019 Arduino Tutorials En
97/100
Wiring4 The connection of the
-
8/16/2019 Arduino Tutorials En
98/100
Progra$ing4
?e will nee! another li-rary to work with the
-
8/16/2019 Arduino Tutorials En
99/100
lcd&setRursor(!1)% // lcd&setRursor to de'ine sign and ro here the te>t
//should start
lcd&print(I+ood ,uckSI)% // lcd&print to sho te>t on the ,R.
}
(=tension4
Must like with the simple ;% you can show the result of measurements on the
-
8/16/2019 Arduino Tutorials En
100/100
lcd&setRursor(!)% //8Measurement