Using the Ultrasonic Distance Sensor

Learn to sense distance with Sparki's Ultrasonic Distance Sensor


Lessons You Should Know


In this lesson, we will start learning how to do some basic tasks with sensors, specifically with the Ultrasonic Distance Sensor, included on Sparki. This lesson only gives a basic example on how it works, a more advanced explanation is shown here. Please take a few minutes to read that link if you are completely new to Ultrasonic Distance Sensors and want to know more.

Ultrasonic Identify

This sensor finds objects with sound the same way a bat or dolphin does. One part of the sensor is a speaker that sends out a sound wave. The other part is a microphone that then measures how long it takes for the sound wave to come back. The longer it takes to come back, the further away the object:

Basic Sensor Usage

The ultrasonic ranger easy to use because it is a single block.  This block tells Sparki, or “returns”, the distance in centimeters that the sensor measures as a number. Here is what this block looks like:

Be careful because this sensor is not as fast as Sparki’s brain. So, you will always need to add a small delay if you are going to read it. Here is what the delay block looks like:

Let’s do a small example, where we get the number given by the sensor and use it to make some music.

Today, we’re going to make Sparki into a musical instrument using the ultrasonic distance sensor as input, and the buzzer as output.

This kind of instrument is close to one that is called a theremin. It is played by putting your hand in front of the sensor. The closer you put your hand (or anything else) in front of Sparki, the lower the tone will go.

This code senses the distance in centimeters, then multiplies that number by 10, and sends it to the buzzer. The buzzer gets this distance-multiplied-by-10 number, and turns it into a sound. This sound is the buzzer vibrating this number times a second, or ‘hertz’. It then waits 10 milliseconds, and does it all over again. Here is the block program:

If you change the number that multiplies the number from the ultrasonic distance sensor block, and with the time length of the notes, you can get different sounds:

Moving the Head

One nice feature of Sparki is that the ultrasonic sensor is mounted on a rotating head. It’s powered by Sparki’s servo, so we can control it with the following blocks (please read the this link if you want to know more about servos):

You should give this block a number. That number will be the degrees that it turns Sparki’s head. Here is a guide on how degree numbers move Sparki’s head. At zero, Sparki’s head is centered. The bigger the number, the more to the right. The smaller the number, the more to the left:


Here is a basic example of moving Sparkis head. Here’s what happens:

  1. Sparki’s head moves to the left (-90 degrees)
  2. Sparki waits one second (1000 milliseconds)
  3. Sparki’s head moves to the center (0 degrees)
  4. Sparki waits one second (1000 milliseconds)
  5. Sparki’s head moves to the right (90 degrees)
  6. Sparki waits one second (1000 milliseconds)
  7. Sparki starts the program all over again

Remember when having Sparki move: it needs fresh batteries, it’s On/Off Switch ‘On’, and to be watched to prevent it from falling off edges.


  1. What do you think happens when the sensor is used against materials that can absorb the mechanical ultrasonic waves? Why not try some experiments?
  2. Have you seen this sensor used in real modern cars? For doing what?
  3. And what about animals? Which animals use this kind of sensor to navigate?

You may notice that the sensor’s numberes are not always accurate. We explain how to help with this in our Signal Filtering lesson.