ultrasonic
Deutsch   English   

4. ULTRASONIC SENSOR (DISTANCE SENSOR)

 

 

YOU LEARN HERE...

 

how you can measure and analyse distances with an ultrasonic sensor.


 

HOW DOES AN ULTRASONIC SENSOR WORK?

 

An ultrasonic sensor has a transmitter and a receiver for the ultrasonic waves. When measuring distance, it transmits a short ultrasonic pulse and measures the time it takes for the pulse to travel to the object and back again. From this, it can determine the distance using the known speed of sound.

 

The Maqueen sensor can measure distances in the range from approx. 5 to 200 cm and returns the values in cm. If there is no object in the measuring range, it returns the value 255 or -1.



 

EXAMPLES

 


Example 1: Recognising an object

The robot moves forwards and measures the distance to the object in front of it every 200 milliseconds. If the distance is less than 20 cm, it stops.

 

Program:

from mbrobot import *
#from mbrobot_plus import *

forward()
repeat:
    d = getDistance()
    print(d)    
    if d < 20:
        stop()    
    delay(200)
► Copy to clipboard

The getDistance() function returns the distance to the object. The sensor value is queried in an infinite loop and stored in the variable d. delay(200) specifies the measurement period. Use the if-structur to check the sensor values. The indented instruction stop() is only executed if the condition after if is fulfilled. If the robot is connected to the computer via USB cable, the sensor values are displayed in the terminal window with print(d).


Example 2: A control system for the distance
to the object


Your programme should ensure that the robot remains at a certain distance from your hand. If it is too close, it should move backwards, otherwise forwards.

You use the if-else structure to evaluate the sensor values. If the condition after if is true, the statement after if is executed, otherwise the statement after else is executed.

 


Program:

from mbrobot import *
#from mbrobot_plus import *

setSpeed(30)
forward()
repeat:
    d = getDistance()
    if d < 20:
        backward()
    else: 
        forward()    
    delay(100)
► Copy to clipboard


Example 3: Searching for an object
A robot with an ultrasonic sensor is to find an object and then move to it and knock it over. At the start, the robot is facing in any direction, i.e. it must first turn until it detects the object.

 

Program:

from mbrobot import *
#from mbrobot_plus import *

def searchTarget():
   repeat:  
      right()
      delay(50)    
      dist = getDistance()
      if dist != 255:
         right()
         delay(500)
         break
 
setSpeed(20)
searchTarget()
forward()
► Copy to clipboard

You define the search process in the searchTarget() function. The robot turns a small angle to the right and measures the distance. If it detects an object (the sensor no longer returns the value 255 or value -1), it rotates a little further so that it is directed towards the centre of the object. You can cancel the repeat loop with breakto end the search process. The searchTarge() function is called in the main programme and the robot moves to the object after the successful search process. Solve task 3 and complete the programme so that the robot stops in front of the object.

A modern industrial plant without sensors is hardly conceivable today. We are also surrounded by sensors in our everyday lives. Modern cars have 50 - 100 different sensors: Distance sensors, RPM and speed sensors, petrol tank fill level sensor, temperature sensor, etc.

When parking, distance sensors are used that work in a similar way to those of our little mbRobot.

 

 

 

 

REMEMBER YOU...

 

The getDistance() instruction returns the value of the ultrasonic sensor. The sensor values are measured periodically in a repeat loop, delay(100) determines the measurement period. This instruction is important because otherwise the values are queried too frequently, which can lead to the microprocessor being overloaded.

 

 

TO SOLVE BY YOURSELF

 

 

1.

The control system in example 2 is not yet optimal. If you remove your hand completely, the robot often reacts in a confused manner, as it either detects distant objects or nothing at all. Optimise the program so that the robot stops if the distance is greater than 50 cm.

 
if d < 20:
   ... 
elif d >= 20 and d < 50:
   ... 
else:
   ...    
 

To do this, you use the if-elif-else structure, with which you can programme a multiple selection.

2.

A robot with an ultrasonic sensor is to find a higher object (column of cardboard, candle, can...) by turning slowly in place and measuring the distance. If it detects an object at a distance of less than 40 cm, it triggers an alarm for 4 seconds.


 
3.

A robot should find an object in the same way as in example 3 by turning in place and analysing the values from its ultrasonic sensor. When it detects an object, it moves towards it and stops at a distance of 10 cm from the object.

4.
A robot with an ultrasonic sensor tries to find the exit from a confined space. It first turns slowly in place and if it does not detect a wall, it moves off.