HC-SR04 on Arduino Uno

Steps done: 0 of 5

~9~10UNOGND5V2001005020105ATMEGA328P-PU

Guess the answer and press Run

50cm

distance to the object, cm50time, s0.0

Lesson steps

How long does the echo last?

An object stands 50 cm from the sensor. Pick an answer before you run it.

The object is 50 cm away. How long is the pulse on ECHO?

Sketch

hcsr04_distance.inoTo board
// HC-SR04: VCC to 5V, TRIG to D9, ECHO to D10, GND to GND

const int trig = 9;
const int echo = 10;
int nearCm = 10;     // closer than this the monitor prints "close", cm
int waitMs = 200;    // pause between readings, ms

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long us = pulseIn(echo, HIGH, 30000);
  int cm = us / 58;
  Serial.print(cm);
  if (cm < nearCm) {
    Serial.println(" см, близко");
  } else {
    Serial.println(" см");
  }
  delay(waitMs);
}

Tap a line or an orange number

Serial monitor

Serial, 9600 baudboard time 0.0 s

About the lesson

The HC-SR04 on a stand: VCC to 5V, TRIG to D9, ECHO to D10, GND to GND. In front of the sensor there is a track with a toy car, a box, a ball or a wall, the distance is set by a slider or the back and forth button. About 12 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.

Build it for real

Parts

  • Arduino Uno R3
  • HC-SR04 range finder
  • 4 male-to-female jumper wires

Wires

FromTo
Rangefinder VCC5V
TRIGD9
ECHOD10
GNDGND

The HC-SR04 runs on 5 V. On ECHO it outputs 5 V: that is fine for an Uno, but ESP32 and other 3.3 V boards need a divider.

The sketch below goes to the board with the default numbers: nearCm 10, waitMs 200. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.

Flash it to your own board

The whole sketch

// HC-SR04: VCC to 5V, TRIG to D9, ECHO to D10, GND to GND

const int trig = 9;
const int echo = 10;
int nearCm = 10;     // closer than this the monitor prints "close", cm
int waitMs = 200;    // pause between readings, ms

void setup() {
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig, LOW);
  long us = pulseIn(echo, HIGH, 30000);
  int cm = us / 58;
  Serial.print(cm);
  if (cm < nearCm) {
    Serial.println(" см, близко");
  } else {
    Serial.println(" см");
  }
  delay(waitMs);
}

What to keep in mind on a real board

Soft and slanted surfaces reflect sound worse: fabric, foam and a wall at an angle give gaps and random numbers. Closer than 2 cm the module does not measure.

If there is no echo, pulseIn() waits 30000 µs and returns 0, and the sketch prints 0 cm. In your own project, it is better to skip such readings.

The sketch takes 2688 bytes of flash and 218 bytes of RAM.

Licenses of the lesson code

The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.

The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.

MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.