# Build an Arduino Ultrasonic Distance Meter with OLED and LED Alert

**One-sentence pitch:** Measure the distance to nearby objects in real time using an ultrasonic sensor, and get a visual warning when an object gets too close.

**Difficulty:** Beginner  
**Estimated build time:** 1–2 hours  
**Platforms:** Arduino UNO R3 / compatible board  
**Suggested categories:** Measurement, Obstacle detection, Educational electronics

## Story

### Introduction

This project demonstrates how to build a compact, USB-powered distance meter using an Arduino UNO, an HC-SR04 ultrasonic sensor and a 0.96-inch OLED screen. The OLED displays the measured distance in centimetres. When an object comes within 15 cm of the sensor, a red LED turns on to indicate a proximity warning.

The project introduces three core maker skills: ultrasonic distance measurement, I2C OLED interfacing and threshold-based output control. It can be adapted for robotics demonstrations, collision warnings or classroom exercises.

### How it works

The Arduino sends a brief trigger pulse to the HC-SR04. The sensor transmits an ultrasonic burst and returns an ECHO pulse whose duration represents the round-trip travel time of the sound. The Arduino estimates distance by multiplying the pulse duration by the approximate speed of sound and dividing by two:

Distance (cm) = Echo duration (microseconds) × 0.0343 / 2

The code treats timeouts or values outside the sensor's approximate 2–400 cm range as invalid, and shows **NO ECHO** rather than an incorrect distance. For a valid reading at or below 15 cm, the warning LED illuminates. This is an educational distance indicator, not a safety-rated collision detector.

### Components (add these in Hackster's “Things” section)

| Hardware | Quantity | Source |
|---|---:|---|
| Arduino UNO R3 compatible development board | 1 | https://rajivelectronics.com/product/arduino-uno-r3-dip-ic-clone-development-board-atmega328p-compatible-board |
| HC-SR04 ultrasonic distance sensor | 1 | https://rajivelectronics.com/product/hc-sr04-ultrasonic-distance-sensor-module |
| 0.96-inch 128x64 SSD1306 4-pin I2C OLED display | 1 | https://rajivelectronics.com/product/0-96-o-led-display-screen-module-4-pin |
| 5mm red LED | 1 | Any suitable red LED |
| 220 Ohm resistor | 1 | Standard through-hole resistor |
| Breadboard | 1 | 400-point or 830-point |
| Jumper wires | 1 set | Male-to-male |
| Suitable USB data cable | 1 | For your particular board |

**Software:** Arduino IDE; Arduino libraries **Adafruit GFX Library** and **Adafruit SSD1306**. These library dependencies may prompt for additional supporting packages.

### Step 1: Prepare the hardware

Disconnect the UNO from USB before making connections. Put the HC-SR04, OLED and LED on or beside your breadboard. Check that the OLED module is specifically marked as 5V-compatible before connecting it to the Arduino 5V pin. If it is 3.3V-only, do not follow the 5V wiring shown; power and level-shift it according to its datasheet.

### Step 2: Wire the HC-SR04

- Sensor VCC → Arduino 5V
- Sensor GND → Arduino GND
- Sensor TRIG → Arduino digital pin D9
- Sensor ECHO → Arduino digital pin D10

### Step 3: Connect the OLED

For a **5V-compatible 4-pin SSD1306 I2C OLED**:

- OLED VCC → Arduino 5V
- OLED GND → Arduino GND
- OLED SDA → Arduino A4 (SDA)
- OLED SCL → Arduino A5 (SCL)

The sketch assumes I2C address `0x3C`. Some modules use `0x3D`; change `OLED_ADDRESS` if your display uses that address.

### Step 4: Add the proximity warning LED

Connect Arduino pin D6 to one side of a 220 Ohm resistor. Connect the other side of that resistor to the LED anode (longer leg). Connect the LED cathode (shorter leg/flat-edge side) to GND. The resistor must be in series with the LED.

### Step 5: Install software and upload

1. In Arduino IDE, select the correct board under **Tools → Board** and the connected COM/serial port.
2. In **Library Manager**, install **Adafruit GFX Library** and **Adafruit SSD1306** (and prompted dependencies).
3. Open `Arduino_OLED_Distance_Meter.ino`, verify the pins and OLED address, and upload.
4. Open **Serial Monitor** at 9600 baud for an additional text readout.

### Step 6: Verify the operation

Hold a flat object approximately 30 cm in front of the sensor; the display should show a distance and **STATUS: CLEAR**, with the LED off. Move the object closer than 15 cm; the LED should turn on and the display should show **ALERT: TOO CLOSE**. Remove the object or aim the sensor into an area with no detectable reflection; the screen may show **NO ECHO**. Check readings against a ruler and record your real results before publishing.

### Troubleshooting

- **Blank OLED:** Check VCC/GND, SDA/SCL, display voltage rating and I2C address; use an I2C scanner sketch if necessary.
- **Always NO ECHO:** Check TRIG and ECHO wiring, common ground, and sensor direction; soft fabric or angled objects can give weak reflections.
- **LED never turns on:** Check resistor placement, LED polarity and that `ALERT_DISTANCE_CM` is set to `15.0`.
- **Readings vary:** Aim at a flat, rigid target and keep the sensor stable. Temperature and reflected sound affect ultrasonic measurements.

### Possible improvements

You can add a buzzer for an audible warning, smooth measurements using a moving average, or replace the LED with a simple traffic-light indicator. Always update the code and bill of materials when adding hardware.

### Parts and learning resources

This tutorial was prepared for the electronics learning community by **Rajiv Electronics, Pune, India**. Component links are provided in the **Things** list so builders can find the exact hardware used. General component information and additional maker supplies are available at https://rajivelectronics.com/.

**Before publishing:** Assemble and test the actual circuit, take your own clear cover photo and step-by-step photographs, and attach the wiring diagram and source code. Do not describe the sample checks above as completed test results until you have verified them on your hardware.
