Arduino Tutorial

originally written by xxx, March 9, 2009 14:26 (http://www.ladyada.net/learn/arduino) manually pasted into OpenOffice by Friedl on June 2, 2009

Introduction
So, I get two or three emails a day, all basically asking the same thing: "Where can I learn about electronics?" In general, most of these people have seen some of my projects and want to be able to build similar things. Unfortunately, I have never been able to point them to a good site that really takes the reader through a solid introduction to microcontrollers and basic electronics.
UNTIL NOW!!!

Table of Content
Arduino Tutorial...................................................................................................................................1 Introduction .....................................................................................................................................1 Goes well with: Arduino Starter Pack and a nice Merlot ...............................................................3 Lesson 0: Pre-flight check...Is your Arduino and computer ready? ..............................................11 Lesson 1: The "Hello World!" of electronics, a simple blinking light...........................................20 Lesson 2: Sketches, variables, procedures and hacking code........................................................37 Lesson 3 - Breadboards, resistors and LEDs, schematics, and basic RGB color-mixing .............42 Lesson 4: The serial library and binary data - getting chatty with Arduino and crunching numbers .......................................................................................................................................................63 Lesson 5: Buttons & switches, digital inputs, pull-up and pull-down resistors, if/if-else statements, debouncing and your first contract product design.....................................................81 Lesson 6: Wiring up an LCD to an Arduino................................................................................116 Appendix: HELP!........................................................................................................................128 Goes well with: Arduino Starter Pack and a nice Merlot I designed this tutorial course to accompany the Arduino starter pack sold at the Adafruit webshop. The pack contains all the components you need (minus any tools) for the lessons Tools Here are some recommended tools:

Multimeter/Oscilloscope A meter is helpful to check voltages and continuity. Check out my recommended basic multimeter and where to buy.

Flush/diagonal cutters. Great for cutting component leads and wires. Check out my recommended basic diagonal cutters and where to buy.

Wire strippers very handy for making wire jumpers! Check out my recommended basic wire strippers and where to buy.

If you need to get any soldering done, you may also want.... Soldering iron. One with temperature control and a stand is best. A conical or small 'screwdriver' tip is good, almost all irons come with one of these. A low quality (ahem, $10 model from radioshack) iron may cause more problems than its worth! Do not use a "ColdHeat" soldering iron, they are not suitable for delicate electronics work and can damage the kit (see here) Check out my recommended basic soldering iron and where to buy.

Solder. Rosin core, 60/40. Good solder is a good thing. Bad solder leads to bridging and cold solder joints which can be tough to find. Dont buy a tiny amount, you'll run out when you least expect it. A quarter pound spool is a good amount. Check out my recommended basic solder and where to buy.

Share and Enjoy! All of the content in the Arduino Tutorial is CC 2.5 Share-Alike Attrib. You can use the text and pictures all you want, providing you do all the hosting, and indicate the attribution like: "This tutorial is by Limor Fried and from http://www.ladyada.net/learn/arduino". Thanks! Love it? Hate it? See a mistake? Post it to the forums! Credits and other links To some extent, the structure of the material borrows from: The impressively good "What's a microcontroller?" book (& now PDF) by Parallax. It's for BASIC Stamps but is still a good read even if you don't plan to use Stamps. "Spooky Arduino" - Todbot's excellent (if fast-paced!) class on Arduino projects for Halloween. Arduino Booklet - With some really great drawings!

Goes well with: Arduino Starter Pack and a nice Merlot
I designed this tutorial course to accompany the Arduino starter pack sold at the Adafruit webshop. The pack contains all the components you need (minus any tools) for lessons 0-10. If people like this system, I will add another pack for another half-dozen lessons, numbers 11+ 1. Starter pack contents 2. Tools you'll want 3. Assemble the protoshield and 9V battery pack Starter pack contents Description Assembled Arduino board, preferrably a Diecimila (or whatever the latest version is) Distributor Quantity

Adafruit

1

Or any computer supply store 1 Proto shield kit See assembly instructions here Adafruit 1 Tiny breadboard Adafruit 1 9V DC switch-mode power supply with 2.1mmm barrel plug Adafruit 1 . Any length is OK.1mm barrel plug Adafruit 1 9V battery case with 2. Standard A-B cable is required.Adafruit USB Cable.

5% 1/4 W Brown Black Orange Gold 5 .Red 5mm LED Digikey 5 Green ultrabright LED Digikey 1 Red ultrabright LED Digikey 1 Blue ultrabright LED Digikey 1 1K Potentiometer Elexp (Marked 102) 1/2W 10K Potentiometer Elexp (marked 103) 1/2W 1 1 100 ohm resistor.0 Kohm resistor. 5% 1/4 W Brown Black Brown Gold 1. 5% 1/4 W Brown Black Red Gold 5 Generic 5 10 Kohm resistor.

CdS Photocell DigiKey 1 12-18" of 22 gauge solidcore wire in differing colors.. Check out my recommended basic diagonal cutters and where to buy. but you probably have a few of these tools kicking around the house. Check out my recommended basic multimeter and where to buy. . Wire strippers very handy for making wire jumpers! Check out my recommended basic wire strippers and where to buy. Tools 4 Tools are not included in the starter pack. you may also want. If you need to get any soldering done.. Ask a friend if you can borrow them? Multimeter/Oscilloscope A meter is helpful to check voltages and continuity.. Great for cutting component leads and wires. Flush/diagonal cutters.

$10 model from radioshack) iron may cause more problems than its worth! Do not use a "ColdHeat" soldering iron. Bad solder leads to bridging and cold solder joints which can be tough to find. if you don't know how. A quarter pound spool is a good amount. A low quality (ahem. Dont buy a tiny amount. Solder. Good solder is a good thing. 60/40. they are not suitable for delicate electronics work and can damage the kit (see here) Check out my recommended basic soldering iron and where to buy.Soldering iron. Rosin core. One with temperature control and a stand is best. Start by unscrewing the plastic cover from the barrel plug . The 9V instructions follow! Learn how to solder by looking here. Stuff to do You'll want to solder up your protoshield and 9V battery holder. almost all irons come with one of these. you'll run out when you least expect it. A conical or small 'screwdriver' tip is good. Check out my recommended basic solder and where to buy. The protoshield instructions are here.

Hook the red wire onto the middle tab and the black wire to the outer tab as shown. Place the 2. see the image below Use a pair of pliers (or your fingernails) to make little hooks out of the ends of the red and black wires. Then bend the hooks over all the way so .Thread the barrel cover onto the red and black wires so that the round part is near the pack itself.1mm barrel plug in a vise or 3rd hand tool to keep it steady.

. the two wires should not be poking out. Make sure you heat the black wire tab well as its large enough that it may take a moment for the solder to melt onto the tab well. Make sure the soldering is neat.that the wires can't come loose while you solder Solder both hooks to the tabs with a touch of solder.

Gently pull the red wire towards the black wire and bend over the tabs so that they hold the two wires together. Just make sure its snug. Don't crimp them too tightly or the tab can/will cut into the red wire. .

It will verify the Arduino is working as intended and that the computer . A tutorial on how to do this is here.. Lesson 0: Pre-flight check..Is your Arduino and computer ready? Introduction This lesson won't teach any electronics.Now screw the plug cover on to protect the connector You should probably use your multimeter to test continuity between the inner conductor and the outer conductor to make sure they are not shorted. really. Its more for making sure that everything is setup and ready for the future lessons.

$5 Adafruit 9V DC power plug with 2.1mm barrel plug. etc.you are using is compatible. Radio Shack. positive tip Optional Any electronics supply store (Best Buy. Any length is OK. Standard Or any computer A-B cable is supply store required. preferrably a Adafruit Diecimila or Duemilanove (or $35 whatever the latest version is) but NG is OK too Adafruit USB Cable. Do you have everything you need? For this lesson you will need some stuff! Make sure you have this stuff or you will be unable to complete this lesson Image Description Distributor Assembled Arduino board.) $10 .

$1 Preparing the Arduino! Take your Arduino out of its protective bag. Look at it and make sure it looks kinda like this: Diecimila Arduino Or like this: .4 Rubber bumpers Optional These are included in Arduinos purchased from the Adafruit shop. otherwise any hardware store should have them.

here is an Arduino that is missing the two round silver capacitors! OK. put the rubber bumpers on the bottom of the board. this is essential! . This will protect your Arduino from spills and dirty tables. If your table is made out of metal. For example. make sure to contact the store you bought it from.NG Arduino If there's anything missing or really wrong. now that you are satisfied that your Arduino looks good.

.4 or lower kernel only! If you have a Linux computer." .4 or higher Linux v2. the driver is built-in and you are good to go. Under Windows use Unzip or rightclick and select "Extract All. download the USB VCP driver from the FTDI website I strongly suggest visiting the site and downloading the most recent drivers. and you have a 2. v 10.. 2007 the following links are the most recent. You can always run uname -a to get the kernel version. as of Sept. • • • • Windows XP/2000/Vista Mac OS X PPC Mac OS X Intel.Download Driver You'll want to grab the USB drivers before plugging in the Arduino for the first time. Then extract the driver bundle onto the Desktop.6 kernel. First.

The jumper-setting step is only for Diecimila and OLDER arduinos! Make sure the Power Jumper is set correctly. If it's on the two pins nearer the DC Jack then it means you are planning to power the Arduino using a 9V battery or wall adapter. The other end should be . Right next to the USB jack. there is a jumper with 3 pins. The computer will then power the Arduino. You'll want it set as shown in the picture above. The most common way to do this is to plug one end of the USB cable into the Arduino and the other end into a computer. One end should be thin. that means you are planning to power the Arduino via the USB cable. Make sure your cable is a A-B cable.Power up! (USB) Now we are ready for the moment of truth. it's time to plug your Arduino in and power it up. If the jumper is on the two pins nearest USB jack. rectangular.

Sometimes monitors or keyboards have a USB port you can plug into.square. once you've verified you can power the Arduino and upload sketches no problem. Later on. Most of the time this is fine. Plug the thin end into your computer Make sure that the USB cable is plugged in directly to a computer port. then you can try plugging it into other ports. Plug the square end into your Arduino . but I strongly suggest you plug it directly into the computer as that will eliminate any possible problems. Same goes for USB hubs.

The video is from plugging into a Windows computer. And a few blinking orange lights. on the left side.If you're plugging it into a Mac or Linux machine. If no lights or blinking occurs. double check: . as shown in this video (download mp4 here) Click To Play play_blip_movie_385086().You should get a small green light on the right side of the Arduino. as shown here. you may not get as much blinking from the orange lights.

The jumper-setting step is only for Diecimila and OLDER arduinos! Make sure the Power Jumper is set correctly. It means that the outside of the plug is negative and the inside is positive. it's the wrong kind. If it doesn't fit or is wobbly. your Arduino may be faulty. Verify that you have a 9V DC 100-500mA power adapter. To verify the DC plug is the right shape. A center-negative plug will not work with the Arduino. optional!) Another way to power up the Arduino is to plug in a battery or wall adapter into the DC jack. The jumper should be set so that it connects the two pins close to the DC jack . you can look for these clues Make sure the symbol near the bottom of the label is present.1mm barrel plug and positive tip. with a 2. You can learn how to test wall adapters using a multimeter here. just try plugging it in. and computer If you still can't get it working. Next its time to install the driver! Follow these links for instructions for each of the supported operating systems • Windows • Mac • Linux Power up! (9V DC. USB cable. If the box doesn't have any information about the adapter.• • • • Is the USB cable plugged into the computer and into the Arduino? Is the computer on? Is the jumper set correctly? Try another USB port.

the software (should be) identical on all platforms. Any LED is fine as long as it looks sorta like the photo. polarity.Plug in the adapter and verify you get the green light! If not. etc. double check: • • • • Is the DC adapter plugged in? Is the DC adapter the right kind? Check voltage. plug size. This lesson will basically get you up and running using the Arduino software and uploading a sketch to the Arduino board. a blinking light. Is the jumper set correctly? Try another adapter. If you still can't get it working. just a USB cable and an Arduino. it is finally time to make your Arduino do something! We're going to start with the classic hello world! of electronics. with a plastic bulb and two legs Make sure you've gone through Lesson 0 first! . a simple blinking light Introduction Ah yes. Linux will be added once I figure out how to get it working (yay) Do you have everything you need? Not much is needed for this lesson. Once you've completed this step we can continue to the really exciting stuff. If you have an older Arduino you may also need an LED. Lesson 1: The "Hello World!" of electronics. which is when we start writing our own sketches! These instructions mostly show Windows software. your Arduino may be faulty. Except when indicated.

This Any part is only required electronics $1 for NG rev c Arduinos supply store (and maybe other older ones). as long as it has two wire legs. preferrably a Diecimila (or whatever the latest version is) $35 Adafruit USB Cable. Diecimila Arduino's have this part 'built-in' Download the Software The first thing to do is download the Arduino software. Or any computer supply store $5 LED .Optional Nearly any LED is OK. The packages are quite large. 30-50 MB so it may take a while to finish .Assembled Arduino Adafruit board. Any length is OK. Go to the Arduino Software Download page and grab the right file for your OS. As of Sept 2007 the version is 009 but you should use whatever is most recent. Standard A-B cable is required.

Unpack and Install Extract the package onto the Desktop Windows Mac OS X .

Windows Mac OS X .

Startup! Double click the Arduino software icon Windows Mac OS X To open up the workspace .

but there's a chance you have an ATmega8.I think I get the red error text shown because I already have Arduino installed. Look for the chip on the Arduino that looks like this: . Almost all Arduinos use the ATmega168. Either way. it isn't a problem if you do or don't see it. Select chip The first step is to configure the Arduino software for the correct chip.

will be fixed soon). the program will remember next time it's run. On a Mac it will be something like tty.If the text says ATMEGA8-16P then you have an atmega8 chip. This preference is saved so you only have to set it once. Go back to lesson 0 to remind yourself of which port it is.usbserial-xxxxx Windows port selection . Select port Next. If the text says ATMEGA168-20P then you have an atmega168 chip. On a PC it will probably be something like COM3 or COM4. If it says "ATMEGA328P-20P" you have an atmega328p chip Make sure the correct chip is selected (this picture is really old. its time to configure the Serial Port (also known as the COM Port).

if you have multiple Arduino's. However. Open blink sketch Sketches are little scripts that you can send to the Arduino to tell it how to act. the program will remember next time it's run.Mac port selection This preference is saved so you only have to set it once. Go to the File menu -> Sketchbook -> Examples -> Digital -> Blink The window should now look like this. Let's open up an Example Sketch. So every time you plug in a new Arduino. double check that the correct port is selected. with a bunch of text in the formerly empty white space and the tab Blink above it . they may be assigned difference COM ports.

