Making Things Move: DIY Mechanisms for Inventors, Hobbyists, and Artists - Dustyn Roberts (2010)
Appendix: BreadBoard Power and Arduino Primer
This appendix includes sections on getting power to your breadboard as well as setting up the Arduino hardware and software.
Getting Power to Your Breadboard
Directly plugging a battery pack into your breadboard is not the best way to get power to it. This method, used extensively in Chapter 6, is not technically a good practice because the four AA batteries can give you anywhere between 4V to 6.4V, depending on if the batteries are rechargeable and how new they are. However, chips like the H-bridge used in Project 6-5 are more comfortable with a steady voltage between 4.5V to 5.5V. And most motors will want a different voltage than your logic circuit. The following methods help get a more regulated power supply into your breadboard:
• Use hook-up wire to bring regulated power from a benchtop supply into your breadboard (see Project 10-3 for reference).
• See Tom Igoe’s notes on soldering a power supply connector to plug directly into your breadboard and use with an AC adaptor (http://itp.nyu.edu/physcomp/Labs/Soldering).
• Use one of SparkFun’s (www.sparkfun.com) breadboard power supplies (like PRT-09319, shown in Figure A-1) to give you a reliable 5V supply. You’ll want to solder on some male header pins (PRT-00116) in order to plug it into a breadboard.
• Use Adafruit’s (www.adafruit.com) adjustable breadboard power supply (version 1.0). It comes as a kit you need to assemble, but can give you smooth power from 1.25V to 20V and up to 1.25mA, depending on the power supply you plug in.
FIGURE A-1 SparkFun’s breadboard power supply (PRT-09319)
If you can’t find a power supply that exactly matches the voltage your logic circuit or motor wants, there are a couple of tricks you can use. Linear regulators take a high-input voltage and make it lower. The LM7805 5V regulator (SparkFun COM-00107) is popular and converts an 8V to 15V input to a 5V output (see an example at http://itp.nyu.edu/physcomp/Labs/Breadboard). Step-up regulators do the opposite: They take a low voltage and make it higher. These are a bit more involved to use, so it’s best to go with a ready-made module like SparkFun’s PRT-08999, which can take in 1V to 4V and output 5V. (For an example of how to work with boost regulators, see www.ladyada.net/make/mintyboost/process.html.)
An Arduino is like a mini-computer that acts as the brains for your project once you get past just wanting to turn something on and off. Why Arduino? A handful of other development boards out there do basically the same thing. They have a microcontroller (mini-computer) on board, places to plug in stuff, and a way to get power onboard. These include MIT’s Handyboard, Phidgets, the Make Controller, and others. We work with Arduino in this book for several reasons:
• Price At $30 per board and free software to run it, you can get up and running without making a huge investment.
• Compatibility It works on Windows, Mac OSX, and Linux systems.
• Convenience The board is very flexible and can be used to control motors, blink LEDs, and do so many other things that I won’t even try to go through them here. An Arduino can be the backbone of any project that requires more smarts than a simple switch.
• Ongoing support and development The hardware and software are both open source and extensible, which means that you or anyone else can make improvements that might be folded into later generations of the Arduino. The Arduino system is very popular and has sold more than 150,000 modules, so it has extensive online documentation, sample code, help forums, sample projects, and so on to get you going and keep you going. The Arduino team members use and teach the platform themselves, so they are committed to constant improvements.
Make the Arduino Play Nice with Your Computer
There are two parts to the Arduino system:
• The Arduino board you hold in your hand (see Figure A-2)
FIGURE A-2 The Arduino board
• The Arduino integrated development environment (IDE) you run on your computer
NOTE This introduction draws from the online guide (http://arduino.cc/en/Guide/Windows) and Getting Started with Arduino by Massimo Banzi (Sebastopol, CA: Make, 2008).
Follow these steps to get the system up and running on your computer and write your first program to blink a light:
1. Get an Arduino (SparkFun DEV-00666) and a USB A to B cable (like SparkFun CAB-00512).
2. Go to www.arduino.cc, click the Download the Arduino Software link, and choose the correct download for your operating system (Windows, Mac, or Linux). This tutorial goes through download, install, and first steps with an Arduino Duemilanove on a PC running Windows Vista. (For step-by-step instructions for newer Arduino versions or on other operating systems, go to http://arduino.cc/en/Guide/HomePage.) Download the file, uncompress it, and save that folder wherever you want, such as in your C:\Program Files folder.
3. Plug the Arduino into the computer with a USB cable. The green power light (marked PWR) on the Arduino board should go on, an orange LED (marked L) next to pin 13 might start flickering, and the Found New Hardware window will pop up. To install the drivers that let your computer talk to the Arduino board, click the recommended option that allows your computer to locate and install the drivers itself. You’ll go through this twice, because there are two items that the computer needs to install before it can talk to the Arduino.
4. Once the drivers have been installed successfully, launch the Arduino application by double-clicking the icon in the folder you downloaded in step 2. Check to make sure the correct model of Arduino is selected by selecting Tools | Board from the menu bar and choosing the Arduino Duemilanove, as shown in Figure A-3.
FIGURE A-3 Select the correct board from the Arduino application.
5. Now you need to figure out to which port on your computer your Arduino is hooked up. Open the Device Manager by clicking the Start menu, right-clicking Computer, and choosing Properties. Click Device Manager in the upper-left corner. Expand Ports (COM & LPT). The Arduino will appear as a USB serial port and will have a name like COM3 or COM4, as shown in Figure A-4. Now go back to the Arduino application and choose that port from the Tools | Serial Port menu.
Now everything is set up, and your Arduino and your computer can communicate with each other.
Now Make It Blink
To get the Arduino to do anything for you, you need to give it instructions in a language it understands. These instructions are written in a special code language based on C/C++. A set of instructions that you create in the IDE is called a program or a sketch. Let’s try an example to get familiar with this language.
FIGURE A-4 Identifying the serial port
1. Open the Blink example by navigating to File | Examples | Digital | Blink. This will open in a new window.
2. To get this sketch onto your Arduino board, first click the Verify button (see Figure A-5). This verifies that the code is correct and translates the instructions into a program that the Arduino board can run. It will say “Done Compiling” at the bottom of the Arduino application when this is finished.
3. Click the Upload button (see Figure A-6). This tells the Arduino to stop whatever it’s doing and listen for instructions. In a few seconds, you should see the TX (transfer) and RX (receive) lights on the board flickering. A few seconds later, you should see the tiny orange LED (marked L) next to pin 13 on the board blinking orange—1 second on, 1 second off. At this point, your sketch is stored onboard the Arduino’s tiny microcontroller brain. This sketch will live on the Arduino, even if you turn off the board or reset it (until you upload a new sketch).
FIGURE A-5 Always verify a program before uploading it.
Congratulations! Now you know how to get the Arduino to do your bidding. If you have any trouble, see the online guide at http://arduino.cc/en/Guide/Troubleshooting.
Now Make It Blink BIG
In the previous example, you probably noticed the words at the top of the sketch are gray and surrounded by a /* at the start and a */ at the end. These symbols are used to give yourself notes about the sketch, but don’t actually mean anything to the Arduino. The same applies to anything following a // symbol. The next part of the sketch declares a variable, in this case telling the Arduino which pin the LED is plugged into. This LED is already part of the board, but you can use an external LED on pin 13 as well.
FIGURE A-6 Upload the sketch to your Arduino board.
Every Arduino program has a setup method that runs once at the start of the sketch, and a loop method that runs over and over again as long as the Arduino is powered. The setup method here just sets ledPin (pin 13) as an output, so Arduino knows to do something to it instead of listening for something to happen (which would be declared as an INPUT). The loop just says to turn the ledPin HIGH (on) for a delay of 1,000 milliseconds (1,000 milliseconds = 1 second), then LOW (off) for 1 second.
Now if you plug in an LED with the long leg to pin 13 and the short leg directly next to it in ground (marked GND, see Figure A-7), your external LED should blink along with the little one. Don’t try this on any other digital pin than 13. The Arduino has a resistor on the board going to pin 13 so the voltage drops to a level that satifies the LED. If you plug the long leg directly into any of the other digital inputs on that row, you’ll probably fry it.
FIGURE A-7 Arduino with external LED plugged in
CAUTION Do not plug things into (or unplug things from) an Arduino while it is powered. This is bad and could give you some very weird errors, and, worst case, mess up your microcontroller chip (the brain on the Arduino). These are relatively easy to replace, but if you follow this advice, you should never have to do so.
Now that you know how to affect the outside world with code that lives on your Arduino, you should be ready to tackle the projects in Chapter 6 and beyond. This book does not go into depth on how to write code. Check Getting Started with Arduino by Massimo Banzi for an Arduino-specific introduction to programming.