Introduction: Better Smarter Mousetrap

Ralph Waldo Emerson once said "build a better mousetrap, and the world will beat a path to your door." Perhaps that’s what motivated Tom to keep trying to catch Jerry using ever more elaborate Rube Goldberg-esq traps that ultimately failed. Few things can be said to be inspired by both Ralph Waldo Emerson and the Tom & Jerry cartoon, but the better mousetrap is one of them. With a smattering of technology and peanut butter, we can make that better mousetrap a reality.

With winter fast upon us, I’ve already been victim to the annual rodent indoor migration. I happened across one of them in the basement while looking for something. Scared the crap out of me. Instead of waiting for signs of mice, I thought it might be a good idea to set up a semi-permanent trap.

Just because we don’t like having Mrs. Frisby in our house doesn’t mean we have to use a death trap. I wanted to build a trap that doesn’t kill. Imagine a semi-permanent trap that’s set out in basement, garage, or attic. I don’t want to have to go to the basement or attic to check the trap everyday. And I don’t want the little critters to die of starvation while they’re detained. So I need the trap to notify me via email the moment a mouse is captured. Since the traps are also located in dark places, I also want an LED as an easy line-of-sight indicator.

Interesting thing about mice is that they’re territorial. If you have a couple of mice in close quarters with no where to go, things might deteriorate into a battle royale. Could get pretty nasty, and defeats the goal of a humane trap. The best solution for catching multiple mice is to have more than one trap, and make each trap only let in a single mouse.

With those things in mind, these are my design goals for making a real smart mousetrap.

1) Easy to build, escape proof, live and let live
2) Notifies me the moment a mouse is caught, via email and audible notification
3) Trap only lets in one mouse at a time
4) Simple LED indicator to tell you if there's a mouse in the trap

First the video demo, then the steps for the build.

See next step for parts list.

Step 1: Parts List & Wiring

Parts List:

1. Spark Core ($39) or the Photon shipping in 2015 ($19)

2. Bucket ($5)

3. Large piece of cardboard

4. PIR Motion Sensor ($3, cheaper on ebay)

5. DHT11 Tempearture/Humidity Sensor ($2, cheaper on ebay)

6. Hobby Servo ($3)

7. Pieces of plastic or sheet metal, whatever you have laying around.

8. Resistors, 220 Ohm and 10k Ohm

9. Dupont male-female cables

Wire up the Spark Core breadboard circuit following this wiring diagram. Click to enlarge.

Your circuit should look something like this.

I included a temperature sensor in the circuit because they're so inexpensive.

Step 2: Assemble Trap Arm

Next, we need to assemble the trap arm using the servo. The trap arm is what moves the "occupied" door in and out of position, and prevents more than one mouse in the trap. I'll show you the end result of this step, and then detail how I made it. You may take liberties with the supplies you have available.

This video is looking at the underside of the card board top. You can see how the arm moves the door into position, and latches it in place.

Cut the card bard into shape (with a hole big enough for a mouse to fall through). Cut some pieces for an arm and a door and attach to the arm of the servo. For the parts, I used left over vinyl siding material. You can use sheet metal or any other flat, stiff material that you can cut to shape. It should look something like the following series of pictures.

Cut a hole roughly the shape of the servo motor. Poke the servo through the card board. The servo should rest on the extra plastic protrusions and not fall through.

Create a sort of slanted latch for the door, so that the door gets "locked" into place and the latch helps hold it in, in case mice from above want to get in.

Step 3: Assemble Motion Sensor

Cut a hole on top of the card board just big enough to poke the motion sensor's globe through. This is what will tell us when a mouse falls into the trap.

Step 4: Programming

There are two software pieces to program. The Spark Core uses MQTT to talk to the home automation server called OpenHAB. The MQTT broker I'm using is called Mosquitto. Aside from programming the Spark Core, you'll have to decide where OpenHAB and Mosquitto will be installed. They can be installed on a Raspberry Pi (Linux) or a Windows or Mac PC. It's up to you, and it really doesn't make much of a difference other than power consumption. The configuration is the same.

Download the attached Spark Core program to your Spark Core. If you need help, follow the Spark Core's getting-started-guide.

