Preview Mode Links will not work in preview mode

Learn Programming and Electronics with Arduino

Mar 9, 2017

The first program you usually write when learning a new programming language is called, “Hello World”. Its only function is to display the words “Hello World” on the computer monitor.

When learning to program microcontrollers such as the Arduino, the equivalent of “Hello World” is a program that blinks an LED. Guess what it is called – Blink.

You Will Need
An LED (any color works fine)
A 220 Ohm Resistor
An alligator clip (not essential but makes the circuit easier)
Fourteen small and smooth rocks from the a western pacific island (not essential but adds an esoteric feel)
NOTE: On most Arduino boards there is an LED soldered right by pin 13 – it is actually connected to pin 13 – so if you do not have an LED laying around (or a resistor for that matter), you can use the board mounted LED – it will blink with the same sketch.

Step-by-Step Instructions
Insert the short leg of the LED into the GND pin on your Arduino (use the GND pin closest to pin 13).
Connect the 220 Ohm resistor to pin 13 on the Arduino. It doesn’t matter which way you connect the resistor.
Use the alligator clip to connect the long leg of the LED to the other leg of the resistor. If you do not have an alligator clip, twist the two leads together as best you can to get a steady electrical connection.
Plug the Arduino board into your computer with a USB cable.
Open up the Arduino IDE.
Open the sketch for this section.
Click the Verify button on the top left. It should turn orange and then back to blue.
Click the Upload button. It will also turn orange and then blue once the sketch has finished uploading to your Arduino board.
Now monitor the Arduino board – the LED should be blinking.

This image was made using Fritzing.

The Arduino Code

  Turns on an LED on for one second, then off for one second, repeatedly.
  Most Arduinos have an on-board LED you can control. On the Uno and
  Leonardo, it is attached to digital pin 13. If you're unsure what
  pin the on-board LED is connected to on your Arduino model, check
  the documentation at
  This example code is in the public domain.
  modified 8 May 2014
  by Scott Fitzgerald
// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);
// the loop function runs over and over again forever
void loop() {
  digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);              // wait for a second

Discuss the Sketch
On the previous page, you can see the Arduino code that constitutes the Blink program – make sure to read each line, even if doesn’t make any sense yet.

Notice the comments at the top of the program. Note the use of the multi-line comments syntax /* */. It is always a good idea to take time and see what the programmer has to say about the sketch they wrote. The comments will likely be concise, describing how the program works or what it should accomplish. A few may even tell you how to connect the circuit.

The next block of code you encounter in the Blink sketch is…

void setup( ) {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
Recall that the setup() function is in almost every Arduino sketch you encounter. Inside the curly braces is code that will only be run once by the Arduino. For this sketch notice the function pinMode() is inside the curly braces of the setup() function.

Let me start by saying that pinMode() is a wonderful function. If you recall, functions can take arguments. The pinMode() function takes two arguments – it wants a pin number and a mode for that pin. The pin number is easy, 0 to 13 for digital pins, and A0 to A5 for analog pins.

The mode is an easy designation as well, you want the pin to be either an INPUT (good for reading a sensor) or an OUTPUT (good for powering an LED).

When a pin is set as an INPUT, it prepares the pin to read voltages that will be applied at the pin. When a pin is set as an OUTPUT, it prepares the pin to output voltage – more on this later.

In this example, we want to light an LED, this requires that voltage is applied at pin 13. Therefore, we need the mode of pin 13 set as an OUTPUT. Keep in mind that setting the mode of the pin to OUTPUT does not apply a voltage, it enables the pin to supply a voltage once it is programmed to do so.

Moving on to the final block of code, we come to our favorite and ubiquitous function void loop()…

void loop( ) {
  digitalWrite(led,HIGH);// turn the LED on (HIGH is the voltage level)
  delay(1000); // wait for a second
  digitalWrite(led,LOW); // turn the LED off by making the voltage LOW
  delay(1000); // wait for a second
You may recall that void loop() runs over and over again. In this loop, we see two functions: digitalWrite() and delay().

The function digitalWrite() is used to apply either HIGH or LOW voltage to a pin. HIGH will apply 5 volts to the pin you designate and LOW will apply 0 volts. If you apply 5 volts to a pin that is connected through an LED to ground, then the LED will light up.

There is a voltage difference between the pin and ground, thus current is able to flow through the LED. If you apply 0 volts to the same pin the LED will not light up because no current is being “pushed” through the circuit – the voltage at ground and at the pin are both zero.

digitalWrite() takes two arguments, the pin number and the level of voltage, either HIGH or LOW, as we have discussed above.
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
We want to start the loop by applying HIGH voltage to pin 13, where our LED is attached. In the digitalWrite() function we use the variable name ‘led’ to refer back to the value 13 – which was previously assigned.

Notice that there is no need to explicitly write 13 in the digitalWrite() function, instead we are able to use the variable ‘led’. Variables are awesome like that – they can take the place of numbers and are much easier to change and track. Every time we use the ‘led’ variable, the Arduino will see 13. If we later decide to move our LED to pin 10, then all we have to do is change the value of ‘led’ once, and all the instances of ‘led’ are changed too.

Once digitalWrite() function has been executed, the LED will get bright – we just applied 5 volts, so hey, that makes sense. The next thing we do is delay the Arduino sketch to enjoy the bright glow of our LED. To do this, we use the delay() function.

The delay() function takes one argument – the number of milliseconds you want the program to delay. In this case, we want 1000 milliseconds, which equals one second.

First we said, “apply high voltage” and now we say – wait one second. Our LED will glow for exactly one second. But that gets old, and we have to stay true to the name of the sketch, so next we tell the Arduino to write a LOW voltage to pin 13 – to do this we use the same function as before, namely digitalWrite(), but, this time, we want LOW voltage instead of HIGH.

Now the LED goes dark because no current is flowing. In order to sustain that darkness we use the delay() function again for one second. Now the LED is dark for one second.

We are at the end of the loop. We turned the LED on for a second, then we turned it off for a second – what next? Once the Arduino completes the loop, it starts at the top of the loop again and repeats like a broken record.

Once again, the LED will light up, delay a second and then go dark for one second. And repeat – now you have a blinking LED – pretty cool for just a couple lines of code!


Try on Your Own Challenge
Change the value of the delay functions. What happens?
Change the number of the led variable to 12 and move the long leg of your LED to pin 12. See what happens.
Further Reading