5. Case 04: Obstacle-avoidance Driving

5.1. Purpose

  • Programme to set the TPBot avoiding an obstacle automatically.

5.2. Material


5.3. Software


5.4. Programming

  • Click “Advanced” to see more choices in the MakeCode drawer.


  • We need to add a package for programming. Click “Extensions” in the bottom of the drawer and seach with “tpbot” in the dialogue box to download it.



  • Set a triangle in the micro:bit and the TPBot moving forward at the speed of 50%; Set the LED headlights in green.

  • Save the variable of the distance from the TPBot to the obstacle in the forever brick, judge if the items of i<15 and i≠0 are both true, if yes, set TPBot stopping immediately and the headlights flashing three times; set it reversing for 1 second at the speed of 30% and then turning left for 0.5 second at the speed of 30%; finally set the headlights showing green and the TPBot moving forward at the speed of 50%.


5.5. Conclusion

  • Power up the TPBot to move forward and it stops immediately with the headlights flashing three times if an obstacle was detected, then it reverses and turns left to keep moving forward with the headlights in green.

5.6. Exploration

5.7. FAQ

Q: The car does not work with the code in the wiki.
A: It should be the batteries that are lack of power, please try to fix it by adding the value of the speed in the code.

5.8. Relevant File