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)
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:
-
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:
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:
- Automatic Liquid Overflow Detection with Ultrasonic Sensor: Detect liquid levels in a tank to prevent overflow using ultrasonic sensors.
- PID Control of DC Fan with Arduino and Ultrasonic Sensor: Use an ultrasonic sensor to control the speed of a DC fan via PID control.
- Intruder Sensor with Ultrasonic Sensor: Detect intruders and trigger alarms using an ultrasonic sensor.
- Automatic Water Overflow Controller with Arduino: Control water levels in tanks to prevent overflow with an ultrasonic sensor.
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.