Skip to content

Blink an LED on Arduino Uno Using C/Cpp (Hardware & Simulation) : Step-by-Step Guide

blink led

Table of Contents

Abstract

Arduino UNO Development board is equipped with ATmega328p chip, which has 32K bytes of Flash program memory, 2k bytes of SRAM and 1K bytes of EEPROM. UNO board supports Embedded System C, Cpp and arduino code.

In this article we go through.

  • Accessing the built-in led (13) of the Arduino UNO board.
  • Turning on and off built-in led using arduino code.
  • Simulation using wokwi.
  • Hardware demo.

🧭 Pre-Request

  • OS : Windows / Linux / Mac / Chrome
  • Arduino IDE.

Hardware Required

  • Arduino UNO
  • Micro USB Cable.
Components Purchase Link
Arduino UNO Purchase Link
Mini B USB Cable Purchase Link

Don't own a hardware 😢

No worries,

Still you can learn using simulation. check out simulation part 😃.

Connection Table

Built-in led of Arduino UNO is internally connected to GPIO 13.

Particular GPIO Remarks
Built-in LED 13 Internally Connected

Built-in LED

📂 Code

void setup() {
  // Initializing built-in LED as OUTPUT pin
  pinMode(LED_BUILTIN, OUTPUT);
  // pinMode(13, OUTPUT);

}

void loop() {

  // Turning ON built-in LED
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);

  // Turning OFF built-in LED
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);

}

Code Explanation

👉 Accessing built-in led of Arduino UNO board.

1
2
3
4
5
6
void setup() {
  // Initializing built-in LED as OUTPUT pin
  pinMode(LED_BUILTIN, OUTPUT);
  // pinMode(13, OUTPUT);

}
  • LED_BUILTIN (GPIO pin 13) is configured as OUTPUT pin (Line number 3)
  • You can alternatively use GPIO 13 in place of LED_BUILTIN.

👉 Blinking LED

void loop() {

  // Turning ON built-in LED
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);

  // Turning OFF built-in LED
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);

}
  • Continuous loop is achieved using loop method.
  • digitalWrite is used to change the state of GPIO pin.
  • digitalWrite(LED_BUILTIN, HIGH); sets the value of GPIO 13 to HIGH or 1, which in turn turns ON the LED. (line number 11)
  • Delay of 1 second is achieved through delay(1000); method. (line number 12 & 16)
  • digitalWrite(LED_BUILTIN, LOW); sets the value of GPIO 13 to LOW or 0, which in turn turns OFF the LED. (line number 15)

Try It

  • Change the value in the sleep method and observe the change in the on and off time.
    • delay(2000), delay(4000) , etc
  • the values in terms of milli seconds.
    • delay(1000) : 1000 ms or 1 s
    • delay(500) : 500 ms or 0.5 s

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page

Result

Above code turns ON LED for 1 second and OFF for 1 second. Similar to a square wave of 0.5 Hz output connected to an LED.


Extras

Components details

Modules / Libraries Used

-Nil-