Using the Ultrasonic Distance Sensor (miniBloq)

Introduction

In this lesson, we will start learning how to do some basic tasks with sensors, specifically with the ultrasonic ranger included within Sparki. Although we will explain here the basics of usingthis sensor, the inner workings are better explained here. Please take a few minutes to read that link if you are completely new to ultrasonic ranger finders.

Ultrasonic Identify

Basic Sensor Usage

The ultrasonic ranger is really easy to use.  There is just one instruction, which basically returns the distance read by the sensor: Be careful because this sensor is not as fast as Sparki’s brain. So, you will always need to add a delay if you are going to read it inside the program’s loop. Let’s see a small example, where we read the value returned by the ranger and use it to generate music. This is called a theremin, and  is a musical instrument that is played by holding your hand in front of a sensor. The farther out from the sensor, the higher the tone.  Today, we’re going to make Sparki into a theremin using the ultrasonic range finder as input, and the buzzer as output:   If you play around a bit with the number that multiplies the sensor readings from the ultrasonic sensor block, and with the duration of the notes, you can get different sounds:

Moving the Head

One nice feature of Sparki is that the ultrasonic ranger is mounted on a rotating head. It’s powered by Sparki’s servo, so we can control it with the following instructions (please read the previous link if you are not familiar with this kind of servos): As you will see when using that block, you should specify the number of degrees to rotate Sparki’s head. The following image may help us understand this value:   Here is a very simple example: Warning_Triangle Please remember to check that the batteries are properly connected (and charged!). And as we are going to use the servos here, please check that the On/Off Switch is on. Another important thing to take care of when playing with the robot’s motors is to be careful not to be working over a table. A fall from that table could permanently damage your Sparki.

Questions

  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?
 

Related Lessons

So, now that we know how to read the sensor and how to move it, we are ready to go to the next lesson: Wall Avoidance. Finally, at this time, you may have noted that some times, the readings are not that accurate. In future lessons, we will try to explain some techniques to reduce these and other problems when reading sensors.  

SparkiDuino Programming Environment

This lesson is also available for the SparkiDuino Programming Environment. SparkiDuino will allow you to program your Sparki using standard Arduino syntax.