Arduino Circuits and Projects Guide - Elektor
Arduino Circuits and Projects Guide - Elektor
1 Introduction .................................................................................. 9
1.1 The Arduino Project................................................................... 10
1.2 Book Structure and Target Audience ............................................ 10
4.4.7 Switches........................................................................ 49
4.4.8 Silicon Diodes................................................................. 49
4.4.9 Transistors ..................................................................... 49
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
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¶V ZKHUH 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 ¿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.
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$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.
7KLVERRNLVDLPHGDWWKRVHZKR¶YHDOUHDG\KDGVRPHEDVLFH[SHULHQFHLQWKHHOHFWURQ-
LFV¿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, be-
JLQQHUVZLOO¿QGLWKHOSIXOWRJRWKURXJKWKHERRN¶VFKDSWHUVLQRUGHUHYHQLIQRWHYHU\
single project is constructed.
10
Figure 4.1:
Breadboard
,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
+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
Figure 6.2:
Control of a Single 7-Segment
Display.
60
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 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À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-
DJH'XHWRWKHWKUHHFRQ¿JXUDEOHDGGUHVVSLQVXSWR3RIWKHVHVHQVRUVPD\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
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.
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
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\XVLQJIDVWSXOVHZLGWKPRGXODWLRQ3: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ÀXHQFHRQWKHVHYDULDEOHV,WFUHDWHVDVLPSOH¿[HGIUH
188
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.
: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
&ORVLQJWKHFRQWUROORRSLVVLPSO\DPDWWHURIDOLJQLQJWKHSRZHU/('VRWKDWLWLOOXPL-
nates 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
Index
G
2-D camera swivel ...........................225 Geocaching .....................................113
7-segment displays........................... 62 Gravitation compensator ..................214
A H
$PSOL¿HU ........................................185 +H[DGHFLPDOGHEXJJHU .....................155
Analog channels ..............................233 +XPLGLW\VHQVRUV .............................105
Arduino ............................................. 9
Arithmetic operators ......................... 31 I
Arrays ............................................. 36 ,&EXV ..........................................149
ATMega168...................................... 16 ,'(................................................. 17
ATMega328...................................... 16 ,OOXPLQDWLRQFRQWURO..........................210
Audio processing .............................197 ,QWHU,QWHJUDWHG&LUFXLW%XV ..............149
,5UHFHLYHU .....................................171
B ,5UHPRWHFRQWURO ............................174
%DQGSDVV¿OWHU ................................200
Bar graph display ............................. 59 L
Bar graph voltmeter .......................... 81 LC displays ...................................... 76
Bar graph .......................................233 Levitation .......................................217
Batteries ........................................107 Libraries .......................................... 41
Blink ............................................... 24 Light barriers ..................................112
Breadboards .................................... 43 Light meter.....................................115
Living room box ..............................241
C Living room hygrometer ...................105
Calibration ....................................... 86 Logical operations............................. 32
Circular memory buffer ....................200
Constants ........................................ 35 M
Control loop ....................................207 Magnet ..........................................216
Control...........................................215 Microcontroller ................................. 15
Cranino ..........................................228 Minimal Arduino ............................... 45
Crystal ...........................................123 Mood lights ...................................... 11
0XOWLFRORU/('V ................................ 48
D
Data loggers ...................................237 N
DCF77 module ................................145 1L0+EDWWHULHV ................................107
DCF77 radio module ........................123 NTC ................................................ 95
Digital clock ....................................130
Diodes ............................................ 49 O
Displays .......................................... 59 Optical sensors ...............................112
Dot matrix displays ........................... 67
P
E PC keyboards ..................................178
(OHFWULFDODSSOLDQFHV .........................141 PCF8583 ........................................164
(OHFWURO\WH ....................................... 94 PD controller ...................................218
(OHFWURO\WLFFDSDFLWRUV ....................... 89 Photodiodes ....................................112
(OHFWURPDJQHW.................................228 Phototransistors ..............................112
([WHUQDOYROWDJH ............................... 15 Physical Computing..........................221
Potentiometers ................................. 47
F POV ................................................ 54
Fuse bits ........................................... 9 3RZHU/(' ............................... 53, 212
259
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