That means check it over for mistakes (sort of like editing) and then translate it into an application that is compatible with the Arduino hardware. .Verify / Compile The first step to getting a Sketch ready for transfer over to the arduino is to Verify/Compile it.

such as NG. Older Arduinos. must be manually reset before uploading a sketch. . so you don't need to do anything.After a few seconds. shown here. Diecimila Arduino's have built-in auto-reset capability. you must reset the board. you should see the message Done compiling. This means the sketch was well-written and is ready for uploading to the Arduino board! Reset (NG only) To tell the Arduino that it should prepare itself for a new Sketch upload. in the Status Bar and Binary Sketch Size: in the Notification area. To do that simply press the black button on the right hand side of the board.

Select Upload to I/O Board from the File menu After a few seconds you should get this screen. . press the Reset Button now.Upload Now it's time to upload. with the message Done uploading. the green light is on and the correct Serial Port is selected. in the status bar. Make sure the Arduino is plugged in. just before you select the Upload menu item. If you have an NG Arduino.

did you press reset just before selecting Upload menu item? Is the correct Serial Port selected? Is the correct driver installed? Is the chip inserted into the Arduino properly? (If you built your own arduino or have burned the bootloader on yourself) • Does the chip have the correct bootloader on it? (If you built your own arduino or have • • • • .If you get the following error message "avrdude: stk500_getsync(): not in sync: resp=0x00" that means that the Arduino is not responding Then check the following: If you have a NG Arduino.

If you get the following error Expected signature for ATMEGA Then you have either the incorrect chip selected in the Tools menu or the wrong bootloader burned onto the chip If you get the following error: can't open device "COM10": The system cannot find the file specified (under Windows. You can use a program like FTClean to clear out old COM ports you aren't using anymore. Once you've cleaned out the ports. COM port value may vary) It means that you have too many COM ports (maybe you've got 9 Arduinos?) You should make sure that the port is numbered as low as possible. Alternately. if you're sure that the ports are not used for something else but are left over from other . go back and verify that the correct driver is installed (lesson 0) and that you have the correct serial port selected in the menu.burned the bootloader on yourself) If you get the following error message: It means you dont have a serial port selected. you'll have to reinstall the driver again (see lesson 0).

. you can simply change the COM port using the Device Manager.. Don't forget to select the new port name in the Arduino software. Select the USB device in the Device Manager. Video of all steps Here is a video showing the timing of the steps described so far.. The lower port names may say (in use) but as long as the other USB devices aren't plugged in... and in the next window change the COM port to something like COM4 or COM5. right click and select Properties Then click Advanced. it shouldn't be a problem.USB devices. This is a little riskier than just using FTClean.

Object 1 Insert LED (NG Arduinos) Some older Arduinos don't have a built in LED. its easy to tell if yours does or not If you have a Diecimila or other Arduino with a built in LED you will see a translucent part as shown .

and instead you will see two silver dots If you don't have an LED. the LED factory cuts the legs at different lengths. That means if you put it in backwards it will not work! To help you put the LED in right. you'll need to add your own.If you have an NG rev C or other Arduino without an LED. LEDs are directional components. the translucent part will not be there. as long as it has two legs and kinda looks like the one shown here. The longer leg goes in the hole marked 13 and the shorter one goes in the hole marked GND . Any LED will do.

make sure the LED is inserted as indicated before. Here is a video of the entire uploading process. There is a short wait while the software prepares to transfer the sketch. there is a 7 second delay until the sketch starts. the upload process is quite fast. transfer the sketch and start the sketch. indicating the sketch is being transfered. The board will automatically reset itself.Watch! If you have a Diecimila Arduino. When its done. The little translucent LED will start blinking Click To Play play_blip_movie_386213(). . Right after I press the Reset Button I start the sketch upload. just start the Upload from the software.If you have an NG arduino. Then the two small orange lights blink.

arduino. We'll start off easy by just modifying something that already works. looking at each line and trying to understand what its doing.Click To Play play_blip_movie_386202().. delay(1000). when the sketch starts // sets the digital pin as output // run over and over again // // // // sets the LED on waits for a second sets the LED off waits for a second Comments Lets examine this sketch in detail starting with the first section: . Next step is to start writing your own sketches. HIGH). in the correct holes.If you don't get a blinking LED. Then we will start hacking the sketch! Blinkie Start up the Arduino software and open the Blink example sketch.. Lesson 2: Sketches. It takes a bit of getting used to but we will go through these examples slowly. and so on. } void loop() { digitalWrite(ledPin. OUTPUT). When you select Compile/Verify from the menu. which is a programming language that is very popular and powerful. the Arduino software looks over the document and translates it to Arduino-machine-language . Sketches are written in text. /* * Blink * * The basic Arduino example. as you did in Lesson 1. } // LED connected to digital pin 13 // run once. * * http://www. Sketches themselves are written in C. digitalWrite(ledPin.cc/en/Tutorial/Blink */ int ledPin = 13. We use pin 13 because. void setup() { pinMode(ledPin. Turns on an LED on for one second. * then off for one second. LOW). procedures and hacking code Introduction OK you've gotten your Arduino set up and also figured out how to use the software to send sketches to the board. it has either a built-in LED * or a built-in resistor so that you need only an LED. and perhaps try a different LED as it may be bad. delay(1000). To start we will venture deep into the Blink sketch. just like a document. The sketch itself is in the text input area of the Arduino software. * depending on your Arduino board.which is not human-readable but is easy for the Arduino to understand. variables. make sure you put the part in the right way.

} // run once. If you remember your math. * * http://www. . each starting with //. * then off for one second. We use pin 13 because. Anything between the /* and */ is ignored by the Arduino. Variables Lets look at the next line: int ledPin = 13. one line comments. // LED connected to digital pin 13 This is the first line of actual instruction code. which turns out to be a sentence telling the computer that we would like it to create a box named ledPin and to put the number 13 in that box. it has either a built-in LED * or a built-in resistor so that you need only an LED.. OUTPUT). which is short for integer which is a fancy way of saying whole number The second part of this sentence is ledPin which is the name of the box The third part is an =. a whole number (integer) which is assigned to ledPin Procedures Lets move on to the next section void setup() { pinMode(ledPin. which is basically like a computerized sentence. The rest of the line. all computer sentences end with a . You can tell if something is a comment because there is a /* at the beginning and a */ at the end. The first part we can easily understand is the part to the right. when you wrote it and what its supposed to do. you may recall that the box is also known as variable. * depending on your Arduino board. the stuff before the //. it is text that is not used by the Arduino. We also see in the middle there is a statement. (semicolon) OK all we have left is the statement itself. which basically says that the variable (box) named ledPin should start out equaling whatever is after the = The fourth part is 13. box-type int box-name ledPin = = stuff-to-put-in-box 13 The first part of this sentence is int. Comments are very useful and I strongly encourage every sketch you make have a comment in the beginning with information like who wrote it. Much like human sentances end with a . (period)./* * Blink * * The basic Arduino example. which is also a comment..arduino. its only there to help humans like us understand whats going on. we know its a statement because it ends with a . you can use // as well as /* */. Turns out if you want to make a small comment. // is often used for short. It is also extremely unlike English (or any other human language). when the sketch starts // sets the digital pin as output OK we've got two comments. is what is called a statement. Turns on an LED on for one second. We understand comments already so lets skip that. and so on. In this example the person who wrote the comment decided to make it look pretty and add *'s down the side but this isn't necessary.cc/en/Tutorial/Blink */ This is a comment.

indicating the correct procedure for washing a cat. for cat to get clean. pinMode(ledPin. . The name of the procedure is wash the cat. when the sketch starts There is one statment in this procedure. LOW). we see that it is named setup and it has no input values and it returns void. thats are used to indicate the beginning and end of the procedure. one after the other. We're going to skip the first statement for now and go straight to statement #2. OUTPUT). } // a procedure To better understand procedures. grab the cat. void loop() { digitalWrite(ledPin. That is. this one called loop which also has no inputs or output.(semicolon) however there's a whole bunch more stuff before and after it. find the cat. wait 3 minutes. Procedure calls We're onto the next bunch of text. If you perform all of the statements then you should be able to turn a dirty can into a clean cat. We also recognize another procedure. (That doesnt mean it doesn't do anything. This bunch of code is an example of a procedure. lets use an analogy to the kinds of procedures we're used to clean cat wash the cat(dirty cat) for washing the cat { turn on the shower. returned value void procedure name setup (input values ) () { statements } { pinMode(ledPin. Its just like a procedure that we use to perform a task step by step. Looking again at the procedure. This procedure has multiple statements. it uses a dirty cat as the input and returns a clean cat upon success. Inside the procedure are a bunch of statements. OUTPUT). to show when its complete) void setup() // run once. its used to group statements together so that we can refer to them all with one name. delay(1000). delay(1000). } // run over and over again // // // // sets the LED on waits for a second sets the LED off waits for a second Using our now well-honed technique we recognize that the text to the right is all comments. this procedure doesnt return anything. a procedure is a collection of statements. just that it doesn't have a tangible number or whatever. an open bracket { and a closed bracket }. // sets the digital pin as output We'll return to this statement in detail later. There are two brackets. suffice to say it is a way of telling the Arduino what we would like to do with one of the physical pins on the main processor chip. digitalWrite(ledPin. HIGH). } // wait This is a procedure for washing the cat. release cat. Now you're probably asking yourself "what is void?" Well thats a computer-scientist way of saying nothing. put cat under shower.

The second and fourth statements are the same.. Stop what you're doing for (number of milliseconds) amount of time. and have something to do with a delay. LOW). Not a big deal. change the number in the delay procedure calls to 500 (from 1000) as shown void loop() { digitalWrite(ledPin. (We will use the phrasing calls a procedure). such as MyBlink .. delay(500). procedure name delay (input values) (1000) . there's a procedure something like this: void delay(number of milliseconds) { "Dear Arduino. In your Arduino software. Modifying the example Now that we've analyzed the entire program it's time to make some changes. delay(500). This statement says "Dear Arduino. this example is not proper code) Turns out this delay procedure works pretty well. This means that somewhere out there. lucky for us. and that pin is powering the LED so in essence its turning the LED on and off. Stop what you're doing for a short amount of time. This statement is very similar to the "wait 3 minutes. you'll get the warning that it's read-only. we see that it is also a procedure call. This time for some procedure called digitalWrite. Basically." command in our cat-washing procedure. it always does whats in the setup procedure first. the statement performs a procedure call. and all we have to do is tell it how many milliseconds (1/1000th of a second) to wait and it will do the job for us. someone else wrote a procedure called delay which we can call upon to do the work for us. Special Procedures . Then it does whatever is in the loop procedure over and over and over. we call upon an accountant to do it for us. you can save it under a new name. Thanks!" To do this. } // run over and over again // sets // waits // sets // waits the for the for LED on a second LED off a second If you try to save the sketch. Returning to the first statment. . HIGH). except to explain that its turning a pin on the Arduino chip on and off. We'll also skip this one in detail for a bit. Kind of like if we need to do our taxes and we dont know how.Setup() and Loop() I do want to mention quickly here that the two procedures we've mentioned so far are extra-special in that when the Arduino first wakes up after being reset. digitalWrite(ledPin. giving them the paperwork input and getting tax return as the result. Thanks!" } (Of course. we want the Arduino to take a break but don't quite know how to do it.forever! Or at least until you turn it off.

Once the Arduino has been updated with the new sketch you should see a faster-blinking light than before If the LED is not blinking faster.Compile/Verify the sketch and Upload it. What happens? Highlight the text below to see the answer Intense strobe action! . using your Lesson 1 techniques. Modify the code so that the light is on for 100 msec and off for 900 msec Exercise 2. check: • Did you make the changes to the delay procedure call to make it 500? • Did the compile/verify complete successfully? (should look like the screenshot above) • Did the upload complete successfully? (should look like the screenshot above) Exercises Now it time for you to make modifications to the sketch and experiment with different delay values Exercise 1. Modify the code so that the light is on for 50 msec and off for 50 msec.

an LED you added). and practiced uploading sketches to the Arduino some more.Exercise 3. connecting up new parts with wire. You also learned how to modify a sketch. resistors and LEDs. What happens? Highlight the text below to see the answer The LED creates a dashed trail of light in the air. Lesson 3 . and basic RGB color-mixing Introduction You've started modifying sketches. What happens? Highlight the text below to see the answer The light is no longer blinking Now pick up the Arduino and gently wave it back and forth. Adafruit preferrably a Diecimila (or whatever the latest version $35 is) but NG is OK too . When the Arduino is waved in the air. we see streaks of light from the blinks. schematics. and learned a bit of the language used to talk to the Arduino.Breadboards. in a dark room. and played a bit with the onboard LED (or if you have an NG. you have finished the second lesson! In this lesson you examined the Blink sketch in detail. Modify the code so that the light is on for 10 ms and off for 10 ms. We will do this by adding a solderless breadboard to our setup. but its blinking so fast that our eyes can't pick it up. so it looks like a blur. Get your gear Assembled Arduino board. Wrap up Congrats. The next step is to start adding onto the hardware component of the Arduino. z What do you think is happening here? Highlight the text below to see the answer The LED is blinking.

$5 LEDs For this lesson. Any supply store length is OK. Standard A-B computer cable is required.50 . Make sure you get a Any "5mm" or "3mm" LED. electronics with two legs. a red. $1 Arduino Prototyping Shield with tiny breadboard Adafruit $15 + Adafruit $7. green and blue LED are best. "Ultrabright" LEDs (1000 mcd rating or higher) are preferred Any electronics Three 1KΩ Resistors supply store (brown black red gold) Any values from 300 Ω to 2KΩ are probably OK.Adafruit Or any USB Cable. as shown in supply store the example image.

a chunk of plastic with a bunch of holes. . They allow you to make quick circuits. black and some other color. The metal strips are springy so that when you poke a wire into the hole.. inside the breadboard are many strips of metal that connect the rows and columns together. Make sure its not stranded wire! Say hello to the solderless breadboard! Solderless breadboards are an important tool in your quest for electronics mastery. the clips grab onto it. They often look like this Any hardware store Basically. They're also inexpensive and reusable.Adafruit Standard solderless breadboard If you dont have a protoshield. test out ideas before making a more permanent Printed Circuit Board. something special is going on inside the breadboard! Although you can't see it. this is a substitute $5 Many hobby shops and electronics stores will have these Hookup Wire Get 22 gauge solid-core wire in red. You can pick on up at any hobby shop or electronics supply store. However.

