Story
An ultrasonic sensor (or sonar sensor) operates just like bats do in order to get a grasp of their surroundings or how whales navigate: they emit an ultrasonic signal that is inaudible to humans. By the time the echo reaches an object and gets sent back to the ear, the bat knows how far away an object is relative to their body.
Required hardware components
1 x Calliope mini
1 x Seeed Grove Ultrasonic Ranger popup: yes
What can the sensor be used for?
You can measure distances fairly accurately with the sensor, then you can see if something gets closer to the sensor than a certain amount of centimeters (called the threshold) that you determine. For example, if you build a robot and don't want it to crash into a wall, you can set it to stop when something gets closer than a predetermined distance (again the threshold). You can also use it as an alarm. As soon as someone walks into the set range, the measured distance changes and the alarm can then be used. As soon as someone walks into the set range, the measured distance changes and the alarm can then go to the set range.
How does the sensor work?
You plug the ultrasound sensor into the right Grove connector, then you have to switch the power on (1) and off (0) once very briefly on pin 16 (yellow cable, SIG). This is called a digital signal (1 or high = on, 0 or low = off). This is the signal for the sensor to emit an ultrasound sound and wait for the echo that comes when the sound hits an object and comes back again. We do this when button A is pressed.
Now you have to wait on pin 16 for a digital input signal. This is done with the block "on pin x pulsed y". As soon as the ultrasound receives the echo, the ultrasound sensor switches on the current on the cable that arrives at pin 16 very briefly. The further away the object is, the longer the current remains turned on. The duration is a few microseconds (µs) to a few milliseconds (ms). This is much too fast for us humans. The Calliope mini can easily measure this however, and the duration of the signal indicates the distance.
The duration of the pulse shows us the distance, but not yet in cm. To get the value in cm, you have to divide the pulse duration by 58.
In the example we saved the value in a placeholder named "distance" and then display the distance on the screen.
The block with the gravitational pull below is necessary because otherwise the Calliope mini gets confused. In order to avoid confusion the Calliope mini first has to write and then read all on the same pin.
Schematics
This text as well as the image is published under a CC BY-SA 3.0 DE license popup: yes. It was originally published in German popup: yes
by Boris Crismancich and translated into English by the Calliope team.