Ultrasound sensors are a useful way of measuring distance. Ultrasound sensors communicate with the kit using two wires. A signal is sent to the sensor on the trigger pin, and the length of a response pulse on the echo pin can be used to calculate the distance.

Ultrasound should only be considered accurate up to around two metres, beyond which the signal can become distorted and produce erroneous results.

Wiring up the sensor

The sensor has four pin connections: ground, 5V (sometimes labelled vcc), trigger and echo. Most ultrasound sensors will label which pin is which. The ground and 5V should be wired to the ground and 5V pins of the Arduino respectively. The trigger and echo pins should be attached to two different digital IO pins. Take note of these two pins, you’ll need them to use the sensor.

If the sensor always returns a distance of zero, it means the trigger and echo pins are connected the wrong way! Either change the pin numbers in the code, or swap the connections.