solderless breadboards can be very flakey. Heres how to do it with just diagonal cutters. it could be that the little metal clips on the inside aren't working well. using wire cutters . If you're having problems with your circuit.. it has only 17 row pairs and no rails. For example the breadboard on the left has 30 row pairs and 2 sets of double rails on either side. There are short ones that connect 5 row holes at a time. we will show pictures of both the tiny breadboard on a protoshield and also using a 'standard' breadboard without a shield. especially as they age. after this lesson.Cut the wire first. a real wirestripper tool to remove the insulation. OK? Warning! Distressing as it may sound. Try poking it with your finger. Breadboards are almost always made so that they have two sets of 5-hole rows and on either side there are a pair of rails.. and then there are very long ones that connect 25 (or more!) column holes at a time. The one on the right is quite small. You can use a fingernail or. you'll need jumper wires. best of all. you'll be more on your own to figure out how to connect up the standard breadboard. or moving it to a different section. The long columns are called rails and the short strips are called rows. Say hello to wires! To use the breadboard.In the images above you can see how there are two kinds of metal strips. In this lesson. just takes a few tries and then its really easy. However. These are basically 22 gauge solid-core (not stranded) wires that are cut down and have the insulation pulled off.

.Nick the insulation. then pull it off.

To connect rows together. just stick the wire ends without insulation into the square holes! Now is a good time to practice making jumpers. such as an mp3 player has easily a thousand resistors inside of it! . An electronic gadget. go forth and make a few 3" long jumpers! Say hello to the resistor! The resistor is the most basic and also most common electronic part.

Say hello to the LED! We've had some time with the LED already. but sometimes your local hobby shop will only have 1/2W.. Higher wattage resistors are larger and usually more expensive.Gold • What is the value of this resistor? Highlight the text below to see the answer The stripes are yellow (4) .. well. There are also website calculators that you may find very handy Remember: Just because the stripes are in a certain order doesn't mean the resistor has a direction! Resistors are the same forward and backwards. This is known as the resistor color code. (Or you can use a multimeter to measure the resistance accurately) Click here to view a reference chart that you can print out (in color) and use as your guide. The light-emitting part.0 MΩ). They work either way! Note on Wattages. its not possible to tell the resistance of a resistor just by looking at it.a resistor! Resistors have one job to do.7KΩ • What happens if you put a resistor in backwards? Highlight the text below to see the answer Ha! Trick question.. The LED component turns current into light. Most resistors you'll see range between 1 ohm and 1 megaohm (1.Brown . Eventually you'll get really good at telling the value of a resistor just by glance but to start off you'll want to use a reference chart. Wth a fire hydrant. One common way of thinking about this is if we were talking about water current. By resisting current they control where and how fast it flows. Thin pipes let less water through (high resistance). We've used the LED to make a blinking light in lessons 1 and 2. that makes sense. we use 1/4W resistors. you want low resistance. Resistance is measured in ohms.Red .. That's why they're called resistors. With a water fountain.red (* 100) = 4700 Ω = 4.Red . it is not possible to put a resistor in 'backwards'. If you mixed up the two pipe sizes.0KΩ resistor? Highlight the text below to see the answer Brown .. you'd want high resistance.Gold • What is the color code for a 5% 220Ω resistor? Highlight the text below to see the answer Red .Black . The bigger the resistance value (in ohms) the more it fights. thick pipes let a lot of water through (low resistance). Quick quiz! • What is the color code for a 5% 1. In all these examples. and that is to resist the flow of electricity (otherwise known as current). Unless otherwise noted you can use 1/16 W or 1/2W or whatever you can get your hands on. then pipes are like resistors. but lets get to know her a little better. often written as the symbol Ω. But what is this mysterious diode? . much like any sort of light bulb.Behold.violet (7) . The word LED stands for Light Emitting Diode. it doesnt matter which way they are used. and its a real pain when you first start electronics. Since the resistive element is inside a ceramic casing. You'll have to read it by looking at the colored stripes on the body of the resistor. you wouldnt be able to put out a fire and you'd hurt yourself while trying to get a drink.

Look again! Its a tiny LED As we mentioned before. Imagine such a one-way street with a traffic policeman in front. he will not let you. Arduino NG users had to make sure that they inserted the LED in the right way. If you recall from lesson 1. the chunk of metal is larger on the negaive side. If you want to turn onto the street the wrong way. If you place the LED in backwards it won't work. Diecimila Arduino users already have the LED (a very very small one) soldered onto the circuit board the right way. The positive leg is slightly longer and if you look inside. its easy to figure out which side of an LED is positive and which one is negative. Likewise the diode simply does not let current go through it the wrong way. Current in a diode can only flow from the positive side to the negative side. .A diode is basically a one-way street for current.

so it doesnt matter which way it goes in.0K ohm is a good place to start.Light up my breadboard We're going to now use the breadboard to light up an LED. If you have a protoshield. you'll need to use wires to reach the Arduino. Run one wire (red) to the 5V socket on the Arduino. an LED and a 1. Place the resistor and LED as shown. The colors aren't essential but they will help you remember what the wires are connected to! . Run the other wire (black) to one of the GND sockets on the Arduino. make sure its assembled first. The resistor doesn't have a direction. Make sure the longer leg of the LED is to the right. You will need a breadboard. If you have a regular breadboard you'll need 2 jumper wires as well. We'll cover how to figure out the best resistor value later on. the LED will survive the experience! However. Then. You can remove the backing to stick it on (which is permanent) or you can just use double-sided tape. Important Note! While LEDs will not work when placed backwards. place the tiny breadboard on top.0K ohm resistor (brown black red gold). connected to the resistor. Click for a high resolution photo if necessary! If you're using a standard breadboard. using an LED without a series resistor is a sure-fire way to kill it! (You'll see a bright flash and it may turn dark) Always use a resistor! A 1. you don't have to worry about whether it will be damaged: as long as there is a series resistor of at least 100 ohms next to it.

a Roland TB-303 synthesizer clone Each electronic component has a schematic symbol. • Is the red wire going into the hole labeled 5V? Is the black wire going into one of the holes labeled GND? • Try another LED in case this one is damaged • Make sure the parts are as shown in the image above. The positive and negative battey terminals are often called the power supply. check the following: • Is the Arduino plugged in? (look for the little green light on the Arduino as in lesson 0) • Is the LED in backwards? Try flipping it around. • Are the parts firmly placed in the breadboard? Loose parts are a common breadboard problem. as they supply power to our circuit.one way you could do that is to take a good photo. Lets say you want to "save" this design and send it to a friend to check out and build for herself. • Is the LED on and its just very dim? Try turning down the lights or looking at it head on: some LEDs are very directional. if you have a wire in one row and the resistor in the other. then the negative pin is connected to a resistor which goes to the negative terminal of the battery. Being able to read and write schematics is a key skill! Here is a schematic for a really big project. they aren't connected and it wont work! Scheming schematic Hooray. Basically you've connected the LED and resistor in series (one after the other) to a 5V 'battery'. The battery is supplying the current that flows through the LED. For resistors the symbol looks like this: . If not.. The positive terminal is called power (as thats where current flows from) and the negative terminal is called ground. But a better way is to draw a wiring diagram. A wiring diagram is also known as a schematic. Then it wouldn't matter if your camera wasn't very good.Plug in the Arduino. try jiggling them with a finger and see if it starts working. which is a simplified drawing of the part. The positive pin of the LED is connected to the positive terminal of the battery. This wont damage the LED. you should see the LED light up. you just built your first circuit! Its quite simple but still worth explaining. making it light up. Schematics are the standard method for people to trade information about circuits. just in case. as it is where current flows to..

positive pin on the left. The LED symbol. we often write important information like what the resistor value is.Resistor symbol And the symbol for LED's look like this: LED symbol. This is the direction in which current flows. . has an arrow thing going on. what color and size the LED should be. however. negative pin on the right You can see that the resistor symbol is symmetric. Power and ground also have symbols: Power and Ground symbols The only thing we need to do now is indicate how the LED and resistor are hooked up and show the 5V and ground connections. The little arrows that are coming out of the symbol indicate that this is a diode that emits light. A barebones schematic Next to symbols. and the voltage associated with the power supply. just like resistors themselves.

make a guess of what will happen: Will the LED stay lit? Will the LED go out? Something else? Now make the change to your breadboard: .. we'll connect it to ground.A well documented schematic! For practice.. We're going to make a very small modification to our wired up circuit In our new schematic. try drawing your own schematic on a piece of paper. Before you change your breadboard. instead of connecting the resistor to +5V power. A Quick Rewiring.

You will notice that.. Re-visiting an old friend Start up the Arduino software again and open up the MyBlink sketch from lesson 2. the LED has gone out.. you could make the LED blink! Hmm. you can turn the LED on and off. If you were very fast at it. By connecting the resistor to +5V or ground. . Upload the sketch to your Arduino. If you left it with delay times of 10ms. Now change your breadboard wiring so that it matches this schematic. That is because it is no longer connected to a power source and current is not flowing. in fact.. you may want to modify it so its back to 500ms on and 500ms off.

instead of connecting the resistor to 5V or ground. if its still in the socket.That is. connect it to the Arduino pin socket labeled 13. If you have an NG Arduino. . you'll need to remove the old LED you used.

delay(500). both the on-board LED and the wired LED will blink in unison. A new pin Now change the wiring so that the resistor is connected up to pin socket #12 . If you have a Diecimila Arduino. LOW). } // run over and over again // sets // waits // sets // waits the for the for LED on a second LED off a second We didn't quite explain what digitalWrite does. HIGH). Lets look at that code again void loop() { digitalWrite(ledPin. digitalWrite(ledPin. but now it should be clear: the digitalWrite procedure connects the pin indicated by the first input (ledPin) to either the +5V power supply or to ground depending on the second input (HIGH or LOW) This is a pretty awesome capability and is the basis of all electronics! You may want to think about how cool it is for a few moments. delay(500).You should see the LED turn on and off.

now! // LED connected to digital pin 12 Re-compile and verify the sketch. then send it over the the Arduino.The LED isn't be blinking anymore! Lets fix it! Go back to the beginning of the sketch and find this line again int ledPin = 13. Note that if you have a Diecimila Arduino you will not see any blinking on the onboard LED. // LED connected to digital pin 13 This is the line of code that indicates which pin is connected to the LED. Thats because its connected to pin 13 only! Exercises! . Change it so that it is now connected to pin 12 int ledPin = 12. The LED should now be blinking again.

. • Change around your wiring so that it matches this schematic: Make sure to modify you sketch so that the ledPin is 13 again. and modify the sketch so that the LED blinks. green and blue LED.. You can't tell which one is which until they are lit so just build the circuit and then rearrange them if needed.• Spend some time experimenting with different pins. It's time to go full color! Find a red. just like before. Adding a green LED.. What does the LED do? Highlight the text below to see the answer It blinks just like before If you have a Diecimila Arduino. When the pin is HIGH (connected to +5V) the onboard LED is on. Re-compile and upload it to the Arduino. Connect the LED to different pin sockets. If you have the Arduino Starter Pack they will be the three clear LEDs. OK sure you've had plenty of practice messing around with LEDs. what do you notice about the breadboard LED and the on-board LED? Highlight the text below to see the answer They are alternating when they blink Why do you think that is? Highlight the text below to see the answer When the pin is LOW (connected to ground) the breadboard LED is on: current is flowing from +5V to ground through the pin.

Go back to your sketch and change it so it looks like this: int redPin = 12. #11 and #12. 12 int greenPin = 11. green and blue LED schematic In this schematic we will have three LEDs connected to three different pins: #10. pin 11 // Red LED connected to digital pin // Green LED connected to digital .Red.

