Extra 5% OFF Use Code: OL05
Free Shipping over ₹999

Interface LEDs with Arduino

Introduction

The LED is one of the simplest and most popular electronic components. It is used to indicate the status of a device, create visual effects, and much more. In this tutorial, you will learn how to control an LED using an Arduino.

Required Components

  • Arduino UNO R3 SMD
  • 5 mm LED
  • 220-ohm resistor
  • Breadboard
  • Jumper wires

Pinout

Circuit Diagram / Wiring

  • Connect the longer leg (anode) of the LED to digital pin 13 through a 220-ohm resistor.
  • Connect the shorter leg (cathode) to the GND pin.

Arduino Code / Programming

  • Open the Arduino IDE.
  • Go to Tools > Board and select your Arduino board (e.g., Arduino Uno).
  • Go to Tools > Port and select the port to which your Arduino is connected.
  • Copy the provided code into your Arduino IDE.
int ledPin = 13; // Pin where the LED is connected

void setup() {
  pinMode(ledPin, OUTPUT); // Set the LED pin as output
}

void loop() {
  digitalWrite(ledPin, HIGH); // Turn the LED on
  delay(1000);                // Wait for 1 second
  digitalWrite(ledPin, LOW);  // Turn the LED off
  delay(1000);                // Wait for 1 second
}

  • Verify and upload the code to your Arduino board.

Explanation of the Code

  • This code turns the LED on and off in 1-second intervals.
  • The setup() function sets the LED pin as an output, and the loop() function repeatedly toggles the LED state.

Challenges

Challenge 1: Fibonacci Blink Sequence

Problem Statement:

  1. The LED connected to pin 13 should blink according to the Fibonacci sequence.
  2. The Fibonacci sequence starts with 0, 1, and each subsequent number is the sum of the previous two.
  3. Use the Fibonacci sequence to determine the LED’s ON time (in milliseconds), starting from the second Fibonacci number (1). The OFF time should always be 500ms.
  4. Limit the maximum ON time to 8 seconds (8000ms) to prevent overly long delays.
  5. After reaching the maximum ON time, reset the sequence and start over from the beginning.

Code:

const int ledPin = 13;     // LED connected to digital pin 13
int prev = 0, curr = 1;    // Variables to hold Fibonacci numbers

void setup() {
  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  // Blink the LED using the current Fibonacci number for ON time
  digitalWrite(ledPin, HIGH);
  delay(curr * 1000);      // ON time in milliseconds (1 sec = 1000ms)
  digitalWrite(ledPin, LOW);
  delay(500);              // OFF time fixed at 500ms

  // Generate next Fibonacci number
  int next = prev + curr;
  prev = curr;
  curr = next;

  // Limit the ON time to 8 seconds
  if (curr > 8) {
    prev = 0;
    curr = 1;
  }
}

Challenge 2: LED Strobe with Increasing Speed

Problem Statement:

  1. The LED connected to pin 13 should blink like a strobe light, starting with a slow rate (e.g., 1000ms ON, 1000ms OFF).
  2. After each blink cycle, the time between blinks should reduce by 10%, making the strobe speed up.
  3. The minimum ON and OFF time should be capped at 100ms to prevent excessive speed.
  4. Once the minimum time is reached, the cycle should reset and start with the initial slow speed again.

Code:

const int ledPin = 13;     // LED connected to digital pin 13
int delayTime = 1000;      // Initial delay time (in milliseconds)

void setup() {
  pinMode(ledPin, OUTPUT); // Set LED pin as output
}

void loop() {
  // Blink the LED
  digitalWrite(ledPin, HIGH);
  delay(delayTime);        // ON time
  digitalWrite(ledPin, LOW);
  delay(delayTime);        // OFF time

  // Decrease the delay time by 10% each cycle
  delayTime = delayTime * 0.9;

  // Ensure the delay time doesn't go below 100ms
  if (delayTime < 100) {
    delayTime = 1000;  // Reset to the original delay time
  }
}

Challenge 3 : Prime Number Blinker

Problem Statement:

  1. The LED connected to pin 13 will blink in a pattern based on prime numbers.
  2. The LED will stay ON for a duration equal to the next prime number (in milliseconds), starting from the first prime (2).
  3. The LED will then turn OFF for 500ms between each prime number.
  4. After reaching a prime greater than 100ms, the pattern should reset and start over from the first prime number.

Code :

const int ledPin = 13;  // LED connected to digital pin 13
int primeNumbers[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};  // Array of prime numbers
int primeIndex = 0;

void setup() {
  pinMode(ledPin, OUTPUT);  // Set LED pin as output
}

void loop() {
  // Blink the LED based on prime numbers
  digitalWrite(ledPin, HIGH);
  delay(primeNumbers[primeIndex] * 100); // ON time (prime number * 100ms)
  digitalWrite(ledPin, LOW);
  delay(500); // OFF time

  // Move to the next prime number
  primeIndex++;

  // If we exceed the list of prime numbers, reset
  if (primeIndex >= sizeof(primeNumbers) / sizeof(primeNumbers[0])) {
    primeIndex = 0;
  }
}

More Challenging Questions

Challenge 1: Random LED Blink with Delays

Problem Statement:

  1. The LED connected to pin 13 should blink ON and OFF at random intervals.
  2. The ON and OFF times should be randomly selected between 100ms and 2000ms (2 seconds).
  3. Each time the LED turns on, it should blink a random number of times between 1 and 5 before turning off.
  4. Use the random() function to generate the random intervals and number of blinks.

Challenge 2: Breathing LED Effect (No PWM)

Problem Statement:

  1. The LED should simulate a breathing effect, where it gradually increases in brightness and then decreases (without using PWM).
  2. Achieve the effect by controlling the ON time and OFF time dynamically, using small steps to simulate brightness.
  3. The LED should “breathe” slowly, taking about 3 seconds to complete one full cycle (bright to dim to bright).

Please feel free to submit all replies in the comment box

Testing and Troubleshooting

  • If the LED doesn’t blink, double-check the wiring and the resistor value.
  • Ensure that the LED is connected correctly (anode to pin 13, cathode to GND).

    Leave a Reply

    Your email address will not be published.

    Need Help?