Skip to content

Using Micro-python

Micropython IDE

You can develop Micropython apps for the micro:bit using the online python IDE:

Micropython IDE

Visit http://python.microbit.org to use it online

Mu Editor

The Mu Editor is another good option as it can be installed on Mac and Windows:

Mu Editor

Visit Mu Editor Download to download Mu editor.

Documentation

See http://microbit-micropython.readthedocs.io/ for the API reference

Basic Python

Variables and (no) data types


# a variable takes any kind of value

a = 100  # numbers
b = "test" # text
c = [1,2,3,4] # arrays
d = { 'label' : 'value' } #dictionaries

Commands

# conditional
if True:
    print 'Always true'
else:
    print 'Never exectued'

# while loop
while True:
    print 'loop'

# for loop
for i in range(0,10):
    print i

Micropython commands

Hello world

from microbit import *
display.scroll('Hello, World!')

Button A

from microbit import *
while True:
    if button_a.is_pressed():
        display.show(Image.HAPPY)

Button A + B

from microbit import *
while True:
    if button_a.is_pressed():
        display.show(Image.HAPPY)
    if button_b.is_pressed():
        display.show(Image.SAD)

Capacitive Touch Sensor

from microbit import *
touches = 0
while True:
    if pin0.is_touched():
        touches += 1
        display.scroll(str(touches))

Digital Input

from microbit import *
while True:
    while (pin0.read_digital() == 0):
        display.show(Image.SURPRISED)
    display.show(Image.ASLEEP)

Digital output

from microbit import *
while True:
    pin1.write_digital(1)
    sleep(1000)
    pin1.write_digital(0)
    sleep(1000)

PWM Output

from microbit import *
pin1.set_analog_period(1)
while True:
    for brightnessValue in range(0,1024,1):
        pin1.write_analog(brightnessValue)
        sleep(1)
    for brightnessValue in range(1023,-1,-1):
        pin1.write_analog(brightnessValue)
        sleep(1)

Analog input

from microbit import *
while True:
    potValue = pin2.read_analog()
    display.scroll(str(potValue))

Display

from microbit import *
while True:
    display.scroll('Hello, World!') # scroll text
    display.off() # switch off the display
    display.on() # switch on the display
    display.setPixel(x,y,brightness) # set an individual pixel
    display.show(Image.SMILE) # display an image see http://microbit-micropython.readthedocs.io/en/latest/image.html

Temperature sensor

from microbit import *
while True:
    display.scroll(str(temperature()))

Magnetometer (Compass)

from microbit import *
while True:
    display.scroll('Heading %s' % compass.heading())

Show a compass

from microbit import *

compass.calibrate()

while True:
    needle = ((15 - compass.heading()) // 30) % 12
    display.show(Image.ALL_CLOCKS[needle])

Accelerometer - Gestures

from microbit import *
while True:
    if accelerometer.is_gesture("shake"):
        display.show(Image.SURPRISED)

Accelerometer - Sensor data

from microbit import *
while True:
    display.scroll(str('X:%s' % accelerometer.get_x()))
    display.scroll(str('Y:%s' % accelerometer.get_y()))
    display.scroll(str('Z:%s' % accelerometer.get_z()))

Speech

Connect a speaker to Pin 0 and Pin 1

from microbit import speech
while True:
    speech.say("Hello, World")

Music

Connect a speaker to Pin 0 and GND

import music
music.play(music.NYAN)

Driving Neopixels

"""
    neopixel_random.py

    Repeatedly displays random colours onto the LED strip.
    This example requires a strip of 8 Neopixels (WS2812) connected to pin0.

"""
from microbit import *
import neopixel
from random import randint

# Setup the Neopixel strip on pin0 with a length of 8 pixels
np = neopixel.NeoPixel(pin0, 8)

while True:
    #Iterate over each LED in the strip

    for pixel_id in range(0, len(np)):
        red = randint(0, 60)
        green = randint(0, 60)
        blue = randint(0, 60)

        # Assign the current LED a random red, green and blue value between 0 and 60
        np[pixel_id] = (red, green, blue)

        # Display the current pixel data on the Neopixel strip
        np.show()
        sleep(100)