Python microbit basics: buttons and touch pins

Python microbit basics: buttons and touch pins

This beginners tutorial talks you through how to write python programs for a micro:bit that can respond when you press a button or touch one of the input pins along the bottom of the device.

Try it with code
Try the code: Python microbit basics: buttons and touch pins

The code below turns a micro:bit into a counter: press button A to count down, button B to count up and touch pin 0 to reset back to 0. Try it out and then read through how it works.

You could use this to keep score in a competition, especially if you use two or more micro:bits – one for each team.

After line 3 imports the micro:bit module so that python can start controlling the inputs and outputs, line 5 and 6 create two new variables: counter and new_value :

# http://blog.withcode.uk/python-microbit-basics-buttons-and-touch-pins
# micro:bit counter
from microbit import *

counter = 0
new_value = 0

# keep looping forever
while True:
    
    # decrease by 1 if button A was pressed
    if button_a.was_pressed():
        new_value = counter - 1
        
    # increase by 1 if button B  was pressed
    if button_b.was_pressed():
        new_value = counter + 1
        
    # reset to 0 if you touch pin 0
    if pin0.is_touched():
        new_value = 0
    
    # stop counter from going less than 0
    if new_value < 0:
        new_value = 0
        
    # scroll new value if it's changed
    if new_value != counter:
        counter = new_value
        display.scroll(str(counter))
        
    # avoid hogging all the CPU time / using too much power
    sleep(50)

The variable counteris used to store the number that is displayed on the micro:bit at any given time.

The variable new_valueis used to detect if the number has changed so that the new number can be scrolled on the screen.

Both variables store whole numbers only (e.g. 1, 2 or 3, not 1.4, 1.7 or 3.8). We call these types of numbers integers

# http://blog.withcode.uk/python-microbit-basics-buttons-and-touch-pins
# micro:bit counter
from microbit import *

counter = 0
new_value = 0

# keep looping forever
while True:
    
    # decrease by 1 if button A was pressed
    if button_a.was_pressed():
        new_value = counter - 1
        
    # increase by 1 if button B  was pressed
    if button_b.was_pressed():
        new_value = counter + 1
        
    # reset to 0 if you touch pin 0
    if pin0.is_touched():
        new_value = 0
    
    # stop counter from going less than 0
    if new_value < 0:
        new_value = 0
        
    # scroll new value if it's changed
    if new_value != counter:
        counter = new_value
        display.scroll(str(counter))
        
    # avoid hogging all the CPU time / using too much power
    sleep(50)

Lines 9 makes all of the lines that are indented underneath it (lines 10-33) repeat forever.

You can see on line 12 and 16 how to check if a button has been pressed. There are two ways of doing this: button_a.is_pressed()checks to see if the button is currently being pressed at that exact moment in time whereas button_a.was_pressed()checks to see if the button has been pressed at all since last time you checked (or since the program started).

 

Line 20 checks to see if someone is currently touching pin 0. You can test this in the simulator using the I/O pins tab when you run the code in your browser:

How to test the I/O pins one the micro:bit simulator
How to test the I/O pins one the micro:bit simulator

There are 20 pins on the micro:bit that run along the bottom edge of the device. You can find out about each of them on the micropython documentation page but there are four types:

Analog pins: You can input (sense) a value from -1024 to 1024 or output (send) a value in the same range

Digital pins: You can input (sense) if they’re turned on / off, or output (send) an on / off signal to them.

Power pins: These are connected to the 3v power supply and can be used for powering other gadgets like additional LEDs

Touch pins: These are the easiest to use – they can detect the change in capacitance that happens when a human finger touches them so you can use them as touch sensors.

Pins 0, 1 and 2 can be used as touch pins. These are the larger, numbered pins along the bottom of a micro:bit.

# http://blog.withcode.uk/python-microbit-basics-buttons-and-touch-pins
# micro:bit counter
from microbit import *

counter = 0
new_value = 0

