Introduction: Arduino: Sending Digital Output

About: Director, Strategic Engagements, DX-TED, Microsoft

In this lesson you will wire up an Arduino with a single LED and a 560 Ohm resistor then code and upload the firmware that writes sequential on/off commands to make the LED blink. This is the 'Hello, World' of Arduino.

While this lesson could be done using the onboard LED, one of the objectives of this lesson is to familiarize yourself with connecting input and output devices (an LED is an output device) to the Arduino and controlling them with software.

What you will need:

(1) Arduino Yun*

(1) 5mm Green LED

(1) 560-Ohm 1/4 Watt resistor (Green-Blue-Brown)

*For this lesson series you are using an Arduino Yun. The reason for using the Yun (vs. other less expensive Arduino boards) is because in future lessons you will make use of the fact that the Yun has on-board Wi-Fi and a Linux distribution. Neither of those are relevant for this lesson, so if you have a different Arduino board (e.g. an Arduino Uno) you can use it. The ARDX Starter Kit for Arduino from Seeed Studio is a good kit with lots of parts (LEDs, resistors, servos, etc.), but it ships with an Arduino Uno instead of the Yun (the Uno doesn't have onboard Wi-Fi or the Linux distribution we will use in later lessons).

Step 1: Wiring the LED

The first step is to wire up the Arduino to be able to send on and off commands to the LED. You can simply wire your board according to the diagram (wire colors don't matter, but help with identification of purpose).

LED

Insert a LED into the breadboard as shown in the diagram. For reference, an LED has one lead that is longer than the other. The longer lead is the positive (+) lead, and the shorter lead is the negative (-) lead.

Wires

Connect the wires as shown in the diagram:

  1. Black: Connect the GND pin to the black/negative side-rail on the breadboard.
  2. Green: Connect pin 9 to the breadboard in the same row as the positive (+) lead from the LED.

Resistor

Connect a 560-Ohm resistor from the row that the negative (-) lead from the LED is in to the negative (-) side-rail.

Step 2: Writing the Code

Using the Arduino IDE create a new sketch. The new sketch has two stubbed out methods.

void setup() {
// put your setup code here, to run once:
}

void loop() {
// put your main code here, to run repeatedly:
}

Prior to the setup method you will declare a variable for the pin that is connected to the LED (a variable is a little bit of overkill here, but this is good practice for lessons to come).

// define a variable for the pin connected to the LED
int ledPin = 9;

void setup() {
    // put your setup code here, to run once:
}

As indicated by the comment, the setup method runs once, when the application (also known as firmware) runs. The code in setup will run once and once only. This is where you will instruct the pin that the LED is connected to to be an output pin (e.g. send voltage out rather than read voltage in).

void setup() {
// initialize the LED pin as an output pin.
pinMode(ledPin, OUTPUT);
}

Next you will define the firmware code that will make the LED blink. The loop method does exactly what it sounds like it does - it loops indefinitely. To make the LED blink you simply tell the output pin to send HIGH or LOW voltage out. To do this you use the digitalWrite method which takes the pin number (remember we declared a variable for the pin number) and the voltage (HIGH or LOW).

void loop() {
// turn the LED on by sending HIGH voltage
digitalWrite(ledPin, HIGH);
// turn the LED off by sending LOW voltage
digitalWrite(ledPin, LOW); }

This will cause the LED to blink as fast as possible, which means you may not see the blinking. To control the blink rate you can add a delay in between the on and off (HIGH and LOW) calls.

void loop() {
// turn the LED on by sending HIGH voltage digitalWrite(ledPin, HIGH); // Add a one-second (1,000 millisecond) delay
delay(1000);
// turn the LED off by sending LOW voltage digitalWrite(ledPin, LOW);
// Add a one-second delay
delay(1000); }

Step 3: Verify the Target Board

Before you can upload the firmware to the Arduino you need to make sure you are targeting the right board and it is connected to the Arduino IDE.

  1. Connect the Arduino to you computer using the USB cable.
  2. In the Arduino IDE verify the board is connected by clicking on the TOOLS menu and then the BOARD menu item. If the correct board is not selected, click on the correct board (i.e. Arduino Yun).

Step 4: Verify the Port

Verify the correct port is selected by clicking on the TOOLS menu and then the PORT menu item. If the correct port is not selected (as indicated by the board it is connected to), click on the correct port.

Step 5: Upload the Firmware

Click on the right-pointing arrow icon just under the EDIT menu (the tooltip reveals "Upload"). This will compile the firmware and upload it to the Arduino. Within a few seconds you will see the lights on the Arduino blink indicating that it is receiving new firmware, then the LED should start blinking. You can play around with the delay times to make the LED blink faster or slower or in some kind of pattern.

Congratulations! You have written and uploaded your first Arduino firmware. Next you will learn how to read analog input from a sensor.