Arduino STEM DIY (Session 4): Display Ultrasonic Distance on an OLED Screen with I²C
Share
Have you completed the HC-SR04 ultrasonic distance sensor project from the previous lesson? In this tutorial, we will upgrade the project by adding an I²C OLED display, allowing the Arduino to show the measured distance directly on a screen instead of relying on the Serial Monitor.
Using an Arduino Uno R3, an HC-SR04 ultrasonic sensor, and a 0.91-inch SSD1306 OLED display, you will build a simple distance display while learning the basics of I²C communication and Arduino display libraries.
What You Will Learn
- Understand OLED displays and basic I²C communication.
- Connect an OLED display to an Arduino Uno R3.
- Install and use the Adafruit SSD1306 and GFX libraries.
- Display HC-SR04 distance readings on an OLED screen.
- Handle situations where no valid echo is received.
Suitable for: Arduino beginners, parents and children, students, and makers
Estimated time: 30–45 minutes, depending on experience
Difficulty: Beginner to early intermediate
Prerequisite: Lesson 3 – HC-SR04 Ultrasonic Distance Measurement
1. Components Required
- Arduino Uno R3 development board – Runs the program and connects the modules.
- HC-SR04 ultrasonic sensor – Measures the distance between the sensor and an object.
- 0.91-inch I²C OLED display (SSD1306, 128 × 32) – Displays the measured distance.
- Breadboard and jumper wires – For solderless prototyping.
- USB cable and computer – For uploading the program and viewing the Serial Monitor.
Related products:
- Arduino Uno R3 Development Board
- HC-SR04 Ultrasonic Sensor Module
- 0.91-inch I²C OLED Display Module, 128 × 32 SSD1306
- Browse OLED, TFT LCD and Display Modules
- Browse Arduino and STEM DIY Accessories
Specification note: This tutorial is based on a 0.91-inch SSD1306 OLED with a 128 × 32 resolution and an I²C interface. The referenced module supports 3.3V–5V DC, and its common I²C address is 0x3C. Other OLED modules may use different controller chips, resolutions, or addresses. Check your module specifications before using the example code.
2. What Is an OLED Display?
OLED stands for Organic Light-Emitting Diode. OLED pixels emit their own light, so the display does not need a separate backlight like a traditional LCD. This allows compact modules to show clear text and graphics.
In Arduino DIY projects, OLED displays are often used to show sensor readings, temperature, humidity, battery status, timers, and simple menus. The 128 × 32 resolution used in this project means the display has 128 pixels horizontally and 32 pixels vertically. It is suitable for showing a distance value and short status messages.
3. What Is I²C Communication?
I²C (Inter-Integrated Circuit) is a communication protocol commonly used between microcontrollers and electronic modules. A typical four-pin I²C OLED uses power, ground, and two signal lines:
| OLED Pin | Function | Arduino Uno R3 |
|---|---|---|
| VCC | Power input | 5V (the module used here supports 5V) |
| GND | Ground | GND |
| SDA | Data line | A4 / SDA |
| SCL | Clock line | A5 / SCL |
I²C uses SDA to transfer data and SCL to provide the clock signal. Since the display needs only two main signal lines, it helps conserve the Arduino's I/O pins. On the Arduino Uno R3, SDA and SCL correspond to A4 and A5. If your board has dedicated SDA and SCL pins, you can also use the pins labelled on the board.
Wiring notes: Disconnect USB power before wiring. Do not assume every OLED supports 5V. Do not swap SDA and SCL. The OLED, HC-SR04, and Arduino must share a common GND.
4. Arduino Wiring
Keep the HC-SR04 connections from the previous lesson and add the OLED display.
1. HC-SR04 Ultrasonic Sensor
| HC-SR04 Pin | Arduino Uno R3 |
|---|---|
| VCC | 5V |
| GND | GND |
| Trig | D9 |
| Echo | D10 |
2. OLED Display
| OLED Pin | Arduino Uno R3 |
|---|---|
| VCC | 5V |
| GND | GND |
| SDA | A4 / SDA |
| SCL | A5 / SCL |

