Introduction: Arduino - Blinking LED

About: Visit my blog on Tumblr to get more than just the tutorials that are posted there, but I will try to get all of them posted on both sites!

The ‘Hello World!’ of Arduino, it’s the Blinking LED!

If you asked neigh any Arduino user what the first program they wrote was [For Arduino], chances are they would tell you it was this. All you need to get started is:

- Arduino [I used a UNO, but any will do]

- An LED A resistor [There are many calculators to work out the exact ohm rated resistor you will need, but I generally just use a 330ohm one]

- Wire

- Solderless breadboard

Step 1: The Circuit

First, connect pin 7 on your Arduino to a spot on your breadboard, then your resistor. On the other side of the resistor, insert your LED.

NOTE: LEDs are polarised, meaning that they have a certain way they need to be connected if you are to not blow them. Connect the positive lead of the LED to the resistor and run a wire from the ground lead to the GND pin on the Arduino. The result should look like the schematic on this tutorial.

Step 2: The Code

Now for the coding half. This is where the real magic of Arduino happens. This little blue board can be programmed to do almost anything you can think up. The code we need to write for this is fairly simple, but first you need to download the Arduino IDE from their website.

To begin, you want to set up your IDE for writing your code. Every Arduino program needs these two things for it to work. Start by writing this:

void setup() {
}
void loop() {
}

How this works is that when you reset your Arduino or boot it up, the code within the ‘void setup’ section is run. Once that is finished, ‘void loop’ gets run over and over until power is removed from the Arduino. Before your ‘void setup’ though, we want to assign pin seven a name so that we know what we are controlling on it later one. Write before ‘void setup’:

int led = 7;

This assigns the name ‘led’ the integer 7. Now every time we write ‘led’ in our code, Arduino will interpret that as 7. Within your ‘void setup’, we want to write a line of code that will let the Arduino know that we want pin 7, or led, to act as an output. An output is a pin that is either HIGH or LOW, meaning it is either ON or OFF. But we don’t need to worry about that just yet. Just remember that an OUTPUT gives out electricity, and INPUT collects information from pins. After void setup() {, write:

pinMode(led, OUTPUT);

Keep in mind that it is very important that each line ends with a semicolon.
Now for the actual controlling of the LED. After void loop() {, write the following:

digitalWrite(led, HIGH);

This will set pin 7 HIGH, or ON, meaning that while it is high it is outputting voltage. If you ran your code now, the LED would light up and stay lit. Give it a try if you want. But the point of this tutorial was to make the LED blink, was it not? On the next line, write “delay(1000);”
This will make the Arduino pause for a full second. If you wanted it to be half a second, write 500 instead of 1000. You can make this number anything you want. But it still doesn’t blink.
On the line after that, write:

digitalWrite(led, LOW);

This turns your LED off after the period you set it to delay. Run your code now. Notice anything strange? It still doesn’t blink! Give it some thought and see if you can work it out yourself, keeping in mind what i’ve told you about the void loop function.

Worked it out?

Don’t worry if you didn’t, it could be confusing. Also if you didn’t even try well then you my friend are mean :P
Anyway, the reason is because it is looping as soon as it reaches the line that turns the LED off, going right back to where it turns on. There isn’t any time for you to observe the LED being off. The solution? Add another delay line after you turn the LED off. Hey presto, you have a blinking LED!

Your final code should look like as follows:

int led = 7;
void setup() {
pinMode(led, OUTPUT);
}

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

Step 3: Going Further

Using what we spoke about with integers, you could create something a little more interesting, like altering the code for the delay as such:

int led = 7;
int del = 1000;

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

void loop() {
digitalWrite(led, HIGH);
delay(del);
digitalWrite(led, LOW);
delay(del);
}

This is simply replacing the number 1000 with a stored integer. You can use this to your advantage in many different ways. I won’t explain the following code, you can work out how it is working for yourself, but it is just one example of the endless possibilities.

int led = 7;
int del = 5000;

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

void loop() {
digitalWrite(led, HIGH);
delay(del);
digitalWrite(led, LOW);
delay(del);
del = del - 100;
}