OLED Distance Meter
Combine an ultrasonic measurement and OLED output without relying on a serial window.

Wire it up
Disconnect power before changing connections. Follow terminal names rather than wire colors.
| From | To | Why it matters |
|---|---|---|
| HC-SR04 (sensor1) · VCC | Uno · 5V | Power connection |
| HC-SR04 (sensor1) · GND | Uno · GND.2 | Ground reference |
| HC-SR04 (sensor1) · TRIG | Uno · 7 | Match this terminal to the sketch |
| HC-SR04 (sensor1) · ECHO | Uno · 8 | Match this terminal to the sketch |
| SSD1306 OLED (oled1) · VCC | Uno · 3.3V | Power connection |
| SSD1306 OLED (oled1) · GND | Uno · GND.1 | Ground reference |
| SSD1306 OLED (oled1) · SDA | Uno · A4 | Match this terminal to the sketch |
| SSD1306 OLED (oled1) · SCL | Uno · A5 | Match this terminal to the sketch |
The complete sketch
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
void startDisplay() {
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) while (true) {}
display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE);
}
float readDistance() {
digitalWrite(7, LOW); delayMicroseconds(2);
digitalWrite(7, HIGH); delayMicroseconds(10); digitalWrite(7, LOW);
unsigned long us = pulseIn(8, HIGH, 30000);
return us ? us * 0.0343 / 2.0 : -1;
}
void setup() { pinMode(7, OUTPUT); pinMode(8, INPUT); startDisplay(); }
void loop() {
float cm = readDistance();
display.clearDisplay(); display.setCursor(0, 0);
display.println("DISTANCE");
if (cm < 0) display.println("No echo");
else { display.print(cm, 1); display.println(" cm"); }
display.display(); delay(150);
}
The launch buttons use this same code and a matching saved circuit.
Build it step by step
Open a complete project
Launch in Simulator creates a separate Uno sketch and its matching wired circuit. Existing sketches are preserved. Press Run to compile and execute.
Explore the live circuit
Change the HC-SR04 distance and watch the OLED value update.
Try it on hardware
Disconnect power while wiring. Match terminal names in the table, select the exact board, Verify, then connect and upload. Check component ratings and power requirements first.
Understand the sketch
Two independent interfaces
D7/D8 carry trigger/echo while A4/A5 carry I2C. A timeout is displayed as No echo, never as a false zero-distance reading.
If something is different
No echo
Check TRIG D7 and ECHO D8 separately from SDA/SCL. A real HC-SR04 uses 5 V logic.
Expected output and tested scope
The OLED updates its distance presentation as the modeled ultrasonic distance changes.
Version 1 · Last source- and build-matched test: 2026-10-25. Actual shipped AVR-GCC WASM compilation and Uno AVR engine execution; no physical hardware test.
Supported tutorial target: Arduino Uno R3 / ATmega328P. Compile: PASS · Modeled execution: PASS. Other boards require a separate review of pins, voltage, libraries and the exact build profile.