Deutsch   English 

5. Remote Control of Robots

Two micro:bits can communicate with each other via Bluetooth. In a typical application a mbRobot can be remote controlled with a second micro:bit. The necessary functions are imported from the module radio (only realmode).

In this simple but tricky communication model each micro:bit (also called node) is a transmitter and a receiver at the same time. First both nodes create a wireless Bluetooth connection by calling radio.on(). With radio.send(msg), the transmitter sends a message (as a string) to the other node's receiver memory. The receiver retrieves the oldest message from the memory with the radio.receive() command.

In the following examples, the micro:bit, which is not built into the mbRobot, acts as transmitter and the mbRobot as receiver.

Example 1: The red LEDs of the robot are switched on and off remotely.
The two devices create the Bluetooth connection by calling radio.on(). Clicking the left button executes the radio.send("on") command and sends the message "on", clicking the right button sends the message "off". The robot receives the transmitted string with radio.receive() and switches both LEDs on or off.

Transmitter (micro:bit):   Receiver (mbRobot)
from microbit import *
import radio

while True:
    if button_a.was_pressed():
    elif button_b.was_pressed():
► Copy to clipboard
# (receiver)
import radio
from microbit import *
from mbrobot import *

while not button_a.was_pressed():    
    rec = radio.receive()
    if rec != None:
        if rec == "on":
        elif rec == "off":
► Copy to clipboard

Explanations of the program code:

import radio: Imports the module radio that contains the commands for the Bluetooth communication
radio.on(): Activates the communication channel
radio.send("on") Sends the string "on"
rec = radio.receive() Fetches the oldest message from the receiver memory (returns None if the memory is empty)

Example 2: Remote controlling the robot
With a second micro:bit as remote control the mbRobot moves as follows:
No button pressed →stopped (state = "STOP")
Left button pressed →move left (state = "LEFT")
Right button pressed →move right (state = "RIGHT")
both buttons pressed →move straight ahead (state = "FORWARD")

It is advantageous to introduce a state variable for such tasks, where the current state is stored. In particular, a message is only sent if the state has changed.

Transmitter (micro:bit):   Receiver (Roboter)
from microbit import *
import radio

state = "STOP"
oldState = ""
while True:
    if button_a.is_pressed() and button_b.is_pressed():
        state = "FORWARD"'F')
    elif button_a.is_pressed():
        state = "LEFT"'L')
    elif button_b.is_pressed():
        state = "RIGHT"'R')
        state = "STOP"'S')
    if oldState != state:
        oldState = state   
► Copy to clipboard
# Mr5d.html (receiver)
import radio
from mbrobot import *

while True:
    rec = radio.receive()    
    if rec == "FORWARD":
    elif rec == "LEFT":
    elif rec == "RIGHT":
    elif rec == "STOP":
► Copy to clipboard


Explanations of the program code:"F") The letter "F" is shown on the LED display
radio.send(state) Depending on which buttons were pressed, FORWARD, LEFT, RIGHT or STOP are sent
if oldState != state: A message is only sent if the state has changed




Create a course with some objects and use the program above to move the mbRobot as fast as possible from start to end.



Mark 4 locations according to the picture and write a program so that the robot moves remotely to each location and then back to the starting location. To do so, it must also be able to move backwards.