Lesson 07: Ultrasonic Distance Sensor (HC-SR04)

Course: Robotics & Arduino Lesson 07 of 12

Measure distance using echo timing, wire the HC-SR04 and read in centimetres.


Learning Objectives

By the end of this lesson you will be able to:


Hardware Required

See the Kit List on the course overview page for the full component list. For this lesson you will specifically need the components mentioned in the steps below.


Step-by-Step Guide

  1. HC-SR04 pins: VCC (5V), GND, Trig (trigger), Echo (receive).
  2. Wire: VCC → 5V, GND → GND, Trig → pin 10, Echo → pin 11.
  3. Principle: Trig sends a 10µs pulse. Sensor emits 8 ultrasound bursts. Echo goes HIGH until return.
  4. Duration of Echo HIGH = round-trip time in microseconds.
  5. Formula: distance (cm) = duration / 58.0 (based on speed of sound).
  6. Code: digitalWrite(TRIG, LOW); delayMicroseconds(2); digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);
  7. long duration = pulseIn(ECHO, HIGH);
  8. float distance = duration / 58.0;
  9. Print distance to Serial Monitor. Test by moving your hand closer and further away.
  10. Add an LED that turns red when distance < 20cm (obstacle nearby).

Extension Challenges


Review Questions

  1. Explain in your own words what this lesson’s main component does.
  2. What would happen if you changed one key parameter in your sketch?
  3. How does this lesson’s content connect to the final robot build in Lesson 12?

Troubleshooting Tips


Back to Course Next: Lesson 08