Tutorial For Combined Line Following and Object Avoidance on mBot

The mBot comes with 3 programs which can be run: the Manual program, the Object Avoidance program and the Line Following program.  Have you ever wondered what the code behind these programs actually looks like though?  In this tutorial, we will develop code to get out mBot to do both line following AND object avoidance.

Overall Structure

So I like to get some pseudocode down before I start writing my programs, and on the simplest level, this program should look something like this:

If there is an obstacle in front of mBot

do nothing


line track.

And this program is going to run forever, so I guess my main structure is like this:


Object Avoidance

Now, I want to put in the condition of the if-statement.

In pseudocode – Is the ultrasonic sensor detecting a nearby (let’s say with in 10cm) obstacle?

In mBlock code –2

I also want to put in the code I want to execute if the condition is met.

In pseudocode  – stop the motors.

In mBlock code –3

If the condition is not met, then I want to line track.  But I will deal with that later.  It is good programming practice to test what you have written often.  So just to check that this part of the code works, I am going to set the else part to – run the motors.  When I have tested this code, and am happy that the object avoidance is working correctly, I will change this.


I tested that and found that my robot was running backwards instead of forwards.  This was because I had plugged the robots into the wrong slots.  I swapped the wires over and ran my code again, and the robot moved forward.

NOTE:  As of mBlock v3.3.5, the ultrasonic sensor returns a value of 400 if it is out of range.  Therefore this extra step is not necessary.


But there was still one more problem.  The robot sometimes stopped when there was no obstacle anywhere near it.  Can you think of why this might be?  The range of the ultrasonic sensor is 3cm to 400cm.  If the ultrasonic sensor detects a distance outside of this range, it returns a value of 0.  A zero was satisfying the condition of my if-statement, and so the robot was stopping.  I fixed this problem by using AND:



NOTE:  As of mBlock v3.3.5, the ultrasonic sensor returns a value of 400 if it is out of range.  Therefore this extra step is not necessary.

Line Tracking

Now time to track the line.  The line follower sensor gives 4 different readings:

0 – Both sensors detect a dark surface

1 – The left sensor detects dark, the right sensor detects light.

2 – The left sensor detects light, the right sensor detects dark.

3 – Both sensors detect a light surface.

Again, I want to think clearly about what I am trying to achieve by writing some pseudcode:

If line sensor = 0

Go forward

Else if line sensor = 1

Turn left

Else if line sensor = 2

Turn right



Putting this pseudocode into mBlock resulted in this:


However, on testing, the robot was very jittery and often stopped at the crossover point on the figure-of-8 line-follower course provided with the mBot robot.  How can I solve these problems?

Firstly, the jittery movement.  I looked more closely at the turn left, and turn right commands.  These commands tell the mBot to perform a point turn.  That is, one motor moves forward at a certain speed, and the opposite motor moves backward at the same speed.  Point turns are great, but for our purposes here, a swing turn would be better – where one wheel moves faster than the other.  To program this, we will need to assign specific values for the motor power levels where:

M1 = left motor

M2 = right motor

To solve the robot stopping at the crossover point, I simply eliminated the code that stopped the motors.

In the end my code looked like this:


Challenge:  Can you assign values for the variables normalSpeed, slowSpeed, and fastSpeed to make the robot run as smoothly as possible?

Putting it together

Did you notice something about the last code picture?  That’s right, the object avoidance part of the code has disappeared.  That’s because when I was testing, I wanted to just focus on the relevant part of the code.  I had already written and tested the object avoidance part, so I removed it for the time being.  Now that the line tracking part is working, it’s time to put it all together.

Challenge:  Put the program together to make the robot track a line and avoid objects.


Posted on