Hardware requirements:



Upload the program to Arduino

Example programs

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  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  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 block 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

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