Datasheet
Shield, Lights, Servo Motors • Chapter 2
Robotics with the BOE Shield-Bot • 53
void setup() // Built-in initialization block
{
pinMode(13, OUTPUT); // Set digital pin 13 -> output
}
Now that digital pin 13 is set to output, we can use digitalWrite to turn the LED light on
and off. Take a look at the next picture. On the left,
digitalWrite(13, HIGH) makes the
Arduino’s microcontroller connect digital pin 13 to 5 V, which turns on the LED. On the
right, it shows how
digitalWrite(13, LOW) makes it connect pin 13 to GND (0 V) to turn
the LED off.
Here’s the
loop function from the next sketch. First, digitalWrite(13, HIGH) turns the
light on,
delay(500) keeps it on for a half-second. Then digitalWrite(13, LOW) turns it
off, and that’s also followed by
delay(500). Since it’s inside the loop function’s block, the
statements will repeat automatically. The result? The light will flash on/off once every
second.
void loop() // Main loop auto-repeats
{
digitalWrite(13, HIGH); // Pin 13 = 5 V, LED emits light
delay(500); // ..for 0.5 seconds
digitalWrite(13, LOW); // Pin 13 = 0 V, LED no light
delay(500); // ..for 0.5 seconds
}
Example Sketch: HighLowLed
Reconnect the programming cable to your board.
Create and save the HighLowLed sketch, and run it on your Arduino.
Verify that the pin 13 LED turns on and off, once every second. (You may see the
LED flicker a few times before it settles down into a steady blinking pattern. This
happens when reprogramming the Arduino.)
digitalWrite(13, HIGH);
digitalWrite(13, LOW);