PIR Movement Alarm With BBC Micro:bit and External Speaker
Introduction: PIR Movement Alarm With BBC Micro:bit and External Speaker
This article shows how to make a simple movement alarm using MicroPython on a BBC micro:bit V2, a 3.3V compatible, miniature passive infrared (PIR) sensor module, a small external speaker and a few extra components. The program plays low bitrate sound samples when a warm, moving object is detected.
The project was implemented with components soldered onto a Kitronik :CREATE Proto Board. A breadboard was used to ensure the circuit worked before soldering onto the final board.
This project should work on both the micro:bit V1 and V2 but the V2 has a few advantages:
* the smaller built-in speaker can be used together with the external 25mm speaker, this is useful as it covers high frequencies well;
* the 3.3V regulator is uprated offering 270mA rather than 90mA from the edge connector;
* it has more flash storage which may be usable in the future for longer sound samples.
The audio samples are still fairly quiet on this setup and will only be audible in a quiet room. Simple tones are more practical for loud alarms as both speakers are more responsive at certain high frequencies.
Supplies
* BBC micro:bit.
* Kitronik :CREATE Proto Board.
* CUI CMT-2512-585T 25mm 32ohm speaker - Mouser | Digi-Key - for alternatives the maximum current needs to be within the limits of the micro:bit.
* AM312 PIR sensor module or similar 3.3V compatible module like HC-SR505. Always check the documentation and labelling of pinouts. Avoid "standard" HC-SR501 modules as these are typically designed for 5V use.
* 2N2222 NPN transistor or 2N3904 - most NPN transistors will be ok, check the pinouts and verify the maximum power and current are suitable for the speaker.
* Diode, 1N4001 or similar.
* 1k resistor.
* Solder (and soldering iron!).
* Some short insulated wires.
* Optional:
+ 4 small self-adhesive rubber feet at least 3mm tall - these will prevent the board scratching surfaces.
+ Multimeter - useful for checking the soldered connections are good using the resistance mode or continuity mode.
Step 1: Circuit Diagram
The circuit is very simple using power from the micro:bit edge connector (GND and 3V) and two GPIO pins.
1. The P0 pin is commonly used for audio output on the micro:bit and on the V2 is also used for the built-in speaker on the back of the micro:bit.
2. The P8 pin is used as an input for the PIR sensor module.
The GPIO outputs can drive very low current devices directly like piezo speakers but the current required for the 25mm speaker is too high and would stress the output. An NPN transistor (Q1) is used here to buffer the (square wave) output and switch the higher current through the speaker (J1). 1003
The resistor (R1) is limiting the current across the base-emitter junction of Q1 when P0 is high (3.3V). The circuit follows the datasheet recommendation with a flyback diode (D1) across the speaker to deal with the back EMF when the square wave causes the transistor to abruptly switch on/off.
The digital P0 output will be pulse-width modulated (PWM) when playing audio samples to approximate the original analogue audio signal.
Step 2: Breadboard Implementation
A breadboard is useful to:
1. check the circuit works,
2. evaluate sensors and
3. allow easy refinements before a more rigid implementation.
The AM312 sensor modules are often advertised as having a high signal of around 2 seconds when they detect motion. This is useful because this duration is long enough to allow simple level-based pin reading which will work even if the program is doing other processing for some of the time.
Some quick testing in CircuitPython's REPL from another microcontroller showed this is more-or-less accurate and that the device did not give false indications if no one was in the room for ~6 minutes.
>>> while True:
... for idx in range(80):
... print("X" if pir_p6.value else "." , end="")
... time.sleep(0.4)
... print()
...XXXXXXX........XXXX..........................................................
................................................................................
...9 more lines full of dots...
................................................................................
..................................................XXXXXXXXXXXX.....XXXXX........
...XXXXX..... 1003