Introduction: Cheap Wireless Mousetrap Alarm Using an ATtiny85
A couple of weeks ago I made the annual expedition up to our attic to fetch the Chrismas decorations. As I was passing the boxes down to my wife I realised we had some uninvited lodgers, evident by their droppings which were strewn all over the place. The next day I went to the hardware store to buy a couple of mousetraps.
However, it is a bit of a hassle to get up to our attic, and therefore checking the traps every day would be a chore. I needed something to alert me whenever the traps sprung to avoid unnecessary trips to the attic. Whilst I was at the hardware store I noticed a cheap wireless doorbell with a receiver that plugged in to the mains and a battery operated transmitter - only £6! So my initial instinct was to somehow connect the mousetrap to the transmitter so that the button would be pressed when the trap springs.
After trying out some configurations (and getting my fingers smashed) I could not figure out a reliable way to physically join the button to the trap. So I needed a better solution.
The traditional wooden base mousetraps I bought can be wired like an electrical switch - connect one wire to the retaining pin and the other to the spring. So I thought about simply connecting the doorbell to the trap electrically. However, when the trap is set the switch is closed and when the trap is sprung the switch is open. This is the wrong way round for this purpose. And anyway, the doorbell would sound continuously, which would not only be very annoying but would drain the battery very quickly. The electrical route would obviously need some additional circuitry to make it viable.
Eventually I designed and built a simple circuit based on an ATtiny85 microcontroller to monitor the traps and sound the alert when sprung. This Instructable involves some very basic soldering and programming an ATtiny85 which really is nothing to be scared of. You will not need to fully understand every step to make your own circuit, but I will try to be as informative as I can in the hope you might learn something, like I did, in the process.
Step 1: Tools and Materials
If you don't have any of the components to hand then EBay is your friend. Try to source as many of the components as possible from a single seller to save on postage.
- ATtiny85-20PU microcontroller (or ATtiny45).
- 8 pin DIP IC socket (cheap is good!).
- cr2032 coin cell battery.
- cr2032 pcb battery holder.
- Plug in wireless doorbell (cheapest in the shop!)
- 9 x 25 hole stripboard.
- 2n2222 npn transistor (x2).
- 10kΩ 1/4 Watt resistor.
- 330Ω 1/4 Watt resistors (x2 - optional).
- 100nF ceramic capacitor.
- 4 Pins 5.08mm Pitch Pluggable Terminal Block (optional)
- A suitable length of thin insulated wire.
- Some bare jumper wire.
- Soldering iron - no need for a fancy one!
- Stripboard track cutting tool - or a general purpose 4mm-ish drill bit
- 1.5mm drill bit - to widen the holes on the board to accommodate the screw terminals (optional).
- Archimedes drill to hold the above bit (optional)
- Digital multimeter (optional).
- Wire stripper (or utility knife).
- Miniature pliers/wire cutter
- Some means of programming an ATtiny85 - e.g. an Arduino (see this great Instructable).
Step 2: Wiring the Traps
The traps I bought were 'Little Nipper' type wooden ones but I'm sure you could connect any type of wooden mousetraps in a similar fashion.
Cut two lengths of your thin insulated wire, I would suggest about 400mm pieces, and strip the ends. Insert one end of one wire between the tine of the spring and the wooden base and wind one end of the other wire around the retaining pin staple. The two trailing leads could be inserted into a screw terminal plug (convenient), or left bare for soldering onto the board.
Connect multiple traps in series, connecting the tine from the first trap to the staple of the next, and so on.
Make sure the wires don't interfere with the operation of the trap. You could staple or tape the wires to the wooden base if you like.
Step 3: Schematic
Here is the schematic which I made using Fritzing. I include it here just for completeness.
J1 is the mousetrap circuit - the microcontroller will check this circuit every 10 minutes or so to see whether it is open (trap sprung) or closed (trap still set).
J2 is the doorbell transmitter circuit - if the microcontroller detects a sprung trap it will switch on this circuit for about 250 milliseconds to ring the doorbell.
Q1 is a 2n2222 npn transistor - this acts as a switch to turn on the doorbell transmitter circuit if a trap has sprung. Note that the negative wire from the 12V doorbell transmitter is connected to the ground of the 3V microcontroller circuit. But don't worry, your ATtiny85 won't see any of that 12V.
Q2 is a 2n2222 npn transistor - acts as a switch to turn on, or enable, the mousetrap circuit. The microcontroller will only enable the circuit for a fraction of a second every ten minutes. This is a battery saving measure.
R1 is a pulldown resistor which supplies a LOW value (i.e. negative, or ground) to pin 6 of the microcontroller. A positive signal from the mousetrap circuit will have a much lower resistance and would therefore swamp the weak LOW from R1. However if the mousetrap circuit is open, i.e. no signal, then the microcontroller would read the pin as LOW. Leaving pins not connected to neither HIGH (positive) nor LOW (ground) confuses the ATtiny85!
Step 4: Making the Circuit Board
You could design a fancy printed circuit board for this project if you like, but this simple circuit fits very well on a standard 9 x 25 hole stripboard. If you've never soldered before don't worry, there are plenty of excellent tutorials on the Web - try this one from Sparkfun.
Some of the better stripboards are supplied smeared with flux ready for soldering. However I have had mixed results in the past so I always clean my boards to a perfect polished shine with isopropanol before I start.
Cut the Tracks
Use the track cutting tool or a sharp drill bit between your finger and thumb to cut the tracks as indicated in the diagram. You only need to cut through the copper layer, do not drill right through the board!
Widen the holes
If you are going to solder on a terminal block (optional) you might need to widen the holes to accommodate it. Use a 1.2 to 1.5mm bit in an Archimedes drill and work from the copper side to avoid delamination. I use a piece of double sided adhesive tape to hold down the piece onto a scrap piece of wood for this.
First prepare the jumpers. I've used bare tinned copper wire (22 swg) for this purpose. First cut a suitable length of wire and use pliers to put a 90 degree bend close to one end. Then carefully bend the other end to make a staple-shaped piece. Try to make them so they lie flush against the board when they are inserted. Dip the ends in flux before you solder them in place.
I suggest you solder the components in the order indicated in the diagram. Note the orientation of the transistors and the IC socket, and make sure the battery holder is empty before you solder it.
Step 5: Hacking the Transmitter
'Hacking' is probably too grand a word for this step which merely involves soldering on two bits of wire. If you bought the cheapest wireless doorbell you could find, I would bet that the transmitter pcb looks exactly like the one I have!
Most types, if not all, will have a momentary pushbutton similar to the one in the photo. One pin is soldered directly to the positive battery clip of the pcb - note the big blob of solder in the photo. Turn on your iron, get one piece of insulated wire (about 10cm) and strip a little off one end. Dip this end in flux and touch it with a tiny blob of molten solder to tin it. Solder this onto the big solder blob on the pcb taking care not to dislodge the battery clip whilst the solder is molten. Get another piece of wire and repeat for the other connection (the red arrow in the photo). Don't strip too much insulation and don't be too generous with your solder so as to avoid unintentional shorts.
If your transmitter is a different model to the one in the photo then insert the battery and plug in your receiver. You can find out which two pins you should solder your wires to by shorting the button pins with a spare piece of jumper wire until the bell rings.
Step 6: The Code
Download the attached .ino file and use your favoured method to compile and upload to your ATtiny85. My favoured method is to use an Arduino UNO as a programmer as described here. Set your ATtiny to run at 1MHz before uploading if possible. If you just want to get on with it then you have my permission to skip to the next step, but I hope you will find the following bit informative.
This code takes advantage of the ATtiny85's sleep mode to conserve battery power. It goes something like this:
Wake up >> Switch mousetrap circuit on >> Check whether mousetrap circuit is open >> If open, switch on transmitter circuit for 250 milliseconds >> Switch mousetrap circuit off >> Go to sleep for 10 minutes... etc.
When in sleep mode, the microcontroller uses a tiny tiny amount of power. In theory the little cr2032 battery could keep it going for months, or even years. Since it only wakes up for a fraction of a second every ten minutes or so, power requirement is kept low. If you're really crazy about getting the most out of your battery, I can see at least one place where you could maybe add a resistor to the circuit and there are some tweaks you could do to the code - but I'll let you figure it out. Please comment if you do!
So how does the ATtiny know when it's time to wake up? The code sets up something called a 'Watchdog Timer' (WDT) to trigger an interrupt when it times out (or overflows). By default, a WDT overflow will cause the ATtiny to reset if the program gets stuck for some reason, but we don't want this to happen here. Luckily, this functionality can be disabled and the WDT can be set up just to fire an interrupt.
The attached code is mostly a pretty standard Arduino sketch. If you're an Arduinohead like I am, parts of this code may border on some unfamiliar territory. It involves some bit math (see this fantastic introduction) with reference to the ATtinyx5 datasheet. The program manipulates the Watchdog Timer Control Register of the ATtiny85 - see Section 8.5.2 of the datasheet for details. This register is an eight bit chunk of memory in the chip which controls what the watchdog timer does. Each bit of this register has a certain meaning for the microcontroller - they turn certain functionalities on or off depending on whether the bit is a 1 or a 0. These bits are handled in the code by a little bit of C bit math magic!
WDTCR |= (1<<WDCE | 1<<WDE)
WDTCR refers to the Watchdog Timer Control Register, lets say for the sake of this example it is initially 00000001.
The |= is the compound bitwise OR operator in C - it is just a shorthand way of denoting an OR operation on a group of bits (8 bits in the case of WDTCR).
<< is a bitwise left shift operator. A quick look at Section 8.5.2 of the datasheet will show that the WDCE (Watchdog Change Enable) bit of the WDTCR register is the fifth one from the right. WDCE is defined as 4, therefore 1<<4 would shift a 1 value from right to left (starting at position 0) to give 00010000. WDE is defined as 3 which gives 00001000. | is the C notation for bitwise OR. So (1<<WDCE | 1<<WDE) is (00010000 | 00001000) which gives 00011000.
Therefore 00000001 |= 00011000 will give 00011001. Phew!
The ISR(WDT_vect) function is the Interrupt Service Routine that is called each time the watchdog timer causes an interrupt. The WDT_vect identifies the watchdog timer's interrupt vector. The maximum timeout the WDT could be set at is about 8 seconds. Because of this the ISR increments a counter and the microcontroller will only check the mousetrap circuit when the counter hits 75, which would be about 10 minutes.
Step 7: Putting It All Together
It is important that the wires from the transmitter is connected to your board the right way round - see the diagram. I find pluggable screw terminals very handy, but if you haven't got any just solder the wires directly to your board. Insert the ATtiny into its socket (with the dot on the ATtiny the same side as the notch on the socket), pop in the batteries, plug in the receiver and test. Unless you don't mind sitting around for 10 minutes to wait for the alarm, you could alter the code to make the timeout shorter for testing purposes. Just remember to set it back to 10 minutes before deploying.
Step 8: Deploy
Mind your fingers!.. Ouch!
I was very pleased with this little gadget, it worked flawlessly and we caught two mice on the very first night. I measured the battery voltage at 2.7 volt before deployment and after more than two weeks it was still 2.7 volt, so I'm pretty sure this thing could be left for a very long time without a battery change.
Mini marshmallows make excellent bait by the way!
We have a be nice policy.
Please be positive and constructive.