First Sketch: Make an LED Blink

By long tradition (going back to 2006), the first Arduino sketch you write is to make an LED blink.

Arduino pins can be used for input and output, as long as you tell the computer which is which. So in this sketch, we tell the Arduino to set pin 13 to be the LED OUTPUT pin, and then we alternately send electricity to pin 13 (setting the pin HIGH) and cut off the electricity to pin 13 (setting the pin LOW). With each alternation, the LED turns on and off.

We’ll write all the sketches in this book using the Arduino “integrated development environment” (IDE), which, simply put, is special software for writing and uploading code to Arduino.

Parts

  1. Arduino Uno
  2. Breadboard
  3. LED

Install the IDE

Download the Arduino IDE from http://arduino.cc/en/Main/Software, and follow the provided instructions to install it on your computer.

Once you’ve installed the software, open the IDE. You should see a screen that looks something like Figure 1-3.

The Arduino IDE on a Mac.

Figure 1-3. The Arduino IDE on a Mac.

Breadboard the Circuit

The circuit portion of this project is very simple:

Take an LED and place the long lead into pin 13 on Arduino, as you can see in the Figure 1-4 breadboard view.

LED long lead inserted into pin 13 on the Arduino (image made with Fritzing.org).

Figure 1-4. LED long lead inserted into pin 13 on the Arduino (image made with Fritzing.org).

Write the Code

You can find this code in the Arduino IDE under File Examples or on the EMWA GitHub Repository | chapter-1 | blink.

/*
  Blink
  Turns on an LED on for one second,
  then off for one second, repeatedly.
  This example code is based on example code
  that is in the public domain.
*/

void setup() {
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  delay(1000);              // wait for a second
}

In this sketch, the code in loop() simply tells Arduino to set pin 13 HIGH—taking it up to 5 volts—for 1000 milliseconds (one second), followed by setting it LOW—taking it down to 0 volts—for another 1000 milliseconds.

Notice the /* ... */ sections and the // lines in the example above? Those are ways to put comments into your code to explain to others (and to yourself) what the code does: /* and */ tell the computer that everything between those marks should be ignored while running the program. // tells the computer that everything afterward on that line is a comment.

Things to Try

Modify this sketch to make the LED do something different:

  1. Blink twice as quickly.
  2. Blink twice as slowly.
  3. Light up for half a second with a two-second pause between blinks.

Congratulations, you’re an Arduino programmer! Now let’s have some real fun.

Get Environmental Monitoring with Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.