Using the Ultrasonic Distance Sensor

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 using this 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

To use the ultrasonic sensor, one command is needed, which 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 short example: here we read the value returned by the ranger and print it on Sparki’s LCD display, using the sparki.println function: See how easy it is? This program will print out the distance read from the ultrasonic ranger in the upper-left corner of the LCD. It even adds the unit (this sensor is metric, so its readings are in centimeters). Although this is a very simple program, it will be always useful for sensor testing. Some times we can make complex programs involving different kinds of sensors at once. If things do not work as expected (which may happen more often than we would like them to!), printing the read values will always be helpful. We will use this basic sensor debugging technique with the LCD for other sensors too. Finally, please take a few moments to see which is the maximum range of the ultrasonic distance sensor. Also, please take note of the fact that the minimum distance that this sensor can read is around 2 cm in theory. But some times, readings below 4 cm are not really able to be trusted.

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): Also, we can use predefined values for the angle_degrees parameter in order to move the robot’s head to pre-calculated useful positions: 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. The previous program just moves the robot’s head without doing anything with the sensor itself. So what about moving and reading it at the same time? The following code does exactly that: Please take a look to the function that we have added here. Without that function, the same code would be repeated 3 times in order to refresh the display each time we have a new reading.  

Animating a Circle Using the Ranger

Finally, here is a very simple example on how to read the sensor and animate a small circle’s size on the LCD display to represent the readings: Try taking the Sparki with your hand and moving it towards a wall, so you can see the circle growing and shrinking: As you may have guessed, the key line here is where the program calculates the diameter of the circle: There, the diameter variable is assigned with the result of multiplying the sensor reading (which is the return value of the sparki.ping instruction) by the maximum possible diameter for the circle, and dividing it by the maximum possible value for the readings (which is approximately between 3.5 and 4.0 meters = 350 to 400 centimeters). Please note that the 32 number for the maximum diameter is fixed by the display’s height, which is 64 pixels. After doing this, the following if statment is there just to ensure that the diameter variable keeps between 0 and 32. A beep will sound every time the variable goes beyond this limit (just for fun!).  

Questions

  1. What do you think happens when the sensor is used against materials that can absorb the mechanical ultrasonic waves? Why not make 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. And, if you enjoy making music with sensors, we also recommend the Theremin lesson. 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.  

Lesson available for the miniBloq Programming Environment

miniBloq.v0.82.ArcBotics.png This lesson is also available for the miniBloq Programming Environment. miniBloq is a graphical programming environment that you can use to easily program your Sparki.