Build a Humane Mousetrap That Emails You When It's Triggered

by nightcustard in Circuits > Raspberry Pi

714 Views, 13 Favorites, 0 Comments

Build a Humane Mousetrap That Emails You When It's Triggered

FIV6Q35M3OGTF8B.jpg
Completed side view PXL_20241115_200434239.jpg

Humane mouse traps are an excellent idea but are often forgotten after they've been deployed for a while, turning them into a very inhumane way of trapping mice. Traditional kill-traps can be very effective at first but are often avoided by increasingly wary mice and, of course, kill their victims.

Build a smart mouse trap which humanely catches mice and alerts you via email!

  1. The trap sends you an email when it's been triggered so you don't have to keep inspecting it.
  2. The trap has no physical trigger, so there's nothing between the opening and the bait to put off wary mice.
  3. The trap sends you an email every day at a set time to remind you it's active.

There's some mechanical construction involved, plus soldering. It should take (if you're used to soldering etc.) no more than half a day to assemble and wire and then maybe an evening to load and personalise the code.

The smart mouse trap is coded in Micro Python. You don't need to understand Micro Python to successfully build and get the mouse trap operational but you do need to be able to follow the linked instructions to load the code provided below.

Please note: A key feature of this Instructable is the smart trap's emailing capability. This ability depends upon the continuing availability of Google's 'App passwords' which are defined, in Google's words, as such: 'App passwords help you sign in to your Google Account on older apps and services that don’t support modern security standards.'

It's quite possible Google may change how these passwords are generated or even withdraw them altogether, so before embarking on this project, I'd advise skipping forward to Step 7 and following the instructions there to set up a secondary Gmail account and an App password. Once you've done this successfully, you can proceed, knowing everything else is under your control.

Supplies

  1. 1 off Humane mouse trap with sprung door (UK cost approx. £10.00 for two)
  2. 1 off miniature 5v push/pull solenoid with chamfered end (UK cost approx. £5.10)
  3. 1 off wood or plastic packer to mount solenoid (see text)
  4. 1 off Raspberry Pi Pico W or Pi Pico 2 W microcontroller without headers (UK cost approx. £5.80 or £6.60 respectively)
  5. 1 off Adafruit MOSFET driver (UK cost approx. £3.90)
  6. 1 off triple 'AA' battery box with switch (UK cost approx. £13.73 for six)
  7. 1 off IR Break Beam Sensor - 3mm LEDs (pair) (UK cost approx. £2.60)
  8. 1 off 30kΩ 1/4W metal film resistor
  9. 1 off 22kΩ 1/4W metal film resistor
  10. 2 off BAT46 or similar schottky diodes (UK cost 18p each)
  11. 6 off solder posts (or break apart straight header strips)

For mounting the various modules (if not using hot-melt glue):

  1. 10 off 8mm length M2 screws
  2. 10 off M2 washers
  3. 6 off 10mm length M3 screws
  4. 6 off M3 washers
  5. 8 off 3mm long M3 spacers (used for M2 & M3 screws)

Miscellaneous consumables:

  1. Resin cored solder
  2. Quick set epoxy adhesive (to glue solenoid to wood packer)
  3. 20s gauge hook-up wire (for battery box and wiring to MOSFET driver)
  4. 22s gauge hook-up wire
  5. Hot glue (if not using screws)
  6. 8cm length self-adhesive hook and loop tape (to mount the battery box)
  7. Heat shrink sleeve (see text and photos)

Tools:

  1. Soldering iron
  2. Screwdriver(s)
  3. 8mm drill bit (for break-beam sensor LEDs) - a step drill bit is ideal but a normal drill bit is OK
  4. 4mm drill bit (for solenoid mounting holes)
  5. 2.5mm drill bit (for M3 taps)
  6. 1.5mm drill bit (for M2 taps)
  7. M2 tap
  8. M3 tap

Supporting equipment:

  1. Laptop PC running Windows, Linux or MacOS running 'Thonny' or similar (to program the Pi Pico).

