How Can We Help?

Ultrasonic Sensor 2


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.

Compared to the ultrasonic sensor we’ve developed before, 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


  • 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.

Programming guidelines

You can use mBlock 5 to program Ultrasonic Sensor 2.

Connect 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 “3.2 Add and connect mBot2” in the mBot2 Operation Guide.

Distance detection and determination

To obtain the distance between the obstacle and 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 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

Ultrasonic Sensor 2 is equipped with eight blue LEDs. Through programming their brightness, you can use Ultrasonic Sensor 2 to express various types of emotions and information.

Example 1:

After you press button A, 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 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.