Photo of the Arduino Uno R3 and HC-SR04 wiring. See the demonstration video below for the OLED display in operation.
After wiring, double-check VCC, GND, SDA, and SCL. Make sure there are no short circuits before reconnecting USB power.
5. Install the Arduino OLED Libraries
This tutorial uses the Adafruit SSD1306 and GFX libraries to display text and graphics on the OLED.
- Open the Arduino IDE.
- Open Library Manager from the IDE menu.
- Search for
Adafruit SSD1306and install it. - Search for
Adafruit GFX Libraryand install it. - If Library Manager asks you to install additional dependencies, follow the prompts.
6. Complete Arduino Code
The following program reads the HC-SR04 measurement and displays the distance on the OLED. If no valid echo is received, the screen displays No echo!.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int trigPin = 9;
const int echoPin = 10;
const int screenWidth = 128;
const int screenHeight = 32;
const int oledReset = -1;
const int oledAddress = 0x3C;
Adafruit_SSD1306 display(
screenWidth,
screenHeight,
&Wire,
oledReset
);
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
if (!display.begin(
SSD1306_SWITCHCAPVCC,
oledAddress)) {
Serial.println("OLED initialization failed!");
while (true) {
delay(100);
}
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Arduino Distance");
display.setCursor(0, 16);
display.println("Starting...");
display.display();
delay(1000);
}
void loop() {
long duration;
float distance;
// Send the trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Wait up to 30,000 microseconds for an echo
duration = pulseIn(echoPin, HIGH, 30000UL);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Distance:");
if (duration == 0) {
display.setCursor(0, 16);
display.println("No echo!");
Serial.println("No echo - check sensor.");
} else {
distance = duration * 0.0343 / 2;
display.setCursor(0, 16);
display.print(distance, 1);
display.println(" cm");
Serial.print("Distance: ");
Serial.print(distance, 1);
Serial.println(" cm");
}
display.display();
delay(200);
}
Code Explanation
1. Include the libraries: Wire.h handles I²C communication. Adafruit_GFX.h provides basic text and graphics functions, while Adafruit_SSD1306.h controls the SSD1306 OLED.
2. Set the display size and address: This example uses a 128 × 32 OLED and assumes an I²C address of 0x3C. Check the actual address and controller chip if the display does not respond.
3. Initialise the OLED: display.begin() attempts to initialise the display. If it fails, an error message appears in the Serial Monitor and the program stops.
4. Read the ultrasonic echo: pulseIn(echoPin, HIGH, 30000UL) waits up to 30,000 microseconds. If no valid echo is received, the function returns 0.
5. Calculate the distance: The program uses duration * 0.0343 / 2 to estimate the distance in centimetres. The result is divided by two because the ultrasonic wave travels to the object and back.
6. Update the display: setCursor() sets the text position, print() outputs text, and display.display() updates the OLED with the buffered content.
7. Testing the Project
After completing the wiring, installing the libraries, and uploading the program, reconnect the Arduino USB cable. The OLED should show a startup screen first, then begin displaying the distance measured by the HC-SR04.
- Place a flat object in front of the HC-SR04.
- Read the distance shown on the OLED.
- Move the object closer to and farther from the sensor, and watch the reading change.
- Open the Serial Monitor at 9600 baud to compare the serial output with the OLED.
Expected Results
| Test Condition | OLED Display |
|---|---|
| During initialisation | Arduino Distance / Starting... |
| Valid echo received | Measured distance and cm |
| No valid echo received | No echo! |
Readings may differ slightly from the object's actual distance. Surface material, reflection angle, the environment, and the sensor itself can affect the result.
8. Troubleshooting and FAQs
1. The OLED display remains blank. What should I check?
Check that VCC and GND are connected correctly, SDA goes to A4 / SDA, and SCL goes to A5 / SCL. Confirm that the display supports the selected supply voltage and that the Adafruit SSD1306 and Adafruit GFX libraries are installed. If it still does not work, use an I²C Scanner sketch to check the device address.
2. OLED initialisation fails. What should I do?
Check whether the display uses an SSD1306 controller and whether the resolution and I²C address in the program are correct. Not all OLEDs use the same controller; other models may require a different library or initialisation settings.
3. The OLED works, but it keeps showing No echo! What should I check?
Check the HC-SR04 VCC, GND, Trig, and Echo connections. Place a flat object in front of the sensor. An extreme object angle or a surface that does not reflect ultrasound well may prevent the sensor from receiving an echo.
4. Why do the distance readings fluctuate?
Ultrasonic readings can be affected by surface material, object angle, nearby obstacles, and air temperature. Keep the sensor steady and use a flat target. To improve stability further, experiment with taking multiple readings and calculating an average.
5. Can I use a different OLED size?
Yes, but check the display's controller chip, interface, resolution, and voltage requirements. A different model may require changes to the screen dimensions, library, or display layout.
9. STEM DIY Extension Challenges
Challenge A: Add a distance warning
Display TOO CLOSE! when an object enters a selected distance range. This adds a warning instead of showing only a number.
Challenge B: Add an LED indicator
Add an LED circuit so the OLED shows the measured distance while the LED indicates whether the object is within a selected range. Connect a suitable current-limiting resistor in series with the LED.
Challenge C: Improve measurement stability
Take several readings and calculate the average. Compare the results to see whether averaging reduces individual reading fluctuations.
Challenge D: Design your own display interface
Add a title, divider lines, or simple icons to make the OLED screen easier to read.
10. Conclusion
In this lesson, we combined an Arduino Uno R3, an HC-SR04 ultrasonic sensor, and an I²C OLED display to build a device that shows distance measurements directly on a screen. You have explored I²C communication, OLED initialisation, library usage, and displaying sensor readings.
In the next lesson, we will introduce the SG90 servo motor and learn how to control its angle using Arduino, moving from sensing and displaying information to controlling physical movement.
Looking for Arduino boards, sensors, OLED displays, or other STEM DIY accessories? Explore our collections:
Sun Cheong Computer Company Limited
G/F, 246 Apliu Street, Sham Shui Po, Kowloon, Hong Kong