Overview

Completed 1 PXL_20241115_200442051.jpg
Completed side view PXL_20241115_200434239.jpg

During a recent mouse incursion in my attic, I deployed a number of 'Little Nipper' kill-traps and a couple of humane, live capture mousetraps. I also set up an infra-red camera to see if I could work out where the mice were coming in. Although I failed that objective, what the camera did reveal was the mice at first appeared to mostly ignore the humane traps and went straight for the kill-traps - presumably because the bait was in plain view. However, it didn't take too many kills before the remaining mice grew wary of those and started to visit the humane traps. Most turned tail at the trigger mechanism, which was a short ramp connected to a door release mechanism and went off in search of food elsewhere. Accordingly, I came up with this design to remove all barriers between the mouse and the bait. I deployed two traps, which over a couple of days successfully caught the last five mice. Subsequently, one of the traps caught two queen wasps - an unusual feat for a mousetrap!

The design uses a widely-available humane mouse trap; a Raspberry Pi Pico W (or 2 W), which has a WiFi capability; a 5 volt solenoid; a MOSFET solenoid driver; a three 'AA' cell battery box; and an infra-red beam sensor pair. The components are all mounted on the trap itself and the wiring is soldered. You can of course opt to use a breadboard off the trap but reliability will undoubtedly suffer in this case.

Three 'AA' cells are used to power the solenoid and a separate micro USB power supply is used to power the Pi Pico W. The trap could be run completely on batteries for ease of deployment; my rough calculations suggest that if run off four 'D' cells, the cells would have to be replaced every three weeks or so. My mouse-bothered attic has power, so I've stuck with a USB power supply version. If you are going to use batteries you'll need an additional voltage regulator to reduce the voltage to 5v.

When using a micro USB power supply, the separation of solenoid and Pico power is a precautionary design choice to avoid the possibility of upsetting the Pico when the solenoid fires - the solenoid takes around 1 Amp for the brief period it's on; the resulting voltage drop could potentially reset the Pico if a cheap power supply is used. This is avoided when powering the solenoid via a separate battery supply (which will last for months because it's only being discharged when the solenoid fires, and then only for a fifth of a second or so).

The code is available to download on my Github site (link below) with (hopefully) sufficient comments to aid understanding.

No familiarity with the Raspberry Pi Pico W is assumed; I've therefore provided links to the very helpful Pi Pico getting started pages, which are what I used myself to help put this project together.

Some of the soldering required is a little fiddly, especially on the Pico as the pads and their spacing is small. I opted to use a small piece of perf board to expand the Pico's 3.3 volt output as it's only on a single pad. The perf board also mounts a resistive divider and two protection diodes to permit the Pico to measure the battery voltage.

For ease of maintenance/possible reuse I mounted the various components on the trap body using a selection of M2 and M3 screws and spacers. This requires appropriately sized drill bits and taps (annotated above). However, there's nothing stopping you using hot melt glue - it sticks very well (a little too well, I think) to the plastic of the case - just be sparing with it.

The only other thing to note is concerning the solenoid and the sprung door. By trial and error I found the solenoid can be positioned so when the sprung door is pushed open it's latched by the unpowered solenoid. This makes resetting the trap just a matter of pushing the door open until it latches and then cycling power to the Pico.

How It Works

The break-beam sensor pair comprises a low power infra-red LED and an infra-red sensitive detector. The pair is positioned opposite each other across the mouse trap and with nothing in the trap, the light from the LED is seen by the detector. If something breaks the light beam, the detector notices this and sends a signal to the Pico. Note that infra-red light is invisible to people (and mice), so you won't see any light from it when it's powered up.

The smart mouse trap uses a solenoid to release the trap's door when movement is detected by the break-beam sensor pair. A solenoid is a coil of wire with a metal shaft running through it. When an electric current is applied, the shaft is pulled back by the magnetic field generated by the coil. When the current is removed, the shaft is returned to its starting position by a spring.

