+ All Categories
Home > Documents > Mastering Microcontrollers Helped by Arduino -

Mastering Microcontrollers Helped by Arduino -

Date post: 11-Sep-2021
Category:
Upload: others
View: 10 times
Download: 1 times
Share this document with a friend
23
copyright Elektor International Media
Transcript
Page 1: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

Page 2: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

Table of Contents

1. Quick Start Guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.1 Software Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.2 Installing the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91.2.1 Windows (XP or Later) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101.2.2 Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131.2.3 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.3 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.1 Someone Knocks at the Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2 Where Are We Going? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

3. Know Your Opponent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.1 A Short History of Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.2 They are Cute, but What's Inside? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223.2.1 The Processor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.2.2 The Oscillator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233.2.3 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243.2.4 Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253.2.5 Input/Output Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253.2.6 Analog-to-Digital Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.2.7 Digital-to-Analog Converter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.2.8 Communication Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273.2.9 Time Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283.2.10 Other Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

3.3 Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.3.1 Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303.3.2 Loading the Program into the MCU . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.3.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

ernarvuino_14x21_UK.book Page iii Vendredi, 20. septembre 2013 10:12 10

Page 3: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

iv

4. Rapid Prototyping Italian Style . . . . . . . . . . . . . . . . . . . . . . 35

4.1 The Godfather 1, 2 and 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

4.2 Pasta, Cheese and Tomato Sauce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

4.3 Base Ingredients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

4.4 The Kitchen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434.4.1 File Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454.4.2 Edit Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474.4.3 Sketch Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484.4.4 Tools Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494.4.5 Help Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 504.4.6 Manage the Tabs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

4.5 The Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524.5.1 Table Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 524.5.2 The Headwaiter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

5. My First Offense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

5.1 The Wrench . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

5.2 Get to Know the Hood . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

5.3 Preparing the Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

5.4 A Sizeable Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

5.5 The Snitch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

5.6 Flashing Lights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

5.7 Incarcerated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

5.8 Out on Parole . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

5.9 Reintegration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

6. Digital Signals: All or Nothing . . . . . . . . . . . . . . . . . . . . . . . 69

6.1 Surprises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

6.2 More Surprises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

6.3 The Matrix Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

6.4 Charlie to the Rescue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

ernarvuino_14x21_UK.book Page iv Vendredi, 20. septembre 2013 10:12 10

Page 4: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

v

6.5 Repeat Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

6.6 The Story of the Three Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 796.6.1 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 806.6.2 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 816.6.3 do-while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

6.7 More Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

6.8 Ghostbusters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

6.9 Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

6.10 LED Mini-Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

6.11 The Parade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

6.12 A Little Scam . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

6.13 Making New Friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

6.14 Null Does Not Equal Zero . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

6.15 Snow White’s Apple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

6.16 The Core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

6.17 A Trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

7. Analog Signals: Neither Black Nor White . . . . . . . . . . 113

7.1 The Digital Switchover . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1137.1.1 Type Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1167.1.2 The Bulk of the Budget is Spent on Representation Costs . . . . . . . . . 1177.1.3 A Tip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1187.1.4 ADC References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

7.2 Back to Analog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

7.3 Look Ma, No Hands! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1227.3.1 Motor Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1227.3.2 Obtaining a Step Response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1277.3.3 The Compound if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1317.3.4 The PID Controller . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1327.3.5 The Digital Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1357.3.6 Dynamic Duo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.3.7 Nerd Corner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407.3.8 Sneak Preview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

7.4 Recreation: the Misophone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

7.5 A Bit of C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147

ernarvuino_14x21_UK.book Page v Vendredi, 20. septembre 2013 10:12 10

Page 5: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

vi

7.6 The No in Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

7.7 Look Ma, No Arduino! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

8. Communication: an Art and a Science . . . . . . . . . . . . . 155

8.1 Visualize Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1578.1.1 Connect a Liquid Crystal Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

8.2 The Act of Communicating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1608.2.1 Asynchronous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1608.2.2 Synchronous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

8.3 RS-232 or Serial Port? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1628.3.1 A Few Subtleties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1648.3.2 Chaining Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1668.3.3 Breaking the Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1718.3.4 An NMEA 0183A Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1738.3.5 Mutatis Mutandis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1768.3.6 Make a U-turn Now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1788.3.7 A Curly Symbol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

8.4 Two-Wire Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1858.4.1 I²C, TWI and Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1868.4.2 Atmospheric Pressure Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

8.5 Three- and Four-Wire Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . 1958.5.1 Improved Driver for Graphic Display . . . . . . . . . . . . . . . . . . . . . . . . . 1968.5.2 Humidity Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

8.6 All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

8.7 When Arduino Isn’t Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

8.8 Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

8.9 Did you Know? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

9. Clock is Ticking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

9.1 This is Radio Frankfurt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2199.1.1 DCF77 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

9.2 Daisy-Chaining Seconds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

9.3 Decode a String of Bits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2279.3.1 DCF77 Decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

ernarvuino_14x21_UK.book Page vi Vendredi, 20. septembre 2013 10:12 10

Page 6: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

vii

9.4 Millis and Micros, Two Little Functions . . . . . . . . . . . . . . . . . . . . . . . . 232

9.5 PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2339.5.1 Two Types of PWM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

9.6 The Master of Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2349.6.1 DCF77 Transmitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

9.7 Could do Better . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244

9.8 Expecting a Happy Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2469.8.1 Sort Your Infrared Remote Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 248

9.9 Break or Continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

9.10 Divide and Conquer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

9.11 The Structured Union of Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2539.11.1 struct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2539.11.2 union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2549.11.3 typedef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254

9.12 Is It an Image? Is It data? It’s Superfile! . . . . . . . . . . . . . . . . . . . . . . . . 2559.12.1 The SVG File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

9.13 What They Really Say . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2609.13.1 The NEC-1 Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

9.14 To goto Or Not to goto . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

9.15 Frame It Yourself . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2689.15.1 Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2689.15.2 Exposure Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2729.15.3 Capturing Volatile Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

9.16 Occupation: Rioter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

9.17 Summarizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2809.17.1 Normal Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2809.17.2 CTC Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2809.17.3 Capture Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

9.18 May The Force Be With You . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

10. Interrupts - Pandora’s Box . . . . . . . . . . . . . . . . . . . . . . . . . . 283

10.1 My First Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28410.1.1 Timer/Counter 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28410.1.2 Generating a 1 kHz Signal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

10.2 The Devil in Disguise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28710.2.1 What’s Our Vector, Victor? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

ernarvuino_14x21_UK.book Page vii Vendredi, 20. septembre 2013 10:12 10

Page 7: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

viii

10.3 Message in a Bottle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292

10.4 Spinning Out Of Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

10.5 Knock on Any Door . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29610.5.1 Let’s Make a Flip-Flop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

10.6 One Interrupt Too Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29810.6.1 The Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

10.7 Who's That Knocking At My Door? . . . . . . . . . . . . . . . . . . . . . . . . . . . 30010.7.1 Multiplexed Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

10.8 Long Live the Rotary Encoder! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

10.9 Reset In Every Possible Way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30910.9.1 POR, BOR and BOD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

10.10 Let’s Switch Roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31110.10.1 The Annoiser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311

10.11 La Cucaracha . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31410.11.1 The 1-Wire Protocol . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

10.12 Fire! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32210.12.1 The SMBus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

Programs Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Illustrations Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

Tables Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

ernarvuino_14x21_UK.book Page viii Vendredi, 20. septembre 2013 10:12 10

Page 8: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

35

4. Rapid Prototyping Italian Style

Arduino, from Italy, is a rapid prototyping platform for microcontroller applica-tions. This means that it is a set of tools developed to facilitate the design of micro-controller-based circuits without wasting too much time on learning the ins andouts of the platform. With Arduino, you too can do “microcontrollers” and“embedded electronics”. Now I’m gonna make you an offer you can’t refuse: getyourself one of those cheap Arduino boards, install the free software tools, keepyour evenings free and I will teach you how to use it all.

4.1 The Godfather 1, 2 and 3

Arduino is related to the open-source techno-popularizing projects Processingfrom which it borrowed the programming environment and Wiring that providedthe foundations of the code libraries.

Processing “is an open source programming language and environment for peoplewho want to create images, animations, and interactions. Initially developed toserve as a software sketchbook and to teach fundamentals of computer program-ming within a visual context, Processing also has evolved into a tool for generat-ing finished professional work.” (source: www.processing.org)

