Mouse early warning system


Detecting mouse invasion with a Raspberry Pi
The finished mouse detector using a Raspberry Pi

The mouse detection system

In this project, I’ve set up a PIR motion sensor connected to a Raspberry Pi to act as an early warning mouse detecter system.

When movement is detected a text message alert is sent to my mobile phone, telling me to get the mouse traps out!

The Mouse Problem

Living in the countryside, in a stone-built house mouse infestation can be a problem. The biggest issue is that by the time we know that they are in the house, for example, we see one or food in the larder starts to get nibbled, we’re infested.

We find that mouse invasion occurs at various times throughout the year. Having traps permanently in place means checking regularly to see if they have caught anything. As these are in the roofspace (where the mice tend to arrive first), it’s not easy and so doesn’t get done.


Required Parts

  • Raspberry Pi
  • HC-SR501 PIR Motion sensor
  • Plastic electrical cable connector box
  • Length of 4 core telephone cable
  • Dupont connectors

The PIR Motion Sensor


Building the box

Using a 22mm wood drill bit, make a hole in the lid of the plastic cable box.

Although the diameter of the motion sensor dome is 24mm, I’ve found that the 22mm drill makes a bigger hole when drilling plastic. Once cleaned up, it’s a perfect fit.

Drilling the hole for the mouse detector motion sensor

Assemble the motion sensor box

Make up the cable to connect the sensor box to the Raspberry Pi. I am connecting to an existing Pi which is around 3 meters away.

Cut 3 female/female Dupont wires in half, and solder to each end of the length of cable (making sure the colors correspond at each end). Protect from short circuits with heat shrink or electrical tape.

Using a hot glue gun (or just glue), fix the infrared sensor to the lid. Pass one end of the cable through the side hole, and connect to the sensor.

making the cable for the mouse detector

Make the cable

Using hot glue to attach the mouse detector motion sensor

Hot glue the PIR sensor

Wiring the mouse detector motion sensor

Connecting Up

The finished mouse detector motion sensor box

Finished unit

The finished mouse detector motion sensor box

The motion sensor screwed to a rafter in the loft

Raspberry Pi Set-up


Connecting and coding

Connect the sensor to the Raspberry Pi

  • VCC to 5v (pin 2 or 4)

  • GRD to GRD (pin 6 or 9)

  • OUT to GPIO4 (pin 7)

After connection, open your preferred python editor and copy the following code block. Enter your connection details for user and pass, and save the file.

I use Free Mobile and so connect to the Free SMS API to send text messages. I would think most other mobile networks have a similar service.

#!/usr/bin/python3
import urllib.request
from gpiozero import MotionSensor

pir_mice = MotionSensor(4)
url = "https://smsapi.free-mobile.fr/sendmsg?user=********&pass=********&msg=Mouse+Detected"

while True:
    pir_mice.wait_for_motion()
    print("Mouse!")
    urllib.request.urlopen(url)
    pir_mice.wait_for_no_motion()

And that’s it, just run the python script in Python Shell and wait for a mouse alert on your phone.

I’ve been using this early warning system for a couple of years now, and with it, we have been able to deal with the mice as soon as they start to move in.

I am now working on a portable version using a esp8266 board, to use in outbuildings.