The MOSFET driver is effectively an amplifier which converts the low power output from the Pico to a high power drive to operate the solenoid, which requires far more current than the Pico can provide.

The perf board is used to mount battery voltage measurement resistors, protection diodes and also to provide a convenient junction point to connect the 3.3 volt lines together and 0 volt lines together.

  1. The resistors form a small circuit which reduces the battery voltage to a level which the Pico can accept.
  2. This is roughly 4.5 volts to 2.6 volts.
  3. The diodes provide further protection to the Pico.

The Pi Pico W microcontroller is the 'brains' of the mouse trap:

  1. It waits for a break-beam signal caused by a mouse.
  2. It then sends a signal to the MOSFET driver which in turn fires the solenoid, closing the door of the trap.
  3. It sends an email to alert you the trap needs inspecting.
  4. It also sends a routine, daily email telling you it's functional.

Prepare the Mouse Trap

Ready for disassembly.jpg
Levering off mousetrap base.jpg
Mousetrap disassembled.jpg

The first job is to disassemble the mouse trap to remove the parts you don't need and to also provide access to mount the solenoid. All the other components are mounted on the outside of the case and these can be installed with the trap reassembled.

Remove the base of the trap. I used a wide-bladed screwdriver to gently lever each of the four clips in the base whilst easing the base away from the rest of the trap. The first clip is the hardest but once that's free, the others should be easier.

With the base off, the ramp and spring can be removed by simply lifting them away from the housing. Also remove the the bait access door. Put this door and the base to one side - you'll need them on reassembly.

Prepare and Mount the Electronic Trigger Release

Solenoid positioning side view.jpg
Measure solenoid gap vertical view.jpg
Measure solenoid gap side view.jpg
Solenoid positioning plan view.jpg
Solenoid glue surface.jpg
Glueing.jpg
Fitted solenoid view from underneath.jpg
Solenoid PXL_20241107_174456947.jpg
Solenoid PXL_20241107_174431507.jpg

In this and following instructions I'm assuming you'll mount the various parts using screws and spacers. Hot glue is an alternative but be sparing as it sticks very well!

In this step we'll mount the solenoid so it holds the door open when the mousetrap is unpowered and releases it when commanded.

  1. Hold the trap door open with the shaft of the solenoid and measure the gap between the base of the solenoid and the underside of the trap roof. The solenoid is mounted with the small holes in its base nearest the roof of the trap. In my case this dimension was 5mm.
  2. Make a small packing piece approximately 30mm wide by 30mm long and as thick as your measurement plus approximately 2mm, as there's no latitude if the packing piece is too thin.
  3. Drill a 4mm mounting hole about 5mm in from each corner.
  4. After checking the piece is thick enough with a trial fit, glue the solenoid onto the packing piece with epoxy resin (see photo). Note the side to glue is that with the two small holes in.
  5. Once the glue has cured, position the solenoid assembly as shown in the diagrams - a little bit of sideways adjustment will probably be required to ensure the mounting holes avoid the holes in the roof of the trap and to ensure the solenoid shaft will permit the door to be opened and retained by the shaft.
  6. Once you're happy with the position, mark the mounting holes.
  7. Drill each mounting hole with the 2.5mm drill and cut a thread with the M3 tap.
  8. Fix the solenoid assembly to the roof of the trap with four of the 10mm M3 screws and washers.
  9. Check for correct latching operation by opening the trap door and ensuring the solenoid allows the door to pass and then hold it in its open position.
  10. Loosen the screws and adjust if necessary.
  11. The solenoid wires are routed back up to the top of the trap through one of the ventilation holes in the trap roof.

Install the 'Brain' and Sensors

Unwired side view with Pico.jpg
MOSFET driver.jpg
Break-beam holes.jpg
Break-beam sensor - receiver.jpg
Perf board.jpg

This step installs the Pi Pico W (or Pi Pico 2 W) microcontroller (the 'brains'), the MOSFET driver, the perf board and the break-beam sensor pair.

