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.
Aim:
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().
Remarks:
Normally motors are not powered from the 3.3V supply of the micro:bit, but from an external 5 V supply (or higher voltages if required).
Example 2: Regulate motor speed with PWM
Aim:
Connect a 3V DC motor to a transistor amplifier (see chapter Digital In/Out for more information) and increase speed from 50% to 100% and back again endlessly.
Remarks:
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):
# Gear4.java# Turn on left and right arcfrommbutilsimport*
buggy_leftArc(0.3)
sleep(2000)
buggy_stop()
sleep(2000)
buggy_rightArc(0.3)
sleep(2000)
buggy_stop()
Example 4: Line follower
Aim:
Print values returned from line follower sensors.
# Line2.java# Define your own functionfrommicrobitimport*
ldrL = pin2
ldrR = pin1
defisDark(ldr):
return (ldr.read_analog() >100)
ldr = ldrL
whilenot button_b.was_pressed():
value = isDark(ldr)
print(value)
sleep(500)
Aim:
Use a function from the mbutil module.
Aim:
Finally put all together and enjoy the simplicity of the code.
# Line3.java# Use library functionfrommbutilsimport*
ldr = ldrL
whileTrue:
value = isDark(ldr)
print(value)
sleep(500)
# line4.py# Line Followerfrommbutilsimport*whilenot button_b.was_pressed():
if isDark(ldrL) and isDark(ldrR):
buggy_forward()
display.show("F")
elif isDark(ldrL) andnot isDark(ldrR):
buggy_left()
display.show("L")
elifnot isDark(ldrL) and isDark(ldrR):
buggy_right()
display.show("R")
else:
buggy_right()
display.show("R")
sleep(10)
buggy_stop()
display.show("S")
Example 5: Remote control
Aim:
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.
# RemoteControl.pyimportradiofrommicrobitimport*
radio.on()
state ="STOP"
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":
state ="STOP"if oldState != state:
radio.send(state)
display.show(state[0])
oldState = state
sleep(10)
radio.send("STOP")
display.show(Image.NO)