DIY Automatic Light Controller with Ultrasonic Sensor and Arduino

So, I had this idea for an automatic light controller using an Arduino and an ultrasonic sensor (HC-SR04) to turn on a light whenever someone crosses a certain distance. The idea was pretty simple: you walk near the sensor, and it detects your presence, automatically turning on a light—perfect for when you need a little extra brightness, like at the stairs or in a dark room.

What I used:

  • Arduino Board (any Arduino will work, I used a basic Uno for this)
  • HC-SR04 Ultrasonic Sensor (for detecting objects)
  • Relay Module (to control the light)
  • LED (just for testing purposes here, but you can use a real light later)
  • Jumper Wires
  • Breadboard (optional)
  • 6V Sealed Lead Acid Battery (or any other power source you prefer)

automatic Light Controller with Ultrasonic Sensor and Arduino
For this demonstration, I used a typical LED to represent the light, but the goal is really to make this work for something more useful, like an AC bulb near the stairs. I’m powering everything with a 6V sealed lead acid battery, which is pretty reliable for this kind of setup. If you want to dive deeper into the technical side of sealed lead acid rechargeable batteries, check out this article I wrote: Sealed Lead Acid Rechargeable Battery.

How It Works:

The idea is pretty straightforward. The ultrasonic sensor sends out a pulse, waits for it to bounce back, and measures the time it took. Based on that, it calculates the distance between the sensor and an object in front of it. If the object is within a certain distance (like 30 cm in this case), the relay turns on, which could activate a light or any other appliance.

Since this project is designed for use near stairs, I decided to bump the threshold distance to 1 meter (instead of 30 cm) to give people more time to trigger the light as they walk up the stairs. I also factored in the width of the stairs, so a longer detection range makes more sense.

Circuit Diagram:

Here’s the basic wiring layout:

circuit diagram of DIY Automatic Light Controller with Ultrasonic Sensor and Arduino

  • HC-SR04 Ultrasonic Sensor:

    • VCC → 5V (Arduino)
    • GND → GND (Arduino)
    • TRIG → Pin 9 (Arduino)
    • ECHO → Pin 10 (Arduino)
  • Relay Module:

    • VCC → 5V (Arduino)
    • GND → GND (Arduino)
    • IN → Pin 11 (Arduino)

Make sure the relay is rated for the kind of light (or appliance) you plan to control.

Arduino Code:

Here’s the code that makes everything work. It reads the distance from the ultrasonic sensor and triggers the relay when someone is within range.

#define TRIG_PIN 9       // Trigger pin of the ultrasonic sensor
#define ECHO_PIN 10      // Echo pin of the ultrasonic sensor
#define RELAY_PIN 11     // Pin to control the relay (light)

long duration;
int distance;
int threshold = 100;  // Threshold distance in cm (1 meter for this use case)

void setup() {
  pinMode(TRIG_PIN, OUTPUT);  // Set trigger pin as output
  pinMode(ECHO_PIN, INPUT);   // Set echo pin as input
  pinMode(RELAY_PIN, OUTPUT); // Set relay pin as output
  
  Serial.begin(9600);         // Start serial communication for debugging
  Serial.println("System Initialized");
}

void loop() {
  // Send a pulse to the ultrasonic sensor
  digitalWrite(TRIG_PIN, LOW);  
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  // Read the pulse duration
  duration = pulseIn(ECHO_PIN, HIGH);
  
  // Calculate the distance (in cm)
  distance = duration * 0.0344 / 2;
  
  // Print the distance for debugging
  Serial.print("Distance: ");
  Serial.println(distance);
  
  // Check if the distance is below the threshold (1 meter for this use case)
  if (distance < threshold) {
    digitalWrite(RELAY_PIN, LOW);  // Turn on the relay (light)
    Serial.println("Relay State: ON");
  }
  else {
    digitalWrite(RELAY_PIN, HIGH);   // Turn off the relay (light)
    Serial.println("Relay State: OFF");
  }
  
  delay(100);  // Small delay to avoid rapid changes in sensor readings
}

How It Works:

  • Pins:

    • TRIG_PIN connects to the trigger pin of the ultrasonic sensor.
    • ECHO_PIN connects to the echo pin of the sensor.
    • RELAY_PIN controls the relay to turn the light on and off.
  • Distance Calculation: The sensor sends a pulse, and the ECHO_PIN waits for the pulse to return. The Arduino calculates the time difference and converts that to distance.

  • Threshold Check: The system checks if the distance is less than 1 meter. If it is, it triggers the relay to turn the light on. If it’s more than 1 meter, the relay is turned off.

  • Serial Output: The distance is printed to the Serial Monitor for debugging, and it shows the state of the relay (on or off).

     Watch the following video demonstration:

The following video shows simulation the automatic light controller with ultrasonic sensor and Arduino:

Practical Applications:

This simple automatic light controller is perfect for lighting up areas like stairs. It can detect when someone is close enough and automatically turn on the light, making it safer and more convenient to navigate in the dark. You could easily modify the threshold distance for other applications, like controlling lights in a hallway or a room.

Related Projects:

This project isn't just limited to light control. Here are a few other cool ideas using ultrasonic sensors:

Conclusion:

That’s it! This automatic light controller is a great example of how simple tech like an Arduino and ultrasonic sensors can be used to make everyday life a little more automated. Whether it’s lighting up a dark staircase at home or building other automation projects, this is a great starting point. You can customize the setup as needed and control all sorts of devices, turning your home into a smarter, more efficient space.

 

Post a Comment

Previous Post Next Post