Wiring, Italian like Arduino, is a programming framework for microcontrollers“created with designers and artists in mind to encourage a community wherebeginners through experts from around the world share ideas, knowledge andtheir collective experience.” (source: www.wiring.org.co)

Arduino (www.arduino.cc) was conceived at the same school as Wiring and is asimplification of the latter, which in itself was already quite easy to use. OriginallyWiring was not open-source, but under the pressure from the Arduino team, Wir-ing gave in and published its source code. Then the Arduino team could get going.

The goal of Arduino is to make microcontrollers accessible to students, non-spe-cialists, artists, designers, enthusiasts and all those who are interested in the cre-ation of interactive objects and environments, but who do not have (well-developed) electronics and/or programming skills and who do not always have alot of money. Therefore the Arduino team decided that their board should not costmore than $30 (€25), which was indeed the price of the first board. Since thenprices have gone up marginally to reach about $35 (€30) in 2013.

ernarvuino_14x21_UK.book Page 35 Vendredi, 20. septembre 2013 10:12 10

Page 9: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

36

The first Arduino boards were more basic than the Wiring boards, but over timethe Arduino hardware became more elaborated, whereas Wiring extended its rangeby simplifying their boards. Now (in 2013), Wiring S and Arduino Uno boards arevery similar.

Despite the equivalence of the two projects, Wiring did not meet with the samesuccess as Arduino. From the early beginning the Arduino team has done every-thing to spread their word by publishing all source code, circuit diagrams, printedcircuit board designs and detailed documentation for free, whereas the Wiringteam tried to control everything. In the Fall of 2012 over 600,000 Arduino boardshad been sold worldwide.

Arduino libraries are based on those from Wiring and the Integrated DevelopmentEnvironment (IDE), based on the one from Processing, is almost identical to theWiring IDE. The only striking difference is its color: orange for Wiring and lightblue for Arduino. Processing uses shades of grey for its IDE with looks very famil-iar to the two others. Other projects have started to use the Processing IDE as welland we can almost speak of an emerging standard.

The Wiring IDE supports Arduino hardware, but the Arduino IDE does not knowabout Wiring hardware. Initially Arduino limited itself to a few microcontrollersfrom Atmel’s 8-bit AVR family while Wiring openly flirted with microcontrollersfrom for instance Microchip and Texas Instruments and also with MCUs based onan ARM core. Arduino too has been transformed into a multi-MCU platform, buta little “unbeknownst to its own free will”. The Arduino philosophy has beenembraced by many enthusiasts who have ported it to different MCU platformsfrom various manufacturers. It took a few years to modify the Arduino IDE toallow it to include compilers for other microcontrollers as well, but with version1.5 and the introduction of the Arduino Due that sports an ARM Cortex-M3 basedMCU, this goal has been reached.

4.2 Pasta, Cheese and Tomato Sauce

The Arduino platform is built on three pillars:

1. The hardware consisting of a collection of microcontroller and expansionboards. Circuit diagrams and printed circuit board designs are available forfree;

2. Software comprising of programming tools and an extensive library of high-level functions. Everything is free, open-source and multi-platform;

ernarvuino_14x21_UK.book Page 36 Vendredi, 20. septembre 2013 10:12 10

Page 10: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.2 Pasta, Cheese and Tomato Sauce

37

3. Distribution and communication in the shape of a website through which theArduino hardware and software are made available to interested parties. Theaddress is www.arduino.cc (arduino.cc also works). For the curious, .cc isthe internet country code top-level domain reserved for the National Territoryof Cocos (formerly Keeling) Islands, an Australian territory. This top-leveldomain was probably chosen for its low cost, but I digress.

The website is the official reference for everything that concerns Arduino. Thewebsite contains software and hardware updates and all the information necessaryto use it all. If you are looking for a reseller of authentic Arduino hardware, it ishere that you can find one. A forum to ask questions that are not answered in thisbook is also available. The site is in English, but there are sub-forums accommo-dating other languages too.

Leonardo Lilypad

Mega ADK Chinese Uno

Figure 4-1 - Some less common Arduino boards: the Leonardo (without connectors) is equipped with an ATmega32U4 and targets USB devices; the daisy-shaped Lilypad aimed at wearable applications; the Mega ADK for communication with Android devices and a Chinese Uno clone recognizable from the missing “Made in Italy” statement.

