Obstacle Avoidance with Micro:bit Robit Smart Car


The Robit Smart Car is a great starting point if you're new to electronics and programming. It can be easily programmed by using the accessible micro:bit and MakeCode editor.

In this guide, you will learn to use the  Robit Smart Car with a Sonar Module. The smart car will be programmed to move and avoid obstacles.

After completing this guide, you will understand the basics on how to use the Robit Smart Car!

Step 1  Set up the smart car

Before we will start to program the Robit Smart Car, it will need to be set up! 

For this guide, we will need:

  • The Sonar module to be connected to the robit motherboard. Make sure it is connected to Pin 14 on the motherboard. 
  • The two motors are to be connected to M1 and M2.

If you haven't done so already, head over to the Robit Smart Car product page for instructions on how to assemble the smart car.

Step 2  Using MakeCode

  • To program the smart car, we will use MakeCode editor.
  • Click on 'Advanced'
  • Click on 'Extensions'
  • Type 'robit' in the search bar 
  • Click on the robit package and it will be automatically added to the MakeCode editor

Step 3  Moving back and forth!

Let's get started with programming the Robit Smart Car! First, we'll get it to move back and forth forever.

  • Click on 'Robit' tab, and get out a 'Left wheel ... speed ... Right Wheel ... speed' block. 
  • Change it so that the Left wheel is assigned to M1 and Right wheel to M2.
  • Next, get a 'pause (ms) ... ' block and change its value to 1000
  • Get out another 'Left wheel ... speed ... Right Wheel ... speed' block. 
  • This time change its values to M1, -50, M2, -50
  • Jump to step 6 to upload this .hex file to your Micro:bit. The Robit car will now move forward for 1 second and backwards for another second.

Download the complete MakeCode by clicking on the purple download button below.

Step 4  Obstacle avoidance!

For this next example, we'll get the Robit Smart Car to avoid obstacles.

  • Create a variable called 'dist' used to represent the distance from the obstacle. Set it to be 0 in an 'on start' block.
  • Now get out a 'Left wheel ... speed ... Right Wheel ... speed' block.
  • Set the Left wheel to be M1, and Right wheel to be M2. Then set the speed for both to 50.
  • Over in the 'forever' block, place a 'set dist to ...' block
  • Get a 'Ultrasonic pin ... ' block from the Robit tab. Then change its values so it is 'Ultrasonic pin J1 (P13, P14)
  • Doing so, it reads the ultrasonic data from J1 port and then assigns it to the variable, 'dist'.

To prevent collisions with obstacles, we'll next create a condition. When the measured distance is less than 5cm, get the Robit Smart Car to stop. After stopping, we want the Robit Smart Car to then reverse. In the previous example, remember how we got it to move backwards? We do the same thing here, by setting a negative number to M1 and M2. Then set it with a delay time of 500 ms or half a second.

  • So grab an 'if true then ... else ... ' block
  • Grab a 'dist' block over in 'Variables' tab.
  • In the 'Logic' tab, get a ' ... and ... ' block under 'Boolean'. Also grab a ' ... < ... ' block. 
  • Place the 'dist' variable block in it and set it so that 'if dist < 5 and ... '
  • Grab a '... = ... ' block. Then Complete the condition with 'if dist < 5 and dist not equal to 0'
  • Get a 'Motor Stop All' block from 'Robit' tab. Followed by a 'pause (ms) 500' block. 
  • Next, a 'Left wheel M1 speed -50 Right wheel M2 speed -50' block

Step 5  Steering!

The final part of this MakeCode involves getting the Smart Car to avoid obstacles while steering. We'll create a condition. When the distance from obstacle is less than 15cm, generate a random number. If the random number is less than 45, set the motor speed of M1 to be negative. Doing so, the left wheel will run oppositely, completing a left turn. When the random number is beyond 45, the motor speed of M2 is set to be negative. Thus, the right wheel runs oppositely, completing a left turn. 

  • Click on the white plus icon to add an 'else if'
  • Add another 'if ... then ... else' statement and place it in 'else if'
  • Get a 'pick random ... to ...' block. Set its values to pick a random number from 0 to 99.
  • Set the condition to be 'if pick random 0 to 99 < 45 then'
  • Click on 'Robit' tab and ' ... more' Grab a '... Wheel speed ...' block
  • Place a 'M1 wheel speed -50' block in the if condition
  • Click on the white plus icon to add an 'else' condition 
  • In 'the else' block inside 'else if', place a 'M2 wheel speed -50' block. Then add a 'pause (ms) 500)' block
  • In the 'else' statement, add a 'Left wheel M1 speed 50 Right wheel M2 speed 50' block

Download the complete MakeCode by clicking on the purple download button below.

Step 6  Upload to micro:bit

To upload the code to the micro:bit, follow these steps:

  1. Connect the micro:bit to the computer via microUSB cable
  2. Click on the 'Download' button and the hex file will be automatically downloaded to your 'Downloads' folder
  3. Drag and drop the downloaded .hex file to the MICROBIT drive
  4. Leave the micro:bit alone for a few seconds as it blinks. The code is uploading!

Step 7  Robit Smart Car - Obstacle Avoidance in Action!