Install Mosquitto using this download link.

Install OpenHAB using this wiki. Familiarize yourself with the configuration files like items, sitemap, and rules. Follow the OpenHAB configuration to set up the OpenHAB interface.


Switch itm_mousetrap_basement_trap_sta "Basement Trap Status" <mymouse>
DateTime itm_mousetrap_basement_time "Trap Time [%1$tA, %1$tm/%1$td, %1$tI:%1$tM %1$tp]"
DateTime itm_mousetrap_heartbeat "Heartbeat Time [%1$tA, %1$tm/%1$td, %1$tI:%1$tM %1$tp]"
Number itm_mousetrap_basement_temp "Temp [%.1f °F]" <temperature> (All) {mqtt="<[mymosquitto:mouse_basement_temp:state:default]"}
Switch itm_mousetrap_basement_be_lonely "Lonely Mouse Mode"  {mqtt=">[mymosquitto:9996:command:ON:1],>[mymosquitto:9995:command:OFF:0]"}
Number itm_mousetrap_basement_trap_mqtt "trap mqtt" (ALL) {mqtt="<[mymosquitto:mouse_basement_trap_mouse:state:default]"}
Switch itm_mouse_basement_man_rst_mqtt "Mouse Basement mqtt ahhh" <blank> {mqtt=">[mymosquitto:9997:command:ON:1],>[mymosquitto:9997:command:OFF:0]"

Text label="Mouse Traps" icon="firstfloor"
	Frame label=""
		Switch item=itm_mousetrap_vacation
	Frame label="Basement Mouse Traps"
                Switch item=itm_mousetrap_basement_trap_sta mappings=[OFF="Reset"]
                Text item=itm_mousetrap_basement_time
                Text item=itm_mousetrap_basement_temp
                Switch item=itm_mousetrap_basement_be_lonely  
	} //Basement Mouse Traps
}//end frame

rule "Mouse Trap Basement Mouse Detected"
		Item itm_mousetrap_basement_trap_mqtt received update
		if (itm_mousetrap_basement_trap_mqtt.state == 1)
			sendCommand(itm_mousetrap_basement_trap_sta, ON)
			postUpdate(itm_mousetrap_basement_time, new DateTimeType())
			sendMail("", "subject mouse trap" , "a mouse in trap!")
			say("mouse in trap!")
		else	//reset
			sendCommand(itm_mousetrap_basement_trap_sta, OFF)
end //Mouse Trap Basement Mouse Detected

rule "Mouse Trap Basement Reset"
		Item itm_mousetrap_basement_trap_sta received update
		if (itm_mousetrap_basement_trap_sta.state == OFF)
			sendCommand(itm_mouse_basement_man_rst_mqtt, ON)
end //mouse trap basement reset

Open the openhab.cfg file, edit the email settings like this:

######################## Mail Action configuration ####################################
# The SMTP server hostname, e.g. ""

# the SMTP port to use (optional, defaults to 25 (resp. 587 for TLS))

# the username and password if the SMTP server requires authentication, no

# The email address to use for sending mails

# set to "true", if TLS should be used for the connection
# (optional, defaults to false)

That's it.

Step 5: Mouse

Here's a mouse that was caught with the trap. At the time, I didn't have the smarts built yet, but the trap itself works pretty well.

Step 6: More Than Just a Mouse Trap

This mousetrap project utilizes sensor inputs and actuator outputs on a wifi-connected Spark Core. It interfaces with a sophisticated home automation system called OpenHAB. The techniques used in this project can be easily ported over to other home automation projects. For example, this Home Automation project can be adapted to use Spark Core instead of pure Arduino.

The Spark Core can be used for various wifi-connected sensor and actuator nodes, all controlled and monitored via the OpenHAB app.


webzter30 made it! (author)2015-10-06

any ideas? am I doing something wrong in the idea with pasting the code?

webzter30 made it! (author)2015-10-05

this is what the IDE is saying.

