Skip to content

OLED Distance Meter

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

OLED Distance Meter: the matching Arduino Uno circuit rendered in Circuitera’s simulator
Matching Circuitera simulator circuit. Real firmware capture; modeled components are idealized.

Wire it up

Disconnect power before changing connections. Follow terminal names rather than wire colors.

FromToWhy it matters
HC-SR04 (sensor1) · VCCUno · 5VPower connection
HC-SR04 (sensor1) · GNDUno · GND.2Ground reference
HC-SR04 (sensor1) · TRIGUno · 7Match this terminal to the sketch
HC-SR04 (sensor1) · ECHOUno · 8Match this terminal to the sketch
SSD1306 OLED (oled1) · VCCUno · 3.3VPower connection
SSD1306 OLED (oled1) · GNDUno · GND.1Ground reference
SSD1306 OLED (oled1) · SDAUno · A4Match this terminal to the sketch
SSD1306 OLED (oled1) · SCLUno · A5Match this terminal to the sketch

The complete sketch

oled-distance.ino
#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

  1. 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.

  2. Explore the live circuit

    Change the HC-SR04 distance and watch the OLED value update.

  3. 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.

Keep exploring.