This circuit is designed to utilize an HC-SR04 Ultrasonic Sensor to detect proximity and control a relay which in turn switches a bulb on or off based on the detected distance. The Arduino Uno R3 serves as the central microcontroller unit to process the sensor data and control the relay. An AC supply is used to power the bulb through the relay.
#define TRIGGER_PIN 12 // Arduino pin connected to the trigger pin of the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin connected to the echo pin of the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to measure.
#define RELAY_LINE1_PIN 8 // Pin connected to the relay
#include "NewPing.h"
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Setup for ultrasonic sensor pins and maximum distance.
unsigned int critical_distance_cms = 15; // Cutoff distance for detecting motion (in cm).
bool state = 0; // Variable to store the relay status (light), 0 = off, 1 = on.
unsigned long previousMillis = 0; // Variable to track time
unsigned long interval = 15000; // Time interval (15 seconds)
void setup() {
Serial.begin(9600); // Initialize serial monitor with baud rate 9600.
pinMode(RELAY_LINE1_PIN, OUTPUT); // Set relay pin as output.
digitalWrite(RELAY_LINE1_PIN, HIGH); // Turn off light (relay) initially (active low relay).
}
void loop() {
unsigned long currentMillis = millis(); // Get current time
delay(50); // Wait 50ms between pings for stable distance reading.
unsigned int distance = readDistance(); // Read the distance of the detected object by the ultrasonic sensor.
Serial.print("Distance: ");
Serial.print(distance); // Print the distance detected by the ultrasonic sensor (0 = out of set range).
Serial.println(" cm");
// If an object is close to the sensor (less than the critical distance)
if (distance < critical_distance_cms) {
if (!state) { // Only turn on the light if it is off
Serial.println("Motion detected, Light ON!");
digitalWrite(RELAY_LINE1_PIN, LOW); // Turn on light (relay active low)
state = 1; // Update relay status to on
}
previousMillis = currentMillis; // Reset time when object is detected
}
// If 15 seconds have passed since the last detection and the light is still on
if (state && (currentMillis - previousMillis >= interval)) {
Serial.println("Timeout, Light OFF!");
digitalWrite(RELAY_LINE1_PIN, HIGH); // Turn off light (relay active low)
state = 0; // Update relay status to off
}
}
// Read distance from the ultrasonic sensor and return the median value
unsigned int readDistance() {
unsigned int distance = sonar.convert_cm(sonar.ping_median(7)); // Read 7 values and take the median
// If the value is 0, it means no object is in front of the sensor
if (distance == 0) {
distance = MAX_DISTANCE; // Set to maximum distance to avoid unnecessary light switching
}
return distance;
}
This code is designed to read the distance from the HC-SR04 Ultrasonic Sensor and control the relay based on the distance measured. If an object is detected within a critical distance, the relay is activated, turning on the light. If no object is detected within the critical distance for a set interval, the relay is deactivated, turning off the light.