In file included from ../hal/src/stm32f2xx/platform_headers.h:19:0,
from ./inc/application.h:32,
from mousetrap.cpp:2:
../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:80: warning: 'externally_visible' attribute ignored [-Wattributes]
#define retained __attribute__((externally_visible, section(".retained_user")))
mousetrap.cpp:312:86: note: in expansion of macro 'retained'
../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:80: error: section attribute not allowed for '<anonymous>'
#define retained __attribute__((externally_visible, section(".retained_user")))
mousetrap.cpp:312:86: note: in expansion of macro 'retained'
mousetrap.cpp: In member function 'bool PubSubClient::publish(char*, uint8_t*, unsigned int, bool)':
../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:19: error: expected primary-expression before '__attribute__'
#define retained __attribute__((externally_visible, section(".retained_user")))
mousetrap.cpp:322:11: note: in expansion of macro 'retained'
length = writeString(topic,buffer,length);
../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:19: error: expected ')' before '__attribute__'
#define retained __attribute__((externally_visible, section(".retained_user")))
mousetrap.cpp:322:11: note: in expansion of macro 'retained'
length = writeString(topic,buffer,length);
mousetrap.cpp: In function 'void callback(char*, byte*, unsigned int)':
mousetrap.cpp:476:10: warning: unused variable 'buff_topic' [-Wunused-variable]
DHT mydht(2, DHT11);
mousetrap.cpp:477:10: warning: unused variable 'buff_message' [-Wunused-variable]

mousetrap.cpp:484:10: warning: unused variable 'msg' [-Wunused-variable]

mousetrap.cpp: In function 'void loop()':
mousetrap.cpp:612:11: warning: unused variable 'h' [-Wunused-variable]

mousetrap.cpp:614:10: warning: unused variable 'debug_value' [-Wunused-variable]
//send temperature regularly
make[1]: *** [../build/target/user/platform-6mousetrap.o] Error 1
make: *** [user] Error 2


webzter30 made it! (author)2015-10-05

thanks so much for this post.

I am trying to get it to work.

could you please help me out. I am new to particle ide.

I get this error when I try to load your sketch to the proton

../hal/src/stm32f2xx/deepsleep_hal_impl.h:22:80: error: section attribute not allowed for ''
#define retained __attribute__((externally_visible, section(".retained_user")))

thanks so much for you help. I know this must be simple.

electronichamsters made it! (author)electronichamsters2015-10-05

I'm not familiar with that error. What's the line in your code throwing that error?

kpdavis made it! (author)2015-02-03

We are making good progress on our project (which is derived from this project). One issue we have is with the SPARK Core power on self-test. It seems to test the pins during startup. We have a relay connected to the pins and the startup fires the relay. This is not good since the relay actually does stuff in the real world we want to only do under program control. Can we tell the SPARK not to do some of the self-test diagnostics it regularly does?

kpdavis made it! (author)2015-01-22

A friend and I are trying to build this. We are having trouble getting OPENHAB to see the messages sent (published) to MQTT by the SPARC. So we think it may be something in the OPENHAB config. OPENHAB does see MQTT. But does not seem to see the SPARC messages. We added the SITEMAP keyword to the SITEMAP file, we the website now works. When we move the switches on the website, stuff shows up in the OPENHAB log. MQTT log is going to syslog and we can see it running. Is the only thing in the OPENHAB config, the email info?

thanks in advance,


electronichamsters made it! (author)electronichamsters2015-01-22

It's probably your openhab configuration. Check out this wiki:

kpdavis made it! (author)kpdavis2015-01-24

I did not have the MQTT and other bundles I needed. Also, the IMPORTs were missing from the RULES file. I saw them in the DEMO file and added them to your RULES example Working much better now.


Husky Runner made it! (author)2015-01-04

I hope that was a hard boiled egg! Brilliant stuff on this. I'm just starting to work through this. Just ordering parts and stuff, I'm pretty new to all this and just learning really so thanks very much for sharing your work!

midnightcow made it! (author)2014-11-26

Hi, Eric Tsai!

It's Amazing and quirky!!!

electronichamsters made it! (author)2014-11-25

I forgot the mention, the egg is a little warm from cooking. I think that helps to set off the motion sensor.

About This Instructable




Bio: I love home automation. I hate home automation.
More by electronichamsters:Gingerbread Home AutomationTweeting Pregnancy TestPhysical Home Automation Interface
Add instructable to: