The source code of all examples can be downloaded from a link in the right side bar.
Moving robots are considered to be the prototype of robots, probably because it is more humanoid than other machines. Moreover self-driving robots are of great actuality because self-driving cars (and other vehicles like drones) will revolutionize mankind in the near future. The most elementary version of a self-driving car is a platform equipped with two electric motors and a caster wheel. Be activating the speed of the motors independently the vehicle can move forward, backward and turn.
Example 1: Turn motor on/off with digital output
Caution: Never connect a motor directly to the micro:bit pins.
You can not power a DC motor directly from a micro:bit output pin because of the current limitation (5 mA) and more important because a motor acts like a solenoid, so turning a motor off generates voltage spikes caused by induction that may damage the micro:bit.
Connect a 3V DC motor to a transistor driver (see chapter Digital In/Out for more information) and turn the rotation on and off using write_digital().
Normally motors are not powered from the 3.3V supply of the microbit, but from an external 5 V supply (or higher voltages if required).
All the following examples require the Kitronik buggy chassis
and the corresponding motor driver and line follower boards.
Based on the principle to drive a motor as shown in the previous examples a small library module mbutils.py is written that is downloaded to the micro:bit's file system from the MBM distribution when the micro:bit is flashed. The current version can be downloaded from here. Reference code (may not be up-to-date):
# Line2.java# Define your own functionfrommicrobitimport*
ldrL = pin2
ldrR = pin1
return (ldr.read_analog() >100)
ldr = ldrL
value = isDark(ldr)
Use a function from the mbutil module.
Finally put all together and enjoy the simplicity of the code.
# Line3.java# Use library functionfrommbutilsimport*
ldr = ldrL
value = isDark(ldr)
# line4.py# Line Followerfrommbutilsimport*whilenot button_b.was_pressed():
if isDark(ldrL) and isDark(ldrR):
elif isDark(ldrL) andnot isDark(ldrR):
elifnot isDark(ldrL) and isDark(ldrR):
Example 5: Remote control
Use Bluetooth communication between two micro:bits to create a remote control system. Depending on the forward/backward/left/right tilt of the controller, the buggy performs the corresponding movements.
The command strings FORWARD, BACKWARD, LEFT, RIGHT, STOP are sent from the remote controller to the buggy, where they are interpreted and translated into the corresponding action.
The first letter of the current state is shown on the buggy's dot matrix display. A state variable is used to ensure that only state changes are reported.
oldState =""whilenot button_b.was_pressed():
acc_x = accelerometer.get_x()
acc_y = accelerometer.get_y()
if acc_y <-800and state !="FORWARD":
state ="FORWARD"elif acc_y >800and state !="BACKWARD":
state ="BACKWARD"elif acc_x <-800and state !="LEFT":
state ="LEFT"elif acc_x >800and state !="RIGHT":
state ="RIGHT"elif acc_x >-100and acc_x <100and \
acc_y >-100and acc_y <100and \
state ="STOP"if oldState != state:
oldState = state