ESP32 - Ultrasonic Sensor - OLED
This tutorial instructs you how to use ESP32 to read the distance from ultrasonic sensor and display it on an OLED.

Hardware Used In This Tutorial
1 | × | ESP-WROOM-32 Dev Module | |
1 | × | Micro USB Cable | |
1 | × | SSD1306 I2C OLED Display 128x64 | |
1 | × | Ultrasonic Sensor | |
1 | × | Breadboard | |
1 | × | Jumper Wires | |
1 | × | (Optional) DC Power Jack | |
1 | × | (Optional) ESP32 Screw Terminal Adapter |
Or you can buy the following sensor kit:
1 | × | DIYables Sensor Kit 30 types, 69 units |
Introduction to OLED and Ultrasonic Sensor
We have specific tutorials about OLED and Ultrasonic Sensor. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, ESP32 code... Learn more about them at the following links:
- ESP32 - OLED tutorial
- ESP32 - Ultrasonic Sensor tutorial
Wiring Diagram
- The wiring diagram with power supply from USB cable

This image is created using Fritzing. Click to enlarge image
- The wiring diagram with power supply from 5v adapter

This image is created using Fritzing. Click to enlarge image
ESP32 Code - Ultrasonic Sensor - OLED
Quick Instructions
- Open Arduino IDE, Go to Tools Manage Libraries

- Search “SSD1306”, then find the SSD1306 library by Adafruit
- Click Install button to install the library.

- You will be asked for intalling some other library dependencies
- Click Install All button to install all library dependencies.

- Copy the above code and paste it to Arduino IDE
- Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
- Move your hand in front of sensor
- See the result on OLED and Serial Monitor
※ NOTE THAT:
The about code automatically horizontal and vertical center aligns the text on OLED display. See How to vertical/horizontal center on OLED for more detail.
Video Tutorial
Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.