ernarvuino_14x21_UK.book Page 37 Vendredi, 20. septembre 2013 10:12 10

Page 11: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

38

On the hardware side of things, Arduino is a microcontroller board with a USBport through which it can be programmed. Several models exist, but the most com-mon are the small Uno-shaped boards (Diecimila, Duemilanove, etc.) and thelarger Mega 2560-style boards (Mega, Mega 1280, Mega ADK, etc.). There areothers like the Mini, the Micro, the Nano or the round, daisy-shaped Lilypadintended for wearable applications, supposed to be used in clothing. The ArduinoDue is different from all others because it is equipped with a 32-bit microcontrollerinstead of an 8-bit one and it is therefore much more powerful than the others1. TheYún, a combination of an Arduino Leonardo extended with a Wi-Fi system-on-a-chip running Linino (a MIPS GNU/Linux based on OpenWRT) has just beenreleased.

1. The Arduino Due is not treated in this book. Not only did it arrive when I had almost finished this book, but it also makes things more complicated. A bit of Arduino’s simplicity has been traded in for a more powerful microcontroller.

Figure 4-2 - A collection of Arduino compatible add-on boards, or Shields. Shown are a Wi-Fi/Bluetooth shield, an Ethernet shield (white), a graphical display shield with a mobile phone display on it, a shield with a 4 x 4 matrix keypad, a monome 10h with 16 large white LEDs (see www.monome.org) and left from the center an experimental shield that we will build in Chapter 8.

enarvuino_14x21_UK_04.fm Page 38 Mercredi, 25. septembre 2013 8:27 08

Page 12: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.3 Base Ingredients

39

The Arduino board can be programmed in C, C++ or assembler using open-sourcetools available for Windows, Mac OS X and Linux. The hardware is also open andanyone can make their own Arduino board. Schematics and printed circuit board(PCB) designs can be downloaded for free and instructions on how to use it all arepublished on the website. If you buy a cheap Arduino Uno from an unofficialdealer, it is possible or maybe even likely, that you will receive a clone producedsomewhere in China. A genuine Arduino board has “Made in Italy” printed on it,clones do not. Even though all the information to make an Arduino board is avail-able for free on the internet, you are not authorized to make thousands of them.Marketing a compatible board is of course allowed as long as it is not identical.

The Arduino board is equipped with expansion connectors that can accommodatecompatible add-on boards. Such an extension board is called a Shield and there arehundreds of them out there, designed by Arduino users all over the world for allkinds of applications. Several official Arduino-branded shields are also available,such as the Ethernet Shield, the Wireless SD Shield, the Motor Shield or the per-forated Proto Shield for prototyping.

4.3 Base Ingredients

An Arduino board is fairly simple – until the appearance of the Due – since it isbasically an 8-bit microcontroller with expansion slots to which the in- and outputsof the microcontroller are connected. Such a board is also known as a break-outboard and they exist for many components, especially those that are too small touse on a prototyping board or a breadboard. ATmegaX MCUs (where X is a num-ber) belong to the megaAVR family from Atmel. The first Arduino boards wereequipped with an ATmega8, the pre-Uno boards sported an ATmega168 whereasthe Uno has an ATmega328. The Mega 2560 is based on an ATmega2560, and theMega 1280 on an ATmega1280. All these MCUs are more or less compatible; theonly things that differ are the pin count, the memory sizes and the on-chip periph-erals. The Arduino Due is different from all other boards because it is equippedwith an Atmel SAM3X8E 32-bit microcontroller based on an ARM Cortex-M3core.

The microcontroller on an Arduino board contains a small boot loader, whichallows loading a new application into the MCU through a serial port without spe-cial tools and without overwriting the boot loader. With this little piece of soft-ware, reprogramming the board is easy. Unfortunately, modern computers nolonger have serial ports, only USB ports (besides of course other unusable ports

ernarvuino_14x21_UK.book Page 39 Vendredi, 20. septembre 2013 10:12 10

Page 13: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

40

like HDMI, VGA, etc.), and an adapter that converts a USB port into a serial portis required. To avoid having to buy a converter on top of the board, the Arduinodesigners decided to include one on the board.

