Scare your enemies with a Pi prank box
Yes, Les Pounder is very scary, but buying OEP3W amplifiers from AliExpress is worse. He wonders what pranks he can pull with them…
The Raspberry Pi Pico is an exceptionally cheap and resourceful little board. For £4, we get a powerful microcontroller and plenty of GPIO pins. With a few extra components, we can build all manner of fun projects. In this tutorial, we’re using CircuitPython, a version of Python for microcontrollers, to play MP3 files. We need an amplifier, and the OEP3W at £2 from AliExpress, will do the job. But we’re not making a music player. Instead, we are using a sensor to detect movement and trigger an alert sound to play. Scaring away friends, family or next door’s cat.
We’ll go through how to wire up the circuit using a breadboard, then write the code to make it work. We’re using a Raspberry Pi Pico W in this build, chiefly because we had one to hand, but a £4 Pi Pico would be a cheaper and better option.
Building the circuit
There are two key parts of the build: the PIR sensor (input) and the speaker (output).
The PIR (passive infrared) sensor has only three connections: VCC, GND and Data/OUT. VCC connects to the 5V (VBUS) pin on the Pico. GND connects to any GND pin on the Pico. Data/OUT connects to GP2 on the Pico. And that’s it. Your PIR sensor may be a little sensitive, so you can either tweak the potentiometer, hidden around the back, or use a paper cup or funnel to restrict its vision.
The output pins for the speaker are GP0 (SP+) and GND. But GP0 is routed through a 10K potentiometer (a trimmer pot in our build), which acts as a basic volume control. One side of the potentiometer connects to GP0, the other to GND. The centre leg connects to SP+ on the amplifier. Turning the potentiometer changes the volume level. Turning clockwise should increase the volume. If not, swap the GP0 and GND connections at the potentiometer.
The OEP3W is a tiny yet powerful amplifier. You need to solder up the pins; we soldered them so the board could be used in a breadboard. The OEP3W requires power, from the Raspberry Pi Pico W, input audio (SP+ connects to the centre pin of the potentiometer, SP- to GND on the Pico) and then it connects to a speaker. We used a spare laptop speaker that we hacked. You can find cheap speakers online.
See the schematic in the download – it explains where each connection goes and you can easily trace the connections. Take one section at a time.
Connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button. This forces the Pico into bootloader mode and a new drive, RPI-RP2,
appears in the file manager. Open a browser to https:// circuitpython.org/board/raspberry_pi_pico/ for the Raspberry Pi Pico or https://circuitpython.org/board/ raspberry_pi_pico_w/ for the Pico W. Download the latest stable version of CircuitPython for your Pico. From the Downloads folder, copy the CircuitPython file (a UF2 firmware image) to the RPI-RP2 drive. After a few moments the drive disappears and is replaced with CIRCUITPY. This is where we’ll write the project code.
Open your favourite text editor. We chose Thonny
as it can work directly with CircuitPython devices.
Create a new file and start the project code by importing a series of modules. The first is time, used to control the speed at which the project code loops. Next we import board and digitalio. These enable our code to interact with the GPIO, reading and changing the GPIO pin status as needed. Finally we have audiomp3 and audiopwmio, used to create audio via the GPIO. import time import board import digitalio import audiomp3 import audiopwmio
Our next task is to set up the GPIO pins for the sensor and audio. We create an object called sensor and use that to set up GP2 as an input. Using an internal resistor on the GPIO pin, we pull GP2 down, essentially turning the pin off (0 volts). Then we create another object, audio, and tell the code that our speaker is on pin GP0. sensor = digitalio.
DigitalInOut(board.GP2) sensor.switch_to_ input(pull=digitalio.Pull.DOWN) audio = audiopwmio.
PWMAudioOut(board.GP0)
The next object, decoder, loads an MP3 for playback. This needs to be saved to the root of CIRCUITPY. We used Audacity to create an MP3 alert sound. We followed Adafruit’s guidance: “Mono and stereo files less than 64kbit/s work, with sample rates from 8kHz to 24kHz. The RP2040 has a PWM output with 10 bits, so there’s not much point in using high bit rates.” We made a mono MP3 at 24kb/s. decoder = audiomp3.MP3Decoder(open(“alert-tone.
mp3”, “rb”))
Now we move to the main loop. This runs as long as the Pico is powered. Its first task is to print the status of the alarm pin, GP2. The PIR sensor has a default state of HIGH, so the pin reports TRUE.
while True: print(sensor.value)
If the sensor is triggered, then the status changes to FALSE, and this is where a conditional test runs a section of code.
if sensor.value == False:
When the alarm is triggered, the code prints a message to the Python Shell – this is more for debugging issues than general use. It then plays the MP3 alert sound.
print(“ALARM TRIGGERED”) audio.play(decoder)
The next loop only runs when the audio is playing. It essentially checks that the audio is playing. If so, the loop goes around and checks again. This is here to ensure that the alert sound plays through without retriggering or overlapping. while audio.playing:
pass The last section of code is a simple else conditional, which prints that the alarm is in standby mode, waiting to trigger. Outside of the conditional loop, but inside the while True loop, we pause the code for 0.5 seconds to reduce the workload of the microcontroller. else:
print(“ALARM STANDBY”) time.sleep(0.5) Save the code as code.py to the Pico. This triggers the board to reboot and the alarm is ready for testing. Wave your hand in front of the sensor, or ask a friend to walk in front of it. The alarm sounds and scares your victim.
The project can be removed from your computer and powered with a USB power bank. Connect the Pico to the power bank and hide the sensor from sight.