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

Arduino-Controlled Ultrasonic Relay Switch for Automated Lighting

Image of Arduino-Controlled Ultrasonic Relay Switch for Automated Lighting

Circuit Documentation

Summary

The circuit is designed to control a light bulb using an Arduino UNO microcontroller in conjunction with an HC-SR04 Ultrasonic Sensor and a 12V Single Channel Relay. The system operates by detecting the presence of an object within a certain distance from the ultrasonic sensor. If an object is detected within the predefined critical distance, the state of the relay is toggled, which in turn switches the light bulb on or off. The relay is powered by a 12V supply, and the light bulb is connected to a 220V power source.

Component List

Arduino UNO

  • Microcontroller board based on the ATmega328P.
  • It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.

HC-SR04 Ultrasonic Sensor

  • An ultrasonic distance sensor capable of measuring distances from 2cm to 400cm.
  • It has four pins: VCC, TRIG, ECHO, and GND.

12V Single Channel Relay

  • An electromechanical switch that allows control of a large current circuit by a smaller current signal.
  • It has six pins: NC (Normally Closed), COM (Common), NO (Normally Open), IN (Input), GND, and VCC.

Power 220V

  • A power source providing 220V AC.
  • It has two wires: hot wire and neutral wire.

Bulb

  • An electric light with two pins: +VE and _VE.

Wiring Details

Arduino UNO

  • D12 connected to TRIG pin of HC-SR04 Ultrasonic Sensor.
  • D11 connected to ECHO pin of HC-SR04 Ultrasonic Sensor.
  • D8 connected to IN pin of 12V Single Channel Relay.

HC-SR04 Ultrasonic Sensor

  • VCC connected to VCC of 12V Single Channel Relay.
  • GND connected to GND of 12V Single Channel Relay.
  • TRIG connected to D12 of Arduino UNO.
  • ECHO connected to D11 of Arduino UNO.

12V Single Channel Relay

  • VCC connected to VCC of HC-SR04 Ultrasonic Sensor.
  • GND connected to GND of HC-SR04 Ultrasonic Sensor.
  • IN connected to D8 of Arduino UNO.
  • COM connected to +VE of Bulb.
  • NO connected to neutral wire of Power 220V.

Power 220V

  • Neutral wire connected to NO of 12V Single Channel Relay.
  • Hot wire connected to _VE of Bulb.

Bulb

  • +VE connected to COM of 12V Single Channel Relay.
  • _VE connected to hot wire of Power 220V.

Documented Code

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters).

#define RELAY_LINE1_PIN 8

#include "NewPing.h"
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

unsigned int critical_distance_cms = 50;  // Cutoff distance at which the light will switch.
bool state = 0;

void setup() {
  Serial.begin(9600); // Open serial monitor to see ping results.
  pinMode(RELAY_LINE1_PIN, OUTPUT);
  digitalWrite(RELAY_LINE1_PIN, HIGH);  // Turn the light off initially.
}

void loop() {
  delay(50);  // Wait 50ms between pings.
  unsigned int distance = readDistance(); // Current distance of any object facing the ultrasonic sensor.

  Serial.print("Ultrasonic: ");
  Serial.print(distance); // Send ping, get distance in cm and print result.
  Serial.println("cm");

  // Toggle the relay if an object is within the critical distance.
  if (distance < critical_distance_cms) {
    while (distance < critical_distance_cms) {
      distance = readDistance(); // Check if the object has moved away.
      delay(5);
    }

    state = !state; // Change the state of the relay.

    if (state) {
      Serial.println("Door Open!");
      digitalWrite(RELAY_LINE1_PIN, LOW); // Turn the light on.
    } else {
      Serial.println("Door Closed!");
      digitalWrite(RELAY_LINE1_PIN, HIGH);  // Turn the light off.
    }
  }
}

// Function to read the distance from the ultrasonic sensor.
unsigned int readDistance() {
  unsigned int distance = sonar.convert_cm(sonar.ping_median(7)); // Median filter to reduce noise.

  if (distance == 0) {
    distance = MAX_DISTANCE; // Treat no reading as maximum distance.
  }
  
  return distance;
}

This code operates the ultrasonic sensor and relay to control the light based on the proximity of an object to the sensor. It uses the NewPing library for interfacing with the ultrasonic sensor and includes a median filter to improve the reliability of the sensor readings. The relay is controlled by toggling its state when an object is detected within the critical distance.