Training Session 4:

Programming robots and actuators

Monday 30 July, 6pm-8pm, Postdoc Centre, 16 Mill Lane, Cambridge (Register here)

The mBot

The robot consists of: Arduino UNO compatible main control board; Light Sensor, Me Ultrasonic Sensor, Me Line Follower Sensor; Bluetooth/USB communication ports
1.5V AA Batteries*4 power supply
170 * 130 *90mm(L*W*H)1034g.

The basic chassis has two electric motors that individually power its wheels, with ultrasonic sonar sensor for distance measurement and infrared sensors for line tracking. However, the  chassis allows mounting of additional sensors and actuators.

More details at:

Instructional youtube videos: click here

 MakeBlock mBot


mCore board
Arduino ports for onboard devices

Light sensor A6
Buzzer: D8
Button: A7
2x Neopixel RGB LEDS D13
Motor 1:  D6, D7
Motor 2: D4, D5
IR emitter: D3
IR receiver: D2

The board also has solder points for ports A0-3, D9-12 and I2C SDA,SCL

1. Assemble the robot

The mBot devices are provided as a kit with full assembly instructions. Please assemble. This should take around 20-30 minutes. A PDF version of the instruction manual can be found at the following link:

Click to download the mBot assembly manual

The robots can be controlled using the provided IR controller, Bluetooth or programmed via XOD, using the USB port. When it is first turned on, the mBot has 3 pre-set control modes: Obstacle avoidance mode, Line-follow mode, and Manual control mode. These can be switched using the IR controller or button on the robot (details in the manual), and used to test the device after assembly.

mBot downloads

Before you can connect your mBot to your PC, Mac or mobile device, you need to install the mBot USB drivers. The mBot manufacturers have provided a range of programming tools that can be used with the robot: Installing the MakeBlock software packages also provides access to the USB drivers - and this may the simplest way to get up and running. This will also provide an alternative programming platform to explore.


Alternatively, drivers for the CH340/341 USB-serial chip can be found online

Troubleshooting guide for mBot

Mac drivers

Cheat Sheet

2. XOD PID controller for the mBot

