Ultrasonic Sensor 2

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 on the right receives the ultrasonic waves reflected.
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



Built-in chip to improve the operation stability



Blue LED
(additional function)





  • 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 distances between the ultrasonic sensor and the obstacles can be calculated.

Real-life example

  • Bats locate objects with ultrasonic waves.

Programming guide

You can use mBlock 5 to program the ultrasonic sensor 2.

Connect the ultrasonic 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 the ultrasonic sensor 2, use:


To determine whether an obstacle is found within the output range of 5 cm to 300 cm, use:


Generally, the distance detection and determination functions are used to implement obstacle avoidance.

For example:


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.


Example 1:


After you press button A, the ultrasonic sensor 2 expresses the emotion of happiness by "smiling" when you shake it.

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:


Example 2:

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.

Was this article helpful?
0 out of 0 found this helpful



Please sign in to leave a comment.