During development the USB port of the PC is also used to power the board, butwhen the application is finished and the board has to survive on its own, it must bepowered in another way. An on-board voltage regulator offers some freedom in thechoice of the external power source.

Besides a pushbutton to restart the MCU (reset) and some configuration jumpers,the board does not contain much else. An LED is often present – it is used in sev-eral Arduino programming examples – and it can be helpful to determine if theArduino environment (board + programming software) is functioning as it should.

The MCU’s clock is based on a 16 MHz crystal oscillator. This is an importantdetail, because the boot loader relies on this value to adjust the baud rate of theserial port. If you overclock the board (the MCU is specified up to 20 MHz) theboot loader will have communication problems.

Figure 4-3 - Drawing of the Arduino Uno board showing the most important components. The board was designed for educational use, which is why it has this atypical outline that allows to explain to students how to position the board without requiring any knowledge about what exactly is on the board.

ernarvuino_14x21_UK.book Page 40 Vendredi, 20. septembre 2013 10:12 10

Page 14: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.3 Base Ingredients

41

Figure 4-4 - The Arduino Mega (2560) is longer than the Uno because of the extra connectors needed to access the additional I/O that the ATmega2560 offers compared to the ATmega328 of the Uno. The Mega is compatible with the expansion boards for the Uno.

ernarvuino_14x21_UK.book Page 41 Vendredi, 20. septembre 2013 10:12 10

Page 15: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

42

That's all there is to know in case you would like to make your own Arduino com-patible board. Basically, all you have to do is to program the boot loader into theATmega device of your choice, clocked at 16 MHz. If you have a USB-to-serial-port converter or a computer with a real serial port, the USB portion of the boardcan be omitted. An RS-232 to TTL level adapter is essential in the last case (andmaybe also in the first case, depending on the adapter).

Building an Arduino compatible board seems easy, but there are some complica-tions. The most important one is the fact that an AVR programmer is required toload the boot loader into the microcontroller. Such a tool is inexpensive, but itmeans an extra piece of hardware. Another obstacle may very well be the bootloader that is not necessarily available as an executable for the AVR of yourchoice. Therefore use the ATmega328 (or ATmega168) or prepare yourself toadapt and recompile the boot loader. This is not an impossible task, but being anovice, you are probably not yet up-to-speed to do this successfully.

Finally, the MCU has some configuration fuses that must be programmed beforeyou can use it. For this you also need an AVR programmer. The fuse values dependon the AVR type but, fortunately, the Arduino IDE contains all the necessary data.The IDE is capable of programming the boot loader into the microcontroller andconfigure its fuses for you, but your AVR programmer must be supported by theIDE.

Figure 4-5 - A homemade Arduino compatible board, component side (left) and solder side (right).

ernarvuino_14x21_UK.book Page 42 Vendredi, 20. septembre 2013 10:12 10

Page 16: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.4 The Kitchen

43

When you have completed your Arduino compatible board, you can give it a name.You are not allowed to call it “Arduino Something” since only boards approved bythe Arduino team are entitled to the appellation. A solution is provided by theFreeduino community (www.freeduino.org) that allows the use of the Freeduinoname for homemade Arduino boards. On their website there even is a tool to gen-erate names that rhyme with Arduino.

4.4 The Kitchen

The details on how to install the Arduino IDE can be found in Chapter 1.

The Arduino IDE is rather simple. It is written in Java and includes:✦ A rudimentary text editor to write the program;✦ Some shortcuts to configure the IDE and to quickly find examples and help;✦ Functions to compile the program and load it into the MCU;✦ A simple terminal to communicate with the board over the serial port.

The six most frequently used functions have buttons below the main menu. Fromleft to right we have Verify, Upload, New, Open, Save and Serial Monitor.

Figure 4-6 - This is all you need for an Arduino compatible board.

ernarvuino_14x21_UK.book Page 43 Vendredi, 20. septembre 2013 10:12 10

Page 17: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

44

A program is called a Sketch and it is written in C or C++ (or assembler ifnecessary).

With version 1.0.1 the IDE was extended with some major new features includingmenu translation into some thirty languages. By default, the IDE is in English, butit can be translated to, for instance, French, Danish or Lithuanian.

