Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Uno Smart Distance-Based Relay Control with Ultrasonic Sensor

View and Edit Project In Cirkit DesignerImage of Arduino Uno Smart Distance-Based Relay Control with Ultrasonic Sensor

Circuit Documentation

Summary

This circuit is designed to measure distance using an HC-SR04 Ultrasonic Distance Sensor and control an AC bulb through a 1 Channel Relay based on the measured distance. The system is powered by an Arduino Uno R3 microcontroller, which reads the distance from the ultrasonic sensor and toggles the relay to turn the AC bulb on or off depending on the distance.

Component List

  1. Arduino Uno R3

    • Description: A microcontroller board based on the ATmega328P.
    • Pins: D8, D9, D10, D11, D12, D13, GND, AREF, SDA, SCL, D0/RX, D1/Tx, D2, D3, D4, D5, 6, D7, A5/SCL, A4/SDA, A3, A2, A1, A0, Vin, 5V, 3.3V, RESET, IOREF, NONE, USB Jack, Power Jack
  2. 1 Channel Relay 5V

    • Description: A relay module that allows control of high voltage devices with a low voltage signal.
    • Pins: VCC, GND, IN, NC, COM, NO
  3. HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

    • Description: An ultrasonic sensor used for measuring distance.
    • Pins: VCC, TRIG, ECHO, GND
  4. AC Bulb

    • Description: A standard AC bulb.
    • Pins: P, N
  5. AC Supply

    • Description: An AC power supply.
    • Pins: +ve, -ve

Wiring Details

Arduino Uno R3

1 Channel Relay 5V

HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

AC Bulb

AC Supply

Code Documentation

const int relayPin = 8; // Pin connected to the relay
const int trigPin = 9; // Pin connected to the HC-SR04 TRIG
const int echoPin = 10; // Pin connected to the HC-SR04 ECHO
const int numReadings = 5; // Number of readings to average

void setup() {
  // Initialize pin D8 as an output for the relay
  pinMode(relayPin, OUTPUT);
  // Initialize pins D9 and D10 for the ultrasonic sensor
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // Start the serial communication
  Serial.begin(9600);
}

void loop() {
  long totalDuration = 0;
  for (int i = 0; i < numReadings; i++) {
    // Send a 10us pulse to trigger the sensor
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

    // Read the echo pin and calculate the distance
    long duration = pulseIn(echoPin, HIGH);
    totalDuration += duration;
    delay(50); // Small delay between readings
  }

  long averageDuration = totalDuration / numReadings;
  int distance = averageDuration * 0.034 / 2;

  // Print the distance to the serial monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // If the distance is less than 100 cm, turn the relay on
  if (distance < 100) {
    digitalWrite(relayPin, HIGH);
  } else {
    // If the distance is 100 cm or more, turn the relay off
    digitalWrite(relayPin, LOW);
  }

  // Small delay before the next measurement
  delay(100);
}

This code initializes the pins for the relay and the ultrasonic sensor, reads the distance from the sensor, and toggles the relay based on the measured distance. If the distance is less than 100 cm, the relay is turned on, otherwise, it is turned off. The distance is also printed to the serial monitor.