Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

Arduino-Controlled Ultrasonic Sensor for Automated Lighting

Image of Arduino-Controlled Ultrasonic Sensor for Automated Lighting

Circuit Documentation

Summary

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.

Component List

HC-SR04 Ultrasonic Sensor

  • Description: A sensor that measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.
  • Pins: VCC, TRIG, ECHO, GND

Arduino Uno R3

  • Description: A microcontroller board based on the ATmega328P, widely used for building digital devices and interactive objects.
  • Pins: USB Port, Power Jack, IOREF, RESET, 3.3V, 5V, GND, VIN, Analog Pins (A0-A5), Digital Pins (0-13), AREF, SDA, SCL

12V Single Channel Relay

  • Description: An electrically operated switch that allows you to control a high power circuit with a low power signal.
  • Pins: NC, COM, NO, IN, GND, VCC

Bulb

  • Description: An electric light with a wire filament heated until it glows.
  • Pins: +VE, _VE

AC Supply

  • Description: Provides alternating current to power the bulb.
  • Pins: +ve, -ve

Wiring Details

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to 5V of Arduino Uno R3 and VCC of 12V Single Channel Relay
  • TRIG: Connected to Digital Pin 12 of Arduino Uno R3
  • ECHO: Connected to Digital Pin 11 of Arduino Uno R3
  • GND: Shared with GND of 12V Single Channel Relay

Arduino Uno R3

  • 5V: Provides power to HC-SR04 Ultrasonic Sensor and 12V Single Channel Relay
  • Digital Pin 12: Connected to TRIG of HC-SR04 Ultrasonic Sensor
  • Digital Pin 11: Connected to ECHO of HC-SR04 Ultrasonic Sensor
  • Digital Pin 8: Controls IN of 12V Single Channel Relay

12V Single Channel Relay

  • VCC: Powered by 5V from Arduino Uno R3
  • IN: Controlled by Digital Pin 8 of Arduino Uno R3
  • COM: Connected to +VE of Bulb
  • NO: Connected to -ve of AC Supply
  • GND: Shared with GND of HC-SR04 Ultrasonic Sensor

Bulb

  • +VE: Connected to COM of 12V Single Channel Relay
  • _VE: Connected to +ve of AC Supply

AC Supply

  • +ve: Connected to _VE of Bulb
  • -ve: Connected to NO of 12V Single Channel Relay

Documented Code

#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.