Avoid barriers

Author: Yisong Xu, Si Xu

mBot starts when the onboard button is pressed and moves in a certain speed. When there is a barrier in front, mBot will turn right; when there is not, mBot keeps moving forward.

Hardware requirements:



Upload the program to Arduino

Example program download:

Avoid barriers.sb2


Example: scratch 3.0 programs
kids programming language


Script description
Use the onboard button to start moving forward mBot. The value of the ultrasonic sensor indicates the distance between mBot and any barrier in front of it. Given the threshold of 10cm, it indicates there is no barrier in front when mBot is more than 10cm from the object, and mBot keeps its moving forward; it indicates there is barrier in front when mBot is less than 10cm from the object, and mBot needs to change direction to avoid the object.

In this example kids programming language indicates that if  is true, the “if” blocks will be loaded or the “or” blocks are loaded. Use  to let mBot turn right to avoid barrier and use kids programming language to control the right-turning angle. Or you can use other actions to avoid barriers, for example, let mBot move back for a certain distance and turn right or left.


Extended tasks

Task 1 Modify the script and use kids programming languageblock to achieve barrier avoidance.
Task 2 Change mBot’s action when it encounters a barrier and let mBot move back and turn around when it meets a barrier.


Attached–ultrasonic sensor element diagram

kids programming language

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

—— More examples


# arduino download # coding robots for kids # scratch download # scratch 2.0 # kids programming language # how to make games on scratch # scratch 3.0