...

Arduino LED blink with/without Arduino library

To start with Arduino first we start with Arduino LED blink using Arduino Uno board and as well as we will focus on the microcontroller of Uno board that is Atmega328/P, LED blink is very simple with Arduino library but you can’t learn with this, for learning you have to bypass Arduino Uno board and focus on Atmega328/P.

Arduino blink LED with Arduino Library

Code Explanation

Blink LED with Library is very simple to start, Open Arduino IDE then Click on File > Example 01. Basics > Blink. You will see a code that has Arduino basic functions 1. Void setup() and 2. Void loop(),

1.Void setup()

This function runs once when you press reset or power the board.

2.void loop()

This function runs repeatedly.

In the setup() function, we initialize the digital pin as an output, the name of this pin is LED_BUILTIN, this PIN is connected to the built-in LED of the Arduino Uno board. The code is

pinMode(LED_BUILTIN, OUTPUT);

In loop() function to blink LED with a time of 1 second, we set LED_BUILTIN PIN HIGH then delay for 1 second(or 1000 milliseconds) then set LED_BUILTIN PIN LOW then delay for 1 second (or 1000 milliseconds), This is loop() function so it will run continuously. Upload this code to the Arduino Uno board, we will see the LED on Arduino Uno will start blinking.

Code

/*                                
 *                                 Arduino LED BLINK
 *                            https://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
}

Performing Blink-Led task how much we have learned?

The answer is we only achieve the task Arduino blink LED and learning is negligible. For learning please follow further this post till the end.

Arduino blink LED without Arduino Library

To blink the LED of the Arduino Uno board without Arduino library we have to code at the register level, first, we see a schematic of the Arduino board.

Schematic of Arduino Uno

Arduino Uno Schematic

In the above image of the schematic, we can see the Builtin led of Uno board is connected to PIN # of 13 of Uno board, and this PIN is mapped (please see first ATmega328/P Arduino Pin Mapping) to PB5 PIN of Atmega328/P.

Registers of Atmega328/P for Blink LED

DDRB – The Port B Data Direction Register

First, we have to set the direction of this PIN by using The port B data direction of Register (DDRB) in datasheet page no 72.

The Port B Data Direction Register

LED is connected to PIN # 5 of port B, so make PIN # 5 is HIGH(‘1’ or TRUE) and other is LOW(‘0’ or False),

DDRB=0b00100000;

Or

 DDRB=0x20;

It is equivalent to pinMode(LED_BUILTIN, OUTPUT);

PORTB – The Port B Data Register

For blink LED that is connected to PIN # 5 of port B we have to set(HIGH) bit # 5 of  The Port B Data Register for LED on then clear (LOW) the bit # 5 of The Port B Data Register.

Port B Data Register of Atmega328P

For set PB5

PORTB = 0b00100000;

Or

PORTB = 0x20;

For Clear PB5

PORTB =0b00000000;

Or

PORTB=0x00;

It is equivalent to

digitalWrite(LED_BUILTIN, HIGH);  

and

digitalWrite(LED_BUILTIN, LOW);  

Code

/*
 *                       Arduino LED Blink using Register
 *                       https://www.ucbeginner.com/              
 */

void setup() {
   DDRB=0b00100000;
  // or DDRB=0x20;
}

void loop() {
     PORTB = 0b00100000;
     // or PORTB = 0x20;
     delay(1000);                       // wait for a second
     PORTB =0b00000000;
    //or PORTB=0x00;
     delay(1000);
}

Upload this code to the Arduino Uno board, we will see the LED on Uno will start blinking.

Conclusion

To learn microcontroller (for now ATmega328/P) we have started with Arduino Uno which has Atmega328/P, first, we performed a very simple task that is Arduino LED blink, Simply achieve this task use Arduino library and for learning, microcontroller performs this task with register level code. We will learn more by implementing this register level code strategy on the further tasks of Atmega328/P.

9 Comments

    1. It means the 5th bit of port B register, built-in LED of Arduino Uno board is connected to this.

  1. I thought it was going to become some boring old publish, however it seriously compensated for my time. Ill post a link to this web page on my blog. Im certain my website visitors will locate that extremely useful.

  2. Thank you for the auspicious writeup. It in fact was a
    amusement account it. Look advanced to more added agreeable from you!
    By the way, how could we communicate?

Comments are closed.

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