Skip to main content

Grove Ultrasonic Ranger: how it works

Lab 3b reads distance with the Grove Ultrasonic Ranger. The lab sheet has the sketch; this page explains how the sensor measures and why it sometimes gives strange numbers.

How it measures​

  1. The sensor sends a short burst of ultrasound (40 kHz, too high to hear).
  2. The sound bounces off an object and comes back.
  3. The sensor measures the time until the echo returns.

Sound travels about 343 m/s = 0.0343 cm per microsecond, and the sound goes there and back, so:

distance (cm) = time (µs) × 0.0343 ÷ 2 (≈ time ÷ 58)

Example: an echo after 1166 µs → 1166 × 0.0343 ÷ 2 ≈ 20 cm.

Grove vs HC-SR04​

Grove Ultrasonic Ranger (lab)HC-SR04 (common online)
Pins3 used: VCC, GND, SIG4: VCC, Trig, Echo, GND
Trigger/echoShare one pinSeparate pins
CodeLibrary: ultrasonic.MeasureInCentimeters()digitalWrite(trig...), pulseIn(echo...)
Rangeabout 3–350 cmabout 2–400 cm

Online HC-SR04 tutorials do not work as-is with the Grove sensor (they expect two pins). Use the Grove library.

ArduinoGrove cableSensor
5VRedVCC
GNDBlackGND
D7 (any digital pin)YellowSIG

Ultrasonic ultrasonic(7); must use the same pin number as the yellow wire.

Why readings go wrong​

ReadingCause
0Echo never came back (timeout): nothing in range, wrong pin, loose wire, or library not matching the sensor
Very large (e.g. 500+)Object out of range, or sound bounced off something far away
Jumps between valuesSoft (cloth, foam) or angled surfaces scatter the sound; a hand is a poor target
Too shortAnother object in the cone (the sensor "sees" in a cone about 15° wide): table edge, wires, breadboard
Wrong below 3 cmToo close: the echo comes back while the sensor is still sending

Test with a flat, hard object (a book) held straight in front of the sensor.

Steadier readings​

  • Take several readings and use the median (middle value), which ignores odd spikes better than an average.
  • Leave at least ~60 ms between readings, so the previous echo has died away.
  • Ignore readings of 0 (treat them as "no reading") rather than storing them.

Problems​

ProblemFix
Always 0Wrong pin in Ultrasonic ultrasonic(7), yellow wire not on that pin, or no 5V.
Ultrasonic.h: No such file or directoryInstall the Grove Ultrasonic Ranger library (Library Manager, or the ZIP from the lab: Sketch → Include Library → Add .ZIP Library).
'class Ultrasonic' has no member named 'MeasureInCentimeters'A different "Ultrasonic" library is installed. Remove it and install the Grove one.
Works alone, wrong with the LCD/temperature code addeddelay too short between readings, or an LCD clear() flicker making it look wrong. Print to Serial to check.