Our First Program

Our little Nano is already running a program. They all ship with the Blink program installed on them at the factory. You can see a little red light emitting diode (LED) blinking every second on the tiny board.

The Arduino software we just downloaded and installed (it's called the Arduino IDE, which is short for Integrated Development Environment, but we can just call it the editor, or the IDE) has quite a few example programs we can look at. One of them (in the Basics menu) is called Blink.

Here is a condensed version of it (I have removed the comments to make it clear which parts are actually needed):

Here is the code you will want to Copy and Paste into the editor window of the IDE:

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

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

To run this program yourself, follow these steps:

  1. Erase any code that is in the editor window of the IDE.
  2. Use Copy and Paste to enter the code into the IDE.
  3. Click on the Upload button to compile the program and send it to the Nano.

At this point, nothing much seems to have changed. That's because our new program is the same as the one that was making the little LED blink in the first place.

But now we can look at the program in a little more detail, so we can make changes, and see what they do on the computer.

Let's look at the first little bit of the program:

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

In every Arduino program, there are two functions. One is called setup(), and the otherloop().

The setup() function is called first, and it is called only once. When it is called, any instructions inside it are performed by the Nano. In our setup() function, there is only one line -- pinMode( 13, OUTPUT );  -- which tells the computer to make pin 13 an output. Pins are the little metal legs of the computer chip, and pin 13 is the one that is connected to the red LED. Making it an output means we can make electric current come out of it, and that makes the LED light up. Our other choice is to make it an input, and we will talk about that later when we want to sense something about the outside world.

The rest of our program is the loop() function:

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

The loop() function is called after the setup() function, and it is called over and over again, for as long as the little computer has power.

Our loop() function has four lines of code. The first line turns on the LED, by setting pin 13 to HIGH. This makes 5 volts appear on that pin, and the LED lights up.

Next, our program delays for 100 milliseconds. That's a tenth of a second. Not a lot of time, just enough to let us see the LED flash.

The third line, digitalWrite( 13, LOW ); turns off the power to pin 13. The LED goes dark.

The last line delays for a full 1000 milliseconds. That's a whole second.

After those four lines are run, the loop() function is done, and it returns control to the program that called it (a hidden function called main(), which is called when the computer first gets power). The main() function just calls loop() again, over and over. So our little LED blinks on for a tenth of a second, stays off for a full second, and blinks on again, forever, or at least as long as the computer has power.

Now we are ready to make some changes. Suppose we wanted the light to stay on for a bit longer, say one second. We would change the first delay, so instead of 100, it read 1000. Try it:

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

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

Remember, once you have made a change to the program, you have to click on the Upload button to compile the program and send it to the Nano.

Is the light now blinking on for a longer time?