BBC micro:bit
Knock Sensor (MicroPython)

Introduction

A knock sensor is a sensor designed to detect a vibration or knock. We can make use of a simple piezo element (the thing at the heart of those little buzzers you might have been using).

For this project, I have used a 'naked' piezo element. You should be able to get this to work with a normal buzzer too. A buzzer is generally the same thing inside a plastic case. The piezo element looks like this,

Piezo Element

Circuit

Connect the two wires of the piezo element to the breadboard. You may need to strip a little of the insulation to do this. The resistor needs to be at least 1 Megaohm - I got the best results with that value. Less than this is not really an option. The element produces a very high voltage when knocked.

The black lead of the element connects to GND. Place the 1M resistor across the red lead to GND. Then add your signal connection to pin 0 on the same part of the ciruit as the red lead.

micro:bit Circuit

Programming

Download the program and then tap.

from microbit import *

threshold = 150
while True:
    reading = pin0.read_analog()
    if reading>threshold:
        display.show(Image.HAPPY)
        sleep(250)
    else:
        display.clear()
    sleep(10)

Challenge

By checking the time at which the sensor is knocked, you can design a program that responds to a specific (maybe secret) pattern of knocks. This takes a while and may need some experimentation to get there, but the effect is superb. Playing a tune or lighting up the matrix when the knock is detected is cool.