github icon rss icon

Humane Raspberry Pi Mouse Trap

August 30, 2019

Once in a while a mouse moves into my unfinished attic. Mice are disease-ridden breeding machines, but I would rather not torture or kill them. So, despite CDC recommendations, I use live traps and release them to a nearby wooded area.

Live trapping is a chore. You have to check the trap often lest they starve to death, so you only set traps after signs of rodent activity. By that point they have set up a breeding operation and started multiplying. Add a Raspberry Pi, a contact sensor, and gpio_httpd, and you can live trap 24/7, evicting them before they reproduce.


Do as I say, not as I do. Listen to the CDC, especially if you are pregnant, you have a weakened immune system, or you live west of the Mississippi. But if you still want to live trap then read on.


WARNING: affiliate links ahead

You can track several traps with one Pi. Buy more traps, more contact sensors, and more wire if necessary

Other tools and supplies

Prepare your SD card

  1. Download Raspbian Buster Lite
  2. Write the image to your SD card
  3. Set up wireless access and enable SSH
  4. Insert the SD card into your Pi

Assemble your trap

Refer to pinout.xyz for a pinout diagram. Insert the wires underneath the board so they exit the bottom of the Pi Zero case.

Solder more wires to GPIO pins to add traps. You need one GPIO pin per trap. Traps can share the same ground wire.

mouse trap soldered

Secure the magnetic contact sensor to the trap with zip ties, making sure you don’t zip tie the handle to the cage. The sensor will be separated when the trap is closed.

trap assembly 2

Strip the ends of your wires and connect them to the sensor using wire nuts. Use a wire spool and more wire nuts if your Pi will be located further away from the trap.

trap assembly 3

Log in to your Pi

  1. Power up your Pi
  2. Find the IP address of your Pi
  3. SSH into your Pi (Linux/macOS or Windows)
  4. Run passwd and replace the default password

Install Docker

Paste the following into your SSH session:

sudo -- sh -c "
  curl -fsSL https://get.docker.com | bash &&
  usermod -aG docker pi"

At the time of writing ARMv6 users (e.g. Pi Zero W) also need to downgrade containerd.

Finally sudo reboot, SSH back in, and docker run hello-world to test your Docker install.

Start a web server

Now we’re going to use gpio_httpd to read our GPIO pins remotely. Run the following command in your SSH session:

docker run \
  --name mousetrap \
  --device "/dev/gpiomem" \
  --publish "80:80" \
  --restart always \
  --detach \

Test out your trap by typing curl localhost/input/23. You should see 0 if the trap is open or 1 if its closed.

Set your trap

If you already have mice then bait your trap with peanut butter. Otherwise bait your trap with non-perishable food, like beef jerky, and if ants are a problem use seeds.

Unscrew the wire nuts to detach the trap from your Pi before transporting the critter to its new home. And make sure the mouse can run for cover so he doesn’t end up like Whiskers.

Check your trap

Anything that can check the trap and notify you will work. Some simplistic examples:

In an upcoming post I’ll show you how to set up Home Assistant to send an SMS when you catch a mouse or your trap goes offline.

Up Next: Building a Smart Garage Opener

comments powered by Disqus