## RASER Week 4: Arduino LED Passcode Pad - Part 1

This will be the first lesson where we really get into programming the Arduino to do something cool. The Arduino is an amazing tool for making devices that react to external inputs. In this lesson, we'll learn about how to work with both input and output by connecting buttons that can toggle LEDs on and off.

#### The Passcode Game

To see an example of what you can do with input and output on the Arduino, check out this game I built. The point is to push the buttons that make the LEDs light up in the right combination. When you push the right combination all the LEDs start blinking.

Making this game work is a little complex, but it's doable just from the skills we'll learn today. In today's lesson, I'm going to try to explain how to use a button, and how to use the button input to turn on LEDs. At the end, I challenge you to build your own and post the results in the comments!

#### Measuring voltages with the Arduino

In this project, the important thing we're trying to get the Arduino to do is measure the voltage that's coming into a pin. To do that, we have to program the Arduino to do two things:

1. pay attention to a particular pin
2. show us the whether there is a voltage on the pin

The Arduino comes with an example program called DigitalReadSerial that is set up to measure the voltage on pin number 2. The important part of the code looks like this.

`int pushButton = 2;void setup() {  Serial.begin(9600);  pinMode(pushButton, INPUT);}void loop() {  int buttonState = digitalRead(pushButton);  Serial.println(buttonState);  delay(1);       }`

The part at the top sets up pin number 2 to be an INPUT pin instead of an OUTPUT pin. We won't talk to much about that now, but you can read more in Arduino's DigitalReadSerial tutorial.

The "loop" part at the bottom does two things.

reads what voltage is on pin 2
2. Serial.println(buttonState);
prints out the value on pin 2

To see the value that pin 2 is measuring, we have to click on the Arduino Serial Monitor button in the top right.

When we open the Serial Monitor, we'll see a bunch of 0s or 1s printing on the screen. If we connect a wire from pin 2 to the 5 V pin, the Serial Monitor will show us 1s. If we connect a wire from pin 2 to the GND pin, the Serial Monitor will show 0. The Serial Monitor gives us a way to see if a circuit is open or closed by checking what the voltage is on any pin.

(Ideally, we'd always keep a resistor on the wire between 5V and pin 2, but if we're just testing for a few seconds this will be OK.)

#### Building a Simple Button

The easiest way to change the voltage from 0 to 5 V is by using a button. A button is really just a switch that closes an electrical circuit. My friends Pete and Joe built these awesome paper cup buttons that keep the button at the top steady so you can connect it to the Arduino more easily.

We want to set up the circuit so that the button will connect pin 2 to the 5 V. That will cause the Serial Monitor to display 1. When the button isn't pressed, we want pin 2 to be connected to ground. To get started, we'll connect the 5 V pin to the plus (+) holes on our breadboard and connect the GND pin to the minus (-) holes.

After we have the 5V and the GND connected to the breadboard, we just need to set up the button to toggle between each of them. To do this we're going to set up the button. We want one lead to connect to the 5V (the plus terminals on our breadboard) and the other connects to pin 2. However, there really should be a small resistor between the button and pin 2. (I accidentally missed this in the photo. It worked, but that practice could damage the Arduino eventually.) Finally, we're going to add a resistor between pin 2 and GND as well to prevent a short circuit there too.

This might seem confusing, but we need this kind of setup to make the voltage on pin 2 to go back to GND whenever the button isn't pressed.

We can draw the circuit up to show how the resistor prevents a total short circuit when the button is pressed.

We're using a more advanced kind of circuit diagram than we have before, but you should be able to study it to figure out how the electricity runs from the voltage to either pin 2 or GND.

### Discussion Around the Web

Join the Conversation