The photographs show a suggested mounting arrangement.

Pi Pico W (or Pi Pico 2 W) microcontroller:

  1. Position the Pico vertically on the trap housing, with the USB power connector upwards as shown in the photograph.
  2. Mark the centres of each mounting hole.
  3. Put the Pico to one side and carefully drill a 1.5mm hole at each of these centres.
  4. Cut an M2 thread in each of these holes.
  5. Mount the Pico with the 10mm M2 screws, washers and 3mm spacers.

MOSFET driver:

  1. Cut the link on the MOSFET driver board as shown (prevents battery drain).
  2. Position the MOSFET driver on the trap housing as shown.
  3. Mark the centres of each mounting hole.
  4. Put the MOSFET driver to one side and carefully drill a 1.5mm hole at each of these centres.
  5. Cut an M2 thread in each of these holes.
  6. Mount the MOSFET driver with the 10mm M2 screws, washers and 3mm spacers.

Break-beam sensor:

  1. The break-beam sensor pair is positioned roughly 11cm from the trap opening and around 1.5cm above the trap floor; one on each side of the trap housing.
  2. Drill an 8mm hole for each LED and a 2.5mm hole for each securing screw as shown in the photograph. Use a step drill for the 8mm hole if you've got one - they don't grab like a normal drill bit does.
  3. Cut an M3 thread in each of the screw holes.
  4. Mount each break-beam sensor with a 10mm M3 screw and washer as shown.

Perf board:

  1. Cut a small piece of perf board approximately 22mm wide and 17mm long as shown in the picture.
  2. Drill two 2.5mm mounting holes as shown in the picture.
  3. Position the board on the trap housing as shown.
  4. Mark the centres of the two mounting holes.
  5. Put the board to one side and carefully drill a 1.5mm hole at each of these centres.
  6. Cut an M2 thread in each of these holes.
  7. Mount the perf board with the 10mm M2 screws, washers and 3mm spacers.
  8. Unmount the board, ready for component installation.

This is a good point in the project to reattach the base of the mouse trap - offer it up to the housing, ensuring the bait area is at the rear of the trap and press firmly together.

The battery box sits on the top of the mousetrap, held in place by hook and loop tape. This will be installed in the next step.

Wiring

Wiring diagram.jpg
Connection table.jpg
Completed side view PXL_20241115_200422308.jpg
Wired trap #1.jpg
Completed angled view PXL_20241115_200428261.jpg
Perf board top view.jpg
perf board underside.jpg
Battery box wiring #2.jpg
Battery box wiring #1.jpg

The wiring diagram and the wiring table show how the trap is wired.

  1. The solder pads on the Pico are small and close together, so be careful not to bridge any of them.
  2. Use the thicker wire (20s gauge or equivalent) for the wiring between the battery box, MOSFET driver and the solenoid.
  3. Snip the small connector off the solenoid wires and extend the wiring to the MOSFET driver board, using heat shrink insulation as appropriate.
  4. The wiring from the MOSFET driver to the Pico should be the thinner 22s gauge or equivalent.
  5. Check the gauge of the wiring used in the battery box. The wiring in mine was very thin and I rewired it with the same gauge of wire I used for the solenoid circuit (see pictures). This is necessary because thin wires drop too much voltage for the solenoid to fire reliably.
  6. The wiring links between ground and GPIO 17 & 18 is only necessary if you've more than one trap, as the software uses these inputs to determine the trap ID. You can omit them otherwise.
  7. On the perf board, solder the resistors, diodes and solder posts as shown in the pictures. Note no tracks have to be cut.
  8. Use heat shrink sleeve to keep the wiring tidy. You can leave it unshrunk if you'd prefer.
  9. Once wired, attach the battery box to the top of the trap housing with the adhesive hook & loop tape as shown.

Software & Configuration

