Micro:bit Selfies

About the project

In this resource, you will make a micro:bit-controlled selfie booth with a Raspberry Pi and camera module, using Python. This is a great way to get started with hardware and simple text programming.

Project info

Items used in this project

Hardware components

Raspberry Pi Camera Module V2 Raspberry Pi Camera Module V2 x 1
Bbc Micro:bit - An Educational & Creative Tool For Kids Bbc Micro:bit - An Educational & Creative Tool For Kids x 1
Usb A To Usb Micro B 1m Usb A To Usb Micro B 1m x 1
Crocodile clip cables Crocodile clip cables x 1

Software apps and online services

Software installation Software installation To install the software open LXTerminal and type: sudo apt-get update && sudo apt-get install mu -y


What you will need


  • Raspberry Pi camera module
  • BBC micro:bit
  • USB A to micro USB-B
  • 3 x crocodile clip cables


Software installation

To install the software open LXTerminal and type:

  1. sudo apt-get update && sudo apt-get install mu -y

Connect the camera and micro:bit

Build the micro:bit trigger

  • Connect a male-to-female jumper wire to GPIO pin 4 on your Raspberry Pi.
  • Take a crocodile clip cable and connect one end to the pad on your micro:bit labelled 0  and another to the pin labelled GND or ground.
  • Connect another cable to pin and connect the other end to the male end of the jumper wire you used on your Raspberry Pi, like in the diagram below:

Now that you have created your trigger circuit, you can boot your Raspberry Pi and load mu.

Program the micro:bit trigger

  • Open mu and click on New to open a blank untitled file.
  • Click on Save, name the file microbit-trigger.py and press Enter on your keyboard.
  • The first line in your file should already be typed out for you and reads:
  1. from microbit import *

This line imports the micro:bit Python library module for you to use.

  • Next, create a loop by typing:
  1. while True:

Note: Upper-case and lower-case letters are very important when typing Python code. Notice that True  has an upper-case T and a colon at the end. Everything written after this line should be indented by four spaces.

Now we need to set a condition so that when the cable connected to pin 0 on the micro:bit has been touched, it displays a message on the LED matrix and triggers the camera. We do this in Python using the word if like this:

  1. while True:
  2. if pin0.is_touched():
  3. display.scroll("say cheese!")
  4. sleep(500)
  5. pin1.write_digital(1)
  6. sleep(5000)
  7. pin1.write_digital(0)

Each time around this loop, the computer asks if the cable attached to pin 0 is being touched. If it is, then the message ‘say cheese!’ should appear and tell pin 1 to become high or turn on. This is the pin that is connected to the Raspberry Pi. We will use that pin in the next step to trigger the camera to take a picture and store it.

  • Save the file and then click on Flash to send the code to your micro:bit. It’s important that you do this step to test that your trigger works. Once the amber light on the back of the micro:bit stops flashing, press the reset button next to it and then touch the ground and pin 0 crocodile clip cables. This should cause text to scroll across the LED matrix on the micro:bit.

Program the Raspberry Pi camera

  • Locate and open Python 3 (IDLE) from the main menu by clicking on Programming. This is where you will write your Python code for the camera.
  • Create a new file and save it as microbit-camera.py.
  • Begin by importing all the libraries and modules required in this program by typing the following:
  1. from picamera import PiCamera
  2. from gpiozero import Button
  3. from time import sleep
  • Next, you need to tell the computer which pin is going to be used to trigger the camera to take a picture. You connected the microbit to GPIO pin 4. Underneath the imports, type:
  1. button = Button(4, pull_up = False)
  • Then write the sequence of instructions to take a picture when triggered by someone touching the micro:bit cable. First, show a preview from the camera by typing:
  1. with PiCamera() as camera:
  2. camera.start_preview()
  3. sleep(5)

Whilst the preview shows, you can get into position for your selfie.

  • Underneath, type the instruction to wait for the micro:bit trigger:
  1. button.wait_for_press()
  2. camera.capture('selfie.jpg`)
  3. camera.stop_preview()

The program will wait for the trigger from the micro:bit, then take a picture and store it in a file called selfie.jpg before stopping the camera preview.

  • Save your file. Check that your micro:bit trigger is working, before clicking on Run and Run Module to start the camera code.

What next?

  • Can you create a countdown on the micro:bit LED matrix before a photo is taken by the camera?
  • Now that you have the camera taking one picture triggered by the micro:bit, how could you take more pictures and save them as separate files?
  • Take a look at the Getting started with micro:bit resource for more things you could do with your micro:bit.


Photo of Raspberry Pi

Raspberry Pi

Our mission is to put the power of computing and digital making into the hands of people all over the world. We do this so that more people are able to harness the power of computing and digital technologies for work, to solve problems that matter to them, and to express themselves creatively.


Leave your feedback...