image.png
The ultrasonic sensor 2 can be used to detect the distance between an obstacle and it. The transmitter on the left transmits ultrasonic waves, and the receiver receives the ultrasonic waves reflected.
image.png

Compared to the ultrasonic sensor we’ve developed before, the ultrasonic sensor 2 is improved in its casing, chip, and blue LEDs. The blue LEDs can increase the potential for emotion expression and interaction.

Ultrasonic sensor comparison

| |

Ultrasonic sensor 2

|

Ultrasonic sensor

| | —- | —- | —- | | Plastic casing to improve the durability and quality | Yes | No | | Built-in chip to improve the operation stability | Yes | No | | Blue LED
(additional function) | 8 | 0 |

Specifications

  • Output range: 5–300 cm (The output value is 300 when the distance detected is out of the output range.)
  • Output value error: ±5%

Working principle

Human beings can hear sounds of 20 to 20,000 Hz. Sound waves with frequencies higher than 20,000 Hz are called ultrasonic waves. The sound waves are reflected back by the obstacles they encounter and are received by the receiver of the ultrasonic sensor. Based on the time between transmission and receiving, the distance between the ultrasonic sensor and obstacle can be calculated.

Real-life example

  • Bats locate objects with ultrasonic waves.


Ultrasonic Sensor 2 - 图3

Programming guidelines

You can use mBlock 5 to program the ultrasonic sensor 2.
Connect the ultrasoinc sensor 2 to CyberPi, add CyberPi and connect it to mBlock 5, and then add the Ultrasonic Sensor 2 extension. For details about how to add CyberPi and connect it to mBlock 5 and how to add the extension, see “Add and connect mBot2“ and “Add extensions.”

Distance detection and determination

To obtain the distance between the obstacle and ultrasonic sensor 2, use:
image.png
To determine whether an obstacle is found within the output range of 5 cm to 300 cm, use:
image.png

Generally, the distance detection and determination functions are used to implement obstacle avoidance.
For example:
image.png
When no obstacle is within the detection range of the ultrasonic sensor 2, mBot2 moves forward at the speed of 100 RPM; when an obstacle is detected, it moves forward at the speed of 50 RPM; and when the distance between the obstacle and it is shorter than 20 cm, it turns left.

Emotion and information expression

The ultrasonic sensor 2 is equipped with eight blue LEDs. Through programming their brightness, you can use the ultrasonic sensor 2 to express various types of emotions and information.
image.png

Example 1:

image.png
After you press button A, the ultrasonic sensor 2 expresses the emotion of happiness by “smiling” when you shake it. :::info Note: In this example, “smiling” is performed by lighting up the blue LEDs in positions 1, 2, 5, and 6 and turning off those in positions 3, 4, 7, and 8. :::

Alternatively, you can use the emotion block provided on mBlock 5 to express an emotion:
image.png

Example 2:

image.png
After you press button A, place an obstacle close to the ultrasonic sensor 2, and then move the obstacle slowly away from it.
You can see that the blue LEDs become brighter as you move the obstacle. A higher brightness indicates a larger distance.