ESP32 - Ultrasonic - OLED
This tutorial instructs you how to use ESP32 to measure distance and display it on OLED Display using Ultrasonic sensor.
Video Tutorial
you can watch this video tutorial
Hardware Required
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | OLED Display | |
1 | × | Ultrasonic Sensor | |
1 | × | Breadboard | |
n | × | Jumper Wires |
Introduction to RFID Module
The ultrasonic sensor HC-SR04 is used to measure the distance from the sensor to an object by using ultrasonic waves.
Pinout
The ultrasonic sensor HC-SR04 includes four pins:
-
GND pin:connect this pin to GND (0V)
-
VCC pin: connect this pin to VCC (5V)
-
TRIG pin: this pin receives a control pulse from ESP32.
-
ECHO pin: this pin generates a pulse corresponding to the measured distance to ESP32.
How it works
※ NOTE THAT:
This section is the in-depth knowledge. DON'T worry if you don't understand. Ignore this section if it overloads you, and come back in another day. Keep reading the next sections.
-
1- Micro-controller: generates a 10-microsecond pulse on the TRIG pin.
-
2- The ultrasonic sensor automatically emits the ultrasonic waves.
-
3- The ultrasonic wave is reflected after hitting an obstacle.
-
4- The ultrasonic sensor:
- Detects the reflected ultrasonic wave.
- Measures the travel time of the ultrasonic wave.
-
5- Ultrasonic sensor: generates a pulse to the ECHO pin. The duration of the pulse is equal to the travel time of the ultrasonic wave.
-
6- Micro-controller measures the pulse duration in the ECHO pin, and then calculate the distance between sensor and obstacle.
How to Get Distance From Ultrasonic Sensor
To get distance from the ultrasonic sensor, we only need to do two steps (1 and 6 on How It Works part)
-
Generates a 10-microsecond pulse on TRIG pin
-
Measures the pulse duration in ECHO pin, and then calculate the distance between sensor and obstacle
Distance Calculation
We have:
-
The travel time of the ultrasonic wave (µs):
travel_time = pulse_duration -
The speed of the ultrasonic wave:
speed = SPEED_OF_SOUND = 340 m/s = 0.034 cm/µs -
The travel distance of the ultrasonic wave (cm):
travel_distance = speed x travel_time = 0.034 x pulse_duration -
The distance between sensor and obstacle (cm):
distance = travel_distance / 2 = 0.034 x pulse_duration / 2 = 0.017 x pulse_duration
So:
How To Program Ultrasonic Sensor
-
Generate a 10-microsecond pulse in ESP32's pin by using digitalWrite() and delayMicroseconds() functions. For example, pin GIOP23:
digitalWrite(23, HIGH); delayMicroseconds(10); digitalWrite(23, LOW);
duration_us = pulseIn(22, HIGH);
distance_cm = 0.017 * duration_us;
Wiring Diagram
Breadboard Connections
ESP32 Code
#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 OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) #define SCREEN_ADDRESS 0x3C /// See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); #define TRIG_PIN 26 // ESP32 pin GIOP26 connected to Ultrasonic Sensor's TRIG pin #define ECHO_PIN 25 // ESP32 pin GIOP25 connected to Ultrasonic Sensor's ECHO pin float duration_us, distance_cm; void setup() { Serial.begin(9600); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Show initial display buffer contents on the screen -- // the library initializes this with an Adafruit splash screen. display.display(); delay(2000); // Pause for 2 seconds display.clearDisplay(); // clear display display.setTextSize(2); // set text size display.setTextColor(WHITE, BLACK); // set text color pinMode(TRIG_PIN, OUTPUT); // config trigger pin to output mode pinMode(ECHO_PIN, INPUT); // config echo pin to input mode } void loop() { // generate 10-microsecond pulse to TRIG pin digitalWrite(TRIG_PIN, HIGH); delayMicroseconds(10); digitalWrite(TRIG_PIN, LOW); // measure duration of pulse from ECHO pin duration_us = pulseIn(ECHO_PIN, HIGH); // calculate the distance distance_cm = 0.017 * duration_us; display.setTextSize(2); display.setCursor(10, 1); // set position to display display.println("Distance: "); // set text display.setTextSize(3); display.setCursor(10, 30); display.println(distance_cm); display.display(); // display on OLED delay(500); }
Quick Steps
- power up your board
- Open Arduino IDE
- Select the right board
- Select the right port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to ESP32
- See the changes you made
Book Tutorial
We are considering to make the book tutorials. If you think the book tutorials are essential, you can download it. download book
See Also
References
※ NOTE THAT:
Some components works on 3.3v and others works on 5v!