Show the distance in big numbers on the small OLED screen, with the zone and a bar graph. You will install your first Arduino libraries.
| Part | How many |
|---|---|
| Wiring from Lesson 1 (the other parts can stay) | |
| 0.96" OLED display, 4 pins | 1 |
| Male-to-male jumper wires | 4 |
Nothing extra - just the kit and your computer.
The OLED has its own controller chip, the SSD1306. The Adafruit library knows how to talk to it, so we only call commands like print() and fillRect().
The UNO talks to the display over I2C: SDA on A4 carries the data, SCL on A5 the clock. The display answers at address 0x3C (some at 0x3D) - the sketch tries both.
Drawing happens in the UNO's memory first. Nothing appears until display.display() sends the whole picture. The picture needs 1 KB of the UNO's 2 KB of memory, which is why the text uses F("...").
The zones are the same as Lessons 2 and 3. The bar is full width at 100 cm.
Every item is in the kit — nothing extra to buy.
All 9 parts in one box, with this project and 8 more to build from them.
One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Delivery is AED 20, free on orders AED 100+.
Unplug USB before you change any wire. Pin names are the labels printed on the board.
| Part / pin | Connect to | Note |
|---|---|---|
| OLED VCC | + rail (5V) | Rated 3.3-5V. Go by the labels: the pin order varies |
| OLED GND | − rail (GND) | |
| OLED SDA | UNO A4 | I2C data |
| OLED SCL | UNO A5 | I2C clock |
| HC-SR04 | As in Lesson 1 | TRIG pin 9, ECHO pin 10 |
One thing at a time. Tick nothing off until it matches the diagram.
Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.
/*
DIY KIT - Arduino Ultrasonic Distance & Parking Learning Kit
Lesson 8 of 9: OLED Distance Meter
LIBRARIES (once): Tools > Manage Libraries, search "Adafruit SSD1306",
click Install, and choose "Install all" so Adafruit GFX and BusIO come too.
WIRING (unplug the USB cable before you wire)
The sensor from Lesson 1 (TRIG pin 9, ECHO pin 10) stays.
OLED display - go by the labels printed on it, the order varies:
VCC -> 5V (+ rail) GND -> GND (- rail)
SDA -> UNO A4 SCL -> UNO A5
The LEDs and buzzer can stay; this sketch does not use them.
WHAT SHOULD HAPPEN
The OLED shows the zone at the top, the distance in big numbers and a
bar that grows as the target moves away (full at 100 cm).
The Serial Monitor (9600 baud) says which address the display answered on.
F("...") keeps text in the UNO's large program memory instead of its small
working memory (RAM). The display needs 1 KB of RAM for its picture.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int CLEAR_CM = 50; // the same zones as Lessons 2 and 3
const int NEAR_CM = 20;
const int BAR_FULL_CM = 100; // the bar is full width at this distance
const unsigned long ECHO_TIMEOUT_US = 30000;
const int MIN_CM = 2;
const int MAX_CM = 400;
Adafruit_SSD1306 display(128, 64, &Wire, -1); // 128 x 64 pixels, no reset pin
bool displayOk = false;
long readOnceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (us == 0) return 0;
long cm = us / 58;
if (cm < MIN_CM || cm > MAX_CM) return 0;
return cm;
}
// Most of these displays answer at address 0x3C, some at 0x3D. Try both.
bool startDisplay() {
if (display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("OLED found at 0x3C"));
return true;
}
if (display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("OLED found at 0x3D"));
return true;
}
Serial.println(F("OLED not found - unplug USB and check VCC, GND, SDA (A4) and SCL (A5)"));
return false;
}
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
Serial.begin(9600);
Serial.println(F("Lesson 8: OLED Distance Meter"));
displayOk = startDisplay();
if (displayOk) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.display();
}
}
void loop() {
long cm = readOnceCm();
Serial.println(cm); // 0 means no echo
if (displayOk) {
display.clearDisplay();
// Top line: the zone, in small letters.
display.setTextSize(1);
display.setCursor(0, 0);
if (cm == 0) display.print(F("NO ECHO"));
else if (cm < NEAR_CM) display.print(F("NEAR"));
else if (cm < CLEAR_CM) display.print(F("CAUTION"));
else display.print(F("CLEAR"));
// Middle: the distance, 3 times bigger.
display.setTextSize(3);
display.setCursor(0, 22);
if (cm == 0) {
display.print(F("--"));
} else {
display.print(cm);
display.setTextSize(2);
display.print(F(" cm"));
}
// Bottom: a bar, full width at BAR_FULL_CM.
if (cm > 0) {
int width = map(constrain(cm, 0, BAR_FULL_CM), 0, BAR_FULL_CM, 0, 128);
display.fillRect(0, 56, width, 8, SSD1306_WHITE);
}
display.display(); // nothing appears until display() is called
}
delay(150);
}
| In the code | What it does |
|---|---|
| #include <Adafruit_SSD1306.h> | Brings in the library you installed. |
| Adafruit_SSD1306 display(128, 64, &Wire, -1); | A display that is 128 × 64 pixels on the I2C wires (Wire), with no reset pin. |
| startDisplay() | Tries address 0x3C, then 0x3D, and tells you in the Serial Monitor which one worked. |
| display.setTextSize(3); display.print(cm); | Big digits. Then display.display() sends the picture to the screen. |
| Problem | What to check |
|---|---|
| "OLED not found" | Unplug USB. Check VCC, GND, SDA to A4 and SCL to A5 by the labels. Push the OLED pins firmly into the breadboard. |
| "Adafruit_SSD1306.h: No such file or directory" | The library is not installed: do step 1 again and choose Install All. |
| The screen stays black but "OLED found" appears | Upload again. If it still stays black, ask us - the display may need a different setting. |
| The screen shows random dots | Press the UNO's reset button. If it keeps happening, a wire may be loose. |
| It always says no echo | Unplug USB. Check VCC to the + rail, GND to the − rail, TRIG to pin 9 and ECHO to pin 10 - by the labels on the sensor, not their position. Point it at a big flat object 20-50 cm away. |
Draw a frame round the screen: add display.drawRect(0, 0, 128, 64, SSD1306_WHITE); just before display.display(). Then try a circle with display.drawCircle(110, 8, 6, SSD1306_WHITE);
A4 (SDA, data) and A5 (SCL, clock).
The picture is built in memory first and sent to the screen in one go.
Ready-made code you install once and use with #include.
Most problems are one wire in the wrong hole. Check these first:
Open the troubleshooting table for this step →
Hi DIY KIT, I'm stuck on the Arduino Ultrasonic Distance & Parking Learning Kit. Lesson: __ Step number: __ What should happen: __ What happens instead: __ Error message (copied exactly): __ I'll attach a photo of my wiring.
Wire the HC-SR04 and read distances in the Serial Monitor. Compare them with a ruler to see how accurate the sensor really is. Do Start here first.
Green, yellow and red LEDs show how close an object is: green 50 cm and more, yellow 20-49 cm, red under 20 cm. Yellow blinks when there is no echo.
Like a car's parking sensor on your desk: slow beeps under 20 cm and fast beeps under 10 cm, while the LEDs show the zone. A learning model only.
We use cookies that the shop needs to work — signing in, your cart, form security. We also measure how the shop is used so we can stock the right parts and fix what is broken. Choose “Only essential” and we will not load Google Analytics or the Meta pixel, and will not record your visit. Read more.