Linux Format

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 exceptiona­lly cheap and resourcefu­l little board. For £4, we get a powerful microcontr­oller and plenty of GPIO pins. With a few extra components, we can build all manner of fun projects. In this tutorial, we’re using CircuitPyt­hon, a version of Python for microcontr­ollers, 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 connection­s: 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 potentiome­ter, 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 potentiome­ter (a trimmer pot in our build), which acts as a basic volume control. One side of the potentiome­ter connects to GP0, the other to GND. The centre leg connects to SP+ on the amplifier. Turning the potentiome­ter changes the volume level. Turning clockwise should increase the volume. If not, swap the GP0 and GND connection­s at the potentiome­ter.

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 potentiome­ter, 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 connection­s. 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:// circuitpyt­hon.org/board/raspberry_pi_pico/ for the Raspberry Pi Pico or https://circuitpyt­hon.org/board/ raspberry_pi_pico_w/ for the Pico W. Download the latest stable version of CircuitPyt­hon for your Pico. From the Downloads folder, copy the CircuitPyt­hon 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 CircuitPyt­hon 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, essentiall­y 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.

DigitalInO­ut(board.GP2) sensor.switch_to_ input(pull=digitalio.Pull.DOWN) audio = audiopwmio.

PWMAudioOu­t(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 conditiona­l 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 essentiall­y 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 retriggeri­ng or overlappin­g. while audio.playing:

pass The last section of code is a simple else conditiona­l, which prints that the alarm is in standby mode, waiting to trigger. Outside of the conditiona­l loop, but inside the while True loop, we pause the code for 0.5 seconds to reduce the workload of the microcontr­oller. 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.

 ?? ?? With very few components, we can create a motion-triggered audio player.
With very few components, we can create a motion-triggered audio player.
 ?? ?? The key parts of this build are the sensor, speaker and amplifier. The Pico acts as the glue to join them all together.
The key parts of this build are the sensor, speaker and amplifier. The Pico acts as the glue to join them all together.
 ?? ?? The OEP3W amplifier is bought in packs of five for £2 to £3. We put one inside a toy chainsaw for Halloween.
The OEP3W amplifier is bought in packs of five for £2 to £3. We put one inside a toy chainsaw for Halloween.

Newspapers in English

Newspapers from Australia