Pins

The micro:bit has gold pins on its lower edge you can use to connect input and output devices like headphones, LEDs, switches, motors, sensors and other electronic components.

Pins 0, 1 and 2 are GPIO pins: general purpose input/output. There are more pins that you can access with an edge-connector or breakout board.

../_images/music.png

You can attach headphones or an amplified speaker to hear micro:bit sound.

Touch pins

Pins 0, 1 and 2 can work as touch sensors when you also touch the GND pin to complete an electrical circuit:

while True:
        if pin0.is_touched():
                display.show(0)

You can connect these pins using crocodile clips to metal foil to make your own switches or game controllers.

By default the pins work in resistive touch mode, which means you also need to touch the GND pin as well as pin 0, 1 or 2 to complete a circuit.

The pins on the micro:bit V2 can alternatively be made to work in capacitive mode, which means you just need to touch the pin, but not GND as well.

This changes pin 1 to work with capacitive touch:

>>> pin1.set_touch_mode(pin1.CAPACITIVE)

This changes it back to resistive touch:

>>> pin1.set_touch_mode(pin1.RESISTIVE)

Digital input/output

You can use pins 0, 1 and 2 in digital mode. This means they are either on (electricity is flowing through them, represented by the number 1) or off (no electricity is flowing through them, represented by the number 0).

To read the status of digital signals connected to pins 0, 1 or 2:

>>> pin1.read_digital():

This will send a digital 1 (on) signal to pin 1:

>>> pin1.write_digital(1)

This will turn it off:

>>> pin1.write_digital(0)

You could connect a switch or button between the 3v pin and pin 1 and use this to test if the switch is open (0) or closed (1):

while True:
        if pin1.read_digital():
                display.show(1)
        else:
                display.show(0)

Warning

Never connect anything to the pins with a voltage over 3v or you may damage your micro:bit.

Analogue input/output

Pins 0, 1 and 2 can work in analogue mode. This means instead of being off or on (0 or 1), they vary in value between 0 and 1023.

To read the status of analogue signals connected to pins 0, 1 or 2:

>>> display.scroll(pin1.read_analog())

If you attach a wire to the 3V pin and another wire to pin 1 and touch the other ends of the wires across different materials, you can measure how relatively conductive they are. A reading of 1023 would be the most conductive and 0 the least conductive (effectively an insulator).

Never connect anything to the pins with a voltage over 3v.

This example varies the brightness of an LED connected to pin 1 and GND:

for i in range(1024):
        pin1.write_analog(i)
        sleep(3)

Exercises

  1. Create a program that will show a happy emoji on the LED screen when pin0 pin is pressed. Make sure it is in capacitive mode.
  2. Instead of showing an emoji, show the current temperature of the micro:bit instead.