ESP32-Based Ultrasonic Distance Measurement with Dual OLED Display
View and Edit Project In Cirkit Designer
Circuit Documentation
Summary
This circuit integrates an ESP32 microcontroller with two OLED 1.3" displays and two HC-SR04 ultrasonic sensors. The ESP32 serves as the central processing unit, controlling the OLED displays to show distance measurements captured by the ultrasonic sensors. The circuit is designed to read distances from the environment using the ultrasonic sensors and display these readings on the OLED screens.
Component List
ESP32 38 PINS
- Description: A microcontroller with Wi-Fi and Bluetooth capabilities, featuring a wide range of GPIO pins.
- Purpose: Acts as the central processing unit for the circuit, interfacing with the OLED displays and ultrasonic sensors.
OLED 1.3"
- Description: A small display screen that uses organic light-emitting diodes.
- Purpose: To display information such as sensor readings from the ESP32.
HC-SR04 Ultrasonic Sensor
- Description: A sensor that measures distance by emitting ultrasonic waves and measuring the time taken for the echo to return.
- Purpose: To measure distances and provide input to the ESP32 for processing and display.
Wiring Details
ESP32 38 PINS
- GND: Connected to the ground pins of both OLED displays and both HC-SR04 ultrasonic sensors.
- G22 (SCL): Connected to the SCL pin of the first OLED display.
- G21 (SDA): Connected to the SDA pin of the first OLED display.
- G19 (SCL): Connected to the SCL pin of the second OLED display.
- G18 (SDA): Connected to the SDA pin of the second OLED display.
- G4: Connected to the ECHO pin of the second HC-SR04 ultrasonic sensor.
- G0: Connected to the ECHO pin of the first HC-SR04 ultrasonic sensor.
- G2: Connected to the TRIG pin of the second HC-SR04 ultrasonic sensor.
- G15: Connected to the TRIG pin of the first HC-SR04 ultrasonic sensor.
- 3V3: Connected to the VCC pins of both OLED displays.
- 5V: Connected to the VCC pins of both HC-SR04 ultrasonic sensors.
OLED 1.3" (First Display)
- GND: Connected to the ground net.
- VCC: Connected to the 3V3 pin of the ESP32.
- SCL: Connected to the G22 pin of the ESP32.
- SDA: Connected to the G21 pin of the ESP32.
OLED 1.3" (Second Display)
- GND: Connected to the ground net.
- VCC: Connected to the 3V3 pin of the ESP32.
- SCL: Connected to the G19 pin of the ESP32.
- SDA: Connected to the G18 pin of the ESP32.
HC-SR04 Ultrasonic Sensor (First Sensor)
- GND: Connected to the ground net.
- VCC: Connected to the 5V pin of the ESP32.
- TRIG: Connected to the G15 pin of the ESP32.
- ECHO: Connected to the G0 pin of the ESP32.
HC-SR04 Ultrasonic Sensor (Second Sensor)
- GND: Connected to the ground net.
- VCC: Connected to the 5V pin of the ESP32.
- TRIG: Connected to the G2 pin of the ESP32.
- ECHO: Connected to the G4 pin of the ESP32.
Documented Code
/*
* This Arduino Sketch is for an ESP32 microcontroller connected to two OLED
* displays and two HC-SR04 ultrasonic sensors. The code initializes the
* displays and sensors, reads distance measurements from the sensors, and
* displays the readings on the respective OLED screens.
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define I2C_FREQ 400000
#define SDA_1 21
#define SCL_1 22
#define SDA_2 18
#define SCL_2 19
TwoWire I2C_1 = TwoWire(0);
TwoWire I2C_2 = TwoWire(1);
int zz;
int past = millis();
// Declaration for two SSD1306 displays connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display1(SCREEN_WIDTH, SCREEN_HEIGHT, &I2C_1);
Adafruit_SSD1306 display2(SCREEN_WIDTH, SCREEN_HEIGHT, &I2C_2);
// Pin definitions for ultrasonic sensors
const int trigPin1 = 15;
const int echoPin1 = 0;
const int trigPin2 = 2;
const int echoPin2 = 4;
void setup() {
// Initialize serial communication
Serial.begin(115200);
I2C_1.begin(SDA_1, SCL_1);
I2C_2.begin(SDA_2, SCL_2);
// Initialize the OLED displays
if(!display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed for display1"));
for(;;);
}
if(!display2.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed for display2"));
for(;;);
}
display1.display();
display2.display();
delay(1000);
// Clear the buffer.
display1.clearDisplay();
display1.display();
display2.clearDisplay();
display2.display();
// text display tests
display1.setTextSize(1);
display1.setTextColor(SSD1306_WHITE);
display1.setCursor(0, 0);
display1.print("This is some text");
display1.print("it works!");
display1.println("more text here");
display1.println("and even more");
display1.setCursor(0, 0);
display1.display(); // actually display all of the above
// text display tests
// OLED2.invertDisplay(true);
display2.setTextSize(2);
display2.setTextColor(SSD1306_WHITE);
display2.setCursor(0, 0);
display2.println("This is");
display2.println("OLED 2");
display2.setCursor(0, 0);
display2.display(); // actually display all of the above
delay(2000);
display1.clearDisplay();
display1.display();
display2.clearDisplay();
display2.display();
// Initialize the ultrasonic sensor pins
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
}
void loop() {
// Read distance from the first ultrasonic sensor
long duration1, distance1;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.034 / 2;
// Read distance from the second ultrasonic sensor
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2 * 0.034 / 2;
// Display the distance on the first OLED display
display1.clearDisplay();
display1.setTextSize(1);
display1.setTextColor(SSD1306_WHITE);
display1.setCursor(0, 0);
display1.print(F("Distance 1: "));
display1.print(distance1);
display1.print(F(" cm"));
display1.display();
// Display the distance on the second OLED display
display2.clearDisplay();
display2.setTextSize(1);
display2.setTextColor(SSD1306_WHITE);
display2.setCursor(0, 0);
display2.print(F("Distance 2: "));
display2.print(distance2);
display2.print(F(" cm"));
display2.display();
// Wait for a short period before taking the next measurement
delay(500);
}
This code is designed to run on the ESP32 microcontroller. It initializes two OLED displays and two HC-SR04 ultrasonic sensors, reads the distance measurements, and displays the results on the OLED screens. The I2C communication protocol is used to interface with the OLED displays, and GPIO pins are configured to control the ultrasonic sensors. The code includes a setup routine to initialize the components and a loop function that continuously reads the sensors and updates the displays.