Washroom Light Automatic System with Ultrasonic Sensor , OLED Display , 5v Relay using Arduino nano

 Circuit Diagram:




Ultrasonic Relay Toggle System

This project implements a touchless light control system using an Ultrasonic sensor, an OLED display, and a Relay module. The system toggles a light (represented by the relay) ON or OFF whenever an object is detected within a specific range.

🔗 Wokwi Simulation

You can view and run the simulation here:
https://wokwi.com/projects/461731925906866177

🛠 Features

  • Touchless Control: Uses ultrasonic waves to detect proximity.
  • Toggle Logic: Prevents flickering; one detection turns it ON, the next detection turns it OFF.
  • Visual Feedback: Real-time status updates on a 128x64 I2C OLED display.
  • Industrial Interface: Relay output to control high-voltage appliances (simulated).

📟 Component Pinout

Component Pin Name Arduino/MCU Pin
Ultrasonic Sensor Trig Pin 9
Ultrasonic Sensor Echo Pin 10
Relay Module Signal Pin 12
OLED Display SDA A4 (or SDA)
OLED Display SCL A5 (or SCL)

🧠 Logic Explanation

1. Distance Calculation

The HC-SR04 sensor measures return time and converts it to cm:
Distance = (Duration * 0.034) / 2

2. State-Change Detection

The code checks for the moment an object appears (Edge Triggering) to prevent constant toggling while someone stands in front of the sensor.

3. Toggle Mechanism

The relayState flips every time a detection occurs, switching the lights between ON and OFF status.

DIAGRAM.JSON - PROJECT CONFIGURATION
{
  "version": 1,
  "author": "Computer work",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": 52.8, "left": -48.5, "attrs": {} },
    {
      "type": "wokwi-hc-sr04",
      "id": "ultrasonic1",
      "top": -123.3,
      "left": -4.1,
      "attrs": { "distance": "2" }
    },
    { "type": "wokwi-relay-module", "id": "relay1", "top": 163.4, "left": 9.6, "attrs": {} },
    {
      "type": "board-ssd1306",
      "id": "oled1",
      "top": 41.54,
      "left": 221.03,
      "attrs": { "i2cAddress": "0x3c" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 140.4,
      "left": 195.8,
      "attrs": { "color": "cyan" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 140.4,
      "left": 224.6,
      "attrs": { "color": "magenta" }
    },
    {
      "type": "wokwi-led",
      "id": "led3",
      "top": 140.4,
      "left": 253.4,
      "attrs": { "color": "blue" }
    },
    {
      "type": "wokwi-led",
      "id": "led4",
      "top": 140.4,
      "left": 282.2,
      "attrs": { "color": "white" }
    },
    {
      "type": "wokwi-text",
      "id": "text1",
      "top": -163.2,
      "left": -96,
      "attrs": { "text": "Ultrasonic Sensor For Person Detection in this Project" }
    },
    {
      "type": "wokwi-text",
      "id": "text2",
      "top": 211.2,
      "left": 211.2,
      "attrs": { "text": "Room Lights" }
    },
    {
      "type": "wokwi-text",
      "id": "text3",
      "top": 278.4,
      "left": 0,
      "attrs": { "text": "Relay for Switching" }
    },
    {
      "type": "wokwi-text",
      "id": "text4",
      "top": 9.6,
      "left": 297.6,
      "attrs": { "text": "Display" }
    }
  ],
  "connections": [
    [ "ultrasonic1:GND", "nano:GND.2", "black", [ "v19.2", "h-20.4" ] ],
    [ "oled1:GND", "nano:GND.1", "black", [ "v-19.2", "h-67.2", "v105.6", "h-57.6" ] ],
    [ "relay1:GND", "nano:GND.1", "black", [ "h-28.8", "v-38.8", "h115.2" ] ],
    [ "relay1:VCC", "nano:5V", "red", [ "h-19.2", "v-38.4", "h86.4" ] ],
    [ "ultrasonic1:VCC", "nano:5V", "red", [ "v28.8", "h-153.6", "v144", "h163.2" ] ],
    [ "oled1:VCC", "nano:5V", "red", [ "v-28.8", "h-95.85", "v124.8", "h-96" ] ],
    [ "ultrasonic1:TRIG", "nano:9", "green", [ "v19.2", "h-77.2" ] ],
    [ "ultrasonic1:ECHO", "nano:10", "green", [ "v9.6", "h-96.8", "v76.8" ] ],
    [ "relay1:IN", "nano:12", "green", [ "h-76.8", "v-192.2", "h38.4" ] ],
    [
      "oled1:SDA",
      "nano:A4",
      "green",
      [ "v-48", "h67.27", "v240", "h-393.6", "v-105.6", "h76.8" ]
    ],
    [
      "oled1:SCL",
      "nano:A5",
      "green",
      [ "v-57.6", "h86.7", "v259.2", "h-412.8", "v-124.8", "h96", "v-19.2" ]
    ],
    [ "led1:C", "led2:C", "black", [ "v19.2", "h29.2" ] ],
    [ "led2:C", "led3:C", "black", [ "v19.2", "h29.2" ] ],
    [ "led3:C", "led4:C", "black", [ "v19.2", "h29.2" ] ],
    [ "led1:C", "nano:GND.2", "black", [ "v19.2", "h-47.6", "v-172.8", "h-86.4" ] ],
    [ "nano:3.3V", "relay1:COM", "red", [ "v38.4", "h-9.6", "v115.2", "h182.4", "v-76.8" ] ],
    [ "relay1:NO", "led1:A", "green", [ "h20.4", "v7.8", "h76.8" ] ],
    [ "led1:A", "led2:A", "green", [ "v28.8", "h9.6" ] ],
    [ "led2:A", "led3:A", "green", [ "v28.8", "h28.8" ] ],
    [ "led3:A", "led4:A", "green", [ "v28.8", "h28.8" ] ]
  ],
  "dependencies": {}
}
Arduino Code
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// Pin Definitions
const int trigPin = 9;
const int echoPin = 10;
const int relayPin = 12;

// Variables
bool relayState = false;      // Current state of the relay
bool objectPresent = false;   // Whether an object is currently in range
bool lastObjectPresent = false; // Previous detection state
int threshold = 350;           // Detection distance in cm

void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(relayPin, OUTPUT);
  
  digitalWrite(relayPin, LOW); // Start with lights off

  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    for(;;); // Don't proceed if OLED fails
  }
  
  updateDisplay("System Ready");
  delay(1000);
  updateDisplay("Lights: OFF");
}

void loop() {
  long duration;
  int distance;

  // Trigger the ultrasonic sensor
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = duration * 0.034 / 2;

  // Check if an object is within the threshold
  if (distance > 0 && distance < threshold) {
    objectPresent = true;
  } else {
    objectPresent = false;
  }

  // Toggle Logic: Trigger only when an object first appears (State Change)
  if (objectPresent == true && lastObjectPresent == false) {
    relayState = !relayState; // Flip the state
    
    if (relayState) {
      digitalWrite(relayPin, HIGH);
      updateDisplay("Lights: ON");
    } else {
      digitalWrite(relayPin, LOW);
      updateDisplay("Lights: OFF");
    }
    
    delay(5000); // Small debounce delay to prevent double-triggering
  }

  lastObjectPresent = objectPresent; // Save the state for the next loop
  delay(50); 
}

void updateDisplay(String message) {
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 20);
  display.println(message);
  display.display();
}

Comments