Blinking an LED

Now that we have our Arduino connected to our Mac, let’s go ahead and walk through the hardware equivalent of “Hello World,” the blinking LED.

Note

An Arduino program is normally referred to as a sketch.

There are a number of example sketches included in the development environment. The one we’re looking for can be found by selecting FileExamples1.BasicsBlink. The example sketch will open in a new window; see Figure 1-5.

The Blink example

Figure 1-5. The Blink example

Every Arduino sketch consists of two parts: the setup and the loop. Every time the board is powered up or the board’s reset button is pushed, the setup() routine is run. After that finishes, the board runs the loop routine. When that completes, and perhaps somewhat predictably, the loop() is run again and again. Effectively the contents of the loop() sit inside an infinite while loop.

Before we go ahead and build and deploy this example to our Arduino, let’s take a look at the code:

void setup() {
  pinMode(13, OUTPUT);1
}

void loop() {
  digitalWrite(13, HIGH);2
  delay(1000);
  digitalWrite(13, LOW);2
  delay(1000);
}
1

Arduino pins default to INPUT. However, here we set pin 13 to behave as an OUTPUT pin. In this state, the pin can provide up to 40 mA of current to other devices. This is enough current to brightly light up an LED or run many sensors, but not enough current to run most relays, solenoids, or motors. See http://arduino.cc/en/Tutorial/DigitalPins.

2

If the pin has been configured for OUTPUT, its voltage will be set to the corresponding value: 5 V for HIGH, 0 V (ground) for LOW.

Effectively, then, this piece of code causes the voltage on pin 13 to be brought HIGH for a second (1,000 ms) and then LOW for a further second before the loop starts again, bringing the voltage HIGH once more.

As I mentioned before, some of the digital pins on the Arduino board are specialized; pin 13 is one of these. On most boards (including the Uno), it has an LED and resistor attached to it that’s soldered onto the board itself. Therefore, the effect of this sketch will be to turn the embedded LED on and off with a periodicity of 1 second.

While there is already a built-in LED on the board, this will look more impressive if we add a “real” LED as well. While any LED will do, LEDs are directional components and must not be inserted backward. Look carefully at the two legs of the LED; one should be shorter than the other. The shorter leg corresponds to the ground, while the longer leg is the positive.

Insert the short leg into the GND pin, and the longer leg into pin 13, as shown in Figure 1-6.

An LED connected to pin 13; the short leg is inserted into the GND pin

Figure 1-6. An LED connected to pin 13; the short leg is inserted into the GND pin

Uploading the Sketch

The first step of getting the sketch ready for transfer to the Arduino is to click on the Verify/Compile button (see Figure 1-7). This will compile your code, checking it for errors, and then translate your program into something that is compatible with the Arduino architecture. After a few seconds, you should see the message “Done compiling” in the status bar and something along the lines of “Binary sketch size: 1018 bytes (of a 32256 byte maximum)” in the Notification area.

Once you see that message, go ahead and click on the Upload button (see Figure 1-7 again). This will initiate the transfer of the compiled code to the board via the USB connection.

Wait a few seconds; you should see the RX and TX LEDs on the board flashing as the data is transferred over the serial connection from your Mac to the board. If the upload is successful, the message “Done uploading” will appear in the status bar.

A few seconds after the upload finishes, you should see the pin 13 LED on the board (labeled L on the PCB) start to blink (in orange), along with the LED we inserted into pin 13. One second on, one second off. If you see that, congratulations, you’ve just successfully got the hardware equivalent to “Hello World” to compile and run on the Arduino board.

The Arduino development environment with various controls highlighted

Figure 1-7. The Arduino development environment with various controls highlighted

Get iOS Sensor Apps 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.