BBC micro:bit
Tilt Sensor

Introduction

Clearly, the micro:bit has a built-in accelerometer which can be used to work out which way the micro:bit is tilted. The gestures that you have access to in MicroPython give you all of the information that you need for most circumstances. The tilt sensors on this page are a much simpler concept. In its simplest form, the sensor can tell you whether or not you are tilting the sensor along a single axis. The general principle is to have a metal ball that moves when the sensor is tilted, making or breaking a circuit, much as pressing a button completes or breaks a circuit. Older sensors used mercury - Sensors you use should state that they are free of mercury. The sensors on this page are of an on/off nature. You're not getting any measure of how much the sensor is tilted, just the direction.

It turns out that this can be quite useful. There are odd occasions where you want the tilt of the micro:bit and the tilt of another part of the circuit to be independent, perhaps for a device that is used by two people at a time (like a game). At the basic end, you are talking a couple of quid for a sensor.

These are the two I used,

micro:bit circuit

The breakout board on the left is a Sparkfun board called 'tilt-a-whirl'. It was used in a soldering kit of a Simon game and was discontinued some time ago. It's not for sale these days but you may come across one or something similar one day. It measures changes in tilt on 4 directions - forward, back, left, right. The sensor on the right is the one you tend to see these days. You connect it up like you do a button and watch the readings.

Circuit

I put both sensors on the same breadboard but wrote separate programs to test them. You can replace my pin choices with any of the GPIO.

micro:bit circuit

The simple sensor is wired as we would a button, with a 10K resistor.

Programming

For the sensor at the top of the diagram, try this, look at the result and vary as you like,

from microbit import *

while True:
    buttonState = pin0.read_digital()
    if buttonState==0:
        display.show(Image.YES)
    else:
        display.clear()
    sleep(20)

For the tilt-a-whirl sensor, we read 2 digital values. That gives 4 combinations of 1 and 0, a 2 bit value in binary. We use a bitwise left shift to set the second bit and then use a logical OR to set the rightmost or least significant bit. We can use that number to look up the orientation in a list.

from microbit import *

def ReadTilt():
    a = pin8.read_digital()
    b = pin1.read_digital()
    return (a << 1) | b

directions = ["F", "L", "R","B"]
last = -1
while True:
    reading = ReadTilt()
    if reading != last:
        display.show(directions[reading])
    last = reading
    sleep(20)

When I was writing this, I held the breadboard with the pins of the breakout facing me. If you look at the breadboard diagram above and imagine picking it up and rotating it 90° anticlockwise. That's how I held it when I encoded the letter for the direction.

Challenges

  1. Do something interesting with the tilt switch or something more interesting with several of them.
  2. If you are lucky enough to have a go with the tilt-a-whirl breakout or can make a DIY version, you'll find that it offers pretty good performance for a 2 bit output. The example is good enough to make a usable 4-choice input for a game or circuit you are doing.