After my last video introducing the Arduino, some of you commented that you needed an example of how the Arduino works that is super simple to understand. So in this video we’re going to do the absolute simplest of projects. We’re going to be blinking an LED!

Arduino: Blinking an LED Video

We’ve made detailed tutorial video on this topic that we think you might enjoy!

Blinking an LED on the Arduino

We will attach an LED to PIN 12 of the Arduino and make it blink on one second intervals. We’re not even going to use a breadboard in this project.  We will only require an LED, a 220 Ohm resistor, and 6 lines of code. And the last of those 2 of those lines are just repeats with the LED state changed.

Two Quick Basics

What is an LED? An LED is a light emitting diode. A diode is an electronic component that limits the flow of current to one direction. An LED is just a diode that emits light, but the same rules apply. This is why unlike normal light bulbs, an LED has a positive and negative lead. In order to limit the amount of current traveling across the LED and burning it out, you’ll need to add a resistor in line with it to protect it. In our case that will be a 220 ohm resistor.

Second thing of note. In many of the Arduino tutorials on the web they will show wiring this circuit without a resistor and attaching to PIN 13. They will claim pin 13 has an integrated resistor. This is true, but their assumption is wrong. The resistor is wired in parallel to pin 13 and only protects the integrated on-board LED. If you follow those tutorials it is possible you will ultimately burn out the LED or even worse fry the ATmega micro-controller. Don’t do this. Use a resistor.

Parts List for this Project

If you’d like to do this project, we’ve created a simple parts list for you to get started:

Wiring the LED to the Arduino

In order to make this project for blinking an LED as simple as possible to understand, we’re going to skip the breadboard completely. The first thing we’re going to do is just wrap one leg of the resistor around the anode (or positive) lead of the LED. The anode is the longer lead.

Now that we’ve done that, we will insert the cathode (or negative) lead of the LED into the Arduino ground pin. The cathode is the shorter lead.

Now, take the other lead of the resistor and plug it into PIN 12. You can actually use any of the digital I/O pins on the Arduino for this, and after this video you should experiment with moving it around and just changing the pin number in the code!

That’s literally all there is to the hardware side of this project.

RELATED: List of Arduino Sensors

Writing the Code for the Arduino

Now lets move over to the software side. Open the Arduino IDE. You’ll see a void setup, and a void loop. These are the two main functions of the code for blinking an LED on the Arduino. The setup function is where you configure things. The loop function is just what it sounds like. All code you put here will just execute over and over again for infinity (or until the Arduino loses power).

Above the setup function let’s add one single line of code:


int LED_PIN = 12;

This just defines an integer variable and makes our code easier to understand. In our setup function, we need to add another single line:


pinMode(LED_PIN, OUTPUT);

This tells the Atmega that we’re going to use PIN 12 to output 5V. In the loop section we’ll add four more lines to turn the LED on and wait one second and then turn it off:


digitalWrite(LED_PIN, HIGH);

delay(1000);

digitalWrite(LED_PIN, LOW);

delay(1000);

And put it all together you get the following completed Arduino LED blink code:

int LED_PIN = 12;

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

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

Now save your code using file, save and give it a name. I chose BlinkLED.ino.

Finishing the Arduino Blinking LED Project

To program your Arduino for blinking an LED, just click the arrow button at the top left corner and your code will compile and upload to the ATmega micro-controller. Within a second or two your LED should start blinking.

That’s the power of the Arduino platform. It’s incredible simple and easy to understand.

Ramping it up: Arduino Knight Rider Code

If you want to take this to the next level, you’ll just need six LEDs and and six 220 Ohm resistors.  Connect the resistors and LEDs to pins 7 through 12 and upload the following code to your Arduino. If you’d like more information on this and would like multiple examples, check out our Arduino Knight Rider and Cylon Eye article.

int pinArray[] = {12, 11, 10, 9, 8, 7};
int count = 0;
int timer = 10;

void setup(){
  for (count=0;count<6;count++) {
    pinMode(pinArray[count], OUTPUT);
  }
}

void loop() {
  for (count=0;count<5;count++) { digitalWrite(pinArray[count], HIGH); delay(timer); digitalWrite(pinArray[count + 1], HIGH); delay(timer); digitalWrite(pinArray[count], LOW); delay(timer*2); } for (count=5;count>0;count--) {
   digitalWrite(pinArray[count], HIGH);
   delay(timer);
   digitalWrite(pinArray[count - 1], HIGH);
   delay(timer);
   digitalWrite(pinArray[count], LOW);
   delay(timer*2);
  }
}

You’ll the the most awesome Knight Rider effect on your LEDs!

If you run into any problems with this project, leave a comment below and we will do our best to help you out!