Blink LED Arduino | How to blink a led with an Adruino | Full Code and Circuit Diagram

In this tutorial, we will code an Arduino Uno to do the most basic task ever, blink a LED light. So let's get started.

What You Need:

  1. Arduino Uno Board
  2. LED
  3. 220-ohm resistor
  4. Breadboard
  5. Jumper wires


Building the Circuit:

First, lets build our circuit, Place the LED light anywhere on your breadboard. Connect one leg of the LED (the longer leg, called the anode) to one end of your 220-ohm resistor,then, connect the other end of the resistor to pin 13 on your Arduino board. Finally, connect the shorter leg of the LED (the cathode) to one of the Arduino's GND (ground) pins. All this does is the Arduino sends power to pin 13 which is where our LED is, which makes it light up.




Writing the Code:

Here is a simple code that works with our current setup, this code is easy to read and understand for starters.

// LED Blink Program for Arduino Uno

#define led LED_BUILTIN // We're using the built-in LED at pin 13

void setup() {
  pinMode(led, OUTPUT); // Set up pin 13 as an output, so it sends power
}

void loop() {
  digitalWrite(led, HIGH); // Turn the LED on
  delay(1000); // Keep it on for 1 second
  digitalWrite(led, LOW); // Turn the LED off
  delay(1000); // Keep it off for 1 second
}


Here is a more optimized version:

// LED BLINK PROGRAM FOR ARDUINO UNO

#define led LED_BUILTIN //To use Arduino's Built in LED also Connected to pin D13

bool state; //Boolean Variable to store current state of LED

void setup() {

  pinMode(led, OUTPUT); //Set the Led Pin to Output Mode

}

void loop() {
    state = !state; //Toggle from ON to OFF or OFF to ON
    digitalWrite(led, state); //Update LED pin with the state Variable
    delay(1000); //Wait for 1000ms/1s for next Toggle.
}


How the Code Works:

  1. #define led LED_BUILTIN: This line just names pin 13 as 'led' for our convenience. It's easier to remember and use in our code.
  2. setup() function: This part runs once when your Arduino first powers up. We tell our Arduino that pin 13 ('led') is going to be used for outputting power.
  3. loop() function: This is where the action happens and keeps happening. We turn the LED on by sending power to it (digitalWrite(led, HIGH)), wait for a second (delay(1000)), turn it off (digitalWrite(led, LOW)), and then wait another second. It keeps repeating this, which makes our LED blink.


Ready, Set, Blink!

That's all! We've now completed this tutorial. Feel free to check our other tutorials you might find intresting here