Ultrasonic Distance Sensor

The StudioBot has an ultrasonic distance located at the front of the robot. These look like the 'eyes' of the robot.

The Ultrasonic sensor can measure distance by emitting a sound and then listening for the return of that sound. It can use the amount of time that it takes to hear the return sound to infer the distance an object is in front of it.

The following example shows how to use the sensor in its simplest form.

import time
import board
import adafruit_hcsr04

sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.TRIGGER, echo_pin=board.ECHO)

while True:
    try:
        print((sonar.distance,))
    except RuntimeError:
        print("Retrying!")
    time.sleep(0.1)

To read more about Ultrasonic sensors and CircuitPython, have a read of this Adafruit article.

Last updated