2. Sensors


In this exercise we will get to know the three sensors in your mBot. We will learn how you can use these sensors.

You will learn:

  • The working of your mBot distance sensor.
  • How to use the distance sensor in a programme.
  • How to make your mBot avoid obstacles.
  • The working of your mBot line sensor.
  • How to programme the mBot to follow a track automatically.
  • How to use your mBot light sensor in a programme.

Open the mBlock programme on your computer, switch on your mBot and connect it. Perform the steps one by one.


Check the steps in "execute". Answer the following questions:


  • Which steps should I follow?
  • Which steps can I take?
  • How much time do I need?
  • What tools do I need?


Perform the steps one by one. Have you forgotten how to do it? Look at the hints or download the solution.


Measuring distance

ultrasonische sensor

The challenge is to make our mBot stop and turn just before touching the obstacle.  To do this, we need to use the ultrasonic sensor, which looks a bit like our mBot's eyes.

One sensor sends out an inaudible sound (to us), while the other receives it back. The time difference between sending and receiving depends on the distance the sound had to travel. This is how your mBot can estimate distances.

For this activity, you will have to programme your mBot in a way so that it can drive independently through the room. Of course, it will have to stop at an obstacle, make a turn and then drive on.

You will definitely need a new block which measures the distance in the room with the ultrasonic sensor.


Be sure to check which connection your sensor cable (the eyes of the robot) is plugged into. This is called a ‘port’. Normally, the eyes are in port 3.

Which blocks will you definitely need?

  • Use a command “if...then...else...”. When does the mBot need to take a bend?
  • The mBot cannot collide with anything, so if distance < 15 cm, then ...
  • It always has to look out for obstacles.

Now we have a working programme!


a) Maybe you do not like the mBot always turning to the same side when facing an obstacle. If you want it to alternate between left and right, you can let the mBot choose a random number. Depending on the value of this number, you choose between a left or a right bend.

b)    Can you make your mBot produce a sound every time it notices an obstacle?

c)    Can you make your mBot reverse if it notices an obstacle and only then make it turn?


Measuring light

Wouldn't it make sense to light the LEDs when it gets dark? Just like in some cars or street lights...
We can use the light sensor on the mBot for this.

lichtsensor op bord

It produces a value between 0 (very dark) and 1023 (plenty of light).


Can you see to it that the LED lights on the mBot turn on when it is too dark?

You will have to use a variable. If you have already done all the exercises in Scratch, you already know what a variable is. A variable is an ever-changing value. We need to give this value a suitable name so that we know what it refers to.

Do you know how the create a variable?

Link the variable with the value of the light sensor.

Now we can use the "If...then..." button to make the lights turn on when the light drops below a certain value. Otherwise, the lights should go off.


Can you make the lights shine brighter when it gets darker?


Following a line

At the bottom of your mBot, just at the little ball that serves as the front wheel, there is a line sensor.

lijnvolg sensorwerking lijnvolg sensor

By looking at the drawing, you immediately understand how the sensor works. In situation 0 your mBot is fully on the line, in situations 1 and 2 it is to the right or left of the line. The mBot lost the line in situation 3.

Write a programme to make your mBot follow a line. Did the mBot lose the line? Then make it stop automatically.

If “line” is equal to 0, then your robot should move forward. If “line” is equal to 1, then your robot should move to the left. If “line” is equal to 2, then your robot should move to the right.. Use the “if...then...else” blocks.


Try out the limits of your mBot by playing with coloured lines, thick lines, splits,...? Here you can find some ideas: pdf.


Look back at the project you just made. Ask yourself the following questions:

  • Does the programme work like I expected?
  • What is working well?
  • What is not working well?
  • How can I do things differently?