Attention: Safety Recall of Vernier Go Direct Charge Station. Click to learn more.



Obstacle Avoidance

Experiment #C2 from Coding with mBot: Self-Driving Vehicles


In this activity, students recreate the functionality of Mode B–Obstacle Avoidance of the Default Program. Students learn how to read data from the Ultrasonic Sensor in order to measure a distance, control the frequency of a sound from the mBot Buzzer, and trigger the program to autonomously avoid obstacles. Students frequently use the Ultrasonic Sensor in other activities, so we highly recommend doing this activity before attempting later activities.


  • Learn about Mode B of the Default Program.
  • Practice writing code, uploading and running a program on mBot, and troubleshooting when problems arise.
  • Learn how the Ultrasonic Sensor works.
  • Practice using variables, “if-then-else” blocks, and loops in mBlock.
  • Use the Ultrasonic Sensor to control sound frequency.
  • Write code to develop obstacle-avoidance programs for mBot.

Sensors and Equipment

This experiment features the following sensors and equipment. Additional equipment may be required.

Ready to Experiment?

Ask an Expert

Get answers to your questions about how to teach this experiment with our support team.

Purchase the Lab Book

This experiment is #C2 of Coding with mBot: Self-Driving Vehicles. The experiment in the book includes student instructions as well as instructor information for set up, helpful hints, and sample graphs and data.

Learn More