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.
- Raspberry Pi
- HC-SR501 PIR Motion sensor
- Plastic electrical cable connector box
- Length of 4 core telephone cable
- Dupont connectors
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.
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.
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.