Arduino 2: Digital Output

Blink – Arduino controlling digital output

Let’s use the Arduino to blink an LED once every second (on for a half-second, off for a half- second). We will use pin 13 as an output pin – that means that it will be a + voltage pin that Arduino can turn on (+5V) or off (0V). Choose an LED, and put the positive lead  (the long one) into pin 13 of the Arduino, and the short lead into the ground pin GND next to it. Type in the following program:

void setup() {
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}

Upload the program to the Arduino. The LED should blink on and off. Note that pin 13 has a built in resistor (and actually a built-in tiny LED on the board that blinks in time with the one you plugged in). Normally LED’s should not be connected directly to an output pin or power supply on the Arduino, or it will burn out. Always put a 220-ohm resistor (red-red-brown) in series with an LED on any other pin or with a 5V power supply.

Computers can do four basic things: they read input data, they process data, they store data in memory, and they output data.  This simple program does two of them: process and output data. All data in the computer (and any data that is “digital”) is coded as a series of a high (+5) or low (0) voltages – a code with two states, or “binary” code. So the simplest output a computer can perform is to turn something on or off, by making an output wire (or “pin”) HIGH or LOW. In setup(), the command

pinMode(pin_number,mode);

sets a particular pin pin_numer on the Arduino to one of two modes, INPUT or OUTPUT (you must use capitals). An output pin allows it to turn a voltage on or off, to control an outside device, while an input pin would read incoming data. A given pin generally shouldn’t change modes in a program, so this is only done once in setup(). To actually turn the pin on or off in the loop() function, use

digitalWrite(pin_number, state);

which will turn the Arduino pin pin_number to one of two states, HIGH or LOW. A pin that is HIGH is basically “on” or at +5V, while a LOW pin is off.

As a challenge, grab your breadboard, another LED, two 220-ohm resistors and some jumper wire, and make the Arduino control two LED’s on different pins. Hint:  along the long edges of your breadboard are probably two lines of pins that are all connected right along the length of the board. It’s helpful to wire one of these lines to the ground pin on the Arduino, and then plug all the parts that need ground into this long channel. See the diagram below. Now you figure out the programming.

Image made with Fritzing (http://fritzing.org/)

About these ads

One response to “Arduino 2: Digital Output

  1. wie kann ich die 2 lichter so betreiben das die das selbe programm abspielen wenn ich die 2 lichter auf verschiedene ausgänge haben will?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s