To send emails, you'll need a second Gmail account and a password:

  1. Set up a new Gmail account if you haven't already got a second account you can use.
  2. Click on the new account's profile picture and select 'Manage your Google account'
  3. Select 'Security' on the left hand side of the page.
  4. In the 'Search Google account' box at the top of the window, enter 'App passwords' and click on the 'App passwords' result.
  5. In the box headed 'To create a new app-specific password, type a name for it below…' enter 'Mousetrap' or something similar, and click on 'Create'.
  6. Make a note of the resulting password.

The above is correct as of August 2025.

The software is available on my Github site - the link to download it is here. In the window which opens, click on the 'download raw file' option to download the code. Save it on your PC.

Note the software is written for two versions of the mousetrap; a dual version, which uses two solenoids and two beam break sensor pairs and the single version (as described here) which only uses one of each. The software works perfectly well with both versions.

The only other software you'll need is a Micro Python email client. The software assumes the use of this email client - as above, in the window which opens, click on the 'download raw file' option to download the code. Save the umail.py file to the same folder on your PC as mousetrap-i_v1.0.py.

Now we need to initialise your Pi Pico and transfer the above software:

I've only just started with Pi Pico devices and MicroPython. To get things going with the Pico, I'd suggest using this Raspberry Pi Foundation 'getting started' guide which I found very helpful:

  1. https://projects.raspberrypi.org/en/projects/get-started-pico-w

The guide takes you through installing the necessary firmware on the Pico, installing Thonny on your PC, connecting to a WiFi network and more. For this project, you can stop when you reach the guide's WiFi section.

Having completed initialising your Pico:

  1. Open Thonny on your PC.
  2. Connect your Pico to your PC with a USB cable capable of data transfer.
  3. Thonny should find your Pico and connect to it.
  4. In Thonny:
  5. Click on the 'view' menu, select 'Files', navigate to where you've saved umail.py and double click to open it.
  6. Click 'Save as' and select 'Raspberry Pi Pico' in the pop-up window.
  7. Enter umail.py as the file name.
  8. Repeat the above for mousetrap-i_v1.0.py

With mousetrap-i_v1.0.py in Thonny's edit window, change the following to tailor the code to your setup:

SSID = 'your SSID' # your WiFi network name
PASSWORD = 'your WiFi password' # the password of your WiFi network
SENDER_EMAIL = 'sender@gmail.com' # the new gmail address you set up above
SENDER_NAME = 'Sender name' # this can be anything you choose (eg) m. trap
SENDER_APP_PASSWORD = 'xxxx xxxx xxxx xxxx' # the app password noted above
RECIPIENT_EMAIL = 'recipient@gmail.com' # your main email address
  1. Once finished, click on 'Save'.

Testing and Commissioning

  1. Connect your Pico to your PC with a USB cable capable of data transfer.
  2. Load three 'AA' cells into the battery box and switch on the battery (if the battery box has a switch).
  3. In Thonny, select the 'mousetrap-i_v1.0.py' tab and press F5 to start the script.
  4. You should get a response similar to that below:
