Level 2Unit B · Session 10
10

Unit B · Things that move, things that show

How far away is that?

What we are making: a distance meter. Point it at a wall, walk backwards, and watch the number climb.

How it actually works

The HC-SR04 has two openings. One is a tiny loudspeaker, the other a tiny microphone. The board shouts — at a pitch far too high for you to hear — and then counts how long the echo takes to come back.

Sound travels about 340 metres a second, which works out at roughly 58 microseconds per centimetre there and back. So the program divides the echo time by 58 and gets centimetres. That is the whole trick, and it is why the number 58 is in your code.

New words
UltrasonicSound too high-pitched for humans to hear.
TrigThe pin that makes it shout.
EchoThe pin that reports how long the echo took.
pulseIn()An Arduino instruction that times how long a pin stays HIGH.
Arduino UnoUSB to the laptop5VGNDA4A5910LCD1602 I2CVCCGNDSDASCLHC-SR04the one with two eyesVCCTrigEchoGNDTrigEchoTrig is the shout. Echo is the listening. Swap them and you get zero, always.
Distance on a screen. Trig and Echo are different jobs — the labels matter.
A distance meter
1#include <LiquidCrystal_I2C.h>
2 
3LiquidCrystal_I2C screen(0x27, 16, 2);
4int trigPin = 9;
5int echoPin = 10;
6 
7void setup() {
8 screen.init();
9 screen.backlight();
10 pinMode(trigPin, OUTPUT);
11 pinMode(echoPin, INPUT);
12}
13 
14void loop() {
15 digitalWrite(trigPin, LOW); // start clean
16 delayMicroseconds(2);
17 digitalWrite(trigPin, HIGH); // shout
18 delayMicroseconds(10);
19 digitalWrite(trigPin, LOW);
20 
21 long echoTime = pulseIn(echoPin, HIGH); // how long until it came back
22 long cm = echoTime / 58; // sound travels 1 cm in 58 us, there and back
23 
24 screen.setCursor(0, 0);
25 screen.print("Distance: ");
26 screen.setCursor(0, 1);
27 screen.print(cm);
28 screen.print(" cm ");
29 
30 delay(300);
31}

Do it

  1. Wire it up. Trig to 9, Echo to 10 — swapping them gives you zero, every time.
  2. Upload, then hold a book in front of it and move it slowly away.
  3. Check it against a ruler at 10 cm, 30 cm and 50 cm.
  4. Point it at something soft, like a jumper. Then at something hard. Note the difference.

Change it

  1. Add a line that prints "TOO CLOSE" when cm is under 10.
  2. Make the delay shorter and see how twitchy the reading becomes.
  3. Aim it at a wall at an angle rather than straight on. Explain what you see.
💡 Tip
Soft things swallow the echo. A cushion, a curtain or a jumper absorbs the sound instead of bouncing it, so the sensor reports nonsense or nothing. This is not a fault in your wiring — it is a real limitation of the sensor, and worth knowing before you design a project around it.
What you seeWhyWhat to do
Always reads 0Trig and Echo are swapped.Trig to 9, Echo to 10. This is the answer nine times out of ten.
Wild numbers that jump aboutNothing in range, or a soft surface.Point it squarely at a wall to check the sensor itself is fine.
Reads once, then freezespulseIn is waiting for an echo.Move something into range and it recovers on its own.
Works on a wall, not on a personClothing absorbs the sound.A real limitation, not a fault. Design around it.

What you learned

  • Distance can be measured by timing an echo, and the maths is one division.
  • Trig and Echo are different jobs. The labels are not decoration.
  • Every sensor has things it cannot see. Knowing those is part of using it.
Challenge optional — only if you finish early
  • Show the distance as a bar across the screen instead of a number.
9. Is it warm in here?