You are on page 1of 6

Arduino: Digital Inputs and Outputs

handson.dmt.fh-joanneum.at

ARDUINO
Work Sheet: Description: Digital Inputs & Outputs In this worksheet, you'll learn how to use the digital inputs and outputs of an Arduino microcontroller, including the importance of using resistors in your circuits. Youll be connecting two LEDs, a button and 3 resistors. The button will enable you to switch between the 2 LEDs and the resistors guarantees you won't roast your Arduino or blow your LEDs. Have fun! 2 Requirements Prepare the breadboard Add a Digital Input (a switch) Add Digital Outputs (LEDs) Program the Arduino Hardware: Breadboard Hookup wires Arduino Light Emitting Diodes, LEDs Switch or pushbutton Resistors Power battery pack (optional) Software: Arduino Software

Difficulty (1-10): Overview:

Requirements:

Carelse, Pauger, Pilz

Arduino: Digital Inputs and Outputs

handson.dmt.fh-joanneum.at

DIGITAL IOS
The Arduinos ability to sense digital and analog inputs provides us with exciting opportunities to interact with the world around us. This worksheet will get you started doing useful things with these inputs. If you dont have an electronics background, weve provided a worksheet on basic electronics you can refer to. The link to the worksheet including many other introductory tutorials can be found under Useful Resources below.

Instructions
1. Prepare the breadboard Connect power and ground on the breadboard to power and ground on the Arduino. Use the 5v and any of the ground connections.

2. Add a digital input (a switch) Connect a switch/button and resistor to digital input 2 on the Arduino. If you dont have a switch try making your own with metal or wire. Why do I need a resistor for the switch? Digital inputs needs to have a resistor to hold the pin to a known value when the switch/button is not pressed to avoid false readings. You can either connect a pull-up resistor (a resistor connected to the voltage input that results in a HIGH) or a pull down resistor (resistor connected to ground that results in a LOW). In our example we adding a pull-down resistor to ensure that the voltage on the pin will be low when the switch is not pressed, because the resistor pulls down the voltage to ground (0). When the switch is pushed, a connection is made between the pin and +5 volts, so the value on the pin interpreted by digitalRead changes from LOW to HIGH.
Carelse, Pauger, Pilz 2

Arduino: Digital Inputs and Outputs

handson.dmt.fh-joanneum.at

3. Add digital outputs (LEDs) Connect a 560-ohm resistor and an LED in series to digital pin 3 and another to digital pin 4 of the Arduino.

Why do we need resistors for an LED? Without it, the LED by itself will draw an infinite amount of current, and can damage the LED and even worse, the Arduino. The LED appears to the Arduino as a short circuit, since the resistance of an LED is nearly 0. The resistor and LED combination have some resistance, which, using Ohms law, allows you to calculate the current flowing through the LED and resistor.

What happens if the LED is connected incorrectly? No light will be emitted. It is important to note the polarity of an LED. The voltage input connects to the longer end of the LED, namely the anode (+). The ground connects to the shorter end, namely the cathode (-).

Carelse, Pauger, Pilz

Arduino: Digital Inputs and Outputs

handson.dmt.fh-joanneum.at

4. Program the Arduino Write a program that reads the digital input 2: when the switch is pressed, yellow LED on, red LED off, when the switch is released, yellow LED off, red LED on. In the setup() method you need to set your three pins you using as inputs and outputs.
void setup() { //set the switch pin to be an input! pinMode(2, INPUT); //set the yellow LED pin to be an output! pinMode(3, OUTPUT); //set the red LED pin to be an output! pinMode(4, OUTPUT);

In the main loop youll need an if-then-else statement to read the switch.
void loop() { // read the switch input:! if (digitalRead(2) == HIGH) { // if the switch is closed:! digitalWrite(3, HIGH); // turn on digitalWrite(4, LOW); // turn off } else { // if the switch is open:! digitalWrite(3, LOW); // turn off digitalWrite(4, HIGH); // turn on } }

the yellow LED the red LED!

the yellow LED the red LED

When you done writing your sketch, you ready to compile and upload your sketch. Click the Verify button to check for any errors. To upload your sketch to Arduino, press the Upload button. Once you have received a confirmation message to inform you that your sketch has been successfully uploaded to Arduino you ready to rock! Press the button and watch your LEDs light up left to right when pressing and releasing!

Trouble Shooting
The LEDs are too dim. Decrease resistance by replacing the resistor with one that has a lower resistance.

Carelse, Pauger, Pilz

Arduino: Digital Inputs and Outputs

handson.dmt.fh-joanneum.at

It seems like the switch is malfunctioning when I press or release, why is this happening? Your circuit could be suffering from circuit bouncing. For more information on debouncing please follow this link: http://arduino.cc/en/Tutorial/Debounce

Circuit Schematic

Adding a switch

Adding LEDs

Carelse, Pauger, Pilz

Arduino: Digital Inputs and Outputs

handson.dmt.fh-joanneum.at

Useful Resources
Recommended reading for learning the basics on electronics: The Art of Electronics - Horowitz Hill http://www.scribd.com/doc/50550439/The-Art-of-Electronics-Horowitz-Hill# Adafruit Learning System: All About LEDs http://learn.adafruit.com/all-about-leds While there are lots of pages on the web that tell you how to calculate a current limiting resistor for an LED, this one tells you the foundations of the calculations: Foundation Calculations of LEDs: http://www.thebox.myzen.co.uk/Tutorial/LEDs.html LEDs for Beginners: http://www.instructables.com/id/LEDs-for-Beginners/ Ohms Law Calculator: http://diyaudioprojects.com/Technical/Ohms-Law/

Carelse, Pauger, Pilz

You might also like