# keep looping forever
while True:
    
    # decrease by 1 if button A was pressed
    if button_a.was_pressed():
        new_value = counter - 1
        
    # increase by 1 if button B  was pressed
    if button_b.was_pressed():
        new_value = counter + 1
        
    # reset to 0 if you touch pin 0
    if pin0.is_touched():
        new_value = 0
    
    # stop counter from going less than 0
    if new_value < 0:
        new_value = 0
        
    # scroll new value if it's changed
    if new_value != counter:
        counter = new_value
        display.scroll(str(counter))
        
    # avoid hogging all the CPU time / using too much power
    sleep(50)

Lines 23-25 stop the counter value from going below 0 if you keep pressing button a.

Lines 28-30 might take some explaining: if new_value != counter: checks to see if the new_valueis not equal to counter , which will happen if you’ve just pressed one of the buttons.

display.scroll(...)expects its parameter (the value in brackets) to be a string (which is one or more letters / digits / characters). counter is an integer (whole number) rather than a string so str(counter)converts the integer into a string so it’ll work with display.scrollproperly.

 

sleep(50)  on line 33 isn’t necessary but it’s a good idea to have it. If you didn’t and you tried to test the code in the simulator, your browser will crash. Testing it on a micro:bit would work but it’d use more battery power than necessary. In both cases, the problems are caused by the while loop repeating so quickly that the CPU is always busy. sleep(50)  just pauses the program for 50ms, which isn’t long, but long enough for your browser / micro:bit to have a breather.

Python microbit basics: How to display images

Python microbit basics: How to display images

This beginners tutorial talks you through how to get started displaying and manipulating images on a BBC micro:bit.

 

Try it with code
Try the code: python microbit basics: How to display images

Run the python code below in the micro:bit simulator to see three different ways of showing an image:

The easiest way to display an image is to use a built in image such as Image.HAPPY

from microbit import *

# show built in images
display.show(Image.HAPPY)
sleep(1000)
display.show(Image.SAD)
sleep(1000)

# define your own image on separate lines
i = Image("00000:"
          "22222:"
          "44444:"
          "66666:"
          "88888")
display.show(i)
sleep(1000)

# define your own image on one line
i = Image("88888:66666:44444:22222:00000")
display.show(i)

Lines 4 and 6 show you how to display a built in image. The sleep(1000) lines pause the program for 1000 milliseconds (1 second) so that the images are on the screen long enough for you to see them. You can find a list of built in images on the documentation page.

Once you’ve tried out some of the built in images, it’s more fun to create your own images:

from microbit import *

# show built in images
display.show(Image.HAPPY)
sleep(1000)
display.show(Image.SAD)
sleep(1000)

# define your own image on separate lines
i = Image("00000:"
          "22222:"
          "44444:"
          "66666:"
          "88888")
display.show(i)
sleep(1000)

# define your own image on one line
i = Image("88888:66666:44444:22222:00000")
display.show(i)

Lines 10-14 creates  a new Image object called i  and sets the brightness of each of the LEDs according to the numbers.

0 for each LED means that it is switched off and 9 means it’s fully on.

You can’t actually set the brightness of a LED by dimming it like a lightbulb, but you can fool the human eye into thinking the brightness of an LED has been changed by switching it on and off very quickly for varying amounts of time. Python does this for you behind the scenes.

from microbit import *

# show built in images
display.show(Image.HAPPY)
sleep(1000)
display.show(Image.SAD)
sleep(1000)

# define your own image on separate lines
i = Image("00000:"
          "22222:"
          "44444:"
          "66666:"
          "88888")
display.show(i)
sleep(1000)

# define your own image on one line
i = Image("88888:66666:44444:22222:00000")
display.show(i)

Line 19 looks a little more confusing but it’s just a more compact way of defining an image than lines 10-14. Each row of the image is separated by a colon (:) and the brightness of each LED is represented by a number from 0 (off) to 9 (full brightness)

Python microbit basics: How to display text

Python microbit basics: How to display text

The screen on a micro:bit only has 25 pixels in a grid of 5×5 so it can only display one character at a time. There are two main ways to display text on a micro:bit in python: you can scroll the characters from right to left or display a whole character one after the other:

Try it with code
Python microbit basics: How to display text