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

Arduino-Controlled Ultrasonic Sensor Relay for Automated Lighting

Image of Arduino-Controlled Ultrasonic Sensor Relay for Automated Lighting

Circuit Documentation

Summary

This circuit is designed to utilize an Arduino UNO microcontroller to control a relay module based on the distance measured by two HC-SR04 Ultrasonic Sensors. The relay module then switches a bulb on or off. A potentiometer is used to adjust the contrast of an LCD Display 16x2, which is likely used to display information. A resistor is connected in series with an LED to limit the current through the LED.

Component List

Bulb

  • Description: A simple light bulb.
  • Pins: positive, negative

HC-SR04 Ultrasonic Sensor

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

Potentiometer

  • Description: A variable resistor typically used for adjusting levels such as volume or, in this case, contrast for an LCD.
  • Pins: GND, Output, VCC

Relay Module 2 Channel

  • Description: A module with two relays that can control the high power circuit with a low power signal.
  • Pins: GND, IN1, IN2, VCC, NC1, COM, NO1, NC2, NO2

Arduino UNO

  • Description: A microcontroller board based on the ATmega328P.
  • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0-A5, SCL, SDA, AREF, D0-D13

LED: Two Pin (red)

  • Description: A basic red LED.
  • Pins: cathode, anode

Resistor

  • Description: A passive two-terminal electrical component that implements electrical resistance as a circuit element.
  • Properties: 200 Ohms

LCD Display 16x2

  • Description: A liquid crystal display capable of displaying 16 characters per line across 2 lines.
  • Pins: LEDK, LEDA, DB0-DB7, E, RW, RS, VO, VDD, VSS

Wiring Details

Bulb

  • positive: Connected to Relay Module 2 Channel NO1
  • negative: Connected to Relay Module 2 Channel GND, Arduino UNO GND, LCD Display 16x2 LEDK and VSS

HC-SR04 Ultrasonic Sensor

  • VCC: Connected to LED: Two Pin (red) anode, Potentiometer GND
  • TRIG: Connected to Arduino UNO D12 (for one sensor) and D10 (for the other sensor)
  • ECHO: Connected to Arduino UNO D11 (for one sensor) and D9 (for the other sensor)
  • GND: Connected to Resistor pin1, Potentiometer VCC

Potentiometer

  • GND: Connected to HC-SR04 Ultrasonic Sensor VCC
  • Output: Connected to LCD Display 16x2 VO
  • VCC: Connected to HC-SR04 Ultrasonic Sensor GND

Relay Module 2 Channel

  • GND: Connected to Bulb negative, Arduino UNO GND, LCD Display 16x2 LEDK and VSS
  • IN1: Connected to Arduino UNO D8
  • VCC: Connected to Arduino UNO 5V, LCD Display 16x2 VDD and LEDA
  • NO1: Connected to Bulb positive

Arduino UNO

  • GND: Connected to Relay Module 2 Channel GND, Bulb negative, LCD Display 16x2 LEDK and VSS
  • 5V: Connected to Relay Module 2 Channel VCC, LCD Display 16x2 VDD and LEDA
  • D12: Connected to HC-SR04 Ultrasonic Sensor TRIG (for one sensor)
  • D11: Connected to HC-SR04 Ultrasonic Sensor ECHO (for one sensor)
  • D10: Connected to HC-SR04 Ultrasonic Sensor TRIG (for the other sensor)
  • D9: Connected to HC-SR04 Ultrasonic Sensor ECHO (for the other sensor)
  • D8: Connected to Relay Module 2 Channel IN1
  • D7-D2: Connected to LCD Display 16x2 RS, E, DB4-DB7 respectively

LED: Two Pin (red)

  • cathode: Connected to Resistor pin2
  • anode: Connected to HC-SR04 Ultrasonic Sensor VCC

Resistor

  • pin1: Connected to HC-SR04 Ultrasonic Sensor GND
  • pin2: Connected to LED: Two Pin (red) cathode

LCD Display 16x2

  • LEDK: Connected to Relay Module 2 Channel GND, Bulb negative, Arduino UNO GND
  • LEDA: Connected to Relay Module 2 Channel VCC, Arduino UNO 5V
  • DB7-DB4: Connected to Arduino UNO D2-D5 respectively
  • E: Connected to Arduino UNO D6
  • RW: Not connected
  • RS: Connected to Arduino UNO D7
  • VO: Connected to Potentiometer Output
  • VDD: Connected to Relay Module 2 Channel VCC, Arduino UNO 5V
  • VSS: Connected to Relay Module 2 Channel GND, Bulb negative, Arduino UNO GND

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). Maximum sensor distance is rated at 400-500cm.

#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 at 115200 baud to see ping results.
  pinMode(RELAY_LINE1_PIN, OUTPUT);
  digitalWrite(RELAY_LINE1_PIN, HIGH);  // Turn the light off
}

void loop() {
  delay(50);                     // Wait 50ms between pings (about 20 pings/sec).
  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 (0 = outside set distance range)
  Serial.println("cm");

  // Someone is near the door
  if (distance < critical_distance_cms)
  {
    while (distance < critical_distance_cms)
    {
      // Check if they moved away
      distance = readDistance();

      delay(5); // Do nothing until the person moves away from the door
    }

    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
    }
  }
}

// Updates the value of the Ultrasonic reading
unsigned int readDistance()
{
  // Read 7 values from the ultrasonic and get the median value (median filter)
  // Gets rid of noisy reading
  unsigned int distance = sonar.convert_cm(sonar.ping_median(7));

  // The value 0 indicates that the ultrasonic sensor is reading nothing in front of it
  // Set this distance to max distance so the light doesn't switch unnecessarily
  if (distance == 0)
  {
    distance = MAX_DISTANCE;
  }
  
  return distance;
}

This code is designed to read the distance from the HC-SR04 Ultrasonic Sensor and toggle the state of a relay when an object is detected within a critical distance. The relay then controls the power to a bulb. The LCD display is likely used to show status or distance information, but the code for the display is not included in the provided code snippet.