LOW). delay(500). HIGH). pinMode(greenPin.void setup() { pinMode(redPin. digitalWrite(greenPin. double check: • • • • • Is the sketch compiling properly? Did it upload correctly? Are the LEDs in the right way? Are the resistors in the right sockets? Are the LEDs connected to ground on the other side? Is the breadboard wired up right? Check your connections. • Change the loop() procedure code so that both LEDs are on for 500 ms. digitalWrite(greenPin. when the sketch starts // sets the digital pin as output // sets the digital pin as output // run over and over again // // // // // // sets the Red LED on sets the Green LED on waits for half a second sets the Red LED off sets the Green LED off waits for half a second You can just copy and paste this text into your Arduino software window. LOW). Quick quiz What does this sketch do? Compile and upload the sketch to test your hypothesis. digitalWrite(redPin. } void loop() { digitalWrite(redPin. digitalWrite(redPin. delay(500). HIGH). } // run once. then only the red LED is on for 500 ms. // run over and over again // // // // // // // sets the Red LED on sets the Green LED on waits for half a second sets the Red LED on sets the Green LED off waits for half a second sets the Red LED off . LOW). HIGH). OUTPUT). OUTPUT).Highlight the text below to see one possible solution Change the second digitalWrite() procedure call to set the pin LOW. Highlight the text below to see the answer It blinks the two LEDs connected to pins 11 and 12 at the same time Click To Play play_blip_movie_392145(). digitalWrite(greenPin.If you are having problems getting this sketch to work. Exercises! • Change the code so that the LEDs alternate their blinks: Click To Play play_blip_movie_392150(). and the fourth call to set the pin HIGH. delay(500). digitalWrite(redPin. HIGH). HIGH). and finally only the green LED is on for 500 ms Highlight the text below to see one answer void loop() { digitalWrite(redPin. digitalWrite(greenPin. LOW). delay(500). then both LEDs are off.

Now you will add the code for the Blue LED by yourself Step 1. digitalWrite(greenPin. HIGH).digitalWrite(greenPin. pin 11 void setup() { pinMode(redPin. the one from the last step int redPin = 12. HIGH). Add the line of code that will tell the Arduino that bluePin is a digital output. LOW). when the sketch starts // sets the digital pin as output // sets the digital pin as output // run over and over again // // // // // // sets the Red LED on sets the Green LED on waits for half a second sets the Red LED off sets the Green LED off waits for half a second You can just copy and paste this text into your Arduino software window. delay(500). Does it work? If not use your debugging skills to figure out what is wrong and fix it! Color mixing Now that you have red. } // Red LED connected to digital pin // Green LED connected to digital // run once. Step 2. OUTPUT). HIGH). What pin should it be assigned? Examine the schematic above to find out. you can start having fun with color mixing. LOW). Go back to this sketch. } void loop() { digitalWrite(redPin. 12 int greenPin = 11. digitalWrite(redPin. OUTPUT). delay(500). Step 3. LOW). delay(500). digitalWrite(greenPin. green and blue light. delay(500). digitalWrite(redPin. LOW). Add the line of code that will create a variable called bluePin. digitalWrite(greenPin. pinMode(greenPin. Color mixing is the neat ability that our eyes have to combine different light colors and create a new color . } // // // // // sets the Green LED off waits for half a second sets the Red LED off sets the Green LED on waits for half a second Full color adventures! After successfully adding support for the green LED its time to add in the blue LED. Add the 2 lines of code so that the blue LED will be lit when the red and green LEDs are lit Compile and verify your code.

turquioise. if we have both red and blue light mixed together we should get a violet light. Just make a paper box and cut a hole in it. in a light box. You are going to make a color changing light! Modify the sketch so that the emitted light goes in order: red. scissors and some tape. Turquoise (blue & green) and yellow (green & red) Now you are ready for the "final project" of this very long lesson. The tissue acts as a diffuser. You can make a light box out of plain paper. green. helping the light mix nicely . It should pause about half a second between each color change. yellow. Hint: One way to make the color mixing work better is to diffuse the light. Quick quiz Modify your code to create the following colored light: Violet (red & blue). blue violet and back to red. Fill the box with tissue paper.A additive (light) color mixing diagram According to this diagram.

so simply remove it (keeping the mood light LEDs on it you'd like). Now you're ready to learn how to speak! In this lesson we'll learn how to use the Serial Library to communicate from the Arduino board back to the computer over the USB port. Arduino.Click To Play Lesson 4: The serial library and binary data . For this lesson we won't be using the shield.getting chatty with Arduino and crunching numbers Introduction Ah. I remember when you were just crawling around and blinking LEDs. we can examine the RX and TX LEDs on the main Arduino board which will help you with debugging What is a Library? Libraries are great places. We'll use the shield again but for now. it is just our way of connecing up the LEDs and resistors. The shield doesn't contain any programs or data. Then we'll learn how to manipulate numbers and data. and not yet illegal in the United States! If you ever need to learn how to .

but stabs many people one after the other. The word serial means "one after the other. The library we will be using is the Serial Library. its only a few feet. One side sets the pin and the other reads it. setting a pin high or low. but its actually quite different. . you may want to find a Motor Control Library: a collection of procedures that have already been written for you that you can use without having to do the dirty work of learning the nuances of motors. We already studied what a procedure is. you can go to your local library and take out a book. instead of a long cable. say. one right after the other. a serial killer doesn't stop with one murder. essentially. keeping your house uncluttered. In this case. Sure you could buy the book but the library is nice because as a resource you can get the book whenever you need it." For example. which allows the Arduino to send data back to the computer: What is Serial? Serial may sound like a tasty breakfast food. in lesson 3: a procedure is a list of things to do. Software Libraries are very similar. Information is passed back & forth between the computer and Arduino by. where you can use dits and dahs to send messages by telegram. want to control a motor. A library is a big collection of procedures. It's a little like Morse code.do something. like say fix a motorcycle. Just like we used that technique to turn an LED on and off. we can also send data. Serial data transfer is when we transfer data one bit at a time. where all the procedures are related! If you.

a 1. It's run by little ones and zeroes.This is as good as Microsoft Visio can do." "feet. You can group bits together into 8 bits which is 1 byte. yay! (Now. meters and kilometers). . Information has its own system of measurements: A single bit is either a zero or a one. people who are all geeked-out will probably get angry at this point because I'm simplifying things. not a OSI Physical Network Architecture tutorial. its an Arduino tutorial. 1024 bytes (8192 bits) is one Kilobyte (sometimes written KB).) Bits & Bytes The world isn't run by weapons anymore. nearly all computer systems consider 1024 bytes to be a kilobyte." and "miles" (or centimeters.Sneakers Now is a good time to review how data is measured. 1024 KB (1048576 bytes) is one Megabyte (MB) 1024 MB is 1 Gigabyte (GB) An interesting thing to note is while 1000 grams is a kilogram. little bits of data. Well guess what. or energy. how many bytes is that? Use a calculator with lots of digits! Highlight the text below for the answer .0 Kilobyte file on your computer is 1024 bytes: Quick quiz! If your hard disk is 200 Gigabytes. we measure weight with "ounces" and "pounds" (or grams and kilograms) and distances with "inches. That is. or money. It's all just electrons. For example.

you'll notice that they define GB as being 1000 MB. they'll blink when data is being transmitted. Our first sketch is going to be the hello world! program. it will say "hello world!" Create a New Sketch.6% less than you'd expect A familiar friend? We've actually used the Serial communications capability already quite a bit..200 GB * 1024 = 204800 MB 204800 MB * 1024 = 209715200 KB 209715200 KB * 1024 = 214748364800 bytes! Hard drive makers are quite sneaky. its time to get the Arduino talking. One blinks when the Arduino is receiving data (RX) and one blinks when the Arduino is transmitting data (TX) Time for our first sketch Enough chatting amongst ourselves. look carefully at the two LEDs near the USB connector... and save it as HelloWorld . Given this fact. how many bytes can you really store in your 200GB drive? Highlight the text below for the answer 200 GB * 1000 = 200000 MB 200000 MB * 1000 = 200000000 KB How much less storage do you get thanks to the marketing guy who came up with this trick? Highlight the text below for the answer About 4. Next time you upload a sketch. When it starts up.that's how we send sketches to the Arduino! When you Compile/Verify what you're really doing is turning the sketch into binary data (ones and zeros).. etc. the bits are shoved out one at a time through the USB cable to the Arduino where they are stored in the main chip. When you Upload it to the Arduino. and 1 MB = 1000 KB.

then save it /* * Hello World! * * This is the Hello World! for Arduino. That way it knows you really mean to do nothing. it means that the procedure is in the 'default' collection of procedures we . copy and paste the following source code.begin(9600). The library is called Serial and inside the library is a procedure called begin.begin(9600). Even if we have nothing in the setup or loop procedures. If there's no library name. library name Serial . procedure (input values .. This is a library procedure call. the Arduino requires them to be there. * It shows how to send data to the computer */ void setup() { Serial. // run once. name ) begin (9600) . // set up Serial library at 9600 bps We definately see that there is a Serial thing going on.println("Hello world!").Into the new sketch. and it looks like there is a procedure call as well. break } void loop() { } // run over and over again // do nothing! OK first thing to notice is that there's nothing in the loop procedure! We've gutted it. as opposed to forgetting to include them! The first line of code in the setup procedure is this one: Serial. when the sketch starts // set up Serial library at 9600 bps // prints hello with ending line Serial. .and put some stuff into the setup procedure..

Quick quiz! • If the Arduino transfers data at 9600 bits per second and you're sending 12 bytes of data. the line of text we would like it to print. how long does it take to send over this information? Highlight the text below for the answer 12 bytes of data equals 12 * 8 = 96 bits of data. break // prints hello with ending line This line also uses the Serial library.println("Hello world!"). Serial. Lets move on to the next line.begin sets up the Arduino with the transfer rate we want. in this case 9600 bits per second. If we can transfer 9600 bits per second. seeing serial data requires a monitor. say 350 kbps download rate. But what's the 9600 about? The comment says 9600 bps. and just so you know bps stands for bits-per-second (we will refer to this as the baud rate) If you have broadband connection... This is how fast the connection can read and write bits on the wire. Somewhat disappointing since we had so much fun with blinking colored lights before.. So there's some mystery procedure that's called begin. the designers of the Arduino software didn't bother putting it into a library. now compile the sketch and upload it to your Arduino. how long does it take to send over this information? Highlight the text below for the answer This is twice as fast as before.. you may remember reading somewhere that it has. then 96 bits takes 1/100th of a second! • If the Arduino transfers data at 19200 bits per second (19200 baud) and you're sending 12 bytes of data.use.nothing??? It looks like not much is going on here. Good. delay() is so common. And then. It's the procedure that gets the Serial stuff ready. We use two "'s (double quotes) to indicate the beginning and end of a line of text. there's a serial monitor built into the Arduino software! . Lucky for us. For example. The trick here is that while you can see blinking lights quite easily. your broadband connection can transfer data a lot faster than an Arduino!) OK so Serial. so it will take half the time. Note that the 6th letter in println is the letter L not the number 1. which like your display monitor will show us what data is being transfered. well it's not too tough to figure out what it might do. this time it's calling a procedure called println which is just a shorthand for "print line".. This time the input is a quotation. (Needless to say. about 1/200th of a second.

If you have an NG.click it! Hello. try pressing the reset button a few times to make more Hello Worlds! appear. The sketch will start up a couple of seconds later Otherwise.world? What happens next is. the Arduino does not reset itself. make sure that you have the correct baud rate selected in the drop down menu of the Serial Monitor. sadly. press the reset button. Next.. Note that this communication baud rate is indepedent of the upload process... Arduino resets. Arduino resets. . Arduino resets. starts the sketch a few seconds later Mac OS X Arduino does not reset. Voila! It is our sketch! Baud rate match up! If you ever find that you're getting a whole lot of gibberish instead of proper text. the serial monitor will auto-reset the Arduino. If you have an NG Arduino you'll have to wait 7 seconds for the sketch to start.I'm not quite sure what the icon means.. but regardless if you click that button you will replace the black Program Notification area with a Serial Monitor. starts the sketch a few seconds later Diecimila In the very common case of having a Diecimila Arduino. starts the sketch a few seconds later Linux Arduino does not reset. Either way. quite dependent on which kind of Arduino you have Windows NG Arduino does not reset. So. once you've switched to the serial monitor. which is fixed at 19200 bps. this may be a bit annoying but do it anyways.

That's your indication that data was sent. If you look closely at the Arduino. statement from the setup procedure to the loop procedure.println("Hello world!"). The Send button (and the text input next to it) are used to send data to the Arduino.Each time you reset the Arduino. We aren't going to be using it in this lesson so don't be surprised that it doesn't do anything when you click it! 10 PRINT HELLO 20 GOTO 10 Our next sketch will be a minor modification of this one. Quick quiz! • What simple modification should we perform to make the Arduino print Hello World over and over again? Highlight the text below for the answer Simply move the Serial. we'd like it to print it out over and over and over again. Instead of printing out Hello World just once. and prints out Hello again. not blinking • Try waving the Arduino around in a dark room. what do you see? . Then start up the serial monitor. You will see Hello World! scroll by super fast! Quick quiz! • Whats going on with the TX LED? Highlight the text below for the answer It's lit. you will also see the little TX LED blink just as it prints out this message. What's Send do? When you println you are sending data from the Arduino to the computer. it performs the setup procedure. Perform this modification and then compile and upload the new hyper-hello sketch.

Serial.println("Here is some math: "). when the sketch starts // set up Serial library at 9600 bps Serial. /* * Hello World! * * This is the Hello World! for Arduino.println(c). Serial. Serial.print("c = ").begin(9600).print("b = "). Serial. // run once.Highlight the text below for the answer There are little dotted light trails • What's going on here? Hint: Remember lesson 2? Highlight the text below for the answer The data is being transmitted so fast. int b = 10. but it turns out we can also print out numbers pretty easily too. that we can't see the TX LED blinking. Serial.println(a). so that it only prints out Hello World once a second. void setup() { Serial.println(a + b). Serial.begin(9600). Serial. when the sketch starts // set up Serial library at 9600 bps void loop() // run over and over again { Serial. } Now you should spend some time playing with println and making it display a message of your choice! Perhaps add some more println statements in order to make the message longer? Math is hard. let's try programming! We've played around with printing out phrases.print("a = ").println("Hello world!"). int c = 20. // prints hello with ending line break delay(1000). /* * Math is fun! */ int a = 5.. * It shows how to send data to the computer */ void setup() { Serial. // add .print("a + b = ")..it's sending data many times a second! Make the Arduino chill out a little by adding a one second delay to the sketch. } // run once. Serial.println(b).

We've seen that if you use a quoted line of text as input to println procedure. are whole numbers. just ignore the cout stuff which is C++'s way of printing out values) • A C tutorial on operators • A list of all the math operators . Serial.println(a * c). the Arduino looks at what the input to println is. // multiply // divide // subtract } void loop() its empty { } // we need this to be here even though Try out this sketch on your Arduino Note that we're using 2 procedures here.println(b . but many people have written good tutorials on this topic already so I'm going to send you off to read them there! • A C++ tutorial (this one's pretty nice. lets look at this line: Serial. Here's whats going on in the Arduino with this sketch.println(c / b). The print procedure is just like println except it does not print out a "carriage return" at the end. it will display that text.5. In this case you can see that if you use a variable to println it will look up what that variable contains and print that out! It turns out that the Arduino is smart enough to also do math when asked: Serial. It looks up what a is (5) and what b is (10) and then adds them together (+) and then uses that as the value to send to println Note that for now. I could go on and on about operators.print("b .println(a). Serial.Serial.c = "). You can experiment with changing the print's to println's and looking at the Serial Monitor output to verify this for yourself. Serial. Serial. For example. and finds its actually a calculation.print("c / b = ").println(a + b). the original println and now also print. which if you recall. starting a new line. In this case.print("a * c = ").c).14 or 1. Serial. we can only do math using integers. its all very important stuff. That means we can't yet print out numbers like 3.

h" int a = 3. if you have a right-triangle. which are in a library that requires us to include the file math. void setup() { Serial. If you remember from grade school.println(b).h" // include the Math Library header Which basically says "We'd like to use the math procedures.begin(9600).print("h = "). Serial. } void loop() empty { } // we need this to be here even though its The first thing that's new here is this line at the very beginning of the sketch: #include "math.println("Lets calculate a hypoteneuse").print("a = "). Just ignore it for now. Serial.println(h). . the hypoteneuse h can be calculated from the lengths of the two legs. // run once. The second thing that's different here is that when we create the variable h we don't assign it a value. it's not important. to calculate a hypoteneuse. c1 and c2 (which we'll call a & b) a2 + b 2 = h 2 h = √(a2 + b2) /* * Math is fun! */ #include "math. int h. Serial.h where the sqrt procedure lives". Serial.Pythagorean party Let's make our first simple calculator. Serial.print("b = "). int b = 4. when the sketch starts // set up Serial library at 9600 bps // include the Math Library Serial. Serial.println(a). h = sqrt( a*a + b*b ).

i for current and r for resistance. First take the square root of foo. and we'll study it in depth more later.print(drive_gb). You'd like to find the square root of the square root of this number. calling a procedure on the return value of another procedure. Default values If you don't assign a value to a variable."). Serial.begin(9600). Serial. then we call the sqrt() procedure (which does exactly what you may think). when the sketch starts // set up Serial library at 9600 bps } . we assign it the value. Serial.println( sqrt( sqrt(foo) ).print("Your HD is "). Variable = Value . it just means that we don't know what h is going to store yet. Whatever was in h before is lost. // run once. We'll start with the hard drive size in GB. Quick quiz! • Lets say you have a variable "foo" which contains a number. Make sure you don't try to use the variable before you assign it a value! Later on in the sketch. You'll create a Ohm's law calculator. the Arduino just creates the box. Then we assign that to the variable h. have it print out the amount of voltage that can be measured accross the resistor. You can nest procedures and functions all you want. It turns out that this is totally OK. the stuff inside is whatever was in left over in memory.println(" GB large. Drive size calculator Let's write a program that will do that hard drive size calculation we did before. We'll start simple. h = sqrt( a*a + b*b ). we square a and b and then add them together. Since it's not assigned to a value upon creation. to take the square root.) Starting with two variables. Ohm's law says that Voltage = Current * Resistance. it could be any value. then take the square root of that and then use that value as the input to println Now its your turn to create a calculator. What line of code would print out this value? Highlight the text below for the answer Serial. (This is a pretty useful law which forms the basis of electronics. void setup() { Serial. In this line. because we're going to calculate it later. just printing out the drive size in GB /* * Drive size calculator! */ int drive_gb = 5. replaced by the new value.int h. and print out how many MB there are.

"). void setup() { Serial. Serial. lets add a section of code that will print out the number of Megabytes in the hard drive. drive_mb = 1024 * drive_gb. Serial. Serial.void loop() empty { } // we need this to be here even though its Copy and paste this sketch into Arduino software and name the sketch DriveCalc. when the sketch starts // set up Serial library at 9600 bps This time if you compile and upload it you should get .print("Your HD is ").print("It can store ").println(" GB large. Then compile and upload it. Serial.begin(9600).print(drive_gb). } void loop() empty { } // we need this to be here even though its // run once. Serial. OK.print(drive_mb). Serial.println(" Megabytes!"). /* * Drive size calculator! */ int drive_gb = 5. int drive_mb.

" OK let's fix it! All we need to do is change the variable type so that it can store more than 2 bytes of data. If you travel 10000 miles. You may notice that if you put in a 100GB drive size. It turns out that an int type can store only 2 bytes. very strange happens: A 100GB drive should have 102400MB in it. The odometer has only 4 digits. something very. So. Type byte int long /* * Drive size calculator! */ int drive_gb = 100. it can display 0 miles to 9999 miles travelled. long drive_mb. in this case the type is int. Think about your car odometer. we see that "rollover. 10000. "long" // we changed the type from "int" to Size (bits) 8 16 32 Size (bytes) 1 2 4 It looks like we want to use the long type. Then we subtract 1 because like in the car odometer.1 = 65535. the odometer will "roll over" to 0 again. Here is a short list of types we can use. Since the number we're trying to store (102400) is larger than that. not some negative number. remember in lesson 2 we said that when we define a variable we also define the box-type of the variable? The box is where we store the data. from 1 GB to 100 GB. Keeping that in mind. So lets make that change . 0 or 1). and from then on it will display an incorrect value. What's going on here? Introduction to types (part 1) What's happening is that we have an overflow problem. yay! Now try a few different whole numbers for the drive size. Quick quiz! How many bits are in 2 bytes? Highlight the text below for the answer There are 8 bits in 1 byte so 2 bytes is 16 bits To figure out how big a number we can store in a 2 byte-sized box use a calculator and take 2 to the power of the number of bits (since each bit can store 2 values.Which is correct. you can't actually display the final value. in this case the largest number is 216 .

then run the Serial Monitor.print(drive_gb). when the sketch starts // set up Serial library at 9600 bps } void loop() empty { } // we need this to be here even though its Compile and upload this sketch. we did the right thing and it still didn't work.begin(9600)... Serial. drive_mb = 1024 * drive_gb. we're not handling them well. Serial. except now its happening as we do the calculation.void setup() { Serial. // run once. So basically we are still getting an overflow.print(drive_gb). Then we multiply 100 by 1024 to get 102400 and put that in the drive_mb box. // run once. The problem we have now is although the boxes are the right size.print("It can store "). Serial. Uh oh.print(drive_mb).println(" GB large. Serial. If you look at this line. Serial.begin(9600).. Serial. void setup() { Serial..println(" Megabytes!").print("Your HD is "). what's happening here is that the Arduino looks up the value of the variable drive_gb to get 100. long drive_mb."). Except the way that the Arduino software does this is that it creates a temporary variable the same size as drive_gb to store that calculation result before it sticks it into drive_mb.. drive_mb = 1024 * drive_gb. when the sketch starts // set up Serial library at 9600 bps .. Serial. Serial. Here is one way to fix this insiduous bug: /* * Drive size calculator! */ int drive_gb = 100.print("Your HD is "). we didn't actually fix the problem! How frustrating.

By default.println(" GB large. Signed variables can have a positive or negative value. Serial. so you can store negative numbers. the temporary result is stored in a box the same size as drive_mb (a long) instead of an int. Unsigned variables can only store positive numbers."). signed and unsigned. Serial. Serial. drive_mb = drive_mb * 1024. now its working! Introduction to types. Compile and upload this sketch to try it out There you go.println(" Megabytes!").print(drive_mb).print("It can store "). variables are signed. We assumed before that these values were 0 . But then how do we store negative numbers? It turns out that there are two kinds of variables. and that has to do with storing negative numbers. // we need this to be here even though its } void loop() empty { } Now when we do the calculation. There's another important fact to know. Size (bits ) 8 8 16 16 Size (bytes) 1 1 2 2 Minimum Value 0 -128 0 -32768 Maximum Value 255 127 65535 32767 Type unsigned byte byte unsigned int int . part 2 It turns out that I wasn't completely honest in the previous section when I described all the different types.65535 inclusive. drive_mb = drive_gb.Serial. We know that a variable that is 2 bytes large (16 bits) can store 216 different values.

print("Now it is "). However. whats a good data type to use? Highlight the text below for the answer You probably want to use a byte type • Lets say you want to store the age of a human in seconds. This sketch will test out this fact: int test = 32767. You'll need to store this in an unsigned long variable. with gigabytes of memory (RAM).print("Test value is: "). Serial.println(test). you'll actually rollover to -32768. For example.begin(9600). every byte counts! What's weird about signed numbers is that if you reach the end of the positive value range you'll rollver into the negative values.println(test). why bother with different size types? Lets just have every variable be as big as possible and we'll never have roll-over problems. // run once. // we need this to be here even though its } void loop() empty { } . you'll run out of memory really fast and then you'll have major problems. lets say you have a signed int. test = test + 1. Serial. So in this case. If you have the value 32767 in that variable. Serial. the tiny tiny computer in the Arduino has a grand total of 1 Kilobyte of memory. And some of that is used for background stuff you don't see. this is a reasonable thing to do. Why Types? OK so you're probably wondering: "This is such a pain.unsigned long long Quick quiz! 32 32 4 4 0 -2147483648 4294967295 2147483647 • Lets say you have a program that stores the age of a human in years (which so far is no more than 122)." Well. and you add 1 to the variable. when the sketch starts // set up Serial library at 9600 bps Serial. but if you have a bigger sketch. For small sketches sure you can make everything a long and be done with it. what is an appropriate data type now? Highlight the text below for the answer 110 years = 3468960000 seconds. void setup() { Serial. on your desktop computer.

Compile and upload to run the test.

Quick quiz! • Let's say we have a variable that is byte type, it's signed by default. It starts out with the value 127 and we add one to the variable, what will the new variable value be? Highlight the text below for the answer It's a signed variable so it will roll over to -128 • Let's say now it is an unsigned byte type, what happens now? Highlight the text below for the answer Since it is unsigned, it can store much more data, so it will be able to hold the value 128. • If we have an unsigned byte and it starts out with the value 250 and we add 10, what will the value be? Highlight the text below for the answer Even though thie variable can store a lot, it can't store more than the number 255, so it will rollover and we'll end up with the number 4. Now it's your turn! Write some sketches that will help you understand variable sizes, try creating variables of different types and signedness and adding and subtracting. Although this lesson part seems quite boring, and severely lacking in blinky lights, understanding this stuff now will save you from a lot of headaches later when you have data overflows and your program is all wonky and you're really frustrated because you can't figure out why. (Trust me on this one!) Wrapping up, the final project! Now its time for you to expand the drive size calculator. Starting with the DriveCalc sketch, modify it so that it will also calculate how many KB are stored in the hard drive. Test it out with a couple different drive sizes. Once you've got that working, modify it again so that it will also display how much space the drive actually holds thanks to the sneaky math-trick that manufacturers use. Have the sketch display how much storage space is 'missing' (in KB) as well. Here's one possible solution:
/* * Drive size calculator! */ int drive_gb = 100; long drive_mb; long drive_kb; long real_drive_mb; long real_drive_kb;

void setup() { Serial.begin(9600); Serial.print("Your HD is "); Serial.print(drive_gb); Serial.println(" GB large."); drive_mb = drive_gb; drive_mb = drive_mb * 1024; drive_kb = drive_mb * 1024;

// run once, when the sketch starts // set up Serial library at 9600 bps

Serial.print("In theory, it can store "); Serial.print(drive_mb); Serial.print(" Megabytes, "); Serial.print(drive_kb); Serial.println(" Kilobytes."); real_drive_mb = drive_gb; real_drive_mb = real_drive_mb * 1000; real_drive_kb = real_drive_mb * 1000; Serial.print("But it really only stores "); Serial.print(real_drive_mb); Serial.print(" Megabytes, "); Serial.print(real_drive_kb); Serial.println(" Kilobytes."); Serial.print("You are missing "); Serial.print(drive_kb - real_drive_kb); Serial.println(" Kilobytes!"); // run over and over again

}

void loop() { }

Conclusion Good work, you got through one of the more boring lessons. In this lesson you learned how to print text and data to the Serial Monitor. This is essential for debugging future projects! You also learned about data types and storage and how to use the Arduino to calculate stuff.

Lesson 5: Buttons & switches, digital inputs, pull-up and pull-down resistors, if/if-else statements, debouncing and your first contract product design
Introduction We've done a lot so far, blinking lights, printing messages...all of that stuff is output: signals coming from the Arduino. The next step is to start playing with input, with the Arduino responding to outside events. In this lesson we will begin with the most basic kind of input, a push-button switch! What you'll need

Assembled Arduino board, preferrably a Diecimila (or whatever the latest version is) but NG is OK too

Adafruit $35

Adafruit USB Cable. Standard A-B cable is required. Any length is OK. Or any computer supply store $5

5 Red LEDs The brighter the better

Any electronics supply store $3 Any electronics supply store

6mm tact switch (pushbutton) $0.50 One 100 Ω Resistor (brown black brown gold) Any values from 20 Ω to 220 Ω is probably OK.

Any electronics supply store $1

Five 1KΩ Resistors (brown black red Any electronics gold) supply store Any values from 300 Ω to 2KΩ are probably OK. One 10KΩ Resistors (brown black orange gold) Any value from 5KΩ to 100KΩ is probably OK. $1

Any electronics supply store $1

One kind of switch you use every day is a light switch. When on. black and some other color.50 Hookup Wire Get 22 gauge solid-core wire in red. there's tons of them in your house. On the left. which allows current to flow. A light switch is a simple device with two positions. When off.Adafruit $15 Arduino Prototyping Shield with tiny + Adafruit breadboard $7. the switch is open and no current flows. current flows and the light turns on. on and off. the two wires are disconnected. On the right. two wires are connected inside. the switch is closed. . Make sure its not stranded wire! Any hardware store What's a switch? You're probably familiar with switches.

cost about 25 cents. this switch is just a 2-wire switch. Light switches are great but we need something smaller. The two wires connect to the top and bottom. and can plug directly into a breadboard.(thanks wikipedia!) In this photo. We'll be primarily using 6mm tactile button switches. which may make you think that there are 4 wires that are switched on and off. So really. These little switches are a 1/4" on each side. two on each side are actually connected together inside. . These mechanical devices have 4 legs. you can see the internals of a light switch. but in fact. The flat bar that goes verically down the middle is what is physically moved to connect or disconnect.

Normally. they are mechanically connected. Whats the average number of . you may want to straighten out the legs (just squish them with a pair of pliers) so that they look like the button on the left. Quick Quiz! • Find 5 things around the house that have switches. the two wires are disconnected (normally open) but when you press the little button on top. To get the buttons to sit better in the protoshield.

0KΩ resistor.switches per device? Light switch We're going to make our first test of the pushbutton by having it turn on and off an LED light Fig 5. as well as the tiny pushbutton and build the schematic onto your protoshield: .1 You'll note that the schematic symbol for a pushbutton switch is a little bit different than the one above Get out your red LED and 1.

but its kind of hidden in this photo) Make a guess and then build it and test your guess.Power up the Arduino and try pressing the button. Switch capability Before you try to turn a 100W lightbulb on and off using a pushbutton switch. The little switches are only rated for a few volts and milliAmps. The LED should light up when the button is held down (current is able to flow) and go dark when it's released (current is not able to flow). be aware that switches have ratings that will tell you the maximum amount of current and voltage they can switch. Make sure you choose the right switch for the job or you may accidentally cause a small fire! Quick Quiz! • What does this wiring setup do? (The LED is connected to ground. . Big switches such as wall light switches are rated for 120V and many Amperes.

Turn on the Arduino and run this little sketch /* * Switch test program */ int switchPin = 2. Now we get to do the opposite. as shown by our little light switch demo.98 Newtons (100 gf) DigitalRead Switches are great for controlling current. Use your detective skills to figure out the follwing: • What is the maxiumum amount of current this button can switch? Highlight the text below to see the answer 50 mA • What is the maximum voltage you can use this switch for? Highlight the text below to see the answer 24V • What is the recommended Operating Force (how hard the button is pressed) for the B3F-1000? Highlight the text below to see the answer 0. But they're even better as input devices! In previous lessons we set a pin on the microcontroller (say pin 13) to HIGH (5V) or LOW (ground. // Switch connected to digital pin 2 .Highlight the text below to see the answer The switch is oriented so that the LED is always on! These switches have the part number B3F-1000. There's a lot of information. We will set the voltage on a pin to 5V or ground and then use DigitalRead to inquire whether that pin is HIGH or LOW For our first test. here is a datasheet webpage for the part. 0V) using the DigitalWrite procedure. we will use a wire as our switch. but learning how to navigate these sorts of pages is rather important.

var Serial.print("Read switch input: ").println(var). read switch } // run once.2 . INPUT). var // read the pin and save it into // print out the value stored in Now use a wire to alternate between connecting Pin 2 to 5V and Ground through a 100Ω resistor. var = digitalRead(switchPin). which just takes as an input the pin to examine.println(digitalRead(switchPin)). Sure we could use a variable to hold the result from digitalRead() and then use that variable as input to println() but this is much more succinct. println(). switch // sets the digital pin as input to read We also use the new digitalRead() procedure.println(digitalRead(switchPin)). we read the pin and then pass the result as an input to another procedure. // Read the pin and display the value delay(100). That result is either 0 (LOW) or 1 (HIGH) depending on what it saw when it looked at the pin's voltage. INPUT). } You'll note that we have to tell the Arduino to set the pin as an input. pinMode(switchPin. This is pretty easy. In this case. Serial. and watch the serial monitor. when the sketch starts // set up Serial library at 9600 bps // sets the digital pin as input to void loop() // run over and over again { Serial. the value // Read the pin and display The digitalRead() procedure returns a result when its done. Serial.void setup() { Serial. use pinMode() but use INPUT instead of OUTPUT pinMode(switchPin.begin(9600). Fig 5.

Switch input tied HIGH (5v) Switch input tied LOW (ground) You should see it print out two messages depending on whether a the wire jumper connects the input to HIGH (5V) or LOW (ground) voltage. in digital binary land. HIGH is another word for 1 and LOW is another word for 0. Dont forget. .

to protect the pin from short circuits. This is called a floating input voltage. Floating high above the clouds Of course. it will fry the pin for good and possibly destroy the Arduino microcontroller! Whats this 100Ω resistor all about? There's a 100Ω resistor we use to connect the input pin to either HIGH or LOW voltage. When the . and we'd like to replace that with a mechanical switch.3 Our two alternative switch wiring possibilities For example. In both cases. you've basically caused a short circuit at that pin. connecting and disconnecting a wire is a lot of work. Basically. though. and trying out the switch testing sketch above. lets say you accidentally set P2 to be an OUTPUT type pin. the pin is connected to a valid input voltage. in these schematics we can connect and disconnect pin 2 to 5V. When the button is released. pin 2 is not connected to anything. Fig 5.Valid inputs The best way to completely destroy a microcontroller such as an Arduino is to feed it voltages that are much too high. This isn't very good for the pin and could damage it! The 100Ω resistor acts as a buffer. If you write a LOW to the pin (0V) but its connected to HIGH (5V). as long as the button is held down. Only thing is. it's invalid input! Try building up one of these schematics. Make sure your input voltages are between 0 and 5V! Never connect a 9V battery directly into an input pin. it can't alternate connections. our switch can only connect and disconnect two wires. Why is it there? Well. or we can connect and disconnect pin 2 to ground. but then you connected it to 5V.

or it may change. When its released.button is held down you should definately get the right printout. it may keep the old value. but its certainly not reliable! Wiring when the switch is connected to 5V Wiring when switch is connected to ground One solution is to get a switch that alternates connections. . like this one. diagrammed here.

In fact. .4 The problem is.5 The pull-down resistor here is the 10K resistor. there is no longer a strong pull to 5V. When the switch is held down. its let go completely. the 100Ω resistor is connected directly to 5V. Fig 5. But there is still weak pull to ground. make sure you have the right resistor values and that the parts are connected up properly. Despite being a weak connection.Fig 5. these switches are suprisingly complex and 10 times more expensive than a little tactile button! Instead we use a trick called a pull-down resistor. When the switch is released. However. the 100Ω resistor is connected to the 10K resistor which pulls it down to ground. its a very weak pull to ground and can't compete. Build this circuit and try it out with the switch test sketch. so it provides a strong pull to 5V. but since the 10KΩ resistor has 10000 times more resistance than the button. The 10KΩ resistor is also connecting the 100Ω resistor to ground. Here's how to think of it: When you press the button and connect the 100Ω resistor to 5V. when the switch is disconnected. the button has a very small resistance (less than 1 Ω!). It should be very reliable now! If its not working. it's better than nothing and so the resistor pulls the input pin to LOW. The strong 5V connection overpowers the weak ground connection and the input pin reads HIGH.

Note that the strong and weak connections have nothing to do with whether the switch is configured as a pull-up or pull down.You can also use the switch to connect the input to ground.6 Try this schematic as well. and use a resistor as a pull-up resistor. and verify for yourself that the button is now reliable. Fig 5. . The strength of the connection comes from the fact that the button is very low resistance when held down and that the resistor is much much more resistive to current flow than the button.

and when it's released the value is 0. Going much lower will waste more power. what is the value read by digitalRead() when the button is pressed? Highlight the text below to see the answer The returned value is 1 (HIGH) • With the pull-down resistor configuration. what is the value read by digitalRead() when the button is pressed? Highlight the text below to see the answer The returned value is 0 (LOW) • With the pull-up resistor configuration. Would you use a pull-up or pull-down resistor configuration? Highlight the text below to see the answer You would want to use a pull-down resistor configuration. what is the value read by digitalRead() when the button is released? Highlight the text below to see the answer The returned value is 0 (LOW) • With the pull-up resistor configuration. I suggest going with 10KΩ because they are common resistor values and people reading your schematic will be more likely to understand that its 'just a pullup resistor'. (you can see the whole thing here) .7KΩ. it is pretty much univerally used as the 'default' value. the value read from the pin is 1. and the examples here use a 10KΩ resistor as the pullup or pulldown. what is the value read by digitalRead() when the button is released? Highlight the text below to see the answer The returned value is 1 (HIGH) • Lets say you wanted to design a switch so that when its pressed. most of the time you can use as high as 100KΩ or as low as 4. Here is a small part of the Arduino schematic. Quick Quiz! • With the pull-down resistor configuration. going higher may give you unstable results (due to microcontroller-internals that are not that important right now). Is there something special about 10KΩ? Nope! While.Must a pullup/down resistor be 10KΩ? You'll notice that both the Arduino schematic.

We will make a simple digitallycontrolled light. they are both connected to a pin on the Arduino microcontroller called RESET (in the bottom right corner) • Is this switch connected up with a pull-up or pull-down resistor? What value is the resistor? Highlight the text below to see the answer The resistor is a 10KΩ pull-up • The switch is called S1. What is S1 used for? Highlight the text below to see the answer S1 is the button you press to reset the Arduino • Based on what S1 does and what you've learned about pullup/pulldown resistors. The sketch we want to write does the following When the button is pressed.Fig 5. turn on the LED. Iffy statements The next step is to combine inputs (buttons) and outputs (LEDs). When the button is pressed. the LED turns on Which we can rephrase more specifically as If the button is pressed. and how the RESET pin works Highlight the text below to see the answer Normally the RESET pin is pulled up to 5V. look on your Arduino (you may have to remove the shield to see it) to identify S1. describe what you think this circuitry does. . If the button is not pressed. turn off the LED. The Arduino microntroller resets itself when the RESET pin is connected to ground. the pin is connected to ground. Here is how we will wire up the switch and LED.7 There is a switch and a resistor (Europeans use a rectangle instead of a squiggly for resistors).

int val. int switchPin = 2. /* * Switch and LED test program */ int ledPin = 12. That way you can tell if its the input half that isnt working or the output half.8 Build this schematic on your protoshield Copy and paste this sketch into the Arduino software and upload it to the Arduino. Verify that when the button is pressed.Fig 5. If its not working. the LED turns off. the LED turns on and when the button is released. try using println statements to debug your project: when you press the button have it print out a message. // LED is connected to pin 12 // switch is connected to pin 2 // variable for reading the pin status .

void setup() { pinMode(ledPin, OUTPUT); pinMode(switchPin, INPUT); } void loop(){ val = digitalRead(switchPin); val if (val == LOW) { digitalWrite(ledPin, HIGH); } if (val == HIGH) { pressed digitalWrite(ledPin, LOW); } }

// Set the LED pin as output // Set the switch pin as input

// read input value and store it in // check if the button is pressed // turn LED on // check if the button is not // turn LED off

This sketch introduces a completely new and exciting type of statement, the if statement. This is a logical statement, which you may remember from grade school math class. Basically, until now we've had the Arduino just do stuff: blink LEDs, print out messages, etc. But now we want it to make decisions. if if (test statement) ( val == LOW ) { statements to perform if test is True} { digitalWrite(ledPin, HIGH); }

The if statement is the first statement that is conditional, it only runs the statements if a condition is true. In this case, the conditions are "is the button pressed?" and "is the button not pressed?" Some Conditional Tests... Symbol Definition Usage Example if (foo == 5) { Serial.print("Foo is equal to 5"); } == Equality test Make sure you don't confuse this for the assignment operator = ! if (digitalRead(buttonPin) != LOW) { Serial.print("The button pin is not LOW "); } if ( var2 > 10 ) { Serial.print("Variable #2 is larger than 10"); } if ( chickenstock < 10 ) { Serial.print("We have less than 10 chickens in stock"); }

!=

Inequality test

>

Greater-than test

< <=

Smaller-than test

Smaller-than-or-equal-to if ( 20 <= yearstolive ) { test Serial.print("Good news, you have at least 20 years

left!"); } if ( kitten() >= 6 ) { Serial.print("The kitten() procedure returned a number larger than or equal to 6"); }

>=

Greater-than-or-equal-to test

Quick Quiz! • Modify the sketch so that it does the opposite, when the button is pressed the LED turns off and when it is released it turns on. Remember to change the sketch only, use the same circuitry! Highlight the text below to see the answer Swap the lines digitalWrite(ledPin, HIGH); and digitalWrite(ledPin, LOW); • Modify the sketch so that the LED blinks 5 times a second (100ms on and 100ms off) when the button is pressed and is completely off when the button is released. Highlight the text below to see the answer
int ledPin = 12; int switchPin = 2; int val; // LED is connected to pin 12 // switch is connected to pin 2 // variable for reading the pin status

void setup() { pinMode(ledPin, OUTPUT); // Set the LED pin as output pinMode(switchPin, INPUT); // Set the switch pin as input } void loop(){ val = digitalRead(switchPin); // read input value and store it in val if (val == LOW) { // check if the button is pressed digitalWrite(ledPin, HIGH); // turn LED on delay(100); digitalWrite(ledPin, LOW); // turn LED on delay(100); } }

Note that you don't need to do anything if the switchPin is HIGH because at the end of the "val == LOW" statements the LED has been turned off! Now its your turn: add another red LED and resistor to pin 11, modify the sketch so that when the button is pressed one LED is lit and the other one is off and when the button is released the first LED is off and the second LED is lit.

Fig 5.9 Try to wire up the protoshield just from the schematic. If you're having trouble, click here for a photo of the parts wired up. Here is one possible solution sketch:
/* * Switch and 2 LED test program */ int int int int led1Pin = 12; led2Pin = 11; switchPin = 2; val; // // // // LED #1 is connected to pin 12 LED #2 is connected to pin 11 switch is connected to pin 2 variable for reading the pin status

void setup() { pinMode(led1Pin, OUTPUT); pinMode(led2Pin, OUTPUT); pinMode(switchPin, INPUT); } void loop(){ val = digitalRead(switchPin); val if (val == LOW) { digitalWrite(led1Pin, HIGH); digitalWrite(led2Pin, LOW); } if (val == HIGH) { pressed digitalWrite(led1Pin, LOW); digitalWrite(led2Pin, HIGH); } }

// Set the LED #1 pin as output // Set the LED #2 pin as output // Set the switch pin as input

// read input value and store it in // check if the button is pressed // turn LED #1 on // turn LED #2 off // check if the button is not // turn LED #1 off // turn LED #2 on

Do it...or else! Having an LED turn on or off when a button is pressed is quite impressive, but it would be pretty odd if you had to press a button constantly to keep the TV on. What we want is an alternating action switch, where the press-and-release of a button does something, not just press-and-hold. Basically we want to test whether the button was just released, or just pressed.

println("Button just pressed"). Serial. Since we don't know the state of the button when the Arduino is first turned on. state void setup() { pinMode(switchPin.. } } buttonState = val. in val // read input value and store it if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed Serial. // read the initial state } void loop(){ val = digitalRead(switchPin).. watching the serial monitor as you press and release the button.pressed. to see if its changed. we begin by first checking the button pin state and storing it an temporary variable val.println("Button just released"). if (val != buttonState) { if (val == LOW) { // the button state has changed! // check if the button is pressed . // variable to hold the button state This line isn't too unusual. that's when we want to perform an action. we will leave it as unknown (uninitialized). void loop(){ val = digitalRead(switchPin). In the loop procedure. } else { // the button is -not. we initialize (set the initial/starting value) of the button state variable by reading the button value once we've started up and set the pin to an input. When the state changes (an action occurs). we need to keep track of the button input value. This is called the state of a button. Lets go through the new lines of code: int buttonState.To do this. INPUT). int buttonState. // read the initial state In the setup() procedure. its just a variable that is going to hold the state of the button. variable } // save the new state in our Upload it to your Arduino and try it out. in val // read input value and store it OK now to the interesting part. /* * Alternating switch */ int switchPin = 2. // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). buttonState = digitalRead(switchPin). // switch is connected to pin 2 // variable for reading the pin status // variable to hold the last button // Set the switch pin as input Serial. int val.begin(9600).

the Arduino prints out "Button just pressed" over and over again. we would perform the statements in the {} braces. which is the exotic if-else statment. For example: if ( it is raining ) { falling on me? if ( I have an umbrella ) { OpenUmbrella(). variable // save the new state in our Finally.pressed. this means that we perform one test and if that test comes out true we go on to perform another test. one for (val == LOW) and one for (val == HIGH). What happens now? Highlight the text below to see the answer When the button is held down. if (val == LOW) { // check if the button is pressed Serial. If it is different (tested by the != inequality operator ) then we execute the next group of statements. Now we also have an alternative. we simply examine val to deterimine if the last digitalRead() procedure informed us that the button is currently pressed or not pressed. which is what we should do if the test fails! Now we used to perform two tests. we can't open the umbrella if we don't have one. Lets move on and examine the new statement we see.. And there's no point in checking if we have one if its not raining! In the first if statement. Quick Quiz! • Remove (or comment out) the line that says "buttonState = val. we check if the current button state (HIGH or LOW) is different than the last time we looked at the button.println("Button just released")... Serial.println("Button just pressed"). } This statement is easy to understand: before. enclosed by the {} braces. } else else { statements to perform if test is not True} { . is there water // check my purse // Perform the umbrella Of course.. keeping track of what buttonState and val are storing at each line.. Highlight the text below to see the answer . If its not LOW it must be HIGH. nothing is printed • Why does this happen? Go through the sketch. } In the if-else statement. When its released. This code is equivalent but its a little more straightforward. opening procedure } } // look up. buttonState = val. This is more complex than a simple if statement but not much more different than the kinds of decisions we make all the time. if if (test statement) ( val == LOW ) { statements to perform if test is True} { . } else { // the button is -not.Now we see 2 if statements that are nested. we make sure that we've updated the button state variable with the current state. we would run a test and if that test passed." from the sketch and re-upload it to the Arduino..

// Set up serial communication at 9600bps buttonState = digitalRead(switchPin). Simply. Every time the button is pressed. int buttonPresses = 0. decrement the buttonPresses variable (use the -operator. // save the new state in our variable } We've added one new thing in this sketch. Have the buttonPresses variable start at 10. Quick Quiz! • Modify the sketch so that message is only printed when the button is released.begin(9600). have the Arduino print out "We have x presses to go till takeoff!" where x is the number of presses remaining. // // // // switch is connected to pin 2 variable for reading the pin status variable to hold the button state how many times the button has been // Set the switch pin as input Serial. Try this sketch /* * Counting presses */ int switchPin = 2. INPUT). Serial. Step 2. int buttonState.print(buttonPresses). This is a shortcut for "buttonPresses = buttonPresses + 1". the statement "buttonPresses++" increments (adds 1 to) the buttonPresses variable. which is the ++ operator.println(" times"). // increment the buttonPresses variable Serial. but only if the number of presses left . pressed void setup() { pinMode(switchPin. Once you have that working. it sets buttonState to LOW (assuming the button isn't pressed as it is reset). Whenever the button pin is read as HIGH the (val != buttonState) test is true and it prints out a message.When the Arduino starts up. Highlight the text below to see the answer Change the "val == LOW" test to "val == HIGH" • Modify the sketch so its a countdown device! Step 1. } } buttonState = val. Serial. Step 3. The buttonState is never set to HIGH so it never prints "Button is released" and it always passes the (val != buttonState) test Counting presses A pretty useful techinque you'll want to add to your collection of sketch-knowledge is how to keep track of button presses.print("Button has been pressed "). int val. in val // read input value and store it if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed buttonPresses++. not when it's pressed. which does the opposite of ++). // read the initial state } void loop(){ val = digitalRead(switchPin).

and you pick it up! Voice: Hello.is larger than 0 (check the conditional test table above to see how to test if a variable is larger than a number) Step 4. } if (buttonPresses > 0) { Serial. val = 0. a company that .print("We have ").println("EXPLODE!"). Serial. // save the new state in our variable } Design challenge. // read the initial state } void loop(){ val = digitalRead(switchPin). make the Arduino print out "EXPLODE!" on the last button press. } } buttonState = val. Highlight the text below to see one possible answer /* * Takeoff! */ int int int int switchPin = 2.. INPUT). // read input value and store it in val } if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is now pressed buttonPresses--. this is the president of Blinky Lite Fun Company Inc. buttonPresses = 10. if (buttonPresses == 0) { Serial. // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). part 1 The phone rings.println(" presses to go till takeoff!").print(buttonPresses). Serial. buttonState.begin(9600). // Set the switch pin as input Serial. Once you have that working. // // // // switch is connected to pin 2 variable for reading the pin status variable to hold the button state 10 presses to go! void setup() { pinMode(switchPin.

Here is a video demonstrating the functionality the customer wants Click To Play • play_blip_movie_463022(). Write a simple sketch to verify you've wired them up correctly by blinking all the LEDs. this is not a problem. Wire up the switch as shown (or if its already on the breadboard from the previous projects. However.Step 1. If you're having trouble figuring out the wiring. Adapt the "Counting Presses" sketch Instead of a variable called buttonPresses you have a variable called lightMode. Use jumpers when necessary to connect all of cathodes (the negative pin of the LED) to ground. Here is the schematic we found on his desk: All the bike light has to do is turn on when the button is clicked and turn off the next time the button is clicked. leave it as is). • Step 2. When the button is released the LEDs turn off • Step 5. • Step 4. Modify an earlier sketch in this lesson so that when the button is held down. • Step 3. I'll send you over a contract and get started as soon as the documents are signed! The contracts are faxed and signed and now it's time to do your job. We're noticing that a majority of our customers ride bicycles and they'd like to be more safe. all of the LEDs turn on. which starts at 0 (off). When the button is . click here for a high-res photo. Can we hire you to finish the project? You: Sure.specializes in blinky light products. Test the LEDs. If you're having trouble figuring out the wiring. We're thinking of offering a bicycle safety light and we hired an electrical engineer to design a light for us. click here for a high-res photo. Test the switch. he decided to go on a week-long kite surfing expedition and has left us in the lurch. The lightMode variable will keep track of the bike light state. Wire up the 5 red LEDs onto your breadboard as shown in the schematic.

INPUT). HIGH). pinMode(led5Pin.clicked. // switch is connected to pin 2 // variable for reading the pin status // variable to hold the button state // Is the light on or off? int switchPin = 2. OUTPUT). int val. // Set the switch pin as input Serial. digitalWrite(led3Pin. digitalWrite(led1Pin. digitalWrite(led2Pin. OUTPUT). HIGH). 8. // light is on! // save the new state in our variable . // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). } } } buttonState = val. pinMode(led1Pin. LOW). if (val != buttonState) { if (val == LOW) { if (lightMode == 0) { lightMode = 1. int lightMode = 0. int buttonState. 11. } else { lightMode = 0. check the lightMode value. digitalWrite(led4Pin. Highlight the text below to see a hint When the button is pressed. digitalWrite(led5Pin. digitalWrite(led2Pin. digitalWrite(led5Pin. LOW). otherwise set it 0 (off) and turn off all the LEDs. LOW). pinMode(led4Pin. 10.begin(9600). use an if-else statement like this: if (lightMode == 0) { <some stuff goes here> } else { <some other stuff goes here> } Here is one possible solution sketch: /* * */ int int int int int Bike light. void setup() { pinMode(switchPin. OUTPUT). If it is 0 (off). HIGH). OUTPUT). LOW). set it 1 (on) and turn on all the LEDs. digitalWrite(led4Pin. 9. pinMode(led2Pin. revision 1 led1Pin led2Pin led3Pin led4Pin led5Pin = = = = = 12. } // read input value and store it in val // // // // the button state has changed! check if the button is pressed light is off light is on! HIGH). HIGH). digitalWrite(led3Pin. OUTPUT). pinMode(led3Pin. // read the initial state } void loop(){ val = digitalRead(switchPin). LOW). digitalWrite(led1Pin.

It works pretty well. Can you fix this? We'll send you the check next week. It turns out that every once in a while. And that means that once in a while. but we have found a bug in your design. the spring is springy. This works great except that.. the spring bounces back. making and breaking contact a few times before settling.You spend some time looking over your code but can't seem to find the problem. (You should try it out. but actually a mechanical problem. When you release. Inside the little tactile switch is a small disc spring. Here is a screencapture from my Tektronix scope . Here is a video demonstrating the problem. the light doesn't turn on or off. it is a sneaky bug) Click To Play play_blip_movie_463045(). this is a problem. although depending on your button you may or may not see this problem occur. thanks for getting us that bicycle light prototype so quickly. yes. Turns out this is not a software (sketch) problem. when you press the button it bounces around a little in the switch. If you have a oscilloscope. you can look at the input to the Arduino pin in detail to see the "bouncing" in action. thanks! You play a little bit with the bike light prototype and find that. When you push the button you squeeze the spring so that it makes contact with the two wire connections.Brooklyn Debounce The phone rings. well. when we press the button. this is the president of the Blinky Lite Fun Company Inc. and you pick it up! Voice: Hello.

We'll also use a much more generous 10 millisecond delay. that means that the switch has settled on the value. This causes our sketch to hiccup because every once in a while. which will take care of even the most bouncy of switches. // switch is connected to pin 2 . If the two readings are the same. They only occur during the press or release of a button. the voltage into the input pin starts at ground (LOW). Each dotted line lengthwise indicates 250 microseconds (. and when the Arduino checks the pin it thinks that the user pressed and depressed the switch many times. then there are some spikes and finally it goes up to 5V (HIGH). debounced */ int switchPin = 2. there are no spikes. at least 1 millisecond apart. That means that we can check the button twice. We'll require that the two readings must read the same before we perform the rest of the sketch. If the two readings are different. but once in a while they do occur. /* * Bike light. In this image you can see how when the button is released. The center is 0 volts (ground) and each dotted line indicates a change of 2V. How to solve this problem? Well there are some very fancy techniques one can use to debounce a button but there's also a dead-simple one: adding a delay. Most of the time. that means there could have been a bounce. there's a bounced switch. You'll notice that the bounces only occur for half a millisecond. int led1Pin = 12. Thus the light turns on for a few microseconds. and then turns off. This is called a contact bounce! Remember! The bounces don't occur when the button is held down or not pressed.25 milliseconds) The Y axis is voltage.The X axis is time. int led2Pin = 11.

} else { lightMode = 0.int led3Pin = 10. LOW). digitalWrite(led2Pin. HIGH). HIGH). } } } buttonState = val. OUTPUT). // read input value and store it in val delay(10).begin(9600). digitalWrite(led5Pin. HIGH). int led5Pin = 8. digitalWrite(led3Pin. pinMode(led4Pin. // turn light off! digitalWrite(led1Pin. digitalWrite(led2Pin. void setup() { pinMode(switchPin. // turn light on! digitalWrite(led1Pin. OUTPUT). int val. // variable for reading the pin status // variable for reading the . // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (lightMode == 0) { // is the light off? lightMode = 1. LOW). delayed/debounced status int buttonState. digitalWrite(led3Pin. pinMode(led3Pin. INPUT). pinMode(led5Pin. int val2. int val2. digitalWrite(led5Pin. pinMode(led2Pin. OUTPUT). // save the new state in our variable } } Here is the important. LOW). // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). new section of code we added: int val. LOW). int lightMode = 0. HIGH). digitalWrite(led4Pin. LOW). digitalWrite(led4Pin. HIGH). OUTPUT). // 10 milliseconds is a good amount of time val2 = digitalRead(switchPin). int led4Pin = 9. // variable for reading the pin status // variable for reading the // variable to hold the button state // Is the light on or off? // Set the switch pin as input Serial. pinMode(led1Pin. OUTPUT). // read the initial state } void loop(){ val = digitalRead(switchPin).

for bounces if (val == val2) { readings! if (val != buttonState) { changed! // variable to hold the button state // read input value and store it // 10 milliseconds is a good // read the input again to check // make sure we got 2 consistant // the button state has Now we have used a delay() procedure call to space out our input readings. part 2 The phone rings. We take two readings and compare them to make sure that the switch has settled on whatever value we read. in val delay(10). OK that shouldn't be too hard. Only thing is. well. The bike light works great. variable // save the new state in our is in the if statement that makes sure the two input reads are the same. pinMode(led2Pin. pinMode(led1Pin. delayed/debounced status int buttonState. Can you make the light more blinky? We'll send you the check next week. int lightMode = 0. its just not. and you pick it up! Voice: Hello. Note that this line buttonState = val. You should not consider the val variable to hold valid information unless you've verified it against the second read. amount of time val2 = digitalRead(switchPin). OUTPUT). your correction has solved our flaky light problem. INPUT). // switch is connected to pin 2 int val. kthxbye! Well. 11. blinky */ int int int int int int switchPin led1Pin = led2Pin = led3Pin = led4Pin = led5Pin = = 2. 12.. val2. If there's a bounce. this is the president of Blinky Lite Fun Company Inc. void loop(){ val = digitalRead(switchPin). You modify the debounced sketch so it looks like this: /* * Bike light. int val2. 8. Otherwise you will get strange performance Design Challenge. void setup() { pinMode(switchPin. 10. it'll get filtered out by our delay. blinky enough! And this is the Blinky Lite Fun Company Inc. 9. // variable for reading the pin status // variable for reading the // variable to hold the button state // Is the light on or off? // Set the switch pin as input . Try it out and see if it helps make your bike light more reliable.delayed/debounced status int buttonState. OUTPUT).

begin(9600). pinMode(led4Pin. Quick quiz! • Does this sketch work correctly? Highlight the text below to see the answer . digitalWrite(led5Pin. digitalWrite(led1Pin. // read input value and store it // 10 milliseconds is a good // read the input again to check // make sure we got 2 consistant // the button state has // check if the button is // is the light off? // turn light on! HIGH). // read the initial state } void loop(){ val = digitalRead(switchPin). Upload this sketch to your bike light and try it out. digitalWrite(led2Pin. OUTPUT). digitalWrite(led3Pin. // blink on! // blink off! You are feeling pretty proud because you remembered that you did not have to turn off the LEDs when entering the off mode: they're off at the end of the blinky code that is run when lightMode is 1. digitalWrite(led2Pin. OUTPUT). amount of time val2 = digitalRead(switchPin). digitalWrite(led5Pin. for bounces if (val == val2) { readings! if (val != buttonState) { changed! if (val == LOW) { pressed if (lightMode == 0) { lightMode = 1. delay(100). LOW). HIGH). LOW).pinMode(led3Pin. } else { lightMode = 0. digitalWrite(led1Pin. pinMode(led5Pin. digitalWrite(led4Pin. delay(100). Serial. digitalWrite(led4Pin. in val delay(10). OUTPUT). HIGH). LOW). // save the new state in our variable } } LOW). HIGH). HIGH). // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). digitalWrite(led3Pin. LOW). // turn light off! // No need to do anything because the light is 'off' when it finishes with mode 1 } } } buttonState = val.

pinMode(led1Pin.begin(9600). • Use println() procedure calls and your brain to try and figure out why the sketch acts this way. OUTPUT). However. INPUT). it will change the lightMode variable appropriately. pinMode(led2Pin. 12. next time the loop() procedure runs. the next section of code will examine the lightMode variable and then perform the correct actions for that mode. in val // read input value and store it . it only blinks once. void setup() { pinMode(switchPin. int val2. // read the initial state } void loop(){ val = digitalRead(switchPin). the if conditionals are false (the button state has not changed) and so the code that would blink the LED is not run again. Thus the single blink. pinMode(led4Pin. 11. // variable for reading the pin status // variable for reading the delayed // variable to hold the button state // What mode is the light in? // Set the switch pin as input Serial. OUTPUT). What happens just after the button is pressed to turn the light on? What happens the next time the loop() procedure runs? Highlight the text below to see the answer When the button is just pressed. // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). status int buttonState. OUTPUT). int lightMode = 0. Try to fix the code above so it does the right thing. // switch is connected to pin 2 int val. OUTPUT). Here is one solution: /* * Bike light. pinMode(led5Pin. The LEDs are then turned on for 100ms and then off for 100ms.No! • What does it do? Highlight the text below to see the answer When the button is pressed to turn the light on. the if statement conditions are true and the light mode changes form 0 (off) to 1 (on). pinMode(led3Pin. It will also determine whether the button state has changed and if so. 8. 10. 9. The first section will do all the button checking and debouncing stuff. revision 3: blinky */ int int int int int int switchPin led1Pin = led2Pin = led3Pin = led4Pin = led5Pin = = 2. The trick here is that you want to split up your loop() procedure into two sections. Once that dirty work has been done. OUTPUT).

modify the first half of the sketch so that pressing the button cycles through all the modes. LOW). Click To Play • play_blip_movie_466034(). digitalWrite(led2Pin. LOW). for bounces if (val == val2) { readings! if (val != buttonState) { changed! if (val == LOW) { pressed if (lightMode == 0) { lightMode = 1. LOW). delay(100). this is the president of Blinky Lite Fun Company Inc. from mode 0 to mode 3. } // If lightmode is 0. We can't compete with them in the free market if we only have this lame bike light. digitalWrite(led5Pin. We'll send you the check next week. and you pick it up! Voice: Hello. HIGH). variable } // 10 milliseconds is a good // read the input again to check // make sure we got 2 consistant // the button state has // check if the button is // light is off // turn light on! // turn light off! // save the new state in our // Now do whatever the lightMode indicates if (lightMode == 1) { digitalWrite(led1Pin. for reals! The final design challenge is to take the sketch from Design Challenge 2 and upgrade it to have at least 4 modes: off. part 3 The phone rings. blinking LEDs. } } } buttonState = val. HIGH). LOW). digitalWrite(led3Pin. digitalWrite(led5Pin. I love the blinking light. we dont have to do anything because the LEDs are already off! } Design Challenge. } else { lightMode = 0. its just so me! But. OMG. our competitor just brought a bike light into the market that has 4 modes. delay(100). HIGH). all-on. and 'wave'. LOW). flashy light modes. amount of time val2 = digitalRead(switchPin).delay(10). digitalWrite(led2Pin.. digitalWrite(led4Pin.First. HIGH). digitalWrite(led3Pin. We need more blinky. Also. HIGH). digitalWrite(led1Pin. please make our logo bigger. digitalWrite(led4Pin. Use println() procedure calls to verify that you are successfully changing between all of the .

amount of time // read input value and store it // 10 milliseconds is a good . you can use an if-else statement. // read the initial state } void loop(){ val = digitalRead(switchPin). pinMode(led4Pin. 11. // Set up serial communication at 9600bps buttonState = digitalRead(switchPin). INPUT). 8. OUTPUT). 10. pinMode(led2Pin. } } } • After that is working. // switch is connected to pin 2 int val. pinMode(led3Pin. modify the second half of the sketch so that it performs the different effects for each mode. } else { if (condition3) { jump around. status int buttonState. void setup() { pinMode(switchPin. Here is an example of the finished project code /* * Bike light. you can also nest if-else statements! if ( condition1 ) { do this. 9. OUTPUT). but with more than two. pinMode(led5Pin.begin(9600). OUTPUT).modes Need a Hint? Highlight the text below to see a clue With only two modes. OUTPUT). 12. in val delay(10). int val2. } else { if (condition2) { do that. OUTPUT). pinMode(led1Pin. Turns out that just like you can nest if statements. int lightMode = 0. you'll need to handle multiple possibilities. final version */ int int int int int int switchPin led1Pin = led2Pin = led3Pin = led4Pin = led5Pin = = 2. // variable for reading the pin status // variable for reading the delayed // variable to hold the button state // What mode is the light in? // Set the switch pin as input Serial.

HIGH). . // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonState) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (lightMode == 0) { // if its off lightMode = 1. delay(100). HIGH). delay(100). HIGH). digitalWrite(led3Pin. digitalWrite(led1Pin. digitalWrite(led2Pin. digitalWrite(led4Pin. // turn light off! } } } } } } buttonState = val. // make it blink! } else { if (lightMode == 2) { // if its blinking lightMode = 3. digitalWrite(led3Pin. digitalWrite(led3Pin. } if (lightMode == 2) { // blinking digitalWrite(led1Pin. LOW). // turn lights on! } else { if (lightMode == 1) { // if its all-on lightMode = 2. // make it wave! } else { if (lightMode == 3) { // if its waving. LOW). HIGH).val2 = digitalRead(switchPin). digitalWrite(led4Pin. LOW). LOW). LOW). digitalWrite(led4Pin. HIGH). digitalWrite(led2Pin. LOW). HIGH). LOW). digitalWrite(led4Pin. // save the new state in our variable } // Now do whatever the lightMode indicates if (lightMode == 0) { // all-off digitalWrite(led1Pin. lightMode = 0. LOW). LOW). digitalWrite(led1Pin. HIGH). LOW). delay(50). digitalWrite(led3Pin. HIGH). digitalWrite(led2Pin. HIGH). digitalWrite(led5Pin. HIGH). LOW). HIGH). digitalWrite(led2Pin. digitalWrite(led5Pin. } if (lightMode == 3) { // "wave" digitalWrite(led5Pin. digitalWrite(led5Pin. } if (lightMode == 1) { // all-on digitalWrite(led1Pin. digitalWrite(led5Pin.

digitalWrite(led4Pin. delay(50). HIGH). LOW). digitalWrite(led2Pin. delay(50). digitalWrite(led3Pin.digitalWrite(led1Pin. HIGH). digitalWrite(led2Pin. delay(50). digitalWrite(led5Pin. delay(50). For extra credit. HIGH). HIGH). and post your video to the forums For triple-word-extra credit. digitalWrite(led3Pin. use the 9V battery pack to power your Arduino bike light and duct tape it to your shirt! Conclusion blah blah blah Lesson 6: Wiring up an LCD to an Arduino Arduino Tutorial Wiring up an LCD to an Arduino • • • • • • • • • • • • • Intro Starting Lesson 0 Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 LCDs HELP!!! Buy stuff Forums • Home . } } LOW). digitalWrite(led5Pin. come up with with some more flashy modes. digitalWrite(led4Pin. LOW). LOW). LOW).

net • Portfolio • Research • Press • Publication & Presentation • Photos • Wiki (backend) • Projects • Arduino » • Ethernet Shield • GPS Shield • Proto Shield • Motor Shield • Wave Shield • BoArduino • DIGG Button • Drawdio • Fuzebox • Game Grrl • Game of Life • MIDIsense • MiniPOV2 • MiniPOV3 • MintyMP3 • MintyBoost • SIM Reader • SpokePOV • TV-B-Gone • USBtinyISP • Wattcher • Wave Bubble • x0xb0x • XBee • YBox2 • Quickies » • Halloween Pumpkin • Vintage Bike Lite • Kite Arial Photo • Bike Stand • LiIon Bike Lite • More. • ->Instructables • Learn • Arduino tutorial • AVR tutorial • Multimeter tutorial • Soldering tutorial .• About • ladyada...

preferrably a Diecimila (or whatever the latest version is) but NG is OK too Adafruit $35 Adafruit USB Cable. Standard A-B cable is required. but I figure its a popular request. Or any computer supply store $5 2 pieces of 7-pin female header (you can always cut down longer header) Any electronics supply store $3 . Any length is OK. there's no LCD included with the Arduino starter pack. AVR • Blog • Store • Forums þÿ ladyada. Tools • Find Parts • Kits • Laser • uC Annoyances • Open Source Hardware • PCB Design & Tips • PIC vs.net Introduction Search This is a bit of a side note.• Library • Arduino Hacks • Batteries • E. so here we go! What you'll need Assembled Arduino board.E.

2 pieces of 7-pin male header (you can always cut down longer header).

Any electronics supply store $0.50

Arduino Prototyping Shield kit, without breadboard!

Adafruit $15

Character LCD with parallel interface

Pretty much any online electronics shop!

Hookup Wire Make sure its not stranded wire! Any hardware store

Annie, get yer LCD The first step is to get the LCD you want. Parallel LCDs come in a couple different sizes, from 16 characters, 1 line (16x1) to 24 characters, 4 lines (24x4).

In this tutorial I use the 16x2 Picvue from Jameco because thats what was I had in my box of stuff, but you can easily adapt it for any other LCD. The wiring may differ a little bit but that will be covered. Check out the pins The first step is to turn over the LCD and check out the pins. Parallel LCDs almost always have 14 or 16 pins. In this model they are all the way to the left, near the label J1. I soldered 2 7-pin female headers for e-z plugging. Somtimes the pins are along the bottom all in a row.

Look closely at the header for the numbers that show which pins are which. In this model, the first pin is at the top right, the second pin is top left, etc till pin 14 in the bottom left.

When the LCD is flipped over, the pinout will be mirrored, to help me keep track of it, I made a diagram: 1 3 5 7 9 11 2 4 6 8 10 12

13

14

Whatever your pinout is, make sure you have a diagram written out since you will need to refer to it many times! Header up!

OK now that we are diagrammed out, I took the protoshield and soldered 2 7-pin headers at one end of the pcb. Note that they are not all the way to the edge, I left one row of holes so I could easily solder some wires. I also soldered a short piece of header (that comes with the shield kit) at the left so that the LCD will be propped up.

You can now do a test fit to verify how it will look. it hangs over a bit but thats OK by me

I'll solder them in! Next are the two power wires. Parallel LCDs run off of +5V so you can just solder the Vcc wire to 5V and the ground wire to GND. Once I test and verify they are correct. These LCDs are designed for either a 8-bit or 4-bit interface.Shave & a haircut. D5 -> Arduino pin #8. multiple bits at a time. data will be sent to it over a parallel interface. Since we'd like to save pins. That is. Lets connect these to the arduino thusly: D4 -> Arduino pin #7. D6. D4 -> Arduino pin #10 Since I wasnt sure of the wiring. I used the sockets on the protoshield. . 1 (D7) 3 (D5) 5 7 9 (ENABLE) 11 (RS) 13 (GND/VSS) 2 (D6) 4 (D4) 6 8 10 (R/W) 12 (CONTRAST) 14 (+5V/VDD) There's a lot of wiring to be done but we're going to go thru it very slowly so it shouldn't be too bad. D6 -> Arduino pin #9. Double-check your datasheet but almost all parallel LCDs have these pins numbered 4. 2. and D7. 4 bits! Since this is a parallel LCD. 3. D5. and 1 respectively. lets go with the 4-bit interface! The data pins are name D4.

saving another arduino pin . Theres another control line called R/W that you can use to control whether you're reading or writing to the LCD. Since we'll just be writing. that pin can be connected to ground. ENABLE and RS which we connect to pins 12 and 11 respectively.Next are the 2 control wires.

we need to connect a potentiometer to this to make the display visible.The last wire is the contrast control. One pin is connected to +5V. Looks good! . I dont know the specifics of the input current but I used a 10K potentiometer and it worked great. Now place the LCD on top. the pin on the other side is connected to ground and the middle pin is connected to the contrast line.

Test 1 Our first test will be just to connect it up to power and see what happens. if the contrast is all the way down you may not see anything Install LCD4bit library Next you'll want to install the LCD4bit library. At least solder in the male headers. Plug it into an Arduino and power it up.Make sure you finish up the rest of the shield so you can plug it into an arduino. so download it from the Arduino playground Uncompress and install the library by dragging the files into the hardware/libraries folder inside your Arduino installation . You should see the following: Make sure you tweak the contrast potentiometer.

The inside of the LCD4Bit folder: Open up LCD4Bit.cpp with a text editor such as WordPad and scroll down to the part where the pins used are defined .

Make sure the Enable pin is set to 12 and the RS pin is 11. you should see a randomized list of fruits! . as well. Froots! Finally. USING_RW should be set to false. and select from the menu: Sketchbook->Examples->Library-LCD4Bit->LCD4BitExample Upload the sketch to your Arduino with the LCD attached. start up your Arduino software.

there's a lot of bugs. then just copy and paste the code into your own project. I'm using Vista and. but until then. but Arduino and the avr-gcc/avrdude backend are pretty much not Vista-ready. Try XP/2000 which is much more likely to work! avrdude: stk500_getsync(): not in sync: resp=0x00 If you get the following error message "avrdude: stk500_getsync(): not in sync: resp=0x00" that means that the Arduino is not responding. Check out the example code to see how this is done. did you press reset just before selecting Upload menu item? Is the correct Serial Port selected? Is the correct driver installed? Is the chip inserted into the Arduino properly? (If you built your own arduino or have burned the bootloader on yourself) • Does the chip have the correct bootloader on it? (If you built your own arduino or have • • • • .. Appendix: HELP! HELP! While Arduino is a nice little system. Check the following: If you have a NG Arduino. Hopefully they'll be fixed soon.. There are literally dozens of reasons this could be. Sorry pal. here's problems you're likely to run into.Hooray! You're ready to start printing your own messages.

NullPointerException at processing..setDTR(Serial..burned the bootloader on yourself) Note that it is nearly impossible for anyone to debug this.. Make sure the driver is installed and you have the correct serial port number.app.lang. This is a bug in Arduino and will hopefully be fixed in v10. avrdude: can't open device "COM10": The system cannot find the file specified If you get the following error: avrdude: can't open device "COM10": The system cannot find the file specified (under Windows. COM port value may vary) . Try everything.Serial. see lesson 0.Serial. Then you have either the incorrect chip selected in the Tools menu or the wrong bootloader burned onto the chip ser_send(): write error: sorry no info avail (no screenshot as I can't reproduce it) You have the wrong serial port selected.lang. If you get the following error avrdude: Expected signature for ATMEGA is . as there are so many possible issues.app. java.. go back and verify that the correct driver is installed (lesson 0) and that you have the correct serial port selected in the menu.setDTR If you get the following error message java.java:480) It means you dont have a serial port selected. avrdude: Expected signature for ATMEGA is .NullPointerException at processing.

Select the USB device in the Device Manager... You can use a program like FTClean to clear out old COM ports you aren't using anymore. each one has a unique serial number so when it's plugged in it gets its own COM port. you can simply change the COM port using the Device Manager. Alternately. Once you've cleaned out the ports. You should make sure that the port is numbered as low as possible. . you'll have to reinstall the driver again (see lesson 0). right click and select Properties Then click Advanced. if you're sure that the ports are not used for something else but are left over from other USB devices. However that COM port sits around even when the Arduinos are not plugged in. Say maybe you've got 9 Arduinos.It means that you have too many COM ports. and in the next window change the COM port to something like COM4 or COM5. Thus every time you plug in a new USB-serial device the COM number goes up and up until its too high.

it shouldn't be a problem using them. The lower port names may say (in use) but as long as the other USB devices aren't plugged in.Don't forget to select the new port name in the Arduino software. This is a bug in Arduino and will hopefully be fixed in v10.so I'd go for that first. .. This is a little riskier than just using FTClean..

Sign up to vote on this title
UsefulNot useful