Вы находитесь на странице: 1из 6

ARDUINO

! ! ! ! ! !

Work Package: Description:

Digital Inputs & Outputs


In this work package, you'll connect a digital input circuit and a digital output circuit to an Arduino microcontroller. 2 Requirements Prepare the breadboard Add a Digital Input (a switch) Add Digital Outputs (LEDs) Program the Arduino Breadboard Hookup wires Arduino Light Emitting Diodes, LEDs Switch or pushbutton Resistors Power battery pack (optional)

Difficulty (1-10): Overview:

Requirements

! !

Digitale IOs

handson.dmt.fh-joanneum.at

DIGITAL IOS
! !
1. Requirements
For this work package you will need the following parts:! Breadboard Hookup wires Arduino Light Emitting Diodes, LEDs Switch or pushbutton Resistors Battery (optional) !

! ! ! ! ! ! ! ! !
Carelse, Pauger, Pilz !2

Digitale IOs

handson.dmt.fh-joanneum.at

2. 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.! !

!
3. Add a Digital Input (a switch)

! ! !

Connect a switch and resistor to digital input 2 on the Arduino. If you dont have a switch try making your own with a couple of pieces of metal or wire.!

Carelse, Pauger, Pilz

!3

Digitale IOs

handson.dmt.fh-joanneum.at

4. 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. What happens if you don't include the resistor connecting the switch to ground? The resistor connecting the switch is a pulldown resistor. It provides the digital input pin with a reference to ground. Without it, the LED by itself will draw an infinite amount of current, and damage 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. You need to size the resistor to limit the current to what the Arduino is capable of providing.

! ! ! !

What happens if the LED is connected incorrectly? No light will be emitted. Its 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 !4

Digitale IOs

handson.dmt.fh-joanneum.at

5. 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); " "// " " "digitalWrite(4, LOW); " " // " "} ! " "else {! " " "// if the switch is open:! " " "digitalWrite(3, LOW); " " // " " "digitalWrite(4, HIGH); " "// " "}! "}! "! turn on the yellow LED! turn off the red LED!

turn off the yellow LED! turn on the red LED!

When you done writing your programme, you ready to compile and upload your sketch.

! Click the Verify button to check for any errors and upload your sketch to Arduino. ! ! ! ! ! !

Then click the Upload button to upload the program to Arduino. 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!!

Carelse, Pauger, Pilz

!5

Digitale IOs

handson.dmt.fh-joanneum.at

USEFUL RESOURCES

! ! ! ! ! !

The Art of Electronics - Horowitz Hill: Recommended reading for learning the basics on electronics
http://www.scribd.com/doc/50550439/The-Art-of-Electronics-Horowitz-Hill#

Foundation Calculations of 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.
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

!6

Вам также может понравиться