Modules (F) are automatically copied while flashing the micro:bit
Download program code of all add-on modules
Module mbglow (F)
(Snake like display element, real and simulation mode)
(Module import: from mbglow import *)
makeGlow() |
creates a visible glowbug at position (0, 0) directed to the north with its trace enabled. Coordinate system: -2 <=x <=2 (+ to the right), -2 <= y <= 2 (+ upwards), (0, 0) at center pixel |
setSpeed() | sets the speed for movements (0..100) |
show() | enables the visibility for the following movements |
hide() | disables the visibility |
clear() | clears all visible pixels. The glowbug remains at the current position (but is invisible) |
showTrace(enable) | enables/disables the trace for the following movements (pixels are turned on at the glowbug position) |
forward() | moves the glowbug one step in the forward direction |
back() | moves the glowbug one step in the backward direction |
left(angle) | turns the glowbug in 45 degrees increments to the left (angle = 45, 90, 135, 180, 215, 270, 315) |
right(angle) | turns the glowbug in 45 degrees increments to the right (angle = 45, 90, 135, 180, 215, 270, 315) |
setPos(x, y) | sets the glowbug at position (x, y) |
getPos() | returns the current position of the glowbug (as tuple) |
isLit() | returns True, if the pixel at the current glowbug position is turned on |
Module mbrobot (F)
Modul mbrobot_plus
Modul mbrobot_plusV2
forward() | sets robot in forward motion |
backward() | sets robot in reverse motion |
left() | setsrobot in a left turn (one motor in forward, the other in backward rotation) |
right() | sets robot in a right turn (one motor in forward, the other in backward rotation) |
leftArc(radius) | sets robot to a left curve with given radius (in m, approx.) |
rightArc(radius) | sets robot to a right curve with given radius (in m, approx.) |
stop() | stops the movement |
setSpeed() | sets the speed for all movements (15<speed<100), default 50 |
calibrate(offset, differential, arcScaling) | calibrates motors, if the robot does not drive straight offset: minimal power of the motors (-10 to 50) differential: Adjusts power difference of left and right Wheel (-150 to 150), if a robot steers left when driving forward < 0, if steers right > 0 arcScalling: Adjusts the radius by leftArc() or rightArc() (values -50 to 50), if radius to laarge > 0, if radius to small < 0 |
motL.rotate() | rotatate motor left (speed > 0: forwad, speed < 0: backward, speed= 0: stop) (speed 0 - 100) |
motR.rotate() | rotatate motor right (speed > 0: forwad, speed < 0: backward, speed= 0: stop) (speed 0 - 100) |
setPID(1) [only mbrobot_plus] | reduces the speed, the robot moves slowly (mbrobot_plus only) |
setPID(0) [only mbrobot_plus] | switches off PID (mbrobot_plus only) |
delay(ms) | stops program for ms milliseconds |
setLED(1) | turns both LEDs on mbrobot_plus: additional 2:green, 3:blue, 4:yellow, 5: pink, 6:cyan, 7:white |
setLED(0) | turns both LEDs off |
setLEDLeft(n), setLEDRight(n) |
turns LED on (n = 1) or off (n = 0) mbrobot_plus: additional 2:green, 3:blue, 4:yellow, 5: pink, 6:cyan, 7:white |
setAlarm(1) | sets the alarm on |
setAlarm(0) | sets the alarm off |
setServo(S, angle) | rotates the servo motor at port S1 or S2 (P1 or P2) to the given angular position.(angle: 0 to 180) |
irLeft.read_digital(), irRight.readDigital() | infrared sensors return 0, if floor is dark; 1, if floor is bright |
getDistance() | returns the distance (in cm, in range 2..200 approx.) measured with the ultrasonic sensor (255: illegal measurement) |
mesh = [(x1, y1), (x2, y2)...] [nur Simulation] | defined the coordinats of the corners (x1,x2),(x2,y2),..of an object, start from the centre (0,0) of the window |
RobotContext.useTarget ("pics",mesh,x,y) | defined the background "pics" an the position (x , y) of the object mesh |
setBeamAreaColor() [simulation only] | sets the color of the beam area boundaries |
setProximityCircleColor() [simulation only] | sets the color of the proximity circle |
setMeshTriangleColor() [simulation only] | sets the color of the mesh triangles |
eraseBeamArea() [simulation only] | erases the beam area boundaries |
RobotContext.enableTrace(True) [only S] | shows the trace in simulation mode |
RobotContext.enableRotCentre(True) [only] | shows the center of rotation in simulation mode |
reset() [only S] | sets the robot to the starting position/direction |
Module mbrobotmot (F)
(Mini:Maqueen Rover, alternative for mbrobot with motor rotations instead of vehicle movement, real and simulation mode)
(Module import: from mbrobotmot import *)
Remark: In simulation mode, mbrobot and mbrobotmot are identical
motL, motR | instances of left and right motor |
motX.rotate() | starts motor rotation (speed > 0: forwad, speed < 0: backward, speed= 0: stop) |
delay(ms) | stops program for ms milliseconds |
getDistance() | returns the distance (in cm, in range 2..200 approx.) measured with the ultrasonic sensor (255: illegal measurement) |
irLeft.read_digital(), irRight.readDigital() | infrared sensors return 0, if floor is dark; 1, if floor is bright |
ledLeft.write_digital(n), ledRight.write_digital(n) | turns LED on (n = 1) or off (n = 0) |
setLed(n) | turns both LEDs on (n = 1) or off (n = 0) |
setAlarm(on) | with on = 1 starts a sound alarm signal until the function is called with on = 0 |
reset() [only S] | sets the robot to the starting position/direction |
Modul mbalarm (F)
(Real- und Simulationsmodus)
(Modul import: from mbalarm import *)
setAlarm(1) | sets the alarm on |
setAlarm(0) | sets the alarm off |
Module clap
(Mini:Maqueen Rover, external microphone at port P2, ultrasonic sensor removed)
(Module import: from clap import *)
wait_for_clap(timeout = 1000, sensitivity = 75) | waiting for a single clapping and returns True, if a clapping is detected. Returns after the specified timeout (in ms) if no clapping has been detected. sensitivity in the range 0..100 |
wait_for_double_clap(timeout = 1000, spread = 500, sensitivity = 75) | waiting for a double clapping and returns True, if a clapping is detected. The two claps must be made within the spread interval (in ms). Returns after the specified timeout (in ms) if no double clapping has been detected. sensitivity in the range 0..100 |
Module linkup (F)
(using Micro:LinkUp ESP32 coprocessor as I2C slave)
(Module import: from linkup import *)
connectAP(ssid, password) |
connects to the existing access point (hotspot, router) with ssid and password. Returns the received dotted IP address; empty if login fails |
createAP(ssid, password) | creates an access point with ssid and password. If password is empty, the AP is open, i.e. you can log in without authentication |
httpGet(url) | executes an HTTP GET request and returns the response. url in the form "http://<server>?key=value&key=value&...". https can also be used instead of http |
httpPost(url, content) | executes an HTTP POST request and returns the response. url in the form "http://<server>". content in the format "key=value&key=value&...". https can also be used instead of http |
httpDelete(url) | executes an HTTP DELETE request with the given resource |
startHTTPServer(onRequest) | starts an HTTP server (web server on port 80) that listens to HTTP GET requests. For a GET request, the user-defined callback function onRequest(clientIP, filename, params) is called. clientIP: dotted IP address of the client Example: For the URL http://192.168.0.101/on?a=ok&b=3: Return: - a single value (string, float, integer): this is returned unchanged to the browser. It can be an HTML web page or a single value. The webpage must not be longer than 250 characters - a list. The values contained therein are inserted into the % format specifications of the HTML standard file previously saved with saveHTML() and returned to the browser - none: The HTML text file previously saved with saveHTML() is returned unchanged to the browser All HTTP replies are provided with a header 200 OK. The call is blocking. Only the callback function handler is active |
saveHTML(text) | saves the text on the LinkUp as a standard HTML file. It can contain % format specifications, which are replaced with the return values of the callback function handler. If text is empty, the HTML file is deleted |
Module sht (F)
(Sensirion temperature and air humidity sensor at I2C port)
(Module import: import sht
sht.getValues() | returns a tuple with temperature (in degrees Celsius) and and humidity (in percent). I2C address: (SHT31: 0x44, SHT35: 0x45) |
Module sgp (F)
(SGP30 Air Quality (CO2) sensor at I2C port)
Module import: import sgp
sht.getValues() | returns a tuple with CO2 koncentration (in ppm) and VOC (Total Volantile Organic Compunds). The sensor is calibrated to CO2 = 400. Ventilation is urgently required for values higher than 1000. I2C address (SGP30: 0x58) |
Module mqtt
(using Micro:LinkUp ESP32 coprocessor as I2C slave)
(Module import: import mqtt)
Function | Action |
mqtt.broker(host, port = 1883, user = "", password = "", keepalive = 0) |
determines the properties of the broker (IP address, IP port and if necessary authentication details). keepalive determines how long the connection remains open without data exchange (in sec) (default depends on the broker). No connection to the broker is established yet |
mqtt.connectAP(ssid, password) |
connects to the existing access point (hotspot, router) with ssid and password. Returns the received dotted IP address; empty if login fails |
mqtt.connect(cleanSession = True) | connects to the broker. For cleanSession = True, all previous data will be deleted. Returns True if the connection has been established; otherwise False |
mqtt.ping() | sends a ping request to the server to keep the connection open. Returns True if successful; otherwise False |
mqtt.publish(topic, payload, retain = False, qos = 0) | sends a message for the given topic (payload). If retain = True, this message is regarded as the last good/retain message. qos is the Quality of Service level (only 0, 1 supported). Returns True if successful; otherwise False |
mqtt.subscribe(topic, qos = 0 | subscribes the given topic with the given qos level (only 0, 1 supported). A maximum of 50 receiving message tuples (topic, payload) are stored one after the other in a message buffer (maximum lengths topic: 50, payload: 200 bytes). Returns True if successful; otherwise False |
topic, payload = mqtt.receive() | retrieves the first element of the message buffer (the "oldest") as a tuple (topic, payload) and removes the element from the buffer. If there is no data in the buffer, (None, None) is returned. The poll period should be at least 1 sec. Returns None, if the connection to the broker is lost |
mqtt.disconnect() | closes the connection |
Module ntptime
(using Micro:LinkUp ESP32 coprocessor as I2C slave)
(Module import: import ntptime)
ntptime.getTimeRaw(server = "pool.ntp.org" |
returns the current date_time delivered by the given server. Format: tuple (yyyy, mm, dd, h, m, s, week_day, day_of_year) all ints. Time is GMT |
ntptime.getTime(server = "pool.ntp.org") | same, but returns a formatted string (Example: "Tu 2019-06-11 13:04:44 GMT") |
Modul mcp9808
(Microchip temperature sensor at I2C port)
(Modul import: from mcp9808 import MCP9808)
mcp = MCP9808() |
creates a sensor instance |
mcp.temperature() | returns the temperature (in degrees Celsius) |
Realtime Clock (RTC)
(RTC Clock Module DS3231 at I2C port (address 0x68))
(Modul import: import rtc
Function | Action |
rtc.set(yy, mm, dd, h, m, s, w) | sets year, month, day, hour, minute, second, weekday (usually 1..7, 1: sunday) |
rtc.set([yy, mm, dd, h, m, s, w]) | same with list (or tuple) |
rtc.get() |
return a tuple (yy, mm, dd, h, m , s, w) (all ints) |
7-segment display module (4 digits with TM1637 driver)
from mb7seg import FourDigit
from mb7segmin import FourDigit, reduced module, in case of a memory overflow, only (*)
Function | Action |
(*) disp = FourDigit(clk = pin1, dio = pin2, lum = 4) | creates a display instance for a display connected to the given pins. Display luminosity 0..9 |
disp.erase() |
clears the display |
(*) disp.show(text, pos = 0) | shows the text starting at position 0 (far left digit). The text can contain more than 4 characters. text can also be an integer |
disp.scroll(text) | shows a scrolling text |
disp.toRight() | moves the text one position to the right |
disp.toLeft() | moves the text one position to the left |
disp.start() | sets the text to the starting position |
disp.setLuminosity(lum) | sets the luminosity (0..9) |
(*) disp.setColon(enable) | activates/deactivates the colon |
Module bme280
(Temperature-, air humidity, air pressure and height above sea level sensor from Bosch at I2C port)
(Modul import: import bme280
bme280.values() | returns temperature (in Centigrades), humidity (in %), pressure (in hPa) and height above sea level (m) simultaneously in a tuple |
bme20.qnh | sets the air pressure at sea level (in hPa) (assignment) |
Modul tcs34725
(RGB Color Sensor am I2C-Port)
(Modul import: from tcs34725 import ColorSensor)
cs = ColorSensor() | creates a sensor instance |
cs.getRGB() | returns a tuple with the 3 RGB values (each in the range 0...255) |
cs.getLum() | returns the brightness in the range 0..65565 |
Module touchbit
(Remote Control Module "TouchBit")
(Module import: from touchbit import *)
registerCallbacks(buttonPress = None, buttenRelease = None, buttonRepeat = None) | registers callback functions that are called at the corresponding event, if the pollButtons() function is run through |
pollButtons() | must be called periodically so that the callbacks are active |
onButtonEvent(btn, down) | signature of callback |
def onButtonPressed(btn): print("Button", btn, "pressed") def onButtonReleased(btn): print("Button", btn, "released") def onButtonRepeated(btn): print("Button", btn, "repeated") registerCallbacks(onButtonPressed, onButtonReleased, onButtonRepeated) while True: pollButtons() sleep(50)
Module oled
(128x64 pixel OLED module with SSD1306 driver at I2C port)
(Module import: import oled
oled.init() | initializes display (5 rows, 12 columns) |
oled.text(x, y, s) | writes text s on row x starting at column y (x = 0..4, y = 0..11) |
oled.clear() | clears display |
oled.image(filename) | displays the image. The binary image file filename must be created from a BMP image of size 128x64 = 8196 pixels with the tool bmp2oled. All pixels with (r + g + b) / 3 < 100 are displayed in black, the others in white. The tool can be executed with a TigerJython program that contains the single line import bmp2oled. Copy the resulting file to the micro:bit with Tools | Download module. It has a size of 1024 bytes because one byte defines 8 pixels (light or dark) of the display |