The introduction of version 1.5 of the IDE did not add many visible changes. Thereal novelty of this version is the addition of support for the Arduino Due boardwhich, because it is based on a 32-bit microcontroller, needs a special program-ming tool chain.

Figure 4-7 - The Arduino IDE and its three main areas.

ernarvuino_14x21_UK.book Page 44 Vendredi, 20. septembre 2013 10:12 10

Page 18: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.4 The Kitchen

45

4.4.1 File Menu

New (Ctrl + N) – open a new window for a new sketch.

Open… (Ctrl + O) – open an existing sketch.

Sketchbook – open the Arduino IDE’s working folder, the default location for stor-ing sketches. Consult Preferences to find out where this folder is.

Examples – open the Arduino IDE’s example folder.

Close (Ctrl + W) – close the active sketch.

Save (Ctrl + S) – save the active sketch.

Save as... (Ctrl + Shift + S) – saves the active sketch under a new name.

Upload (Ctrl + U) – compile and load the active sketch into the microcontrollerusing the serial port previously selected in the menu Tools -> Serial Port.

Upload Using Programmer (Ctrl + Shift + U) – compile and load the sketchinto the microcontroller using the programmer previously selected in the menuTools -> Programmer.

Page Setup (Ctrl + Shift + P) – prepare the active file for printing.

Print (Ctrl + P) – print the active file.

Figure 4-8 - The File menu.

ernarvuino_14x21_UK.book Page 45 Vendredi, 20. septembre 2013 10:12 10

Page 19: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

46

Preferences (Ctrl + comma) – display the IDE’s preferences window. The windowthat opens shows only the most useful parameters, many others are available in thepreferences.txt file whose location is indicated at the bottom of the window andthat can be edited outside the IDE. Unfortunately, the documentation of theseoptions leaves much to be desired.

✦ Sketchbook location specifies the default location where your sketches aresaved. These are the sketches that are accessible when you click onSketchbook in the File menu. Editor language lets you choose the language ofthe IDE. Editor font size sets the font used to write the sketch. By checkingShow verbose output during the display of messages issued by the compilerand/or programmer is activated. The option Verify code after upload verifiesthat the program has been loaded correctly into the microcontroller, but itslows down the programming process. I recommend activating it only whenyou suspect a communication problem between the computer and the Arduinoboard.

✦ The option Use external Editor is also interesting. If you check this option, theIDE’s editor is disabled. The IDE will compile the latest version of the sketchthat you may have modified (and saved!) in an editor external to the IDE. Itallows you to write the program in the editor of your choice, which isprobably more powerful than the one that comes with the Arduino IDE. Adisadvantage is that the sketch must be opened in two tools (the IDE and theexternal editor) and you have to go back and forth between the two to edit andcompile the sketch.

✦ Check for updates at start-up: the Arduino IDE will contact the Arduinowebsite to see if a new version of the IDE is available;

✦ Update sketch files to new extension on save: IDE versions before 1.0 used thefile extension .PDE for sketches. With the introduction of version 1.0 of theIDE the sketch file extension was changed to .INO. Check this option toautomatically change the extension of old sketches to .INO (with the resultthat older versions of the IDE will no longer recognize the renamed sketches).

✦ Automatically associate .ino files with Arduino: this option allows you tolaunch the IDE by clicking on a sketch with the extension .INO.

Quit (Ctrl + Q) – close all open windows of the IDE. Clicking on the small whitecross in the red square in the upper right of a window only closes that one.

ernarvuino_14x21_UK.book Page 46 Vendredi, 20. septembre 2013 10:12 10

Page 20: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.4 The Kitchen

47

4.4.2 Edit Menu

Undo (Ctrl + Z) – cancel the last editor action.

Redo (Ctrl + Y) – redo the last editor action.

Cut (Ctrl + X) – cut the selected text.

Copy (Ctrl + C) – copy the selected text.

Copy for Forum (Ctrl + Shift + C) – copy the selected text including its colors andlayout for posting it on the Arduino forum.

Copy as HTML (Ctrl + Alt + C) – copy the selected text including its colors andlayout for inclusion in an HTML page.

Paste (Ctrl + V) – paste the selection that was previously cut or copied.

Select All (Ctrl + A) – select all the text in the editor.

