Introduction to Arduino

[Pages:172]Introduction to Arduino

A piece of cake!

by Alan G. Smith September 30, 2011

Cover Photo Credit: Arduino Cake Copyright ? 2011 Alan G. Smith. All Rights Reserved. Cake made by Lisa Smith and family

Introduction to Arduino: A piece of cake! Copyright ? 2011 Alan G. Smith. All Rights Reserved. The author can be contacted at: alan@ The hardcopy of the book can be purchased from The most recent PDF is free at ISBN: 1463698348 ISBN-13: 978-1463698348

This book is dedicated to:

My wife who first encouraged me to teach this class and then put up with my spending countless hours on this book

and also helped with numerous comments on the first proof.

My children who excite me about teaching.

My father who spent many hours with me on the Vic 20, Commodore 64, and the robotic arm science project.

Without his investment, I wouldn't be the engineer I am today.

All who would desire to make something, may this book help you with your inventions.

Whatever you do, work at it with all your heart, as working for the Lord, not for men.

Colossians 3:23 (NIV 1984)

Contents

1 Getting Started

1

1.1 What is a Microcontroller? . . . . . . . . . . . . . . . . . . . . . . . 1

1.2 Install the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.3 The Integrated Development Environment (IDE) . . . . . . . . . . 5

1.4 Our first circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

1.5 Updated Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.6 Our First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.7 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

1.8 Gotchas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

1.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2 Making Light Patterns

17

2.1 "Blinky" . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.2 IF Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

2.3 ELSE Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2.4 WHILE statements . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

2.5 What is truth(true)? . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

2.6 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.7 FOR statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.8 Our New Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.9 Introducing Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

2.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

3 Input

33

3.1 Pushbuttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

3.2 Potentiometers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

3.3 RGB LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

v

Contents

3.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

4 Sound

51

4.1 Our Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

4.2 Simple note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

4.3 Music . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

4.4 Music with functions . . . . . . . . . . . . . . . . . . . . . . . . . . 55

4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

5 Making a digital thermometer

59

5.1 Serial Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

5.2 Measuring the temperature . . . . . . . . . . . . . . . . . . . . . . 62

5.3 Hooking up the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . 66

5.4 Talking to the LCD . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

5.5 Bringing it all together . . . . . . . . . . . . . . . . . . . . . . . . . 71

5.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

6 Graphics (Pictures) on our LCD

75

6.1 Binary and Hex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

6.2 Using graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77

6.3 Making a Chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

6.4 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

7 Sensors Galore

91

7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

7.2 Photo Cell (Light Sensor) . . . . . . . . . . . . . . . . . . . . . . . . 91

7.3 Tilt Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

7.4 Reed Switch (Magnetic Field Detector) . . . . . . . . . . . . . . . . 95

7.5 Piezo Element (Vibration sensor) . . . . . . . . . . . . . . . . . . . 96

7.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

8 Making a rubber band gun

99

8.1 One Servo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

8.2 Joystick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

8.3 Pan/Tilt bracket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

vi

Contents

8.4 Adding a firing mechanism . . . . . . . . . . . . . . . . . . . . . . 106 8.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

9 Make your own project!

111

10 Next Steps

113

A Arduino Reference

115

A.1 Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

A.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

A.3 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

A.4 PCD8544 (LCD Controller) Library . . . . . . . . . . . . . . . . . . 125

B Parts in Kit

127

B.1 First used in Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . 127

B.2 First used in Chapter 2 . . . . . . . . . . . . . . . . . . . . . . . . . 127

B.3 First used in Chapter 3 . . . . . . . . . . . . . . . . . . . . . . . . . 128

B.4 First used in Chapter 4 . . . . . . . . . . . . . . . . . . . . . . . . . 128

B.5 First used in Chapter 5 . . . . . . . . . . . . . . . . . . . . . . . . . 128

B.6 First used in Chapter 6 . . . . . . . . . . . . . . . . . . . . . . . . . 128

B.7 First used in Chapter 7 . . . . . . . . . . . . . . . . . . . . . . . . . 128

B.8 First used in Chapter 8 . . . . . . . . . . . . . . . . . . . . . . . . . 129

C Sample Solutions to Selected Exercises

131

C.1 Chapter 1 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

C.2 Chapter 2 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

C.3 Chapter 3 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

C.4 Chapter 4 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

C.5 Chapter 5 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

C.6 Chapter 6 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 149

C.7 Chapter 7 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 156

C.8 Chapter 8 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

vii

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download