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

ESP32-Based Ultrasonic Distance Measurement System with Wi-Fi and LED Indicators

View and Edit Project In Cirkit DesignerImage of ESP32-Based Ultrasonic Distance Measurement System with Wi-Fi and LED Indicators

Circuit Documentation

Summary

This circuit utilizes an ESP32 microcontroller to interface with four HC-SR04 ultrasonic sensors and four red LEDs. The ESP32 reads distance measurements from the ultrasonic sensors and controls the LEDs based on these readings. Additionally, the ESP32 is configured to connect to a Wi-Fi network and serve a web page displaying sensor data. A buzzer is also included to provide audible alerts based on sensor readings.

Component List

  1. ESP32 (30 pin)

    • Description: A powerful microcontroller with Wi-Fi and Bluetooth capabilities.
    • Pins: EN, VP, VN, D34, D35, D32, D33, D25, D26, D27, D14, D12, D13, GND, Vin, D23, D22, TX0, RX0, D21, D19, D18, D5, TX2, RX2, D4, D2, D15, 3V3
  2. HC-SR04 Ultrasonic Sensor

    • Description: A sensor used to measure distance by using ultrasonic waves.
    • Pins: VCC, TRIG, ECHO, GND
  3. LED: Two Pin (red)

    • Description: A simple red LED used for visual indication.
    • Pins: cathode, anode

Wiring Details

ESP32 (30 pin)

HC-SR04 Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor

HC-SR04 Ultrasonic Sensor

LED: Two Pin (red)

LED: Two Pin (red)

LED: Two Pin (red)

LED: Two Pin (red)

Code Documentation

#include <Wire.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <LiquidCrystal_I2C.h>
#include <BluetoothSerial.h>

BluetoothSerial SerialBT;
WebServer server(80);

const char* ssid = "Senso";
const char* password = "senso123";

// تعريف المتغيرات
int cmFront = 0, cmBack = 0, cmLeft = 0, cmRight = 0;
int lastFront = 0, lastBack = 0, lastLeft = 0, lastRight = 0;

int buzzersensor = 0;
unsigned long prevMillisFront = 0, prevMillisBack = 0, prevMillisLeft = 0, prevMillisRight = 0;

// إعداد الشاشة
LiquidCrystal_I2C lcd(0x27, 16, 2);

// تعريف الدبابيس
#define BUZZER_PIN 15
#define TRIG_PIN_FRONT 2
#define ECHO_PIN_FRONT 4
#define TRIG_PIN_BACK 27
#define ECHO_PIN_BACK 26
#define TRIG_PIN_LEFT 23
#define ECHO_PIN_LEFT 22
#define TRIG_PIN_RIGHT 33
#define ECHO_PIN_RIGHT 25

#define front_led 5
#define back_led 21
#define left_led 13
#define right_led 12

void handleRoot() {
  server.send(200, "text/html", R"rawliteral(
    <!DOCTYPE html>
    <html lang="en">
      <head>
        <title>Senso Settings</title>
      </head>
      <body>
        <h1>Buzzer Sensor Dashboard</h1>
        <script>
          setInterval(function() {
            fetch('/data')
              .then(response => response.json())
              .then(data => console.log(data));
          }, 500);
        </script>
      </body>
    </html>
  )rawliteral");
}

void handleData() {
  server.send(200, "application/json", 
    "{\"front\":\"" + String(cmFront) + 
    "\",\"back\":\"" + String(cmBack) + 
    "\",\"left\":\"" + String(cmLeft) + 
    "\",\"right\":\"" + String(cmRight) + "\"}");
}

void performSensorReadings() {
  cmFront = 0.01723 * readUltrasonicDistance(TRIG_PIN_FRONT, ECHO_PIN_FRONT);
  cmBack = 0.01723 * readUltrasonicDistance(TRIG_PIN_BACK, ECHO_PIN_BACK);
  cmLeft = 0.01723 * readUltrasonicDistance(TRIG_PIN_LEFT, ECHO_PIN_LEFT);
  cmRight = 0.01723 * readUltrasonicDistance(TRIG_PIN_RIGHT, ECHO_PIN_RIGHT);
}

int readUltrasonicDistance(int trigPin, int echoPin) {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  int duration = pulseIn(echoPin, HIGH);
  return duration * 0.034 / 2; 
}

void toggleLED(int pin, unsigned long prevMillis, unsigned long interval) {
  if (millis() - prevMillis > interval) {
    digitalWrite(pin, !digitalRead(pin));
  }
}

void loop() {
  server.handleClient();
  performSensorReadings();
  
  unsigned long currentMillis = millis();
  
  toggleLED(front_led, prevMillisFront, 100);
  toggleLED(back_led, prevMillisBack, 200);
  toggleLED(left_led, prevMillisLeft, 300);
  toggleLED(right_led, prevMillisRight, 400);

  prevMillisFront = currentMillis;
  prevMillisBack = currentMillis;
  prevMillisLeft = currentMillis;
  prevMillisRight = currentMillis;

  if (buzzersensor > 50) tone(BUZZER_PIN, 2000);
  else noTone(BUZZER_PIN);
  
  delay(10);
}

This code sets up the ESP32 to read distance measurements from four HC-SR04 ultrasonic sensors and control four LEDs based on these readings. It also sets up a web server to display the sensor data and a Bluetooth serial connection. The buzzer is activated based on the sensor readings.