ebook img

Arduino Circuits and Projects Guide - Elektor PDF

15 Pages·2014·0.8 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Arduino Circuits and Projects Guide - Elektor

Table of Contents 1 Introduction .................................................................................. 9 1.1 The Arduino Project . 10 1.2 Book Structure and Target Audience ............................................ 10 2 Structure and Function of the Hardware ........................................ 11 2.1 Lots to Choose From: Arduino Versions ........................................ 12 2.2 Available for all Purposes: Shields ............................................... 13 2.2.1 Proto Shield ................................................................... 13 2.2.2 Motor Shield .................................................................. 13 (WKHUQHW6KLHOG. 14 1RWKLQJ+DSSHQVZLWKRXW3RZHUWKH3RZHU6XSSO\ ....................... 15 7KH0LFURFRQWUROOHU+HDUWRIWKH$UGXLQR ..................................... 15 2.5 The PC Connection: USB interface ............................................... 16 3 D evelopment Environment and Programming Basics ..................... 17 ,QWHJUDWHG'HYHORSPHQW(QYLURQPHQW,'( ................................. 17 )RU/LQX[8VHUV$UGXLQR,'(8QGHU8EXQWX ................................. 23 6LPSOH%HJLQQLQJV7KH:DUQLQJ/LJKW ......................................... 24 3.4 General Structure of an Arduino Sketch ....................................... 24 %DVLF(OHPHQWVRIWKH&3URJUDPPLQJ/DQJXDJHIRU$UGXLQR ........... 27 3.6 Program Structures ................................................................... 29 $UGXLQR6SHFL¿F)XQFWLRQV ......................................................... 29 $UGXLQR3XQFWXDWLRQ6\QWD[(OHPHQWV. 30 'DWD6WRUDJH(OHPHQWV9DULDEOHV ............................................... 30 3.10 The Art of Math: Operators ........................................................ 31 3.11 To Be Clear: Logical Operators .................................................... 32 3.12 Unchanging Parameters: Constants ............................................. 35 3.13 Dealing with Related Data of the Same Type: Arays . 36 3.14 Program Control Structures ........................................................ 36 3.15 Controling Timing . 39 3.16 Mathematical Functions ............................................................. 40 3.17 Random numbers ..................................................................... 40 ([WHQGLQJWKH2SWLRQV$GGLQJ/LEUDULHV ....................................... 41 4 Electronic Components and Low-Cost ‘Freeduinos’ ........................ 43 %UHDGERDUGV6LPSOHDQG(IHFWLYHZLWKRXW6ROGHULQJ . 43 4.2 Prototyping Boards: Durable Construction without Chemicals .......... 44 /RZ&RVWµ)UHHGXLQRV¶ . 45 $UGXLQRDQG,WV+HOSHUV%DVLF(OHFWURQLF&RPSRQHQWV ................... 46 4.4.1 USB Cable ..................................................................... 46 4.4.2 Resistors . 46 4.4.3 Capacitors . 47 4.4.4 Potentiometers . 47 /('V ............................................................................. 48 5*%/('V ...................................................................... 48 5 Boek Arduino UK 131010.indd 5 10-12-13 11:33 7$%/(2)&217(176 4.4.7 Switches . 49 4.4.8 Silicon Diodes................................................................. 49 4.4.9 Transistors . 49 5 Helo World . 51 5.1 Cut to the Chaser ..................................................................... 51 ,W*HWV%ULJKWHU&RQWUROOLQJ3RZHU/('V ...................................... 53 5.3 POVino: Persistence-of-Vision Display .......................................... 54 6 Displays and Display Techniques ................................................... 59 6.1 Bar Graph Display: The Classic for Measurement Applications ......... 59 6.2 Simple and Cheap: 7-Segment Displays . 59 'LJLW6HJPHQW'LVSOD\V$.H\&RPSRQHQWIRU,QVWUXPHQWDWLRQ 62 0LQL0RQLWRUIRU6LJQVDQG*UDSKLFV7KH/(''RW0DWUL[ . 67 6.5 Dot Matrix Display as a Two-Digit Digital Display ........................... 70 0LFUR/HDUQVWR:ULWH$OSKDQXPHULF'LVSOD\ ................................ 72 6.7 The LCD .................................................................................. 76 7 Measurement and Sensors ............................................................. 81 )OH[LEOHDQG(DV\WR5HDG$Q/('9ROWPHWHU . 81 9ROW$PPHWHU3UHFLVH,QVWUXPHQWIRUWKH+REE\LVW¶V/DE ............. 83 .LORRKPPHWHUIRU6SHFL¿F$SSOLFDWLRQV . 87 1R0RUH)DXOW\(OHFWURO\WLF&DSDFLWRU:RHV7KHµ(OFDGXLQR¶7HVWHU .. 89 µ3LFRIDUDGXLQR¶0HDVXULQJ6PDOOHU&DSDFLWDQFHV ........................... 91 µ7UDQVLVWLQR¶7UDQVLVWRU7HVWHU . 93 7.7 A Simple NTC Thermometer ....................................................... 95 +RWRU&ROG"7HPSHUDWXUH0HDVXUHPHQW8VLQJWKH$' ...... 98 7.9 Remote Thermometer . 99 µ7KHUPRGXLQR¶3UHFLVLRQ7KHUPRPHWHUZLWK6HJPHQW'LVSOD\ ...... 100 :KHQ$UH:H0RVW&RPIRUWDEOH"±7KH+\JURPHWHU ...................... 105 µ%DWGXLQR¶&DSDFLW\0HDVXUHPHQWIRU5HFKDUJHDEOH%DWHULHV . 107 2SWLFDO6HQVRUV,PSRUWDQWIRU0RUHWKDQ-XVW3KRWRJUDSK\ . 112 5HÀH[/LJKWIRU*HRFDFKLQJ ........................................................ 113 7.15 For Professional Photographers: A Digital Light Meter . 115 +RPHµ5DGDU6WDWLRQ¶'LVWDQFH0HDVXUHPHQW8VLQJ8OWUDVRXQG. 117 8 Timers, Clocks and Interrupts . 123 8.1 Morning and Night Fun: Grand Prix Toothbrush Timer .................... 123 3UDFWLFDODQG$FFXUDWH'LJLWDO&ORFNZLWK/(''LVSOD\ . 130 :KR¶V)DVWHU"$5HDFWLRQ7LPHU .................................................. 135 µ7LPHULQR¶8QLYHUVDO7LPHUZLWKD6HJPHQW'LVSOD\ . 138 3OXJLQ7LPHUWR0DNH/LIH(DVLHU ................................................ 141 8.6 Atomic Precision: The DCF77 Radio Clock ..................................... 142 2XWSXWRI7LPHDQG'DWHWRWKH6HULDO,QWHUIDFH ............................ 145 8.8 Stand-Alone DCF77 Clock with LCD Display .................................. 146 9 Interfaces . 149 8QLYHUVDODQG6LPSOH7KH,ð&,QWHUIDFH ...................................... 149 :KHQ:H5XQ2XWRI3LQV3RUW([SDQVLRQ . 152 +H[DGHFLPDO'HEXJJHU'LVSOD\8VLQJD'LJLW6HJPHQW'LVSOD\ . 155 /&'&RQWUROYLD,ð&8VLQJWKH3&) . 158 9.6 This Time, Fully Digital: The LM75 Thermometer ........................... 160 9.7 Power-Saving: Real-Time Clock with Date Display ......................... 163 6 Boek Arduino UK 131010.indd 6 10-12-13 11:33 7$%/(2)&217(176 $IWHU,QFOXGLQJ7KH,5UHPRWH:LUHOHVV3UDFWLFDO4XLFN7KH,5,QWHUIDFH 167 µ/DPSLQR¶$Q,5&RQWUROOHG5*%/DPS ........................................ 171 7LPHO\/X[XU\$Q,5&RQWUROOHG'LJLWDO&ORFN ............................... 174 2SWLPDOIRU0LFURFRQWUROOHUV7KH36,QWHUIDFH ........................... 178 .H\ERDUGDQG0RXVHDV8QLYHUVDO,QSXW'HYLFHV ........................... 178 9.13 A Complete Microcomputer with LCD Monitor and Keyboard . 181 10 Sounds and Synthesizer . 185 10.1 Simple Tones . 185 7UDQVGXFHUVDQG$PSOL¿HUV . 187 )DVW3:00DNHV,W+DSSHQ1RW-XVWWRQHV%XW6RXQG:DYHV ........ 188 7KHUHPLQ7KH&RQWDFWOHVV0XVLFDO,QVWUXPHQW ............................. 195 10.5 Audio Processing. 197 9&2$7XQDEOH6LQH:DYH6RXUFH .................................... 198 10.5.2 Digital Signal Processing .................................................. 200 10.6 Sound Cloud: A Digital Synthesizer .............................................. 203 11 Digital Control Techniques ............................................................. 207 11.1 Control Types ........................................................................... 208 11.1.1 P Controler . 208 ,&RQWUROOHU .................................................................... 209 3,&RQWUROOHU .................................................................. 209 11.1.4 PD Controller ................................................................. 209 3,'&RQWUROOHU ................................................................ 210 2SWLPXP:RUNVWDWLRQ/LJKWLQJ'LJLWDO,OOXPLQDWLRQ&RQWURO ............. 210 11.3 A Classic of Control Theory: The Gravity Compensator ................... 214 12 Physical Computing ....................................................................... 221 6HUYRV&RQWUROWKH:RUOG ........................................................... 221 µ3KRWLQR¶'&DPHUD6ZLYHO . 225 µ&UDQLQR¶0RXVH&RQWUROOHG&UDQH .............................................. 13 Processing . 231 13.1 Arduino and Processing: A Formidable Team ................................. 232 ,QWHUDFWLRQZLWK3URFHVVLQJ'DWD/RJJLQJ7UHQG*UDSKVHWF ........ 233 14 The ‘Living Room Box’: Our Modular Concluding Project ................ 241 14.1 Always Useful: A Clock ............................................................... 242 &RQWUROIURP$IDU7KH,5,QWHUIDFH .............................................. 243 9&RQWUROIRU+L)LV\VWHPV7HOHYLVLRQV/DPSVHWF. 243 7LPHUVDQG6HQVRUVDVWKH%DVLVIRU+RPH$XWRPDWLRQ ................... 243 ,QGRRUDQG2XWGRRU7KHUPRPHWHUV .............................................. 244 1R0RUH'U\$LU$+\JURPHWHU ................................................... 244 7KH+DUGZDUH ............................................................................ 244 Bibliography ......................................................................................... 249 Listings ................................................................................................ 251 List of Figures ...................................................................................... 253 List of Tables ........................................................................................ 257 Index ................................................................................................... 259 7 Boek Arduino UK 131010.indd 7 10-12-13 11:33 1 Introduction 7KHUHDUHWZRPDLQUHDVRQVIRU$UGXLQR¶VVXFFHVV7KH¿UVWLVWKHFRPSOHWHSURFHVVRU ERDUGZKLFKKDVVLJQL¿FDQWO\HDVHGHQWU\LQWRWKHPLFURFRQWUROOHUKDUGZDUHDUHQD7\SL- FDOEHJLQQHUSUREOHPVVXFKDVEDGSRZHUVXSSOLHVSUREOHPVZLWKVHWWLQJWKHFRQ¿JXUD- WLRQSDUDPHWHUVIXVHELWVDQGFU\VWDOVWKDWZRQ¶WRVFLOODWHGXHWREDGORDGFDSDFLWDQFHV DUHDOOIRUHLJQWRWKH$UGXLQRZRUOG7KHERDUGLVVLPSO\FRQQHFWHGWRD3&¶V86%SRUW DQGRIIZHJR3HRSOH\RXQJDQGROGZKR¶YHQHYHUEHHQLQYROYHGLQHOHFWURQLFVZRQ¶W KDYHDQ\GLI¿FXOW\ The second success factor is the associated programming interface, which is provided as free-of-charge open source software. The second success factor is the associated pro- JUDPPLQJLQWHUIDFHZKLFKLVSURYLGHGDVIUHHRIFKDUJHRSHQVRXUFHVRIWZDUH,QDGGL- tion, installation is quick and easy, so the environment is usable immediately. Simple in- troductory examples encourage rapid progress. The selection of complicated parameters LVQRWUHTXLUHGDQGWKH¿UVWH[DPSOHSURJUDPVPD\EHRSHQHGDQGUXQZLWKLQPLQXWHV Furthermore, the Arduino is backed up with a wealth of software libraries, and the num- EHURIZKLFKJURZVGDLO\RIWHQSUHVHQWLQJEHJLQQHUVZLWKWKHLU¿UVWSUREOHPDIWHUWKH introduction of simple examples, the way forward is not clear. This is often due to the lack of detailed descriptions and explanations. The number of projects on the internet, which are explained to a greater or lesser extent, is more likely to cause confusion. Be- cause these applications are designed by a number of different people, each with their own goals in mind, there is no common thread connecting them. 7KDW¶VZKHUH WKLV ERRN FRPHV LQ 3URMHFWV DUH LQWURGXFHG V\VWHPDWLFDOO\ HDFK LQWUR- ducing a different theme. A practical hands-on approach is employed alongside the necessary theoretical foundations and, in a similar vein, important concepts such as A/D conversion, timers and interrupts are presented using practical projects. There are run- ning lights, fully-functional voltmeters, precise digital thermometers, clocks of all kinds, reaction timers and a mouse-controlled robot crane. Along the way, the reader will gain an understanding of the associated controller techniques and pick them up fully — in the truest sense of the word. The practical projects presented herein will not be relegated to the status of mere ‘lab- RUDWRU\SURWRW\SHV¶%\PHDQVRIDSSURSULDWHKLQWVDQGQRWHVRWKHUSUDFWLFDOGHYLFHV will arise, which may be used for home, hobby and work. The projects are always imple- PHQWHGXVLQJHDV\WR¿QGDQGLQH[SHQVLYHFRPSRQHQWV ,Q WKH¿QDOFKDSWHUDQ$UGXLQREDVHG µ/LYLQJ5RRP%R[¶ LVSUHVHQWHG ,W LVGHVLJQHG modularly and may be adapted to individual requirements. The knowledge gleaned throughout the book is used practically to produce a very useful, yet unusual, device. 9 Boek Arduino UK 131010.indd 9 10-12-13 11:33 1 ,I1N7T5R2O'D8U&C7T,I2O1N 1.1 The Arduino Project The ArduinoFRQFHSWFDPHWREHLQDWWKH,QVWLWXWHIRU,QWHUDFWLYH'HVLJQLQ,YUHD ,WDO\ 7KH VHDUFK IRU D ORZFRVWPLFURFRQWUROOHU V\VWHP IRU GHVLJQ VWXGHQWV OHG WR D handy printed circuit board that contained all of the necessary electronic building blocks. The main objective was the development of an inexpensive microcontroller board that could be utilized quickly and simply by art and design students who had no previous knowledge of programming or electronics. 7KH¿UVWYHUVLRQRIWKH$UGXLQRKDUGZDUHFRQVLVWHGRIDNLWWKDWFRXOGEHVROGHUHGWR- gether easily, and it sold out quickly. Newer versions followed in rapid succession. De- signers and artists from other regions took up the idea and the Arduino principle spread, ¿UVWLQ,WDO\WKHQLQWR(XURSHDQGHYHQWXDOO\WRWKHUHVWRIWKHZRUOG ,QWHUHVWJUHZUDSLGO\LQDSSOLFDWLRQDUHDVRXWVLGHRIDUWDFDGHPLHVDQGGHVLJQVFKRROV The concept of a simple and low-cost hardware platform with a freely available, easy-to- learn programming language was quickly embraced by hobbyists. Finally, schools and XQLYHUVLWLHVLQWKHVFLHQWL¿FDQGWHFKQLFDO¿HOGVUHFRJQL]HGWKHHQRUPRXVSRWHQWLDORIWKH $UGXLQRLGHD1HZKDUGZDUHYHUVLRQVDVZHOODVSOXJLQH[SDQVLRQERDUGVRUµVKLHOGV¶ DURVHDQG$UGXLQRDSSOLFDWLRQDUHDVZHUHOLPLWHGRQO\E\XVHUV¶LPDJLQDWLRQV 0HDQZKLOHWKHQXPEHURI$UGXLQRVVKLSSHGKDVH[FHHGHGWKHPDUNE\IDU,I ',<YHUVLRQVDQGFORQHVDUHFRQVLGHUHGDVZHOOWKHDFWXDO¿JXUHPD\H[FHHGPLOOLRQ ,WLVWKHUHIRUHQRH[DJJHUDWLRQWRFODLPWKDWWKH$UGXLQRPLFURFRQWUROOHUERDUGLVWKH most successful of all time. 1.2 Book Structure and Target Audience 7KLVERRNLVDLPHGDWWKRVHZKR¶YHDOUHDG\KDGVRPHEDVLFH[SHULHQFHLQWKHHOHFWURQ- LFV¿HOG7KHW\SLFDOKLJKVFKRROOHVVRQVRQFLUFXLWV2KP¶VODZHWFDUHIXOO\VXI¿FLHQW ,Q&KDSWHUWKHEDVLFIXQFWLRQVDQGFKDUDFWHULVWLFVRIWKHPDMRUFRPSRQHQWVDUHH[- SODLQHG,QGXHFRXUVHPRUHHODERUDWHSURMHFWVDUHGLVFXVVHGVRWKDWHYHQVWXGHQWV and teachers who come with some pre-existing technical knowledge will encounter new challenges. 7KHSURMHFWVDUHJURXSHGLQWRLQGLYLGXDOWKHPHV+RZHYHUFDUHZDVWDNHQWRSODFHWKH less technically challenging material at the beginning of the book. For this reason, be- JLQQHUVZLOO¿QGLWKHOSIXOWRJRWKURXJKWKHERRN¶VFKDSWHUVLQRUGHUHYHQLIQRWHYHU\ single project is constructed. 10 Boek Arduino UK 131010.indd 10 10-12-13 11:33 ( /(&7521,&&20321(176$1'/2:&267µ)5(('8,126¶ Figure 4.1: Breadboard 4.2 Prototyping Boards: Durable Construction without Chemicals ,IDSDUWLFXODUFLUFXLWKDVEHHQFRPSOHWHO\WHVWHGDQGLVZRUNLQJSURSHUO\LWPD\WKHQ be built on prototyping board. There are different versions available. The most common types are stripboard and perfboard. The former usually results in a circuit with fewer solder connections, although many circuit traces are likely to require severing. On the other hand, boards with individual solder pads require all electrical connections have to be individually created, so no manual severing is required on the board itself, but many more solder connections are required. Figure 4.2: Stripboard Figure 4.3: A Perfboard has Individual Pads 44 Boek Arduino UK 131010.indd 44 10-12-13 11:33 ' ,63/$<6$1'',63/$<7(&+1,48(6 +RZHYHUWKHVHDUHUHODWLYHO\XQFRPPRQDQGWKHUHIRUHH[SHQVLYHVRWKH\ZRQ¶WEHGH scribed any further here. Another option for displaying alphabetical characters, the dot matrix display, is covered in a later section. Figure 6.1: Typical Pin Layout on a Single 7-Segment Display The wiring of a common-cathode 7-segment display is shown in Figure 6.2. Since a numeric digit requires several segments to be lit simultaneously, one might expect an in- GLYLGXDODSSUR[ƻUHVLVWRUIRUHDFKVHJPHQWDVSHUWKHVWLSXODWLRQLQ6HFWLRQ +RZHYHUDVZLOOEHH[SODLQHGLQPRUHGHWDLOLQWKHGHVFULSWLRQRIWKHFRQWUROVRIWZDUH E\XVLQJVRFDOOHGPXOWLSOH[LQJDVLQJOHUHVLVWRUVXI¿FHV Figure 6.2: Control of a Single 7-Segment Display. 60 Boek Arduino UK 131010.indd 60 10-12-13 11:33 7+,67,0()8//<',*,7$/7+(/07+(5020(7(5 The problems associated with NTCs, such as the nonlinear relationship between tem- perature and resistance, as well as the requirement of a second resistor to make up a voltage divider, which introduces additional inaccuracy, etc., all fall away. 7KH/0LVDGYDQWDJHRXVHYHQZKHQFRPSDUHGZLWKWKHDQDORJ$':KHQDQ alog transmission of temperature data takes place over a long cable, errors are eas- LO\ LQWURGXFHG)LUVWO\WKH LQWULQVLF OLQH LPSHGDQFHPD\ OHDGWRWKH¿UVWHUURUV7KHQ electromagnetic radiation, such as from mobile phones and electric motors, may cause unacceptable interference. Lastly, contact stresses at plugs and connections may lead to HUURQHRXVUHDGLQJV)RUVKRUWOLQHVWKHVHLQÀXHQFHVDUHXVXDOO\QHJOLJLEOHEXWDOUHDG\ for lines in excess of 1 m, problems start occurring in practice. Digital transmission via ,ð&RQWKHRWKHUKDQGLVVLJQL¿FDQWO\PRUHUHVLOLHQW $QDORJWKHUPRPHWHUVDOZD\VUHTXLUHWKHXVHRIRQHRIWKHSURFHVVRU¶V$'FRQYHUWHUV and its corresponding pin. For larger applications, this can quickly lead to a pin short- 3 DJH'XHWRWKHWKUHHFRQ¿JXUDEOHDGGUHVVSLQVXSWRRIWKHVHVHQVRUVPD\EH FRQQHFWHGWRWKH,ð&EXVVLPXOWDQHRXVO\ Figure 9.11: LM75 for Local Measurements 7KHSURJUDPVWDUWVE\LQFOXGLQJWKH:LUHKOLEUDU\7KHQIRURXUDSSOLFDWLRQWKHDG GUHVVHVIRUWZRVHQVRUVDUHVHW7KHQWKH/&'OLEUDU\LVLQFOXGHGDQGWKHSLQVGH¿QHG ,Qsetup(), Wire and Lcd are initialized. Then follows the printing of a template for the output of temperature values to the LCD. The main loop begins by setting the most VLJQL¿FDQWDQGOHDVWVLJQL¿FDQWE\WHVIRUHDFKFRQQHFWHGVHQVRU7KHWZRVHQVRUVDUH then read, one after the other. Conversion of the byte values takes place as per the /0¶VGDWDVKHHW)LQDOO\WKHWHPSHUDWXUHUHDGLQJLVRXWSXWWRWKH/&' 161 Boek Arduino UK 131010.indd 161 10-12-13 11:33 $&203/(7(0,&52&20387(5:,7+/&'021,725$1'.(<%2$5' 0RusereDd()cRQ¿rP click = Mouse.read(); mouseX = Mouse.read(); mouseY = Mouse.read(); // send data Serial.print(“X=”); Serial.print(mouseX, DEC); Serial.print(“\tY=”); Serial.print(mouseY, DEC); switch (click) { case Serial.print(“\t left click”); break; case 10: Serial.print(“\t right click”); break; case 11: Serial.print(“\t both click”); break; } Serial.println(); delay(20); } void mouseInit() { Mouse.write(0xff); // reset Mouse.read(); // con¿rm byte times Mouse.read(); Mouse.read(); Mouse.write(0xf0); // remote mode Mouse.read(); // con¿rm delayMicroseconds(100); } ,QRXUODWHUFKDSWHURQSK\VLFDOFRPSXWLQJSURMHFWVDUHSUHVHQWHGLQZKLFKYDULRXVGH- vices are controlled using the mouse. 9.13 A Complete Microcomputer with LCD Monitor and Keyboard Connect a keyboard and a mouse to the Arduino, and you have yourself a complete microcomputer. Together with an LCD display, it forms a system with performance com- SDUDEOHWRWKH¿UVWHDUO\PLFURFRPSXWHUV ,QWKHIROORZLQJVNHWFKDVLPSOHµW\SHZULWHU¶LVSUHVHQWHGDVDVLPSOHDSSOLFDWLRQ)RU WKLVWKHFKDUDFWHUVUHFHLYHGIURPWKHNH\ERDUGDUHGLVSOD\HGRQWKH/&'µPRQLWRU¶ :KHQHYHUDNH\LVSUHVVHGDNH\ERDUGJHQHUDWHVDVFDQFRGH7KHVHVFDQFRGHVGRQ¶W FRUUHVSRQGWRWKH$6&,,WDEOHLQDQ\VLPSOHZD\7KHUHIRUHLW¶VQHFHVVDU\WR¿[HDFK 181 Boek Arduino UK 131010.indd 181 10-12-13 11:33

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.