Science Oxford Logo

CCC - Picnic Protectors

Resources for the 'Picnic Protectors' Creative Computing Club.

During this workshop, you will design and code a micro:bit alarm system.

This page recaps what we discussed as a group.

To skip to the examples and ideas, click here.


Python Beta Editor

Open the new Python editor by visiting python.microbit.org/v/beta. Your screen will look something like this:

screenshot of the Python editor screen

If you see 'Download' and 'Connect' at the bottom of the screen instrad, follow these instructions to pair your micro:bit with your computer.


Edit the code on the screen, then flash it to your micro:bit to see the changes.

Try:


Using the motion sensor

You can find the accelerometer code by looking in the Reference menu for 'Accelerometer'.

The code you are shown includes another while True loop, so we should not drag-and-drop the whole snippet into our current code.

Unlike in Scratch, where you can have lots of forever loops all running at the same time, we can only use one main loop at once in Python - if you put another while True loop underneath the first one, this code will never happen, because the micro:bit is too busy doing the first loop!


To use the accelerometer, we use an if statement - if the accelerometer registers a 'shake' gesture, make something happen.

Edit you code so that it looks like this:

while True:
    if accelerometer.was_gesture('shake'):
        display.show(Image.HEART)
        sleep(1000)
        display.scroll('Hello')

You will need to indent the lines of code that you want to happen when the micro:bit has been shaken.


animated image showing where to add the new code, as per the instructions above

What kind of gestures would be useful for your security system?

An example video is show below - the micro:bit is happy when it is 'face up' , then angry when it is anything else!



while True:
    if accelerometer.is_gesture('face up'):
        display.show(Image.HAPPY)
    else:
        display.show(Image.ANGRY)

Adding an alarm

Sound is important to a security system - you can add music, beeps or sound effects as well as changes to the display.

Have a look through the Sounds section of the Reference menu, there are a few different options you can use.

Try the 'expressive sounds', which are at the bottom of the menu - you can drag-and-drop this directly into your code, as it is only one line.

Think about when you want your sound to happen - the micro:bit will do your code in order - should it play the sound before or after changing the image?

Experiment with the music code as well - the editor will automatically add the 'import music' line to the top of your code - you only need this once.

animated image showing where to add the new code, as per the instructions above

Creating a pressure sensor



A pressure sensor works by having an electrical circuit, that is only complete when something heavy is put on it. If the heavy thing is moved, the circuit is broken (two wires do not touch any more).

If you put the micro:bit in the circuit, then it can tell whether the electricity is able to flow or not.

There are lots of ways that you could choose to set this up, but an example is described here for you to start with...

photo showing an example of a pressure sensor, as described in the instructions below
  1. Tape conductive tape in a line on two seperate pieces of paper - leave some hanging off the edge.
  2. Fold in the edges of one of the pieces of paper, parallel to the piece of tape.
  3. Connect one crocodile clip between one piece of tape and the micro:bit's Pin0.
  4. Connect another crocodile clip between one piece of tape and the micro:bit's 3V pin.
  5. The folder piece of paper is your bottom piece. Turn the other piece of paper upside down, and put it on top, so that the two pieces of tape form a cross shape (see below picture).
  6. Put something heavy on top of the paper.
photo showing the inside of the pressure sensor, with two pieces of tape in a cross shape, as described above

Try this code example:

while True:
    if pin0.read_digital() == 1:
        display.show(Image.HEART)
    else:
        display.show(Image.ANGRY)

What next?

We have shown you some ideas of how to create a security system for your picnic, which options do you think would work best?

Build your security system and test it out on your family - if they manage to steal your picnic, work out how they did it and improve your design.


Some ideas...