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
- The sensor sends a short burst of ultrasound (40 kHz, too high to hear).
- The sound bounces off an object and comes back.
- 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) | |
|---|---|---|
| Pins | 3 used: VCC, GND, SIG | 4: VCC, Trig, Echo, GND |
| Trigger/echo | Share one pin | Separate pins |
| Code | Library: ultrasonic.MeasureInCentimeters() | digitalWrite(trig...), pulseIn(echo...) |
| Range | about 3–350 cm | about 2–400 cm |
Online HC-SR04 tutorials do not work as-is with the Grove sensor (they expect two pins). Use the Grove library.
| Arduino | Grove cable | Sensor |
|---|---|---|
| 5V | Red | VCC |
| GND | Black | GND |
| D7 (any digital pin) | Yellow | SIG |
Ultrasonic ultrasonic(7); must use the same pin number as the yellow wire.
Why readings go wrong
| Reading | Cause |
|---|---|
| 0 | Echo 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 values | Soft (cloth, foam) or angled surfaces scatter the sound; a hand is a poor target |
| Too short | Another object in the cone (the sensor "sees" in a cone about 15° wide): table edge, wires, breadboard |
| Wrong below 3 cm | Too 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
| Problem | Fix |
|---|---|
| Always 0 | Wrong pin in Ultrasonic ultrasonic(7), yellow wire not on that pin, or no 5V. |
Ultrasonic.h: No such file or directory | Install 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 added | delay too short between readings, or an LCD clear() flicker making it look wrong. Print to Serial to check. |