LED auto-off timer using built-in LED

LED auto-off timer using built-in LED

Main Diagram

Hardware Required

  • PICUNO Microcontroller board (built-in LED on GPIO 14)
  • USB cable (for power and programming)

Description

When the program starts, the LED connected to GPIO 14 turns ON. After a delay of 5 seconds, the LED is turned OFF automatically. This demonstrates a basic event-timer mechanism where GPIO output is controlled based on elapsed time.

Circuit Diagram

Circuit

  • Connect the PICUNO board to the computer using a USB cable.
  • No external wires or breadboard are required.
  • The built-in LED is internally connected to GPIO 14.
  • The program automatically handles turning the LED ON and OFF.

Schematic

This project uses the built-in LED on GPIO 14 of the PicUNO board. No additional circuit components are required.

Code - C

int ledPin = 14;  // Built-in LED pin

unsigned long startTime;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);  // Turn LED ON
  Serial.begin(9600);
  Serial.println("LED is ON");
  startTime = millis();
}

void loop() {
  if (millis() - startTime > 5000) {
    digitalWrite(ledPin, LOW);  // Turn LED OFF
    Serial.println("LED is OFF");
    while (true); // Stop loop after turning OFF
  }
}
pinMode() - Sets GPIO 14 as output.

digitalWrite(ledPin, HIGH) - Turns ON the LED.

millis() - Tracks elapsed time since startup.

Once 5 seconds pass, the LED is turned OFF and the loop is halted.

Code - Micropython

from machine import Pin
import time

led = Pin(14, Pin.OUT)

led.value(1)  # Turn LED ON
print("LED is ON")

start_time = time.ticks_ms()

while True:
    if time.ticks_diff(time.ticks_ms(), start_time) > 5000:
        led.value(0)  # Turn LED OFF
        print("LED is OFF")
        break
Pin(14, Pin.OUT) - Initializes GPIO 14 as output.

led.value(1) - Turns LED ON at program start.

time.ticks_ms() - Captures start time in milliseconds.

time.ticks_diff() - Calculates time difference for non-blocking delay.

After 5 seconds, the LED is turned OFF and program exits.