ESP32-Based Dual Ultrasonic Sensor System with Servo Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit involves an ESP32 microcontroller interfacing with two HC-SR05 ultrasonic sensors and a Micro Servo 9G. The ESP32 reads distance measurements from the ultrasonic sensors and controls the servo motor based on the distance readings.
Component List
ESP32 on Baseboard
- Description: A versatile microcontroller with Wi-Fi and Bluetooth capabilities.
- Pins: GPIO23, GPIO22, GPIO1/TX, GPIO3/RX, GPIO21, GPIO19, GPIO18, GPIO5, GPIO17, GPIO16, GPIO4, GPIO0, GPIO2, GPIO15, SD1, SD0, CLK, V, G, GPIO36, GPIO39, GPIO34, GPIO35, GPIO32, GPIO33, GPIO25, GPIO26, GPIO27, GPIO14, GPIO12, GPIO13, SD2, SD3, GND, 5V, 3V, TX, RX, VCC, D22, D21
Micro Servo 9G
- Description: A small servo motor used for precise control of angular position.
- Pins: GND, +5V, PWM
HC-SR05 (Sensor 1)
- Description: An ultrasonic sensor used for distance measurement.
- Pins: VCC, TRIG, GND, ECHO, OUT
HC-SR05 (Sensor 2)
- Description: An ultrasonic sensor used for distance measurement.
- Pins: VCC, TRIG, GND, ECHO, OUT
Wiring Details
ESP32 on Baseboard
- GPIO23: Connected to PWM of Micro Servo 9G
- GPIO19: Connected to ECHO of HC-SR05 (Sensor 2)
- GPIO18: Connected to TRIG of HC-SR05 (Sensor 2)
- GPIO5: Connected to TRIG of HC-SR05 (Sensor 1)
- GPIO17: Connected to ECHO of HC-SR05 (Sensor 1)
- 5V: Connected to VCC of HC-SR05 (Sensor 1), VCC of HC-SR05 (Sensor 2), and +5V of Micro Servo 9G
- GND: Connected to GND of HC-SR05 (Sensor 1), GND of HC-SR05 (Sensor 2), and GND of Micro Servo 9G
Micro Servo 9G
- PWM: Connected to GPIO23 of ESP32 on Baseboard
- +5V: Connected to 5V of ESP32 on Baseboard
- GND: Connected to GND of ESP32 on Baseboard
HC-SR05 (Sensor 1)
- TRIG: Connected to GPIO5 of ESP32 on Baseboard
- ECHO: Connected to GPIO17 of ESP32 on Baseboard
- VCC: Connected to 5V of ESP32 on Baseboard
- GND: Connected to GND of ESP32 on Baseboard
HC-SR05 (Sensor 2)
- TRIG: Connected to GPIO18 of ESP32 on Baseboard
- ECHO: Connected to GPIO19 of ESP32 on Baseboard
- VCC: Connected to 5V of ESP32 on Baseboard
- GND: Connected to GND of ESP32 on Baseboard
Code Documentation
ESP32 on Baseboard
#include <Servo.h>
Servo myservo;
const int trigPin1 = 18;
const int echoPin1 = 19;
const int trigPin2 = 5;
const int echoPin2 = 17;
const int servoPin = 23;
void setup() {
Serial.begin(115200);
// Servo setup
myservo.attach(servoPin);
// Sensor 1 setup
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
// Sensor 2 setup
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void loop() {
// Read distance from sensor 1
long duration1, distance1;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = (duration1 / 2) / 29.1;
// Read distance from sensor 2
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = (duration2 / 2) / 29.1;
// Print distances
Serial.print("Distance 1: ");
Serial.print(distance1);
Serial.println(" cm");
Serial.print("Distance 2: ");
Serial.print(distance2);
Serial.println(" cm");
// Control servo based on distance1
if (distance1 < 20) {
myservo.write(90); // Rotate to 90 degrees
} else {
myservo.write(0); // Rotate to 0 degrees
}
delay(1000);
}
Micro Servo 9G
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
HC-SR05 (Sensor 1)
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
HC-SR05 (Sensor 2)
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}