Stop automatically in front of a barrier

Author: Yisong Xu, Si Xu

mBot moves in a certain speed and stops automatically before a barrier.

Hardware requirements:



Upload the program to Arduino

Example program download:

Stop automatically.sb2


Example programs
kids programming language


Script description
mBot moves in 150 speed and stops when the ultrasonic sensor senses an object less than 10cm away in front of it.


Knowledge points
Point 1 Use the ultrasonic sensors to determine whether mBot will encounter an barrier in front
It can known from Example 18 Value of the Ultrasonic Sensor that an ultrasonic sensor can detect the distance from the object in front of it. A critical value of the distance between the object in front and mBot’s ultrasonic sensor can be defined as the threshold to judge whether mBot should move forward (a threshold is a value of the condition under which an object is changed, which is also called critical value).

Point 2 The use of a block kids programming language
kids programming language indicates if an event  is triggered, the block below is executed; if the event  is not triggered, it is not executed. In this example the value of the ultrasonic sensor indicates the distance between mBot and the object in front of it. Given the threshold of 10cm, mBot will keep moving forward before its distance from the object is less than 10cm; mBot will stop immediately when its distance from the object is less than 10cm.

Extended tasks

Task 1 Modify the script to let mBot keep moving forward when the barrier in front is removed.
Task 2 If you do not use kids programming languageblock but choose kids programming languageblock, can you achieve the effects of this example?


Attached–ultrasonic sensor element diagram

kids programming language

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

—— More examples