[PDF] Arduino - Reference - NITC
[PDF] ARDUINO MEGA2560 ADK (for Android)
[PDF] Arduino - Premiers pas en informatique embarquee - Le blog d
[PDF] Arduino pour bien commencer en électronique et en programmation
[PDF] PDF Projets Arduino pour les Nuls ePub
[PDF] Télécharger Arduino Pour les Nuls, édition poche PDF
[PDF] PROGRAMMATION ARDUINO
[PDF] Initiation ? la mise en oeuvre matérielle et logicielle de l 'Arduino
[PDF] Arduino Programming Notebook - pdf - Arduino Playground
[PDF] Initiation ? la mise en oeuvre matérielle et logicielle de l 'Arduino
[PDF] schematics in pdf - Arduino
[PDF] Package 'AUC ' - R
[PDF] Licencias de salud ocupacional - Ministerio de Salud y Protección
[PDF] Authentification
[PDF] Première connexion ? Base Elèves Premier Degré
IntroductiontoArduino
Apieceofcake!
byAl anG.Smith
September30,2011
CoverPhotoCred it:ArduinoCake
Copyright©2011AlanG.Smith.
AllRight sReserved.
Cakemadeb yLisaSmithan dfamily
IntroductiontoArduino:Apieceofca ke!
Copyright©2011AlanG.Smith.AllR ight sR eserve d. Theauth orcanbecontactedat :alan@introtoarduino.com Thehar dcopyofthebookcanbepurch asedfr omhttp://www.amazon.com Themost recentPDFis freeathttp://www.introtoarduino.com
ISBN:14636983 48
ISBN-13:978-1463698348
Thisbookisded icatedt o:
Myw ifewhofirsten couragedmet oteachthiscla ssand
thenputup withmyspen dingco untlesshou rsonth isbook andalsohe lpedwit hnumerouscommen tsonthefir st proof.
Mychi ldrenwhoexcitemeaboutt eaching.
Myfa therwhospentmanyho urswithmeo ntheVic20,
Commodore64,andtheroboticarms cien ceproj ect.
Withouthisinvestmen t,Iwould n'tbetheengineerIam
today.
Allwhow oulddesire tomakesomething, maythisbook
helpyouwit hyourinventi ons. Whateveryoudo,worka titwithall yourhear t,asworkingfo r theLo rd,notformen.
Colossians3:23(NIV1984)
Contents
1GettingStarted1
1.1Whatis aMicrocontrol ler?.. ... ..................1
1.2Inst alltheSoftware.... ........ ...............3
1.3TheI ntegratedD evelopmentEnvironment(IDE).. ........5
1.4Ourfirst circuit.. ......... ..................7
1.5Updat edCircuit............ ........... ... ... 9
1.6OurF irstPro gram......... ................. .9
1.7Comme nts................. ... ... ... .. ... .12
1.8Gotc has............... ... ... ... ... ... ... .13
1.9Exer cises................... ... ... ... ... ..14
2MakingLightPatterns17
2.1"Blinky ".............. .. ... ... ... ... ... ..17
2.2IFSt atement s......................... ... ..17
2.3ELSE Statements... .........................20
2.4WHIL Estatements..... ......................21
2.5What istruth(tr ue) ?...........................22
2.6Combin ations................... ... ... ... ..24
2.7FORst atemen ts........................ ... ..25
2.8OurNe wCircuit ......... ............ .. ... ..26
2.9Intr oducingArrays.................. .........29
2.10Exer cises................... ... ... ... ... ..31
3Input33
3.1Push buttons........................ ... ... .33
3.2Pot entiometers............................ ..41
3.3RGBL EDs...... ............ .. ... ... ... ... 46
v
Contents
3.4Exe rcises................... .. ... ... ... ... 49
4Sound51
4.1OurCirc uit.... ........... ... ... ... ... ... ..51
4.2Simple note........ ........... ... ... ... ... 52
4.3Music.. ........... ... ... ... ... ... .. ... ..5 3
4.4Musicwi thfunctions. ........... ..............55
4.5Exer cises................... ... ... ... ... ..58
5Makingadigitalthermometer59
5.1Serial Monitor............ ................. .59
5.2Measuri ngthetemperature....... ......... ......62
5.3Hook inguptheLCD......... ..... ......... ...66
5.4Talkin gtotheLCD.... ...... ........ ...... ... 68
5.5Bring ingitalltogether..... ... ... ..............71
5.6Exer cises................... ... ... .. ... ... 73
6Graphics(Pictures)onourLCD75
6.1Binary andHex...... ...... ......... .. ... ... 75
6.2Using graphics...... ................. ... ... .77
6.3Makinga Chart.......... ... .............. ..8 2
6.4Exe rcises................... .. ... ... ... ... 89
7SensorsGalore91
7.1Intr oduction......................... ... ... 91
7.2Phot oCell(LightSensor) ..... ............... ....91
7.3TiltSe nsor.. ................. ... ... ... ... .93
7.4ReedSw itch(Magnetic FieldDetector)... .............95
7.5PiezoE lement(Vibr ationsensor)....... ............96
7.6Exer cises................... ... ... ... ... ..98
8Makingarubberbandgun99
8.1OneSe rvo... .............. ... ... ... ... ... 99
8.2Joys tick.................. .. ... ... ... ... ..101
8.3Pan/T iltbracket........ ................. ... .103
vi
Contents
8.4Addin gafiringmechanism... ... ........... .....106
8.5Exer cises................... ... .. ... ... ... 110
9Makeyourownproject!111
10N extSteps113
AArduinoReference115
A.1Str ucture................. ... ... ... ... ... .11 6 A.2Va riables................... ... ... ... ... ..1 20 A.3Fu nctions................... ... ... ... ... ..1 21 A.4PC D8544(LCDController)L ibrary..... .............125
BPartsinKit127
B.1Firstu sedinChapter 1..... ..... ...............127 B.2Firstu sedinChapter 2..... ...... ..............127 B.3Firstu sedinChapt er3.... ...... ...............12 8 B.4Firstu sedinChapt er4.... ...... ...............12 8 B.5Firstu sedinChapter 5..... ..... ...............128 B.6Firstu sedinChapter 6..... ...... ..............128 B.7First usedinChapt er7.... ...... ...............12 8 B.8Firstu sedinChapt er8.... ...... ...............12 9
CSampleSolutionstoSelectedExercises131
C.1Chapte r1Solutions...... ... ..................131 C.2Chapt er2Solutions..... ... ...................134 C.3Chapte r3Solutions..... ... ...................135 C.4Chapte r4Solutions...... .. ...................140 C.5Chapte r5Solutions...... ... ..................144 C.6Chapt er6Solutions..... ... ...................149 C.7Chapte r7Solutions..... ... ...................156 C.8Chapte r8Solutions...... .. ...................160 vii
Listings
1.1Simple stProgram......... ................. ..9
1.2led1/le d1.pde..................... ... ... ... 10
1.3Blink/ Blink.pde......................... ... .12
2.1blink_if /blink_if.pde..........................17
2.2blink_e lse/blink_else.pde.......................20
2.3blink_w hile/blink_while.pde.....................21
2.4blink_f or/blink_for.pde........................25
2.5lightP attern1/lightPattern1.pde....................28
2.6lightP attern1b/lightPattern1b.pde..................29
3.1butt on1/button1.pde..........................35
3.2butt on2/button2.pde..........................39
3.3Const rain............... ... ... ... ... ... ... 40
3.4pot1/ pot1.pde......................... ... ..42
3.5pot2/ pot2.pde......................... ... ..44
3.6pot3/ pot3.pde......................... ... ..44
3.7rgb_3p ot/rgb_3pot.pde........................48
4.1sou nd_simple/sound_simple.pde..................52
4.2soun d_2/sound_2.pde.........................54
4.3soun d_3/sound_3.pde.........................56
4.4soun d_array/sound_array.pde....................57
5.1blink_if _serial/blink_if_serial.pde..................59
5.2temp _serial/temp_serial.pde.....................63
5.3lcd1/lc d1.pde..................... ... ... ... 69
5.4temp _lcd/temp_lcd.pde........................71
ix
Listings
6.1temp _lcd_graphic/temp_lcd_graphic.pde..............79
6.2temp _lcd_graphic_chart/temp_lcd_graphic_chart.pde......83
7.1phot ocell/photocell.pde........................92
7.2tiltse nsor/tiltsensor.pde........................94
7.3reed 1/reed1.pde............................. 95
7.4knoc k1/knock1.pde...........................97
8.1serv o1/servo1.pde...........................100
8.2joys tick/joystick.pde..........................102
8.3pantilt /pantilt.pde...........................104
8.4rub berBandGun/rubberBandGun.pde................108
x
Chapter1
GettingStarted
Thepurp oseofthisbookistoget yousta rtedonthero adtocreatingthings usingmicro-c ontrollers.Wewilldiscussonlyenoughelectronicsforyouto makethecir cuits,and onlyenoughprogrammingforyouto getstarted.The focuswillbeony ourmakingthin gs .Itismy hopethatasyo ugothroughthis bookyouwil lbefloodedw ithideasofth ing sthatyoucanmake .Sole t'sget going...
Thefirstque stionwe'l lstartwithis:
1.1Whati saMicrocontr oller?
Wikipedia
1 says: cuitcontain ingaprocessorcore,memory,and pro grammablein- put/outputperipherals allco mputershave)andmemory,andsome input/outputp insthatyouc an control.(oftencalledGPI O-GeneralPurposeIn putOutputPins). 1 1
Chapter1GettingStar ted
Forthis book,wewillb eusingtheArdui noUnob oard.Thiscom binesa micro-controlleralongwithalloftheextrastomak eiteasyforyout obuild anddebu gyourprojects. Wewi llbeusingabr eadboa rdinthisbook.T his isarelativelyeasyway toma kecircuitsqui ckly.Breadboardsarema defordoingquickex periments. Theyarenotknownfork eepingci rcuitstogetherf oralongti me.Whe nyouare readytomakeap rojectt hatyouwantt ost ayaroundforawhile,youshould consideranalternativeme thodsu chaswire-wrappingorsolderingoreven makingaprintedc irc uitboard(PCB). Thefirstthi ngyoushou ldnoticeaboutt hebreadbo ardisallofthehole s. Thesearebroke nupinto2s etsofcolumnsandas etofro ws(therowsare 2
1.2Inst alltheSoftware
dividedinthemi ddle).The columnsar enameda,b,c,d,e,f ,g,h,i,andj(from lefttoright ).Ther owsarenumbered1-30.( fromtopto bottom).T hecolumns onthe edges donothavelettersorn umbers . Thecolu mnsontheedgesareconn ectedf romtopto bottominsideof the breadboardtomakeiteasytosu pplypo werandgro und.(Y oucanthinkof groundasthenegati vesideof abatteryan dthepowerasthepositiveside.)
Forthi sbookourpower willbe+5volt s.
Insideofthebreadboa rd, theholes ineachrowareconnectedupto thebre ak inth emiddleofth eboard.ForEx ample:a1,b 1,c1,d1,e1allh aveawire insid e ofthe breadb oardtoconnectthem.Thenf1,g1,h1 ,i1 ,andj1areal lconnecte d. buta1is notco nnecte dtof1.This maysoundconfusingnow,butitwillquickly cometomake sensea swewireupcir cuits.
1.2Install theSoftware
Ifyou haveac cesstotheintern et,therearestep-by-ste pdi rectionsandthesoft- wareavaila bleat:http://arduino.cc/en/Main/Software
Otherwise,theUSBstickinyourk it
2 hastheso ftware undertheSoftware Directory.Therearetwodire ctoriesundertha t.Oneis"W indows"andthe otheris"MacOSX" .Ifyou areinstal lingo ntoLinux, youwil lneedto follo w thedire ctionsat:http://arduino.cc/en/Main/Software
1.2.1Window sInstallations
1.Plu ginyourbo ardv iaUSBandwaitforW indowstobegin itsdriver
installationprocess.Afte rafewmoments,theprocess willfail.(Thisis notunexpe cted.)
2.Clic kontheStartMenu, andop enuptheCo ntrolPanel.
3.Wh ileintheContr olP anel,navigate toSystemandSecurity.Ne xt,click
onSyst em.OncetheSystemwind owisup,opent heDeviceMa nager. 2 Thisbookwasor iginal lywrittent ogoalongwithaclass.Ifyouha vethebook,butn otthe kitgotohttp://www.introtoarduino.comformor einformationa ndallofthesource codeinthis book. 3
Chapter1GettingStar ted
4.Lo okunderPorts (COM&LPT).Yousho uldseeanopenportn amed
"ArduinoUNO(COMxx)".
5.Righ tclickonthe"Ar duinoUNO(COMxx)" porta ndchooset he"Update
DriverSoftwar e"option.
6.Ne xt,choosethe"Br owsemycomputerfor Driversoft ware"option.
7.Fina lly,navigatetoandselect theUno'sdriverfile,named"ArduinoUNO.inf",
locatedinthe"Driver s"f olderofthe ArduinoSoftwaredownload(not the "FTDIUSBDrive rs" sub-directory).
8.Wind owswillfinishupthedriv erinstallati onfromthere.
9.Do uble-clicktheArduinoapplication.
10.Ope ntheLEDblin kexampleske tch:File>E xamples>1.Bas ics>Blink
11.Se lectArduinoUnoun dertheTools>Boardme nu.
12.Se lectyourserialpor t(ifyoudon'tk nowwhichone, disconnecttheUNO
andthee ntrythatdis appearsistherighto ne.)
13.Click theUplo adbutton.
14.A fterthemessage"Do neuploading" appears,youshouldseeth e"L"
LEDb linkingonceasecond.(The"L "LEDisontheAr duin odirectly behindtheUSBport .)
1.2.2MacInstal lation
1.Con necttheboardviaUSB.
2.Dr agtheArduin oapplicationon toyourharddrive.
3.Wh enNetworkPr eferencescomesup,jus tclick"Apply"(rememberthe
/dev/tty/usb.)
4.S tarttheprogram .
4
1.3TheI ntegratedD evelopmentEnvironment(IDE)
5.Ope ntheLEDblin kexampleske tch:File>E xamples>1.Bas ics>B lin k
6.S electArduinoUnoun dertheTools>Boardme nu.
7.Se lectyourserialpor t(ifyoudon'tk nowwhichone, disconnecttheUNO
andthee ntrythatdis appearsistherighto ne.)
8.Click theUplo adbutton.
9.A fterthemessage"Do neuploading" appears,youshouldseethe"L"
LEDbl inkingonceasecond.(The"L "LEDisontheArdu ino directly behindtheUSBconn ection)
1.3TheI ntegratedDevelopm entEnvironment (IDE)
Youuset heArduino IDEonyou rcomputer(picturefoll owing)tocr eate,open, andchang esketches(Ardu inocallsprograms"sketches".Wewillusethet wo wordsinterchan geablyinthisbook.).Sketchesdefinewhattheboardwilldo. Youcane itheruset hebuttonsalongthe topoftheI DEortheme nuit ems. 5
Chapter1GettingStar ted
PartsoftheID E:(fro mleftt oright,toptob ottom)
tobeconver tedintoinstructionsthattheboardunderstands.Th isprocess iscalle dcompiling. andyoup robablywon' thaveaneedtoeither. ) •Op enExistingSk etch-Thisloadsasketchfr omafileonyourcomputer. 6
1.4Ourfirst circuit
•UploadtoBoard-ThiscompilesandthentransmitsovertheUSBca ble toyou rboard. •S erialMonitor-Wewill discussthisinsect ion 5.1. •T abButton- Thisletsyoucreatem ul tiplefilesinyo ursketch.Thisisfor moreadvanc edprogrammingthanwewilldo inthisclass. alsowher eerrormessagesdis playifyoumakeamistak eintypingyour program.(oftencalleda syntaxerror) •LineNumber-Thisshowsyouwhatlinenumberyourcursorison.It is usefulsinceth ecompilergivese rrormessa geswithalinenumber
1.4Ourfi rstcircuit
Beforewegettothe program min g,let's connectanLED .LEDstandsforLig ht EmittingDiode.Adiodeonlya llowselectrici ty toflowthro ughit oneway ,so ifyo uhookitupb ackwardsitwon't wo rk. Ifyou connect theLEDdirectlytopoweran dgroun d,toomuchcurrentwill goth roughthediodeanddestroy it.Tokeeptha tfromhap peningwe willu se aresistortolimitthecurrent.Youcanthinkofaresistorlikeaw at erpipe.The higherthevalueo ftheres istorislikeusi ngasmal lerpip ethatlets lesselectric- ity"flow "through.This isnottechnicallycorrect,butit iscloseenoughfo rthis book.Wewillu sea330 Ohm(Ohmisoften shownasΩ)resistor(Resistanceisquotesdbs_dbs19.pdfusesText_25