LED Blinking using Arduino


In order to blink an LED using Arduino, we first connect perform the hardware connections. Choose a pin of your board that supports digital output. We are using the Arduino Uno board, and we will choose pin 7. The circuit will look like this −

As you can see, one end of a resistor is connected to pin 7 of Arduino Uno. The other end the resistor is connected to the longer leg (positive) of the LED. The shorter leg of the LED is connected to GND.

The value of the resistor can be of the order of 100 Ohms. We'll choose a 220 Ohm resistor. Now, the code will look like the following −

Example

int LED_PIN = 7;
void setup() {
   pinMode(LED_PIN, OUTPUT);
}
void loop() {
   // put your main code here, to run repeatedly:
   digitalWrite(LED_PIN, HIGH);
   delay(1000);
   digitalWrite(LED_PIN, LOW);
   delay(1000);
}

As you can see, we have first defined the LED_PIN. Next, in the setup, we have defined that pin as an Output pin. In the loop, we are setting the pin as high and low consecutively, to generate the blink. In case this code doesn't generate the blink pattern, check your connections. A common mistake is to connect the longer leg of the LED to GND and the shorter leg to the resistor, which won't generate any blinking pattern.

If you don't have an external LED, depending on which board you have, you could use the BUILTIN_LED of the board. All you need to do is replace the first line of the code to the following −

int LED_PIN = LED_BUILTIN;

On the Arduino Uno board, pin 13 is connected to the built-in LED. Different pins may be connected to the built-in LED on other Arduino boards. But you need not worry about it. The LED_BUILTIN variable will assign the correct pin depending on which board you have selected.

Congratulations on your first LED Blinking code on Arduino.

Updated on: 23-Mar-2021

441 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements