At first, you might consider this kind of explanation too unnecessary, a bit like when I was in school and I had to study Dante’s Divina Commedia (every Italian student has to go through that, as well as another book called I promessi sposi, or The Betrothed—oh, the nightmares). For each line of the poems, there were a hundred lines of commentary! However, the explanation will be much more useful here as you move on to writing your own programs.
/ Blinking LED
A comment is a useful way for us to write little notes. The preceding title comment just reminds us that this program, Example 4-1, blinks an LED.
const int LED = 13; // LED connected to
// digital pin 13
const int means that LED is the name of an integer number that can’t be changed (i.e., a constant) whose value is set to 13. It’s like an automatic search-and-replace for your code; in this case, it’s telling Arduino to write the number 13 every time the word LED appears.
The reason we need the number 13 is that the preinstalled LED we mentioned earlier is attached to Arduino pin 13. A common convention is to use uppercase letters for constants.
void setup()
This line tells Arduino that the next block of code will be a function named setup()
.
{
With this opening curly brace, a block of code begins.
pinMode(LED, OUTPUT); // sets the digital
// pin as output
Finally, a really interesting instruction! pinMode()
tells Arduino how to configure a certain pin. All of the Arduino pins can be used either as input or output, but we need to tell Arduino how we intend to use the pin.
In this case, we need an output pin to control our LED.
pinMode()
is a function, and the words (or numbers) specified inside the parentheses are called its arguments. Arguments are whatever information a function needs in order to do its job.
The pinMode()
function needs two arguments. The first argument tells pinMode()
which pin we’re talking about, and the second argument tells pinMode()
whether we want to use that pin as an input or output. INPUT
and OUTPUT
are predefined constants in the Arduino language.
Remember that the word LED is the name of the constant which was set to the number 13, which is the pin number to which the LED is attached. So, the first argument is LED
, the name of the constant.
The second argument is OUTPUT
, because when Arduino talks to an actuator such as an LED, it’s sending information out.
}
This closing curly brace signifies the end of the setup()
function.
void loop()
{
loop()
is where you specify the main behaviour of your interactive device. It will be repeated over and over again until you remove power from the board.
digitalWrite(LED, HIGH); // turns the LED on
As the comment says, digitalWrite()
is able to turn on (or off) any pin that has been configured as an output. Just as we saw with the pinMode()
function, digitalWrite()
expects two arguments, and just as we saw with the pinMode()
function, the first argument tells digitalWrite()
what pin we’re talking about, and just as we saw with the pinMode()
function, we’ll use the constant name LED
to refer to pin number 13, which is where the preinstalled LED is attached.
The second argument is different: in this case, the second argument tells digitalWrite()
whether to set the voltage level to 0 V (LOW
) or to 5 V (HIGH
).
Imagine that every output pin is a tiny power socket, like the ones you have on the walls of your apartment. European ones are 230 V, American ones are 110 V, and Arduino works at a more modest 5 V. The magic here is when software can control hardware. When you write digitalWrite(LED, HIGH
), it turns the output pin to 5 V, and if you connect an LED, it will light up. So at this point in your code, an instruction in software makes something happen in the physical world by controlling the flow of electricity to the pin. Turning on and off the pin will now let us translate these into something more visible for a human being; the LED is our actuator.
On the Arduino, HIGH means that the pin will be set to 5 V, while LOW means the pin will be set to 0 V.
You might wonder why we use HIGH and LOW instead of ON and OFF. It’s true that HIGH or LOW usually correspond to on and off, respectively, but this depends on how the pin is used. For example, an LED connected between 5V and a pin will turn on when that pin is LOW and turn off when the pin is HIGH. But for most cases you can just pretend that HIGH means ON and LOW means OFF.
delay(1000); // waits for a second
Although Arduino is much slower than your laptop, it’s still very fast. If we turned the LED on and then immediately turned it off, our eyes wouldn’t be able to see it. We need to keep the LED on for a while so that we can see it, and the way to do that is to tell Arduino to wait for a while before going to the next step. delay()
basically makes the microcontroller sit there and do nothing for the amount of milliseconds that you pass as an argument. Milliseconds are thousandths of seconds; therefore, 1,000 milliseconds equals 1 second. So the LED stays on for 1 second here.
digitalWrite(LED, LOW); // turns the LED off
This instruction now turns off the LED that we previously turned on.
delay(1000); // waits for a second
Here, we delay for another second. The LED will be off for 1 second.
}
This closing curly brace marks the end of the loop()
function. When Arduino gets to this, it starts over again at the beginning of loop()
.
To sum up, this program does this:
- Turns pin 13 into an output (just once at the beginning)
- Enters a loop
- Switches on the LED connected to pin 13
- Waits for a second
- Switches off the LED connected to pin 13
- Waits for a second
- Goes back to beginning of the loop
We hope that wasn’t too painful. If you didn’t understand everything, don’t feel discouraged. As we mentioned before, if you’re new to these concepts, it takes a while before they make sense. You’ll learn more about programming as you go through the later examples.
Before we move on to the next section, we want you to play with the code. For example, reduce the amount of delay, using different numbers for the on and off pulses so that you can see different blinking patterns. In particular, you should see what happens when you make the delays very small, but use different delays for on and off. There is a moment when something strange happens; this “something” will be very useful when you learn about pulse-width modulation in “Controlling Light with PWM”.
Leave a Reply