Skip to content

Interfacing LED to Arduino Nano and controlling it Using arduino code

blink led

Table of Contents

Abstract

In this article, a comprehensive step-by-step guide to interface an external LED with Arduino Nano board using arduino code. This articles lays the foundation for more advanced embedded system and IoT projects.

🧭 Pre-Request

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

Hardware Required

  • Arduino Nano.
  • LED.
  • Resistors.
  • BreadBoard.
  • Mini USB Cable.
  • Connecting wires.
  • 5V DC power supply (Optional)
Components Purchase Link
Arduino Nano link
LED link
BreadBoard large : small
Connecting Wires link
Mini USB Cable link
5V DC Adaptor link

Don't own a hardware 😢

No worries,

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

Connection Table

Particular GPIO Remarks
LED 1 10 LED-1 Anode to GPIO 10 : High Logic
LED 2 12 LED-2 Cathode to GPIO 12 : Low Logic

Info

  • LED-1 : HIGH Logic LED : ON for GPIO set to 1 : OFF for GPIO set to 0
  • LED-2 : Low Logic LED : ON for GPIO set to 0 : OFF for GPIO set to 1
  • Best practice is to connect LED in low logic.

Circuit Diagram

fig-Connection Diagram

📂 Code

#define LED_1 10
#define LED_2 12

void setup() {
  // Initialize LED as output PIN
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);

}

void loop() {
  // Turning ON External LED
  digitalWrite(LED_1, HIGH); // HIGH Logic
  digitalWrite(LED_2, LOW); // LOW Logic
  delay(2000);

  // Turning OFF External LED
  digitalWrite(LED_1, LOW);  // HIGH Logic
  digitalWrite(LED_2, HIGH); // LOW Logic
  delay(1000);

}

Code Explanation

👉 Accessing LED's.

1
2
3
4
5
6
7
8
9
#define LED_1 10
#define LED_2 12

void setup() {
  // Initialize LED as output PIN
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);

}
  • Define LED_1 to GPIO 10 & LED_2 to GPIO 12.
  • GPIO pin 10 is configured as OUTPUT pin (Line number 6)
  • GPIO pin 12 is configured as OUTPUT pin (Line number 7)

👉 Controlling LED

void loop() {
  // Turning ON External LED
  digitalWrite(LED_1, HIGH); // HIGH Logic
  digitalWrite(LED_2, LOW); // LOW Logic
  delay(2000);

  // Turning OFF External LED
  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, HIGH);
  delay(1000);

}
  • Continuous loop for blinking led is achieved using loop method.
  • digitalWrite(LED_1, HIGH) sets the value of GPIO 10 to HIGH or 1, it turns ON the LED-1. (line number 13)
  • digitalWrite(LED_2, LOW) sets the value of GPIO 12 to LOW or 0, it turns ON the LED-2. (line number 14)
  • Delay is achieved through delay(ms_value); method. (line number 15 & 20)
  • digitalWrite(LED_1, LOW) sets the value of GPIO 10 to LOW or 0, it turns OFF the LED-1. (line number 18)
  • digitalWrite(LED_2, HIGH) sets the value of GPIO 12 to HIGH or 1, it turns OFF the LED-2. (line number 19)

Try It

  • Change the value in the delay method and observe the change in the on and off time.
    • delay(2000), delay(750) , etc

Simulation

Not able to view the simulation

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

Extras

Components details

Modules / Libraries Used

NIL