Categories

Micro:bit compass example in micropython

The micro:bit also has a compass (magnetometer)

The compass has to be calibrated before it can give accurate readings. Calibration involves moving a dot to make a circle on the matrix. Once this is complete you get a smiling face image displayed to show you that you have successfully completed the calibration.

Again you will need to click on the REPL button in the mu editor prior to flashing the micro:bit

Move the Micro:bit about and you should see different values in the console, 0 is equivalent to North

Code

from microbit import *
 
compass.calibrate()
while True:
    bearing = compass.heading()
    print(bearing)
    sleep(100)

As part of an experiment you could use the display object to display one of the corresponding images

 

  • Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE, Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW

 

Micro:bit accelerometer Micropython example

The micro:bit has an accelerometer on board, you can see this in the picture below

microbit

When you flash the program, you should see a reading of the accelerometer x axis being printed at the bottom of the Mu application. You can move the Micro:bit around and the values should change

 

Code

from microbit import *
 
while True:
    x = accelerometer.get_x()
    print(x)
    sleep(1000)

 

You can also use the get_y and get_z() methods to find out the readings on the other axes. here is an example showing this

from microbit import *
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
print ("x: " + str(x) + " y: " + str(y) + " z: "+ str(z))
sleep(1000)

Random image on the Micro:bit using Micropython

The Image class also has the following built-in instances of itself included as its attributes (the attribute names indicate what the image represents). In this example we will randomly display an image on the led matrix when the button a is pressed

 

Code

from microbit import *
 
import random
 
image_array = [Image.HEART, Image.HEART_SMALL, Image.HAPPY, Image.SMILE, Image.SAD,
Image.CONFUSED, Image.ANGRY, Image.ASLEEP, Image.SURPRISED, Image.SILLY, Image.FABULOUS,
Image.MEH, Image.YES, Image.NO, Image.CLOCK12, Image.CLOCK11, Image.CLOCK10, Image.CLOCK9,
Image.CLOCK8, Image.CLOCK7, Image.CLOCK6, Image.CLOCK5, Image.CLOCK4, Image.CLOCK3,
Image.CLOCK2, Image.CLOCK1, Image.ARROW_N, Image.ARROW_NE, Image.ARROW_E, Image.ARROW_SE,
Image.ARROW_S, Image.ARROW_SW, Image.ARROW_W, Image.ARROW_NW, Image.TRIANGLE,
Image.TRIANGLE_LEFT, Image.CHESSBOARD, Image.DIAMOND, Image.DIAMOND_SMALL, Image.SQUARE,
Image.SQUARE_SMALL, Image.RABBIT, Image.COW, Image.MUSIC_CROTCHET, Image.MUSIC_QUAVER,
Image.MUSIC_QUAVERS, Image.PITCHFORK, Image.XMAS, Image.PACMAN, Image.TARGET, Image.TSHIRT,
Image.ROLLERSKATE, Image.DUCK, Image.HOUSE, Image.TORTOISE,Image.BUTTERFLY,
Image.STICKFIGURE, Image.GHOST, Image.SWORD, Image.GIRAFFE, Image.SKULL,
Image.UMBRELLA, Image.SNAKE]
 
while True:
    if button_a.is_pressed():
        display.show(random.choice(image_array))
        sleep(1000)
    else:
        display.clear()

Image examples for the Micro:bit using Micropython

Here we show you how to create a custom image on the led matrix, we use the Mu editor

from microbit import *
im = Image('99999:90009:90009:90009:99999:')
display.show(im)

You can vary the brightness by changing the value of the digits from 0 – 9.

from microbit import *
im = Image('99999:96369:96369:96369:99999:')
display.show(im)

A simple animation example

from microbit import *
imgs = [
Image('90000:00000:00000:00000:00000:'),
Image('00000:09000:00000:00000:00000:'),
Image('00000:00000:00900:00000:00000:'),
Image('00000:00000:00000:00090:00000:'),
Image('00000:00000:00000:00000:00009:')
]
display.show(imgs, delay=1000,loop=True)

You can also set a pixel and move it around

 

from microbit import *
 
img = Image('00000:00000:00900:00000:00000:')
display.show(img)
sleep(1000)
 
while True:
img = img.shift_up(2)
display.show(img)
sleep(1000)
img = img.shift_right(2)
display.show(img)
sleep(1000)
img = img.shift_down(2)
display.show(img)
sleep(1000)
img = img.shift_left(2)
display.show(img)
sleep(1000)

 
shift_left(n)

Return a new image created by shifting the picture left by n columns.

shift_right(n)

Same as image.shift_left(-n).

shift_up(n)

Return a new image created by shifting the picture up by n rows.

shift_down(n)

Same as image.shift_up(-n).