---
title: "The Ultrasonic Sensor"
date: "2021-07-21"
categories: ["Sensor Guides"]
tags: []
excerpt: "Learn all about the ultrasonic sensor!"
featuredImage: "/images/resources/ultrasonic-sensor.png"
---

![](/images/content/d9e944-the-ultrasonic-sensor.png)

The ultrasonic sensor is one of the fives types of sensors on the Micromelon rover.

**What Is It Used For?**

The ultrasonic sensor is used to detect the distance to the closest object in front of the sensor. The sensor is placed on the front of the rover, so we can detect if any obstacles (like walls) are 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 a frequencies higher than 20kHz.  How does it detect objects by using sound? Simply by following the steps below.

**Ultrasonic Distance Sensing**

- The sensor makes the 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 or surface back towards the sensor.

- The sound waves travel 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** The sensor is made up of two different components,** the transmitter** and ** the receiver**. These are called transducers. The transmitter is making the ultrasonic sound and the receiver is listening for the reflected sound waves.  Some ultrasonic sensors use a single transducer as both the transmitter and receiver.

![](/images/content/aaa95f-sensor-detector.png)

**Why Can’t Humans Hear The Sound?**

Ultrasonic sensors typically use frequencies between 30kHz and 500kHz. In the case of the Micromelon Rover, the sensor uses approximately 40kHz. In general, humans can only hear sounds up to 20 kHz.

Wanna know something cool though? If you take a video of the ultrasonic sensor when it is running, the camera’s microphone will hear it. We can hear it playing back in the video. An example of this is on the right. Hear the ticking sound when the sensor is pointed at the microphone?

**Ultrasonic Sensors Exist in Nature!**

Echo location is another name for ultrasonic sensing. Bats and dolphins are just two examples of animals who use ultrasonic sensing to detect where they are going and where to find food.

![](/images/content/624068-micromelon-dolphin.png)

Finley (above) works in Micromelon marketing

Finley (pictured) uses ultrasonic sensing to detect where pods of fish are in the sea when on lunch break. Unfortunately when I start yelling at lunch trying to find food, people get annoyed with me.

### **Ultrasonic Problems & Limitations**

**Sound Waves Reflected Away Or Absorbed**

![](/images/content/b91e97-incorrect-bounce-animation.gif)

When the emitted sound waves don’t reflect back to our sensor we won’t be able to get an accurate reading. This will happen when trying to detect angled or curved surfaces.  Soft or textured surfaces can absorb the sound waves and reduce the range of the sensor.

To ensure you are able to get consistent readings, make sure the ultrasonic is pointed at flat, hard surfaces. A wide variety of surfaces will still be detected, but may be unreliable.

**Rover Ultrasonic Range Is Limited To Approximately 1m**

The further the sound waves need to travel, the quiter the echo becomes.  If the echo becomes too quiet, the robot can no longer hear it and can’t detect the object. In order for the sensor to detect more and more distant objects, the Rover needs to emit a louder sound, which requires more power.

### Programming The Ultrasonic Sensor

To write some code that uses the ultrasonic sensor, open the Micromelon Code Editor and connect Rover. You can use sensor view to see what the ultrasonic sensor is detecting. To open sensor view, click the ROVER button next to your Rover’s name.

In the sensor view dialog, the ultrasonic value is located at the top of the rover diagram. The ‘CM’ next to the value indicates centimetres. Point the ultrasonic sensor at different objects and watch this value to measure how far away they are.

**What does 255cm mean?**

255 is the returned value when the sensor is unable to detect an object. If you are seeing 255 it means you may be attempting to detect objects further away than 1m or because the sound waves are being absorbed or reflected away from the sensor.

![](/images/content/f16a1c-opening-sensor-view.gif)

![](/images/content/bd5aa7-using-sensor-mode-small-small.gif)

**Using The Ultrasonic In Code**

Let’s write a simple program to print our ultrasonic sensor value every second.

![](/images/content/1e6d33-example-code.gif)

- Start by adding a **Print** statement. You can find this block in the ** Utilities** category.

- Place our ultrasonic sensor read inside the print statement.

You can find the **Read Ultrasonic** block in the ** Sensors** category. The Python command is ** Ultrasonic.Read()**.

- Wrap this Print statement in a **While True Loop**.

- Add a **Delay For 1 Second** block so this loop waits 1 second before running again.

- Press Play!

While this is running we will see our ultrasonic value every second.

- You can now write other code that makes decisions based on the ultrasonic sensor.

### Wrapping Up

Now that we’re familiar with what the ultrasonic is, how it works, and how to program with it, it’s time to start doing some activities that use the ultrasonic sensor.
