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

ESP32-Based Ultrasonic Distance Measurement with Dual OLED Display

View and Edit Project In Cirkit DesignerImage of ESP32-Based Ultrasonic Distance Measurement with Dual OLED Display

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

OLED 1.3"

HC-SR04 Ultrasonic Sensor

Wiring Details

ESP32 38 PINS

OLED 1.3" (First Display)

OLED 1.3" (Second Display)

HC-SR04 Ultrasonic Sensor (First Sensor)

HC-SR04 Ultrasonic Sensor (Second Sensor)

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.