In this project, we will build a simple ultrasonic distance meter using an Arduino UNO, HC-SR04 ultrasonic sensor, and a 0.96-inch OLED display.
The system measures the distance between the sensor and an object, displays the result in centimetres, and activates a warning LED whenever an object comes within 15 cm.
This beginner-friendly electronics project demonstrates ultrasonic sensing, microcontroller programming, I2C communication, and proximity detection.
How It WorksThe HC-SR04 sensor emits a short ultrasonic pulse and listens for the returning echo.
The Arduino measures the time between transmission and reception and estimates the distance using the approximate speed of sound.
Distance (cm) = Echo time (microseconds) × 0.0343 / 2
The calculated distance is displayed on the OLED screen.
When the measured distance is 15 cm or less, the warning LED switches on. When the object moves farther away, the LED switches off.
If no valid echo is received, the display shows "NO ECHO".
Components RequiredArduino UNO R3 compatible development board – 1 Qty
- Arduino UNO R3 compatible development board – 1 Qty
HC-SR04 ultrasonic distance sensor – 1 Qty
- HC-SR04 ultrasonic distance sensor – 1 Qty
0.96-inch I2C OLED display, 128x64 SSD1306 – 1 Qty
- 0.96-inch I2C OLED display, 128x64 SSD1306 – 1 Qty
Red LED – 1 Qty
- Red LED – 1 Qty
220 Ohm resistor – 1 Qty
- 220 Ohm resistor – 1 Qty
Breadboard – 1 Qty
- Breadboard – 1 Qty
Jumper wires – 1 Set
- Jumper wires – 1 Set
Compatible USB data cable – 1 Qty
- Compatible USB data cable – 1 Qty
Connect the HC-SR04 sensor to the Arduino UNO:
VCC → Arduino 5V
- VCC → Arduino 5V
GND → Arduino GND
- GND → Arduino GND
TRIG → Digital Pin 9
- TRIG → Digital Pin 9
ECHO → Digital Pin 10
- ECHO → Digital Pin 10
The TRIG pin initiates the measurement, while the ECHO pin sends the returning pulse timing to the Arduino.
Step 2: Connect the OLED DisplayUse a 4-pin SSD1306 I2C OLED module rated for 5V operation.
VCC → Arduino 5V
- VCC → Arduino 5V
GND → Arduino GND
- GND → Arduino GND
SDA → Arduino A4
- SDA → Arduino A4
SCL → Arduino A5
- SCL → Arduino A5
The OLED communicates using I2C, requiring only two signal connections.
Check your particular display's voltage rating before connecting power.
Step 3: Connect the Warning LEDConnect Arduino Digital Pin 6 through a 220 Ohm resistor to the LED anode.
Connect the LED cathode to Arduino GND.
The resistor limits LED current and protects the LED from excessive current.
Step 4: Install Arduino LibrariesOpen the Arduino IDE and navigate to Library Manager.
Install:
Adafruit GFX Library
- Adafruit GFX Library
Adafruit SSD1306
- Adafruit SSD1306
Install any additional dependencies requested by the library manager.
Step 5: Upload the Arduino CodeOpen the supplied Arduino sketch.
Select the appropriate Arduino UNO board and serial port.
Upload the program to the development board.
The sketch reads the ultrasonic sensor, calculates the distance, updates the OLED display, and controls the warning LED automatically.
Step 6: Test the ProjectAfter uploading the code, place a flat object approximately 30 cm in front of the ultrasonic sensor.
The OLED should display the measured distance and indicate "STATUS: CLEAR".
Move the object closer than 15 cm.
The OLED should show "ALERT: TOO CLOSE", and the LED should illuminate.
Move the object away, and the warning LED should switch off.
Compare the measurements with a ruler to check the working accuracy of your assembled prototype.
TroubleshootingOLED not displaying: Check power connections, SDA/SCL wiring, and the I2C address. The supplied code assumes address 0x3C.
No distance reading: Check the ultrasonic sensor's TRIG and ECHO connections, power supply, and target orientation.
LED not working: Check LED polarity, the series resistor, and the connection to Digital Pin 6.
ApplicationsEducational Arduino electronics projects
- Educational Arduino electronics projects
Robotics obstacle detection demonstrations
- Robotics obstacle detection demonstrations
Ultrasonic sensor experiments
- Ultrasonic sensor experiments
Distance measurement prototypes
- Distance measurement prototypes
Proximity warning systems
- Proximity warning systems
STEM and engineering laboratory activities
- STEM and engineering laboratory activities
The project can be expanded with an audible buzzer, adjustable warning distance, or multiple indicator LEDs.
More advanced versions could include data logging or wireless monitoring using an ESP32 board.
About Rajiv ElectronicsThis educational project has been prepared by Rajiv Electronics, Pune, India, to help students, hobbyists, and electronics enthusiasts learn practical microcontroller interfacing.
Electronic components, development boards, sensors, and prototyping supplies are available at:










Comments