MPY: soft reboot
Attempt 1 to connect to the WiFi network.
Connection successful
('Pico's IP address', 'subnet mask', 'network gateway address', 'DNS server address')
Successfully connected with IP: 'Pico's IP address'
Time synchronization successful. Current time: 26/08/25 19:53:33
Mousetrap-I_v1.0: Starting Mousetrap 1 with battery calibration: 5.0 and loop cycles: 35751
  1. This confirms you've set up your SSID and password correctly.
  2. Check your email. You should have received a start up email similar to this:
Mousetrap 1 @ 'Pico's IP address' startup message

26/08/25 18:53:33

Mousetrap-I_v1.0: Mousetrap 1 has started in mousetrap mode and is waiting for a mouse. Solenoid battery voltage is 4.39 volts
  1. This confirms your email details (sender and recipient) are correct.
  2. If you get a message saying the mousetrap has started in 'mouse activity' mode, it means the trap hasn't detected the batteries in the battery box.

Having confirmed correct WiFi & email settings, and the batteries are present, you now need to check the trap works as it should.

  1. With the trap door held open by the solenoid, insert an object into the trap in between the break-beam sensors. Avoid bright lights/sunshine on the trap as this can prevent the break-beam sensor working properly.
  2. The trap door should close and you should receive an email similar to that below:
beam 1 of Mousetrap 1 @ 'Pico's IP address' has tripped

26/08/25 19:42:50

A 60 second count has started to detect further movement.
  1. Within the next 60 seconds, break the detector beam several more times.
  2. After that interval, you should receive an email similar to this:
Mousetrap 1 @ 'Pico's IP address' has probably got a mouse!

26/08/25 19:43:53

Mousetrap 1 is now in wait mode - additional movement detected.
  1. To reset the trap, press 'Stop' on Thonny.
  2. If you repeat the above but don't break the detector beam after the trap has triggered, you'll get this message:
Mousetrap 1 @ 'Pico's IP address' - It's probably not a mouse

26/08/25 19:50:17

Mousetrap 1 is now in wait mode - no additional movement detected.

If you've used different resistor values to those specified above for the battery measurement circuit, or you're just a stickler for accuracy, the voltage measured by the trap should be calibrated:

  1. Measure the voltage output by the battery box - a convenient measurement point is at the MOSFET driver terminals - call this Vbat.
  2. If you don't have a meter, assume Vbat is 4.5 volts if new cells are being used (accuracy won't suffer significantly).
  3. Start the trap software on Thonny.
  4. Note the voltage stated in the start up email - call this Vdisp.
  5. Calculate the calibration factor: Cal = (Vbat * 5)/Vdisp
  6. Stop the code execution in Thonny.
  7. Edit line 49 of the code to substitute '5.0' with the calibration factor calculated above (ie)
'Mousetrap 1': {'battery_cal': 5.0, 'loop_cycles_1h': 35751},
  1. Select 'Save' in Thonny to save your change.
  2. My testing suggests the battery voltage can fall to around 4.1 volts* before the solenoid won't reliably close the trap door (*this will be less if you've used thick wiring)

Once you're happy with the trap's operation:

  1. On Thonny, select the mousetrap-i_v1.0.py window, click on 'Save copy', select Raspberry Pi Pico as the destination and enter 'main.py' as the filename.
  2. Disconnect the cable to your PC.

Any file named 'main.py' is run automatically when power is applied to the Pi Pico, so this ensures the code will run without attaching it to a PC.

Operation & Use

Caught mouse.jpg

To run the trap:

  1. Connect the smart trap Pico to a USB power supply.

Note that unlike the case with Raspberry Pi computers, you can simply pull the power to the smart trap's Pi Pico microcontroller with no risk of corrupting the stored code.

If you receive the trap triggered messages once you've deployed the trap, you'll need to inspect it to confirm the presence or otherwise of a mouse and also to reset the trap.

  1. Transfer the mouse into a spare trap if necessary (see below).
  2. Rebait the trap if necessary.
  3. Recycle USB power to the Pico.
  4. Re-latch the trap door.

Humane mouse traps are often sold in pairs, so I recommend you reserve an unmodified trap to act as a transport container for any mice you catch. Just set your unmodified trap, offer it up to the opening of your smart trap and open the latter's door (it's easier than it sounds). I've found mice caught in the smart trap are only too willing to bolt out of it, even if that's into another trap. The receiving trap will trigger and keep the mouse secure until you're able to release it. This use of a 'carrier' trap reduces wear and tear on your smart mouse trap and keeps it available for further mice.

Don't forget to take the smart trap out of service before going away - either that or switch the battery off and leave it in mouse-counting mode. At least in the latter configuration it will warn you of the presence of mice which can be dealt with when you return.

Variations - a Dual Trap

Dual trap.jpg

A Pi Pico can easily handle two pairs of break-beam sensors and two solenoids, so you can save a little money by building a dual trap.

GPIO 14 (pin 19) is the second break beam sensor input whilst GPIO 13 (pin 17) is the second solenoid output. The software can be left 'as is'.