Learn Arduino with the Arno Learn Arduino with the Arno Peter Gould and Kevin Warner Olympia Circuits www.olympiacircuits.com Copyright © 2013 by Peter J Gould All rights reserved No part of this book may be reproduced, scanned, or distributed in any printed or electronic form without permission Edition 1.1: July 2013 Printed in the United States of America Contents Getting Started What is Arduino? How does it Work? Setting up Your Computer About this Book Electronics Electricity flows like Water 11 Circuits 12 Electronic Components 13 The Arno Board 17 Cicuit 1: Single LEDs 19 Circuit 2: Momentary Switches 20 Circuit 3: RGB LED 21 Circuit 4: Piezo Element 22 Circuit 5: Phototransistor 23 Circuit 6: Infrared Emitter 24 Circuit 7: Thumbwheel Potentiometer 25 Circuit 8: Temperature Sensor IC 26 Putting the Pieces Together 28 Programming 29 The setup() and loop() Blocks 30 Variables 31 Arrays 33 Connecting with Pins 35 Highs, Lows and In Between 36 Wait a Millisecond 39 What if … 40 Going around in Loops 41 Functions 42 Let’s Communicate 45 Am I an Arno or a Keyboard? 48 Some Thoughts on Programming 49 Projects 51 Projects 1: Starting with LEDs 53 Project 1.01 Blink 53 Project 1.02 Blink x2 55 Project 1.03 Blink Faster 57 Project 1.04 LED Chase! 59 Project 1.05 Wait to Blink 61 Project 1.06 Blink a Little Faster Now 63 Project 1.07 LED Fade 67 Project 1.08 RGB Blink 69 Project 1.09 Change RGB Color with SW1 71 Project 1.10 Fade RGB Colors 74 Project 1.11 Reaction Time Game 78 Projects 2: Serial Communication 82 Project 2.01 Hello World 82 Project 2.02 Talk Back 84 Project 2.03 ASCII Values 86 Project 2.04 Ski Game 87 Project 2.05 Demonstration of the String Object 92 Projects 3: The Potentiometer 94 Project 3.01 Read the Potentiometer 94 Project 3.02 ASCIIbet Soup 96 Project 3.03 Potentiometer sets LED Brightness 98 Project 3.04 Potentiometer sets blink rate 99 Project 3.05 LED Chase, Part II 101 Projects 4: The Piezo Element .103 Project 4.01 Bringing the Piezo to Life 103 Project 4.02 Controlling the Piezo with a Function 105 Project 4.03 Piezo C Major 108 Project 4.04 Piezo Greensleaves 111 Project 4.05 Piezo Metronome 116 Project 4.06 Piezo as an Input 120 Project 4.07 Piezo as an Input 124 Project 4.08 Metronome II 127 Project 4.09 Piezo Playback 132 Project 4.10 Piezo Fireworks 136 Project 4.11 Piezo Mosquito 139 Projects 5: The Phototransistor 142 Project 5.01 First Look at the Phototransistor 142 Project 5.02 Light and Sound 143 Project 5.03 Light and Sound II 145 Projects 6: I2C, EEPROM, and bit operations 147 Project 6.01 EEPROM 147 Project 6.02 I2C Address Scan 153 Project 6.03 Read the I2C Temperature Sensor 157 Project 6.04 High Temperature Alarm 163 Projects 7: Emulate a Keyboard or Mouse 166 Project 7.01 Arno Phone Home 166 Project 7.02 Keyboard Alphabet 168 Project 7.03 Move Mouse 170 Project 7.04 Draw Squares 172 Next Steps… 177 Project Index 179 Arduino Language used in the Arno Projects 179 Components used in the Arno Projects 185 Arno Pin Key 187 The Arno Schematic 189 The Arno Shield 191 The purpose of the book is to get true beginners started with Arduino – people who may not know anything about programming or electronics The best way to learn is by doing, so we want to give you just enough information so that you can understand what’s going on and then start doing things This is how we started with Arduino You’ll be using a special Arduino, the Arno, to help you get started right away If you’re using the Arno Shield instead of the original Arno, it’s a good idea to read the last chapter of this book, The Arno Shield, first so that you’re aware of a few differences between the original Arno and the shield Other than these few differences, everything in the book applies to both the original Arno and the Arno shield The Arno is named after the Arno River valley, birthplace of Leonardo Da Vinci Like Da Vinci, we want you to get a good start with the Arno and then move on to bigger and better things In the spirit of learning by doing, we’ll jump right in What is Arduino? It can be confusing at first You might hear people say “I have an Arduino” or “Do you know Arduino?” or “I like to work with Arduino” Arduino is really three things: The hardware Arduino is based on a family of microcontrollers built by a company named Atmel These microcontrollers are really miniature computers They have ways of communicating with Getting Started Getting Started 4.06, 4.07, 4.08, 6.01, 6.02, 6.03, 6.04 Serial.println sin 2.01, 2.02, 2.03, 2.04, 2.05, 3.01, 3.02, 4.05, 4.06, 4.07, 4.08, 5.01, 6.01, 6.02, 6.03, 6.04 1.1 String object charAt 2.05 endsWith 2.05 equals 2.05 length 2.05 replace 2.05 setCharAt 2.05 substring 2.05 toLowerCase 2.05 toUpperCase 2.05 Variable types boolean 1.04, 1.09, 2.02, 2.04, 3.05, 4.03, 4.04, 4.06, 4.05, 4.07 4.08, 4.09 byte 6.03, 6.04 char 2.03, 3.02, 7.02 float 1.10, 3.01, 6.03, 6.04 HEX 6.02 int 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.10, 1.11, 2.01, 2.02, 2.03, 2.04, 2.05, 3.01, 3.02, 3.03, 3.04, 3.05, 4.01, 4.02, 4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.10, 4.11, 5.01, 5.02, 5.03, 6.01, 6.02, 6.03, 6.04, 7.01, 7.02, 7.03, Arrays 182 7.04 long 1.06, 1.09, 1.11, 2.02, 2.04, 3.04, 3.05, 4.01, 4.02, 4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.10, 4.11, 5.02, 5.03, 6.01, 6.04 Wire.begin 6.02, 6.03, 6.04 Wire.read 6.03, 6.04 Wire.requestFrom 6.03, 6.04 Wire.write Project Index Wire object 6.03, 6.04 183 LED1 1.01,1.02, 1.03, 1.05, 1.06, 1.07, 4.06, 4.07 LED2 1.04, 1.11, 3.05 1.02, 1.04, 1.11, 3.05 1.04, 3.05 5.01, 5.02, 5.03 LED3 LED4 photoTran piezo 4.01, 4.02, 4.03, 4.04, 4.05, 4.06, 4.07, 4.08, 4.09, 4.10, 4.11, 5.02, 5.03, 6.04 POT1 3.01, 3.02, 3.03, 3.04, 3.05 RGB LED blueLED greenLED redLED 1.08, 1.09, 1.10, 4.10 1.08, 1.09, 1.10, 1.11 1.08, 1.09, 1.10, 1.11, 3.03, 3.04, 4.05, 4.08, 4.09, 4.10 SW1 1.05, 1.06, 1.09, 1.11, 2.04, 2.05, 3.05, 4.08, 4.09, 4.10, 6.01, 6.02, 7.01, 7.02, 7.03, 7.04 SW2 1.11, 2.04, 6.01, 7.04 Temperature Sensor IC 6.02, 6.03, 6.04 185 Project Index Components used in the Arno Projects Arno Pin Key Pin Element SW1 Description Push Button Switch I2C SDA I2C SCL SW2 irLED Infrared LED LED2 Single LED LED3 Single LED LED4 Single LED redLED 10 greenLED RGBD LED green channel 11 blueLED RGBD LED blue channel 12 / A11 piezo RGBD LED red channel 13 LED1 Single LED 14 MISO 15 SCLK 16 MOSI 17 SS 18/A0 POT1 19/A1 photoTran A2 SW1 Push Button Switch Pin Key RGBD LED red channel Thumbweel Potentiometer Phototransistor Push Button Switch (shield version) 187 D+ VC C GND VC C TXLED VC C A5 A4 A3 A2 A1 A0 RXLED MOSI SC K MISO D 13 D 12 D 11 D 10 D9 D8 D7 D6 D5 D4 D 3/SC L D 2/SD A D1 D0 Schematic AREF 3.3V D- D+ GND VC C RESET VC C VC C GND D 2/SD A D 3/SC L VC C D 13 D 10 D9 D 11 D6 D7 D8 GND VC C GND A0 GND VC C D 12 GND D1 GND D5 VCC GND Q1 VC C D4 A1 The Arno Schematic 189 VCC The Arno Shield The shield needs to be used with a standard Arduinocompatible board We recommend using Olympia Circuit’s LeOlympia board or the Arduino Leonardo board These boards use the same microcontroller as the original Arno board The Arduino Uno will work, too, but its microcontroller (the ATMega 328) cannot the projects that use the keyboard and mouse objects (Projects in this book) Make sure to select whatever board you’re using in the Arduino IDE when programming for the Arno Shield (under Tools > Boards) The shield requires some soldering before you can use it It comes with sets of pins called headers that connect the shield to an Arduino-compatible board Place the pins in the holes at the edges of the Arno shield with the long ends pointing down The holes in the shield are crooked on purpose; this helps to hold the headers in place while you solder them You can also plug the headers into your Arduino-compatible board and then place the shield on top of it to really hold the pins steady while you solder Solder the pins from the top of the shield If you don’t have experience soldering, there are plenty of tutorials on YouTube The SW1 momentary switch (the left-hand button) is connected to the A2 pin of the Arduino-compatible 191 Arno Shield The Arno Shield is packed with the the same project circuits as the orginal Arno Board There are some differences, though You need to be aware of these differences to get the full use of the shield: board In every sketch where you use SW1, you need to assign it to pin A2 instead of pin So when you follow the sketches in the book, you need to replace the line: int SW1 = 1; with the line: int SW1 = A2; The Arno Shield has a different physical layout than the Arno board so it can be plugged into a standard Arduino footprint The locations of the circuits used in the Arno projects are highlighted below Detailed descriptions of the circuits are given in the chapter The Arno Board Circuit 1: Single LEDs Circuit 3: RGB LED 192 Circuit 2: Momentary Switches Circuit 4: Piezo Element Circuit 6: Infrared Emitter Circuit 7: Thumbwheel Potentiometer Circuit 8: Temperature Sensor IC Arno Shield Circuit 5: Phototransistor 193 ... components In the next chapter, we’ll look at how these components make the Arno board work 16 The Arno Board Arno Board The Arno board was built so that you can learn the basics of Arduino without... 179 Arduino Language used in the Arno Projects 179 Components used in the Arno Projects 185 Arno Pin Key 187 The Arno Schematic 189 The Arno Shield 191 The purpose of the book... in the book applies to both the original Arno and the Arno shield The Arno is named after the Arno River valley, birthplace of Leonardo Da Vinci Like Da Vinci, we want you to get a good start with