Max Danilin ( has created some excellent tutorials for the use of XOD with the MakeBlock mBot. In one, he describes the use of the XOD pid-controller node to control self-navigation of the mBot. (A proportional–integral–derivative controller or PID-controller is a control loop feedback mechanism used in automatic control systems to maintain the value of the measured parameter.)

The article describes control of the mBot - including (i) an mbot-motors node to drive robot movement, (ii) an mbot-ultrasonic-sensor node that calculates the distance to obstacles from the mBot ultrasonic sensor. These are connected to the XOD pid-controller node. 

The PID controller calculates an “error” value as the difference between a measured input value and the desired setpoint. The controller attempts to minimize the error by adjusting the output. To control the output, a PID-controller uses coefficients Kp, Ki, and Kd which are based on error changes.

The PID controller will be set with a target value or set point - and provided with a measured value.


 You will need to Kp pin = proportional factor. Coefficient Kp is proportional to the current value of error.

Ki pin = integral factor. This coefficient is used to neutralize the accumulating part of the error. It can eliminate system errors accumulate after a certain amount of algorithm loops.

Kd pin = derivative factor. It estimates the trend of future errors using Kd rate of change. The more rapidly the rate of error changes, the higher the controlling or dampening effect of PID-controller will be.

There are no specific values of Kp, Ki, Kd coefficients. Different systems with a PID-controller will have individually tuned ratios. 

Exercise 1:

Set up the mBot to approach an obstacle and remain at a fixed distance. Experiment with PID parameters. Add basic navigation routines and collision avoidance.


XOD library for tutorial session

A .xodball library of XOD code for the tutorials can be downloaded here.

(You will need to pay attention to the ports used for your circuit - and to match these with the XOD programme).

 Kp = 5

Kp = 5

 Kp = 90

Kp = 90

 Kp = 40

Kp = 40

 Kd = 0

Kd = 0

 Kd = 10

Kd = 10

3. mBot navigation and line following

The mBot comes with dual infrared emitter-sensors that are mounted in the front of the robot - to allow line-following. The use of these are described in Max Danilin's second tutorial article.

The sensors are connected to one of the mBots RJ25 ports. The bot-line-sensor node communicates with the hardware through the S1 and S2 input pins. It has two output pins L and R according to the left and right infrared sensor on the board. True value on an output pin means that there is a white or bright color in front of the infrared sensor. In turn, false value means that the color is black or dark. 

  • Situation 1. Both IR LEDs are on the line. mbot-line-sensor node puts false to the L and R pins. mBot should move forward.
  • Situation 2. The left IR LED is on the line while the right deviates to the right. mbot-line-sensor node puts false to the L pin, and true to the R. mBot should turn to the left.
  • Situation 3. The right IR LED is on the line while the left deviates to the left. mbot-line-sensor node puts true to the L pin, and false to the R. mBot should turn to the right.
  • Situation 4. Both IR LEDs are out of the line. mbot-line-sensor node outputs two true values. mBot should turn around to find the missing line.

A PID-controller requires a single variable input while the mbot-line-sensor outputs two. To solve this problem, subtract node is added. This node subtracts two boolean variables and produces a number. In this way, the subtract node can output the values:

  • 0 for the first situation;
  • -1 for the second situation;
  • 1 for the third situation;
  • 0 for the fourth situation.

We use those four situations to form the “error” concept for the PID-controller. The situation when the incoming value is 0 and both IR LEDs are in line is ideal for us. That’s why we should set up the the TARG pin to 0.

But this patch in its form can work correctly only with the second and third situations. When mBot completely loses the line the incoming value for the pid-controller node will be 0 too. For this case special logic needs to be introduced, which is explained in detail at:

 Ki = 2  Images from: Max Danilin,

Ki = 2

Images from: Max Danilin,

Self-balancing robot

Additional article on building a modified, self- balancing mBot with accelerometer and XOD software also from Max Danilin. Extensive testing and description of PID controller parameters can be found at:


The mBot is equipped with a range of peripheral devices - can include these in any of your programmes - for example to provide collision warnings or feedback?

A0 RJ25 plug 4 (default not-connected)
A1 RJ25 plug 4 (default not-connected)
A2 RJ25 plug 3 ultrasonic
A3 RJ25 plug 3 ultrasonic
A6 light sensor
A7 button
D4 DIR2 - direction motor2
D5 PWM2 - pwm motor2
D6 PWM1 - pwn motor1
D7 DIR1 - direction motor1
D8 buzzer
D9 RJ25 plug 2 linefollower
D10 RJ25 plug 2 linefollower
D11 RJ25 plug 1 (default not-connected)
D12 RJ25 plug 1 (default not-connected)
D13 2 WS2812 Neopixel RGB LEDs

The board also has solder points for ports A0-3, D9-12 and I2C SDA,SCL, so additional components could be added to the robots.Can you engineer robot-robot sensing and communication?

More details about mBot ports at

Additional resources:

XOD libraries for the mBOT: (gabbapeople/mbot-lib loaded in XOD)
We've collected the relevant code from the gabbapeople/mbot-lib library and placed it in the tutorial session code.

MakeBlock downloads for mBot series:
(Instructions, videos, schematics, etc.)

MakeBlock programming information for the mBot:

Thingiverse 3D printed accessories for mBot:
(Including brackets, pen holders, bumpers, etc.)

Collection of mBlock pseudo-code for controlling the mBot:
(includes control of mCore onboard LEDs, sensors, etc.)

MakeBlock Github site:

PID Controller Basics Using XOD and Arduino:

 Circuit diagram for the mCore control board of the mBot

Circuit diagram for the mCore control board of the mBot

Programme logic for obstacle avoidance

Programme logic for line follower

 mBot ultrasonic rangefinder circuit

mBot ultrasonic rangefinder circuit

 mBot line follower circuit

mBot line follower circuit