(edit sidebar)
Intro to Physical Computing Syllabus

Research & Learning

Other Class pages

Shop Admin

ITP Help Pages
Tom's pcomp site
DanO's pcomp site


Digital Input and Output with an Arduino

Overview

In this lab, you'll connect a digital input circuit and a digital output circuit to a microcontroller. Though this is written for the Arduino microcontroller module, the principles apply to any microcontroller.

(:toc Table of Contents:)

Parts

For this lab you will need the following parts:

solderless breadboard
Solderless breadboard
Hookup wire
22-AWG hookup wire
Arduino microcontroller module
Arduino microcontroller module


LEDs
Light Emitting Diodes, LEDs
220-ohm and 10kilohm resistors
560-ohm and 10kilohm resistors
switch
switch or pushbutton


Click on any image for a larger view

Prepare the breadboard

Connect power and ground on the breadboard to power and ground from the microcontroller. On the Arduino module, use the 5V and any of the ground connections:


(Diagram made with Fritzing

Add a Digital Input (a switch)

Connect a switch to digital input 2 on the Arduino. The switch shown below is a store-bought momentary pushbutton, but you can use any switch. Try making your own with a couple of pieces of metal.


Arduino with switch on digital pin 2

Schematic view


(Diagram made with Fritzing)

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.


Add LEDs on pins 3 and 4

Schematic view


(Diagram made with Fritzing)

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 input will behave unreliably.

A note on resistor values: if you don't have a 10-kilohm resistor for the switch, you can use any reasonably high value. 4.7K, 22K, and even 1 Megohm resistors have been tested with this circuit and they work fine. As for the resistor on the LED, the higher the resistor value, the dimmer your LED will be. So 220-ohm resistors give you a nice bright LED, 1-kilohm will make it dimmer, and 10K or higher will likely make it too dim to see.


Program the Arduino

Connect the microcontroller to your computer via USB. When you plug the Arduino into your computer, you'll find a new serial port in the Tools-->Serial Port menu (for details on installing the software, and USB-to-serial drivers for older Arduino models, see the Arduino Getting Started Guide). In OSX, the name will begin with /dev/tty.usbmodem- if it's an Arduino Uno later, and /dev/tty.usbserial- if it's a Duemilanove or earlier. In Windows it will start with COM like all the other serial ports.

Write a program that reads the digital input on pin 2. When the switch is pressed, turn the yellow LED on and the red one off. When the switch is released, turn the red LED on and the yellow LED off.

In your setup() method, you need to set the three pins you're using as inputs or outputs, appropriately.


 void setup() {
   pinMode(2, INPUT);      // set the switch pin to be an input
   pinMode(3, OUTPUT);   // set the yellow LED pin to be an output
   pinMode(4, OUTPUT);   // set the red LED pin to be an output
 }

In the main loop, first you 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 the yellow LED
     digitalWrite(4, LOW);     // turn off the red LED
   }
   else {
     // if the switch is open:
     digitalWrite(3, LOW);     // turn off the yellow LED
     digitalWrite(4, HIGH);    // turn on the red LED
   }
 }
 

Once you're done with that, you're ready to compile your sketch and upload it. Click the Verify button to compile your code. Then click the Upload button to upload the program to the module. After a few seconds, the following message will appear in the message pane to tell you the program was uploaded successfully.

Binary sketch size: 5522 bytes (of a 7168 byte maximum)

Press the switch and watch the LEDs change until you get bored. That's all there is to basic digital input and output!



Get Creative

This is just a suggestion for a short project. It's not a requirement for the class homework.

Many projects can be made with just digital input and output. For example, a combination lock is just a series of switches that have been switched in a particular sequence. Consider the cymbal-playing monkey below:

His cymbals can be turned into a switch by lining them with tin foil and screwing wires to them:


Those wires can be run to a breadboard and used as a switch. Then the microcontroller could be programmed to listen for pattern of cymbal crashes, and if it sees that pattern, to open a lock by switching on a digital output.

Come up with your own physical interface for a combination lock.

  Edit | View | History | Print | Recent Changes | Search Page last modified on September 01, 2013, at 03:21 PM