Rohan Barnwal
Published © GPL3+

Automatic Day-Night LED Strip Controller Using Arduino

A smart Arduino-powered system that senses darkness with an LDR and automatically switches an LED strip ON or OFF for hands-free lighting.

BeginnerFull instructions provided1 hour1
Automatic Day-Night LED Strip Controller Using Arduino

Things used in this project

Hardware components

Arduino UNO Mini
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Gravity: Digital 5A Relay Module
DFRobot Gravity: Digital 5A Relay Module
×1
LED Strip 12v
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Connections

Code

Code

Arduino
// Beginner-friendly: Read button on pin 6 and control a relay on pin 2
// Wiring (recommended):
//  - Relay module VCC -> 5V
//  - Relay module GND -> GND
//  - Relay module IN  -> Arduino digital pin 2
//  - Button one side -> Arduino pin 6
//  - Button other side -> GND
// Note: This sketch uses INPUT_PULLUP, so button connects to GND when pressed.

const int RELAY_PIN = 2;    // Relay control pin
const int BUTTON_PIN = 7;   // Input pin (button or switch)

// If your relay module is "active LOW" (common for many modules), set true.
// If your relay turns ON when IN is HIGH, set false.
const bool RELAY_ACTIVE_LOW = false;

void setup() {
  Serial.begin(9600);                 // Optional: debug output to Serial Monitor
  pinMode(RELAY_PIN, OUTPUT);         // Relay pin is an output
  pinMode(BUTTON_PIN, INPUT_PULLUP);  // Use the internal pull-up resistor

  // Make sure relay starts OFF
  if (RELAY_ACTIVE_LOW) digitalWrite(RELAY_PIN, HIGH); // HIGH -> off for active-low relay
  else digitalWrite(RELAY_PIN, LOW);                   // LOW  -> off for active-high relay

  Serial.println("Ready. Press the button to turn the relay ON.");
}
//Made By Rohan Barnwal 

void loop() {
  // Read the button. Because of INPUT_PULLUP, pressed == LOW.
  int raw = digitalRead(BUTTON_PIN);
  bool pressed = (raw == LOW); // true when button is pressed

  // Simple debounce: when we detect a press state change, wait briefly and re-read.
  static bool lastPressed = false;
  if (pressed != lastPressed) {
    delay(30);                     // small debounce delay (30 ms)
    raw = digitalRead(BUTTON_PIN);
    pressed = (raw == LOW);
    lastPressed = pressed;
  }

  // Turn relay on or off based on button
  if (pressed) {
    // Turn relay ON
    if (RELAY_ACTIVE_LOW) digitalWrite(RELAY_PIN, LOW);  // active-low -> LOW turns ON
    else digitalWrite(RELAY_PIN, HIGH);                  // active-high -> HIGH turns ON
    Serial.println("Relay: ON");
  } else {
    // Turn relay OFF
    if (RELAY_ACTIVE_LOW) digitalWrite(RELAY_PIN, HIGH); // active-low -> HIGH turns OFF
    else digitalWrite(RELAY_PIN, LOW);                   // active-high -> LOW turns OFF
    Serial.println("Relay: OFF");
  }

  delay(100); // small loop delay to avoid flooding Serial
}

Credits

Rohan Barnwal
39 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments