...

Arduino LED blink in 4 simple ways

Arduino LED blink is a very easy and simple way to start learning Arduino programming. It is just like printing “Hello world” to start learning C / C++ language.

There are various methods to blink LED with Arduino, The most basic way to blink LED is to blink LED with Arduino delay(); function, You can find it in Arduino examples, click File > examples > 01. Basics > Blink.

Arduino LED blink in four simple ways

Understand the Arduino Uno hardware

In the Arduino Uno board, the built-in LED is connected to the PB5 pin of the Atmega328P microcontroller and connected to PIN no. 13 of the Arduino board, Here we will focus on the Arduino board not the microcontroller, so using PIN no. 13 we can blink the LED. If you want to learn at the microcontroller level please visit: Arduino blink LED without Arduino Library

Arduino LED blink using delay

It is a Basic example of the Arduino board,  we can blink the LED_BUILTIN of Arduino Uno simply by making the pin no. 13 of Arduino Uno High (‘1’ or true) and low(‘0’ or false) which is attached to LED_BUILTIN.

Code:

/*             www.ucbeginner.com              */

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


void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

Code explanation:

First of all, initialize pin no. 13 or LED_BUILTIN as an output.

pinMode(LED_BUILTIN, OUTPUT);

Turn the voltage High or digital ‘1’.

digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)

Wait for 1000 ms or 1 s

delay(1000);                       // wait for a second

Turn the voltage low or digital ‘0’.

 digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

Wait for 1000 ms or 1s, void loop() run continuously, so LED will blink for always.

Arduino LED blink using millis

To blink LED with delay() is very easy to implement but when we write code for any other task with this blink code the microcontroller will wait for 2 seconds for every iteration of the void loop(), to avoid this wastage of time we use millis().

Code:

/*             www.ucbeginner.com              */

unsigned long previous_time = 0;
unsigned long current_time = 0;
volatile unsigned int  time1s = 0;
boolean LED_status = false;

void setup() {
   pinMode(LED_BUILTIN,OUTPUT);
   previous_time = millis();
 }

void loop() {

  current_time = millis();
  
  if((current_time - previous_time) >= 1000)
    { 
      digitalWrite(LED_BUILTIN, LED_status = ! LED_status);
      previous_time = current_time;
    }
    
}

Code explanation:

In void setup, we store the value of millis() function in variable previous_time, then in void loop store the value of millis() in current_time and check if current_time – previous_time greater than or equal to 1000 ms then change the status(LOW to HIGH/ HIGH to LOW) of LED and copy the value of variable current_time into previous_time to start counting milli seconds from start, if the condition is continuously checking current_time – previous_time if the condition is true then it again change the status of LED.

Exercise task for You.

Connect one external LED to pin no. 12, the LED_BUILTIN has to blink after 1000 ms or 1 second, and the external LED attached to pin no. 12 blink after 500 ms or 0.5 seconds.

Arduino LED blink using a Timer

For high accuracy we have to use timers, the Arduino Uno has three timers which are time0, timer1, and timer2.

Code:

/*             www.ucbeginner.com              */

boolean state = false;

void setup() {
  
  pinMode(LED_BUILTIN, OUTPUT);
  
  TCCR1A = 0; 

  TCCR1B = 0b00000000;
  TCCR1B = 0b00001000;

  //Set prescaler to 1024
  TCCR1B = 0b00000100;
 
  //Reset Timer1
  TCNT1 = 0;

  //for 1sec
  OCR1A = 15625;   

  // Enable Timer1 interrupt
  TIMSK1 = 0b00000010;

  // Enable global interrupts
  sei();                  
}

void loop() {}

ISR(TIMER1_COMPA_vect)
{
  if (state)                // check if ledstate is true then convert to false
  {
    state = false;
  }
  else                         // if ledstate is false then this make it true
  {
    state = true;
  }
  digitalWrite(LED_BUILTIN, state); //Set LED pin state as per ledState 
}

Code explanation

For Arduino LED blink using a Timer please see page no. 110 of datasheet atmega328P, and in void setup makes TCCR1A equal to zero and set the bit WGM13 of reg TCCR1B, set Prescaler to 1024, OCR1A = 15625 for 1 second, enables Timer1 and global interrupt, and changes the variable “state”(LOW to HIGH/ HIGH to LOW) in ISR(TIMER1_COMPA_vect).

Arduino LED blink using counter

Arduino LED blink using counter is very easy to implement, it only has 3 to 4 lines of code.

Code

/*             www.ucbeginner.com              */

long count = 0;
boolean LED_status = false;


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

void loop() {

  count++;                 
  if (count == 320000)
  {
    digitalWrite(LED_BUILTIN, LED_status = !LED_status);
    count = 0;
  }

}

Code explanation

Simply make PIN no. 13 / LED_BUILTIN is output, then in void loop increment the variable count, and if the count is equal to 320000 then change the status(LOW to HIGH/ HIGH to LOW) of LED. it is not accurate but we can blink LED with this approach.

Conclusion

To learn Arduino, the LED blink is a very helpful task, you can blink LED with different methods which four are explained above, for deep learning at the register level you can learn from Arduino blink LED without Arduino Library

Leave a Reply

Your email address will not be published. Required fields are marked *

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.