Issuu on Google+


Table of Contents 1

Introduction .................................................................................. 1.1 The Arduino Project................................................................... 1.2 Book Structure and Target Audience ............................................

9 10 10

2

Structure and Function of the Hardware ........................................ 2.1 Lots to Choose From: Arduino Versions ........................................ 2.2 Available for all Purposes: Shields ............................................... 2.2.1 Proto Shield ................................................................... 2.2.2 Motor Shield ..................................................................   (WKHUQHW6KLHOG...............................................................  1RWKLQJ+DSSHQVZLWKRXW3RZHUWKH3RZHU6XSSO\ .......................  7KH0LFURFRQWUROOHU+HDUWRIWKH$UGXLQR ..................................... 2.5 The PC Connection: USB interface ...............................................

11 12 13 13 13 14 15 15 16

3

Development Environment and Programming Basics .....................  ,QWHJUDWHG'HYHORSPHQW(QYLURQPHQW ,'( .................................  )RU/LQX[8VHUV$UGXLQR,'(8QGHU8EXQWX .................................  6LPSOH%HJLQQLQJV7KH:DUQLQJ/LJKW ......................................... 3.4 General Structure of an Arduino Sketch .......................................  %DVLF(OHPHQWVRIWKH&3URJUDPPLQJ/DQJXDJHIRU$UGXLQR ........... 3.6 Program Structures ...................................................................  $UGXLQR6SHFLÂżF)XQFWLRQV .........................................................  $UGXLQR3XQFWXDWLRQ6\QWD[(OHPHQWV.........................................  'DWD6WRUDJH(OHPHQWV9DULDEOHV ............................................... 3.10 The Art of Math: Operators ........................................................ 3.11 To Be Clear: Logical Operators .................................................... 3.12 Unchanging Parameters: Constants ............................................. 3.13 Dealing with Related Data of the Same Type: Arrays ...................... 3.14 Program Control Structures ........................................................ 3.15 Controlling Timing..................................................................... 3.16 Mathematical Functions ............................................................. 3.17 Random numbers ..................................................................... ([WHQGLQJWKH2SWLRQV$GGLQJ/LEUDULHV .......................................

17 17 23 24 24 27 29 29 30 30 31 32 35 36 36 39 40 40 41

4 

Electronic Components and Low-Cost ‘Freeduinos’ ........................  %UHDGERDUGV6LPSOHDQG(IIHFWLYHZLWKRXW6ROGHULQJ ..................... 4.2 Prototyping Boards: Durable Construction without Chemicals ..........  /RZ&RVWÂľ)UHHGXLQRVÂś................................................................  $UGXLQRDQG,WV+HOSHUV%DVLF(OHFWURQLF&RPSRQHQWV ................... 4.4.1 USB Cable ..................................................................... 4.4.2 Resistors ....................................................................... 4.4.3 Capacitors ..................................................................... 4.4.4 Potentiometers ...............................................................   /('V .............................................................................   5*%/('V ......................................................................

43 43 44 45 46 46 46 47 47 48 48

  

 

 

5

Boek Arduino UK 131010.indd 5

10-12-13 11:33


7$%/(2)&217(176 4.4.7 4.4.8 4.4.9 5  6    7      

Switches........................................................................ Silicon Diodes................................................................. Transistors .....................................................................

49 49 49

Hello World .................................................................................... 5.1 Cut to the Chaser .....................................................................  ,W*HWV%ULJKWHU&RQWUROOLQJ3RZHU/('V ...................................... 5.3 POVino: Persistence-of-Vision Display ..........................................

51 51 53 54

Displays and Display Techniques ................................................... 6.1 Bar Graph Display: The Classic for Measurement Applications ......... 6.2 Simple and Cheap: 7-Segment Displays .......................................  'LJLW6HJPHQW'LVSOD\V$.H\&RPSRQHQWIRU,QVWUXPHQWDWLRQ  0LQL0RQLWRUIRU6LJQVDQG*UDSKLFV7KH/(''RW0DWUL[................ 6.5 Dot Matrix Display as a Two-Digit Digital Display ...........................  0LFUR/HDUQVWR:ULWH$OSKDQXPHULF'LVSOD\ ................................ 6.7 The LCD ..................................................................................

59 59 59 62 67 70 72 76

Measurement and Sensors .............................................................  )OH[LEOHDQG(DV\WR5HDG$Q/('9ROWPHWHU ................................  9ROW$PPHWHU3UHFLVH,QVWUXPHQWIRUWKH+REE\LVW¶V/DE .............  .LORRKPPHWHUIRU6SHFL¿F$SSOLFDWLRQV .........................................  1R0RUH)DXOW\(OHFWURO\WLF&DSDFLWRU:RHV7KHµ(OFDGXLQR¶7HVWHU ..  µ3LFRIDUDGXLQR¶0HDVXULQJ6PDOOHU&DSDFLWDQFHV ...........................  µ7UDQVLVWLQR¶7UDQVLVWRU7HVWHU..................................................... 7.7 A Simple NTC Thermometer .......................................................

81 81 83 87 89 91 93 95

7.9 Remote Thermometer................................................................  µ7KHUPRGXLQR¶3UHFLVLRQ7KHUPRPHWHUZLWK6HJPHQW'LVSOD\ ...... :KHQ$UH:H0RVW&RPIRUWDEOH"±7KH+\JURPHWHU ...................... µ%DWWGXLQR¶&DSDFLW\0HDVXUHPHQWIRU5HFKDUJHDEOH%DWWHULHV ........ 2SWLFDO6HQVRUV,PSRUWDQWIRU0RUHWKDQ-XVW3KRWRJUDSK\ ............ 5HÀH[/LJKWIRU*HRFDFKLQJ ........................................................ 7.15 For Professional Photographers: A Digital Light Meter..................... +RPHµ5DGDU6WDWLRQ¶¶'LVWDQFH0HDVXUHPHQW8VLQJ8OWUDVRXQG......

99 100 105 107 112 113 115 117

Timers, Clocks and Interrupts ........................................................ 8.1 Morning and Night Fun: Grand Prix Toothbrush Timer ....................  3UDFWLFDODQG$FFXUDWH'LJLWDO&ORFNZLWK/(''LVSOD\ ....................  :KR¶V)DVWHU"$5HDFWLRQ7LPHU ..................................................  µ7LPHULQR¶8QLYHUVDO7LPHUZLWKD6HJPHQW'LVSOD\ ....................  3OXJLQ7LPHUWR0DNH/LIH(DVLHU ................................................ 8.6 Atomic Precision: The DCF77 Radio Clock .....................................  2XWSXWRI7LPHDQG'DWHWRWKH6HULDO,QWHUIDFH ............................ 8.8 Stand-Alone DCF77 Clock with LCD Display ..................................

123 123 130 135 138 141 142 145 146

Interfaces ....................................................................................  8QLYHUVDODQG6LPSOH7KH,ð&,QWHUIDFH ......................................  :KHQ:H5XQ2XWRI3LQV3RUW([SDQVLRQ ....................................  +H[DGHFLPDO'HEXJJHU'LVSOD\8VLQJD'LJLW6HJPHQW'LVSOD\ .  /&'&RQWUROYLD,ð&8VLQJWKH3&) ........................................ 9.6 This Time, Fully Digital: The LM75 Thermometer ........................... 9.7 Power-Saving: Real-Time Clock with Date Display .........................

149 149 152 155 158 160 163

 +RWRU&ROG"7HPSHUDWXUH0HDVXUHPHQW8VLQJWKH$'......

      8      9    

98

6

Boek Arduino UK 131010.indd 6

10-12-13 11:33


7$%/(2)&217(176 

 $IWHU,QFOXGLQJ7KH,5UHPRWH:LUHOHVV3UDFWLFDO4XLFN7KH,5,QWHUIDFH  µ/DPSLQR¶$Q,5&RQWUROOHG5*%/DPS ........................................ 7LPHO\/X[XU\$Q,5&RQWUROOHG'LJLWDO&ORFN ...............................  2SWLPDOIRU0LFURFRQWUROOHUV7KH36,QWHUIDFH ........................... .H\ERDUGDQG0RXVHDV8QLYHUVDO,QSXW'HYLFHV ........................... 9.13 A Complete Microcomputer with LCD Monitor and Keyboard ............

167 171 174 178 178 181

10 Sounds and Synthesizer ................................................................. 10.1 Simple Tones............................................................................  7UDQVGXFHUVDQG$PSOL¿HUV.........................................................   )DVW3:00DNHV,W+DSSHQ1RW-XVWWRQHV%XW6RXQG:DYHV ........  7KHUHPLQ7KH&RQWDFWOHVV0XVLFDO,QVWUXPHQW ............................. 10.5 Audio Processing.......................................................................   9&2$7XQDEOH6LQH:DYH6RXUFH .................................... 10.5.2 Digital Signal Processing .................................................. 10.6 Sound Cloud: A Digital Synthesizer ..............................................

185 185 187 188 195 197 198 200 203

11 Digital Control Techniques ............................................................. 11.1 Control Types ........................................................................... 11.1.1 P Controller....................................................................   ,&RQWUROOHU ....................................................................   3,&RQWUROOHU .................................................................. 11.1.4 PD Controller .................................................................   3,'&RQWUROOHU ................................................................  2SWLPXP:RUNVWDWLRQ/LJKWLQJ'LJLWDO,OOXPLQDWLRQ&RQWURO ............. 11.3 A Classic of Control Theory: The Gravity Compensator ...................

207 208 208 209 209 209 210 210 214

12   

Physical Computing ....................................................................... 6HUYRV&RQWUROWKH:RUOG ........................................................... µ3KRWLQR¶'&DPHUD6ZLYHO....................................................... µ&UDQLQR¶0RXVH&RQWUROOHG&UDQH ..............................................

221 221 225

13 Processing .................................................................................... 13.1 Arduino and Processing: A Formidable Team .................................  ,QWHUDFWLRQZLWK3URFHVVLQJ'DWD/RJJLQJ7UHQG*UDSKVHWF ........

231 232 233

14 The ‘Living Room Box’: Our Modular Concluding Project ................ 14.1 Always Useful: A Clock ...............................................................  &RQWUROIURP$IDU7KH,5,QWHUIDFH ..............................................  9&RQWUROIRU+L)LV\VWHPV7HOHYLVLRQV/DPSVHWF.................  7LPHUVDQG6HQVRUVDVWKH%DVLVIRU+RPH$XWRPDWLRQ ...................  ,QGRRUDQG2XWGRRU7KHUPRPHWHUV ..............................................  1R0RUH'U\$LU$+\JURPHWHU ...................................................  7KH+DUGZDUH............................................................................

241 242 243 243 243 244 244 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\SLFDOEHJLQQHUSUREOHPVVXFKDVEDGSRZHUVXSSOLHVSUREOHPVZLWKVHWWLQJWKHFRQÂżJXUDWLRQSDUDPHWHUV IXVHELWV DQGFU\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 proJUDPPLQJLQWHUIDFHZKLFKLVSURYLGHGDVIUHHRIFKDUJHRSHQVRXUFHVRIWZDUH,QDGGLtion, installation is quick and easy, so the environment is usable immediately. Simple introductory 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 numEHURIZKLFKJURZVGDLO\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. Because 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ÂśV ZKHUH WKLV ERRN FRPHV LQ 3URMHFWV DUH LQWURGXFHG V\VWHPDWLFDOO\ HDFK LQWURducing 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 running 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 ‘labRUDWRU\SURWRW\SHVÂś%\PHDQVRIDSSURSULDWHKLQWVDQGQRWHVRWKHUSUDFWLFDOGHYLFHV will arise, which may be used for home, hobby and work. The projects are always implePHQWHGXVLQJHDV\WRÂżQGDQGLQH[SHQVLYHFRPSRQHQWV ,Q WKH ÂżQDO FKDSWHU DQ $UGXLQREDVHG Âľ/LYLQJ 5RRP %R[Âś LV SUHVHQWHG ,W LV GHVLJQHG 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 INTRODUCTION ,1752'8&7,21

1.1 The Arduino Project The ArduinoFRQFHSWFDPHWREHLQDWWKH,QVWLWXWHIRU,QWHUDFWLYH'HVLJQLQ,YUHD ,WDO\ 7KH VHDUFK IRU D ORZFRVW PLFURFRQWUROOHU 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$UGXLQRKDUGZDUHFRQVLVWHGRIDNLWWKDWFRXOGEHVROGHUHGWRgether easily, and it sold out quickly. Newer versions followed in rapid succession. Designers 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-tolearn 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[SHULHQFHLQWKHHOHFWURQLFV¿HOG7KHW\SLFDOKLJKVFKRROOHVVRQVRQFLUFXLWV2KP¶VODZHWFDUHIXOO\VXI¿FLHQW ,Q&KDSWHUWKHEDVLFIXQFWLRQVDQGFKDUDFWHULVWLFVRIWKHPDMRUFRPSRQHQWVDUHH[SODLQHG ,Q GXH FRXUVH PRUH HODERUDWH SURMHFWV DUH GLVFXVVHGVR WKDW HYHQ VWXGHQWV 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, beJLQQHUVZLOO¿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 inGLYLGXDO DSSUR[Ćť 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 temperature 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 easLO\ LQWURGXFHG )LUVWO\ WKH LQWULQVLF OLQH LPSHGDQFH PD\ OHDG WR WKH ÂżUVW HUURUV 7KHQ 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Ă&#x20AC;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 shortDJH'XHWRWKHWKUHHFRQÂżJXUDEOHDGGUHVVSLQVXSWR 3 RIWKHVHVHQVRUVPD\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(â&#x20AC;&#x153;X=â&#x20AC;?); Serial.print(mouseX, DEC); Serial.print(â&#x20AC;&#x153;\tY=â&#x20AC;?); Serial.print(mouseY, DEC); switch (click) { case  Serial.print(â&#x20AC;&#x153;\t left clickâ&#x20AC;?); break; case 10: Serial.print(â&#x20AC;&#x153;\t right clickâ&#x20AC;?); break; case 11: Serial.print(â&#x20AC;&#x153;\t both clickâ&#x20AC;?); break; } Serial.println(); delay(20); } void mouseInit() { Mouse.write(0xff); Mouse.read(); Mouse.read(); Mouse.read(); Mouse.write(0xf0); Mouse.read(); delayMicroseconds(100); }

// reset // conÂżrm byte  times // remote mode // conÂżrm

,QRXUODWHUFKDSWHURQSK\VLFDOFRPSXWLQJSURMHFWVDUHSUHVHQWHGLQZKLFKYDULRXVGHvices 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 comSDUDEOHWRWKHÂż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


6281'6$1'6<17+(6,=(5

7KLV DPSOLÂżHU ZLOO ÂżW FRPIRUWDEO\ RQ D VPDOO EUHDGERDUG )LJXUH  VKRZV D FRUUH VSRQGLQJ FRQVWUXFWLRQ H[DPSOH ,I WKH DPSOLÂżHU LV WR EH GHSOR\HG PRUH SHUPDQHQWO\ stripboard may be used.

Figure 10.3: Construction Example for the Audio AmpliÂżer

Figure 10.4: A Suitable DIY Speaker Enclosure

10.3 Fast PWM Makes It Happen: Not Just Tones, But Sound Waves 7KHVLPSOHVWZD\WRFUHDWHUHDOVRXQGVLVE\XVLQJIDVWSXOVHZLGWKPRGXODWLRQ 3:0  +HUH WKH REYLRXV TXHVWLRQ PD\ EH ZKDW WKH GLIIHUHQFH LV EHWZHHQ WRQHV DQG VRXQG waves. A tone is able to be described fully by a single physical parameter: its frequency. 7KXVDQÂľ$ÂśRQDOOPXVLFLQVWUXPHQWVLVDWDIUHTXHQF\RI+]:K\WKHQGRHVDQÂľ$Âś RQDWURPERQHVRXQGVRGLIIHUHQWWRDQÂľ$ÂśRQDSLDQR"7KHDQVZHULVEHFDXVHDVRXQG LVGHÂżQHGE\PDQ\PRUHFKDUDFWHULVWLFVWKDQVLPSO\LWVIUHTXHQF\&KDUDFWHULVWLFVVXFK DVWLPEUHGHÂżQHGE\WKHPL[RIKDUPRQLFRYHUWRQHVDVZHOODVWKHWHPSRUDOYDULDWLRQ LQDPSOLWXGHRUVRFDOOHGHQYHORSHDUHVLJQLÂżFDQW The tone()IXQFWLRQDOORZVQRLQĂ&#x20AC;XHQFHRQWKHVHYDULDEOHV,WFUHDWHVDVLPSOHÂż[HGIUH

188

Boek Arduino UK 131010.indd 188

10-12-13 11:33


6281'&/28'$',*,7$/6<17+(6,=(5

The sounds generated by this synthesizer are based on a broadband noise spectrum, which is repeated in rapid succession. Analog synthesizers produce similar sounds using UHVRQDQWEDQGSDVVÂżOWHUV1RLVHVDUHGHULYHGIURPWZRFRQWLQXRXVVLJQDOVHDFKSURYLG ed with its own additional decay rate. Five potentiometers enable the adjustment of the following parameters: Analog Analog Analog Analog Analog

A0: A1: A2: A3: A4:

Noise spectrum 1 Decay rate 1 Noise spectrum 2 Decay rate 2 Noise spectrum repeat frequency

:LWK WKHVH ÂżYH DGMXVWPHQW SRWV DQ DPD]LQJ UDQJH RI VRXQGV FDQ EH SURGXFHG 2I FRXUVHWKHV\QWKHVL]HULVH[WUHPHO\H[WHQVLEOH%\FKDQJLQJWKHEDVLFSDUDPHWHUVLWÂśV SRVVLEOHWRFRPSOHWHO\DOWHUWKHVRXQGFKDUDFWHULVWLFV,QDGGLWLRQHIIHFWVVXFKDVWKH reverb discussed above, as well as vibrato or various envelopes, may be created. %\XVLQJOLQHDUSRWHQWLRPHWHUVLQVWHDGRIURWDU\NQREVWKHV\QWKHVL]HUFDQEHÂľSOD\HGÂś TXLWHZHOODVOLQHDUSRWVDUHHDVLO\RSHUDWHGXVLQJMXVWRQHÂżQJHUDQGDOOÂżYHSRWVDUH easily adjusted simultaneously. A construction example is shown in Figure 10.11.

Figure 10.11: Synthesizer Construction Example Using Audacity, the raw sounds produced by the Arduino may be further processed, creating very interesting sound sequences for use as background ambience in videos. 7KHDSSURSULDWHEDFNLQJWUDFNZLOOFHUWDLQO\DGGDSURIHVVLRQDOIHHOWR\RXUQH[W<RX Tube video!

205

Boek Arduino UK 131010.indd 205

10-12-13 11:33


',*,7$/&21752/7(&+1,48(6

Figure 11.3: Brightness Control Circuit &ORVLQJWKHFRQWUROORRSLVVLPSO\DPDWWHURIDOLJQLQJWKHSRZHU/('VRWKDWLWLOOXPLnates the photodiode. 7KHFLUFXLWLVGHVLJQHGVRWKDWWKH/('EULJKWQHVVLQFUHDVHVZLWKLQFUHDVHGFXUUHQWZKLOH the voltage at the analog input increases with increased light intensity. Thus, the control system is directly proportionally polarized. By calculating the error signal as

212

Boek Arduino UK 131010.indd 212

10-12-13 11:33


,1'(;

Index 2-D camera swivel ...........................225 7-segment displays........................... 62 A $PSOL多HU ........................................185 Analog channels ..............................233 Arduino ............................................. 9 Arithmetic operators ......................... 31 Arrays ............................................. 36 ATMega168...................................... 16 ATMega328...................................... 16 Audio processing .............................197 B %DQGSDVV多OWHU ................................200 Bar graph display ............................. 59 Bar graph voltmeter .......................... 81 Bar graph .......................................233 Batteries ........................................107 Blink ............................................... 24 Breadboards .................................... 43 C Calibration ....................................... 86 Circular memory buffer ....................200 Constants ........................................ 35 Control loop ....................................207 Control...........................................215 Cranino ..........................................228 Crystal ...........................................123 D Data loggers ...................................237 DCF77 module ................................145 DCF77 radio module ........................123 Digital clock ....................................130 Diodes ............................................ 49 Displays .......................................... 59 Dot matrix displays ........................... 67 E (OHFWULFDODSSOLDQFHV .........................141 (OHFWURO\WH ....................................... 94 (OHFWURO\WLFFDSDFLWRUV ....................... 89 (OHFWURPDJQHW.................................228 ([WHUQDOYROWDJH ............................... 15 F Fuse bits ........................................... 9

G Geocaching .....................................113 Gravitation compensator ..................214 H +H[DGHFLPDOGHEXJJHU .....................155 +XPLGLW\VHQVRUV .............................105 I ,&EXV ..........................................149 ,'(................................................. 17 ,OOXPLQDWLRQFRQWURO..........................210 ,QWHU,QWHJUDWHG&LUFXLW%XV ..............149 ,5UHFHLYHU .....................................171 ,5UHPRWHFRQWURO ............................174 L LC displays ...................................... 76 Levitation .......................................217 Libraries .......................................... 41 Light barriers ..................................112 Light meter.....................................115 Living room box ..............................241 Living room hygrometer ...................105 Logical operations............................. 32 M Magnet ..........................................216 Microcontroller ................................. 15 Minimal Arduino ............................... 45 Mood lights ...................................... 11 0XOWLFRORU/('V ................................ 48 N 1L0+EDWWHULHV ................................107 NTC ................................................ 95 O Optical sensors ...............................112 P PC keyboards ..................................178 PCF8583 ........................................164 PD controller ...................................218 Photodiodes ....................................112 Phototransistors ..............................112 Physical Computing..........................221 Potentiometers ................................. 47 POV ................................................ 54 3RZHU/(' ............................... 53, 212 259

Boek Arduino UK 131010.indd 259

10-12-13 11:33


Power relay ....................................141 Power supply ................................... 15 Printed circuit boards ........................ 43 Processing ......................................231 Program icons .................................231 Prototyping boards ........................... 44 PS/2 mice.......................................178 Pulse-width modulation ....................188 Pushbuttons..................................... 49 R Random numbers ............................. 40 RC5 code........................................167 Reaction timer ................................135 Relative humidity.............................105 Resistors ......................................... 46 Reverberation springs, plates ............200 Reverberation .................................200 5*%/(' ........................................171 RTC ...............................................163 Running light effects ......................... 51 S Sensors........................................... 81 Seven segment displays .................... 62 Shields ............................................ 13 Shift operators ................................. 33 Structures ....................................... 36 Switches ......................................... 49 Synthesizer ....................................185 T Temperature sensor .......................... 98 Theremin .......................................195 Thermometer ............................ 95, 100 Timer.............................................138 Toothbrush timer .............................123 Transistor parameters ....................... 93 Twilight switch ................................112 U Ubuntu............................................ 23 Ultrasonic transducers ......................117 Upload ............................................ 21 USB cable........................................ 20 V Variables30 Variable types .................................. 31 VCO...............................................198 Verify .............................................. 21

260

Boek Arduino UK 131010.indd 260

10-12-13 11:33


Preview arduino projects and circuits guide