ESP8266-Based Battery-Powered Distance Measurement System with OLED Display and Servo Control
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit involves an ESP8266 microcontroller interfacing with various components including two HC-SR04 Ultrasonic Sensors, a 0.96" OLED display, and a Servo motor. The circuit is powered by a 3.7V battery. The ESP8266 is programmed to read distance measurements from the ultrasonic sensors and display the results on the OLED screen. Additionally, the Servo motor is controlled by the ESP8266.
Component List
ESP8266
- Description: A Wi-Fi enabled microcontroller.
- Pins: 3V, G, D8, D7, D6, D5, D4, D3, D2, D1, D0, A0, S3, S2, S1, SC, S0, SK, EN, VIN, RST, VU
Servo
- Description: A small motor with a control circuit for precise angular movement.
- Pins: gnd, vcc, pulse
3.7V Battery
- Description: A power source for the circuit.
- Pins: +, -
HC-SR04 Ultrasonic Sensor
- Description: A sensor used to measure distance by using ultrasonic waves.
- Pins: VCC, TRIG, ECHO, GND
0.96" OLED Display
- Description: A small OLED display for visual output.
- Pins: GND, VDD, SCK, SDA
Wiring Details
ESP8266
- 3V: Connected to VDD of the OLED display, vcc of the Servo, and VCC of both HC-SR04 Ultrasonic Sensors.
- G: Connected to GND of the OLED display, gnd of the Servo, GND of both HC-SR04 Ultrasonic Sensors, and the negative terminal of the 3.7V battery.
- D8: Connected to SCK of the OLED display.
- D7: Connected to SDA of the OLED display.
- D6: Connected to TRIG of the first HC-SR04 Ultrasonic Sensor.
- D5: Connected to ECHO of the first HC-SR04 Ultrasonic Sensor.
- D3: Connected to TRIG of the second HC-SR04 Ultrasonic Sensor.
- D2: Connected to ECHO of the second HC-SR04 Ultrasonic Sensor.
- D1: Connected to pulse of the Servo.
- VIN: Connected to the positive terminal of the 3.7V battery.
Servo
- gnd: Connected to G of the ESP8266.
- vcc: Connected to 3V of the ESP8266.
- pulse: Connected to D1 of the ESP8266.
3.7V Battery
- +: Connected to VIN of the ESP8266.
- -: Connected to G of the ESP8266.
HC-SR04 Ultrasonic Sensor (First)
- VCC: Connected to 3V of the ESP8266.
- TRIG: Connected to D6 of the ESP8266.
- ECHO: Connected to D5 of the ESP8266.
- GND: Connected to G of the ESP8266.
HC-SR04 Ultrasonic Sensor (Second)
- VCC: Connected to 3V of the ESP8266.
- TRIG: Connected to D3 of the ESP8266.
- ECHO: Connected to D2 of the ESP8266.
- GND: Connected to G of the ESP8266.
0.96" OLED Display
- GND: Connected to G of the ESP8266.
- VDD: Connected to 3V of the ESP8266.
- SCK: Connected to D8 of the ESP8266.
- SDA: Connected to D7 of the ESP8266.
Code Documentation
// Pin Definitions
#define TRIG_PIN1 D1 // Sensor 1 TRIG
#define ECHO_PIN1 D2 // Sensor 1 ECHO
#define TRIG_PIN2 D3 // Sensor 2 TRIG
#define ECHO_PIN2 D4 // Sensor 2 ECHO
void setup() {
Serial.begin(115200); // Start serial communication
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
// Initialize TRIG pins to LOW
digitalWrite(TRIG_PIN1, LOW);
digitalWrite(TRIG_PIN2, LOW);
delay(50); // Wait for sensor stabilization
}
// Function to measure distance
float measureDistance(int trigPin, int echoPin) {
// Send signal to TRIG pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Get response time from ECHO pin
long duration = pulseIn(echoPin, HIGH, 30000); // Max wait time 30ms
if (duration == 0) {
return -1; // Return -1 if measurement fails
}
// Calculate distance (cm)
float distance = (duration * 0.034) / 2.0;
return distance;
}
void loop() {
// Measure distance from Sensor 1
float distance1 = measureDistance(TRIG_PIN1, ECHO_PIN1);
if (distance1 >= 0) {
Serial.print("Sensor 1 Distance: ");
Serial.print(distance1);
Serial.println(" cm");
} else {
Serial.println("Sensor 1 Measurement Failed!");
}
// Measure distance from Sensor 2
float distance2 = measureDistance(TRIG_PIN2, ECHO_PIN2);
if (distance2 >= 0) {
Serial.print("Sensor 2 Distance: ");
Serial.print(distance2);
Serial.println(" cm");
} else {
Serial.println("Sensor 2 Measurement Failed!");
}
delay(500); // Wait between measurements (500 ms)
}
This code initializes the pins for the ultrasonic sensors and measures the distance by sending a signal to the TRIG pin and reading the response from the ECHO pin. The results are printed to the serial monitor.