
HC-SR04 Ultrasonic Distance Meter
Beginner20 minA project that measures the distance to the object in front of an HC-SR04 ultrasonic sensor in centimetres and writes it to the Serial Monitor about 10 times a second. The sensor measures how long the echo of its sound wave takes to come back; the code turns that time into centimetres by dividing it by 58. No extra library needed.

What you need
Wiring steps
- 1
Power the sensor

Plug a red jumper wire into the 5V pin on the Arduino's POWER header. Connect its other end to the HC-SR04's Vcc pin.
- Arduino 5V → HC-SR04 Vcc (red wire)
- 2
Connect the trigger (Trig) line

Plug a green jumper wire into the Arduino's digital pin 2. Connect its other end to the HC-SR04's Trig pin. The Arduino starts a measurement by sending a 10-microsecond pulse on this pin (HC_TRIG 2).
- Arduino pin 2 → HC-SR04 Trig (green wire)
- 3
Connect the echo (Echo) line

Plug a blue jumper wire into the Arduino's digital pin 3. Connect its other end to the HC-SR04's Echo pin. The sensor holds this pin HIGH until the sound wave has gone and come back; the code measures that time with pulseIn (HC_ECHO 3).
- Arduino pin 3 → HC-SR04 Echo (blue wire)
- 4
Ground (GND) connection

Plug a black jumper wire into the GND pin on the Arduino's POWER header. Connect its other end to the HC-SR04's Gnd pin. That completes all four of the sensor's connections.
- Arduino GND → HC-SR04 Gnd (black wire)
Arduino sketch
#define HC_TRIG 2
#define HC_ECHO 3
unsigned long duration;
uint16_t distance;
void setup() {
Serial.begin(9600);
pinMode(HC_TRIG, OUTPUT);
pinMode(HC_ECHO, INPUT);
digitalWrite(HC_TRIG, LOW);
}
void loop() {
digitalWrite(HC_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(HC_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(HC_TRIG, LOW);
// Timeout of 30,000 µs
// (about 5 metres)
duration = pulseIn(HC_ECHO, HIGH, 30000);
if (duration == 0) {
Serial.println("Distance: Out of Range!");
} else {
distance = duration / 58;
Serial.print("Distance = ");
Serial.print(distance);
Serial.println(" cm");
}
delay(100);
}
What the Serial Monitor shows
Distance = 12 cm Distance = 13 cm
If a wire is loose
Distance: Out of Range!
Similar projects
Parking SensorIntermediate45 minLike the parking sensor in a car: the HC-SR04 measures the distance to whatever is in front of it, the buzzer starts beeping once something comes within 1 metre and the beeps get faster as it comes closer; below 10 cm the tone is steady. Green, yellow and red LEDs show the far, middle and near zones, and the distance is written to the Serial Monitor. It teaches timing with millis() instead of delay, so measuring, beeping and the lights run without waiting for each other, writing a function that returns a value, splitting into zones with if / else if, and turning the distance into a beep gap with map. No extra library needed.Open project
OLED Distance MeterIntermediate40 minA meter that shows the distance measured by an HC-SR04 on a 0.96" OLED screen, no computer needed: the distance in large text on top, and below it a bar graph of the last 128 readings scrolling to the right. Move your hand closer and further and watch the graph rise and fall. It teaches driving a screen over I2C, drawing text and lines with Adafruit GFX, keeping readings in a ring buffer, and timing with millis() instead of delay. Install the "Adafruit SSD1306" and "Adafruit GFX" libraries from the Library Manager.Open project
DHT22 Temperature and Humidity MeterBeginner20 minA project that measures humidity and temperature, in both Celsius and Fahrenheit, with a DHT22 sensor and writes them to the Serial Monitor every two seconds. It is more precise than the DHT11 and has a wider measuring range. Before uploading the code, install Adafruit's "DHT sensor library" in the Arduino IDE (and the "Adafruit Unified Sensor" library it asks for).Open project
In the app, tick off every wire, match the parts to your inventory and pick up where you left off.
