The Code, Step by Step

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.

        
        
        
        // Example 01 :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.

        #define LED 13 // LED connected to
                       // digital pin 13

#define is 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 replacement is the first thing done when you click Verify or Upload to I/O Board (you never see the results of the replacement as it's done behind the scenes). We are using this command to specify that the LED we're blinking is connected to the Arduino pin 13.

        
        
        
        voidsetup()

This line tells Arduino that the next block of code will be called setup().

{ With this opening curly bracket, 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. Digital pins can be used either as INPUT or OUTPUT. In this case, we need an output pin to control our LED, so we place the number of the pin and its mode inside the parentheses. pinMode is a function, and the words (or numbers) specified inside the parentheses are arguments. INPUT and OUTPUT are constants in the Arduino language. (Like variables, constants are assigned values, except that constant values are predefined and never change.)

} This closing curly bracket 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 switch the board off.

        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. The first argument (in this case, LED) specifies which pin should be turned on or off (remember that LED is a constant value that refers to pin 13, so this is the pin that's switched). The second argument can turn the pin on (HIGH) or off (LOW).

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 becomes 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 at will now let us translate these into something more visible for a human being; the LED is our actuator.

        delay(1000);       // waits for a second

Arduino has a very basic structure. Therefore, if you want things to happen with a certain regularity, you tell it to sit quietly and do nothing until it is time to go to the next step. delay() basically makes the processor sit there and do nothing for the amount of milliseconds that you pass as an argument. Milliseconds are thousandths of seconds; therefore, 1000 milliseconds equals 1 second. So the LED stays on for one second here.

        digitalWrite(LED, LOW);     // turns the LED off

This instruction now turns off the LED that we previously turned on. Why do we use HIGH and LOW? Well, it's an old convention in digital electronics. HIGH means that the pin is on, and in the case of Arduino, it will be set at 5 V. LOW means 0 V. You can also replace these arguments mentally with ON and OFF.

        delay(1000); // waits for a second

Here, we delay for another second. The LED will be off for one second.

} This closing curly bracket marks end of the loop function.

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

I hope that wasn't too painful. You'll learn more about how to program as you go through the later examples.

Before we move on to the next section, I 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 later in this book.

Get Getting Started 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.