The ultrasonic sensor is one of the five types of sensors on the Micromelon Rover.
What Is It Used For
The ultrasonic sensor detects the distance to the closest object in front of it. It’s placed on the front of the Rover, so we can detect any obstacles (like walls) in front of the Rover and how far away they are.
How The Ultrasonic Sensor Works
To detect objects, the ultrasonic sensor uses sound. Ultrasonic sound is defined as sound waves at frequencies higher than 20 kHz. Here’s how it detects objects with sound:
- The sensor emits a high-pitched sound.
- The sound waves travel outwards from the sensor towards objects or surfaces in front.
- The sound waves hit and reflect off the object back towards the sensor.
- The sensor detects the returning sound waves.
Based on how long it took the sound to bounce back, the sensor calculates how far away the object is using the speed of sound.
The Parts That Make It Work

Transmitter and receiver inside a typical ultrasonic sensor
The sensor is made up of two components, the transmitter and the receiver (called transducers). The transmitter emits the ultrasonic sound; the receiver listens for the reflected waves. Some ultrasonic sensors use a single transducer for both roles.
Why Can’t Humans Hear It?
Ultrasonic sensors typically use frequencies between 30 kHz and 500 kHz. The Micromelon Rover’s sensor uses approximately 40 kHz. Humans can generally only hear sounds up to 20 kHz.
If you take a video of the ultrasonic sensor while it’s running, the camera’s microphone will pick it up, and you’ll hear the ticking sound on playback.
Ultrasonic Sensors in Nature
Echolocation is another name for ultrasonic sensing. Bats and dolphins are two examples of animals that use ultrasonic sensing to detect where they’re going and where to find food.

Finley the dolphin works in Micromelon marketing
Limitations and Common Problems
Sound Waves Reflected Away or Absorbed
Sound waves bouncing off an angled surface and missing the sensor
When the emitted sound waves don’t reflect back to the sensor, you won’t get an accurate reading. This happens when trying to detect angled or curved surfaces. Soft or textured surfaces can also absorb the sound waves, reducing the sensor’s effective range.
To get consistent readings, point the ultrasonic sensor at flat, hard surfaces. A wide variety of surfaces will still be detected, but readings may be unreliable.
Range Is Limited to Approximately 1 m
The further the sound waves need to travel, the quieter the echo becomes. If the echo becomes too quiet, the Rover can no longer detect the object. Detecting more distant objects would require emitting a louder sound, which requires more power.
Programming the Ultrasonic Sensor
Open the Code Editor, connect to your Rover, and open Sensor View. Click the ROVER button next to your Rover’s name to open Sensor View.
In the dialog, the ultrasonic value is at the top of the Rover diagram. The CM next to the value indicates centimetres. Point the ultrasonic sensor at different objects and watch the value to measure how far away they are.
What Does 255 cm Mean?
255 is returned when the sensor is unable to detect an object. If you’re seeing 255, you may be trying to detect objects further away than 1 m, or the sound waves are being absorbed or reflected away from the sensor.
Opening Sensor View in the Code Editor
Using the Ultrasonic in Code
Let’s write a simple program to print our ultrasonic sensor value every second.
Building the example program in the Code Editor
- Add a Print statement (in the Utilities category).
- Place a Read Ultrasonic block inside the print statement (in the Sensors category. Python:
Ultrasonic.read()). - Wrap this print statement in a While True Loop.
- Add a Delay For 1 Second block so the loop waits 1 second between reads.
- Press Play.
While running, you’ll see the ultrasonic value every second. From here you can write code that makes decisions based on the ultrasonic sensor.
Wrapping Up
Now that you’re familiar with the ultrasonic sensor, how it works, and how to program it, try one of the activities below.
Continue Learning
Related resourceActivity: Roaming RoverLearn to avoid obstacles with the ultrasonic sensor.
Related resourceActivity: Puppy BotBranching and looping with the ultrasonic sensor.

