In this beginner-friendly project you will build a digital distance meter that measures how far an object is and displays the reading in real time on a crisp OLED screen.
What you’ll build
A compact distance meter: the Arduino Nano triggers the HC-SR04 ultrasonic sensor, calculates the distance from the echo time, and prints it to the 0.96″ OLED display, updating several times per second.
Components you’ll need
Step-by-step guide
- Place the Arduino Nano on the breadboard and power the rails: connect 5V to the red rail and GND to the blue rail.
- Wire the HC-SR04: VCC to 5V, GND to GND, Trig to D9, and Echo to D10.
- Connect the OLED display over I2C: VCC to 5V, GND to GND, SDA to A4, and SCL to A5.
- Double-check all connections against the wiring diagram before powering up.
- Install the Adafruit_GFX and Adafruit_SSD1306 libraries from the Arduino Library Manager.
- Upload the sketch below, then open the project on the breadboard and move your hand in front of the sensor to watch the distance update live.
The code
Here is the core sketch. It reads the echo pulse, converts it to centimetres, and refreshes the OLED.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire);
const int trig = 9, echo = 10;
void setup(){ pinMode(trig,OUTPUT); pinMode(echo,INPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); }
void loop(){
digitalWrite(trig,LOW); delayMicroseconds(2);
digitalWrite(trig,HIGH); delayMicroseconds(10); digitalWrite(trig,LOW);
long d = pulseIn(echo,HIGH)*0.034/2;
display.clearDisplay(); display.setTextSize(2);
display.setCursor(0,20); display.print(d); display.print(" cm");
display.display(); delay(200);
}Build this project
Add all 5 required components to your cart in one click — total ₹535.
Add all components to cart