Comment/Uncomment (Ctrl + /) – add “//” to the beginning of each line of theselected text to comment it out or remove “//” from the beginning of each line ofthe selection to uncomment it. Note that the shortcut Ctrl + / is for a QWERTYkeyboard and may not work on a keyboard with another layout. Tip for Windows:to change your keyboard in a QWERTY keyboard and back, press Shift + Alt.Now you can find the key that is at the position of ‘/’.

Figure 4-9 - The Edit menu.

ernarvuino_14x21_UK.book Page 47 Vendredi, 20. septembre 2013 10:12 10

Page 21: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4. Rapid Prototyping Italian Style

48

Increase Indent (Ctrl + ]) – shift the selection to the right. Note that the shortcutCtrl + ] is for a QWERTY keyboard and may not work on a keyboard with anotherlayout. Tip for Windows: to change your keyboard in a QWERTY keyboard andback, press Shift + Alt. Now you can find the key that is at the position of ‘]’.

Decrease Indent (Ctrl + [) – shift the selection to the left. Note that the shortcut Ctrl+ [ is for a QWERTY keyboard and may not work on a keyboard with another lay-out. Tip for Windows: to change your keyboard in a QWERTY keyboard and back,press Shift + Alt. Now you can find the key that is at the position of ‘[’.

Find… (Ctrl + F) – find the next occurrence of a string. You can also replace thestring with another.

Find Next (Ctrl + G) – find the next occurrence of the string selected by Find orUse Selection For Find.

Find Previous (Ctrl + Shift + G, since 1.0.1) – find the previous occurrence of thestring selected by Find or Use Selection For Find.

Use Selection For Find (Ctrl + E, since 1.0.1) – select a string to use with the searchfunctions Find, Find Next and Find Previous This function has no visible effect.Select some text, for example the name of a variable, press Ctrl + E and then startthe search with Ctrl + F, Ctrl + G or Ctrl + Shift + G.

4.4.3 Sketch Menu

Verify / Compile (Ctrl + R) – compile the sketch without loading it into the micro-controller.

Import library... (version 1.5 or later) – add a line #include <xxx.h> to the top ofthe sketches main file, the one that bears the name of the sketch. “xxx” is the nameof the library to import. This option provides a sub-option Add Library... forinstalling libraries prepared by other users.

Show Sketch Folder (Ctrl + K) – open the folder of the active sketch in a separatewindow.

Figure 4-10 - The Sketch menu.

ernarvuino_14x21_UK.book Page 48 Vendredi, 20. septembre 2013 10:12 10

Page 22: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

4.4 The Kitchen

49

Add File... – add a file to the sketch. Files that can be compiled (.ino, .pde, .h, etc.)are copied into the folder of the sketch and they are opened in new tabs in the IDE.For other files a Data subfolder is created (if it didn’t exist yet) in the folder of thesketch and the file is copied to this subfolder. Such files will not be compiled.

Import Library... (version 1.0.x) – add a line #include <xxx.h> to the top of thesketches main file, the one that bears the name of the sketch. “xxx” is the name ofthe library to import.

4.4.4 Tools Menu

This menu may take some time to open, especially with Windows XP. So bepatient when you click on it.

Figure 4-11 - The Arduino IDE does not feature debugging. For example, setting breakpoints to stop the sketch is not possible.

Figure 4-12 - The Tools menu.

ernarvuino_14x21_UK.book Page 49 Vendredi, 20. septembre 2013 10:12 10

Page 23: Mastering Microcontrollers Helped by Arduino -

copy

right

Elektor

Inter

natio

nal M

edia

Also Available:

The all-paperless GREEN Membership, whick delivers all products and services, including Elektor magazine, online only.

Your GOLD Membership contains:

• 8 Regular editions of Elektor magazine in print and digital

• 2 Jumbo editions of Elektor magazine in print and digital (January/February and July/August double issues)

• Elektor annual DVD-ROM

• A minimum of 10% DISCOUNT on all products in Elektor.STORE

• Direct access to Elektor.LABS

• Direct access to Elektor.MAGAZINE; our online archive for members

• Elektor.POST sent to your email account (incl. 25 extra projects per year)

• An Elektor Binder to store these 25 extra projects

• Exclusive GOLD Membership card

Join the CommunityTake out a GOLD Membership now!

Take out your Membership now at www.elektor.com/member


Recommended