Introduction: How to Build a Mouse Trap Using Arduino
Hello guys ! Hope you are having an awesome time ! Now the reason I am writing this Instructable is a long story. Well I will make it short. I had hid some of my favorite snacks in my cupboard to hide it from my sister. It was a pretty hard place for finding by any one. But I was wrong :( . A mouse which had a good sense of smell had got into my cupboard ate up all my favorite snacks. I told this thing to my mom but all I got was a long lecture since I had hid it from my sister. So I had to take things into my own hand. That is where I had this idea to build a mouse trap using Arduino and publish it to make sure you guys don't have to face my problem again.
Step 1: Stuff You Might Need
So we are going to arrange a grand party for the mouse(I mean a grand trap)isn't it ? These are the things you might need :
- Arduino
- Breadboard
- Jumper wire
- servo motor
- 330 ohm resistor x2
- ultrasonic sensor
- led x2
- buzzer
- double sided tape
Step 2: The Connections
Now let us make connections for the mouse trap. There are many things you could improve in this connection like adding a PIR motion sensor ( if the mouse is very cunning ! I have a really good experience about that). Now let me explain how the sensor works(yup, this is for science freaks, if you are not one skip to the fourth paragraph).
Let us start with an ultrasonic sensor. There is a trig pin on the ultrasonic sensor. It sends an ultrasonic sound(sound that has a frequency above 20000Hz ) to an obstacle. When the sound hits an objects it reflects the sound back and it falls on the eco pin. Then the sensor measures the amount of time take for the sounds to come back. then you can convert it into centimeters or inches using the code.
Now the led. An led is also known as a light emitting diode (learning this will be helpful if you are planning to sleep in your physics class) . Now the advantage of led over other lights is that it glows even in a low current and also lasts for a long time.
Now for the connection. You have to get two leds a green one and a red one(please feel free to take any other color). The positive terminal of the the green led (the long terminal)is connected to digital pin 6 in arduino. The red pin of the led is connected to digital pin 5. The pins of the ultrasonic sensor(trigpin and echopin) is connected to the the digital pins of arduino. The trig pin is connected to the digital pin 7 and the echo pin to pin 8. The positive terminal of the buzzer is connected to digital pin 10. Please refer the diagram for further explanation.
Step 3: The Bodywork
So are you ready to make that mouse a piece of history? So let us start mission "mouse out of house" ! Now to start with you need a big bottle(a cardboard box will also do).
STEP 1: Cut a hole in the bottle for the ultrasonic sensor(make sure you cut it in such an angle that when the ultrasonic sensor is mounted it can sense anything in the box).
STEP 2: Cut the bottom of the bottle. Now cut it carefully because you should not cut it completely. make sure you should leave some part of the bottom uncut(that is going to be the lock). Please refer picture 2 to be clear.
STEP 3: Cut a piece of cardboard to make a door. Make sure it is of the right length or else the mouse will escape.
STEP 4: Now using a double sided tape attach the servo to the end of the door.Then attach the door to the servo. Just give a trail to make sure the door will open and close smoothly(see picture 2).
STEP 5; Place the ultrasonic sensor in the hole (picture 4).
STEP 6: Now do the connections and you are ready to go !
Step 4: The Code:
Now for the coding. Well what this code does is that when a mouse gets inside the trap the servo rotates 180 degree thus closing the door. Then the green light turns off and the red light turns on. The buzzer is also activated. So now the code is written below:
#include <Servo.h>//include the servo library
int redpin = 5;//name digitalpin 5 as redpin(it is our red led)
int greenpin = 6;//name digital pin 6 as greenpin(it is our green led)
int trigpin = 7;//name pin 7 as trigpin
int ecopin = 8;//name pin 8 as ecopin
Servo servo1;//name our servo as servo1
int distance, duration;//make two variables named distance and duration
int buzzer = 10;//name pin 10 as buzzer
void setup() {
// put your setup code here, to run once:
pinMode(redpin, OUTPUT);//set redpin as output
pinMode(greenpin, OUTPUT);//set greenpin as output
pinMode(trigpin, OUTPUT);//set the trigpin of ultrasonic sensor as output
pinMode(buzzer, OUTPUT);//set the buzzer as output
pinMode(ecopin, INPUT);//set the echopin of ultrasonic sensor as input
servo1.attach(2);//attach the servo to pin 2
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trigpin, LOW);//activate the trigpin
delayMicroseconds(2000);//activate the trigpin
digitalWrite(trigpin, HIGH);//activate the trigpin
delayMicroseconds(15);//activate the trigpin
digitalWrite(trigpin, LOW);//activate the trigpin
delayMicroseconds(10);//activate the trigpin
duration = pulseIn(ecopin, HIGH);//define the duration
distance = (duration/2)/29.1;//define the distance (don't worry if you didn't understand the code all it does is that it //converts the time into distance
if(distance <=15 )//if the distance is less than 15 cm then
{
digitalWrite(redpin, HIGH);//turn the red led on
digitalWrite(greenpin, LOW);//turn the green led off
servo1.write(180);//turn the servo 180 degree
digitalWrite(buzzer, HIGH);turn the buzzer on
}
else
{
digitalWrite(greenpin, HIGH);//turn the green led on
digitalWrite(redpin, LOW);//turn the red led off
digitalWrite(buzzer, LOW);//turn the buzzer off
servo1.write(0);//turn the servo to zero degree
}
}
So that is all for the code now upload this to your arduino and it will do the trick. Also make sure that you keep your mouse trap in a corner because mice have a habit of going around the corner. Also keep some thing for him to eat also, that will also attract him. So mission "mouse out of house" is complete. Now who is in with me for a pizza with extra mozzarella cheese
8 Comments
2 years ago
do you mind posting a video about how you do the whole thing?☺it would be very helpful for me^=^
Question 2 years ago
Hi, I've tried the code but the light green didn't work. is it because the resistance value?
5 years ago
how to remove mouse from trap them when mouse on trap
Reply 4 years ago
I think hitting the reset button would do
Reply 5 years ago
Sorry for the late reply :-( was into some other work. You can just open the cap of the bottle and dump the rat into a net (or any thing else) and kill it the way you wish ;-)
5 years ago
You need to work a little more..:)
Reply 5 years ago
Thanks for sharing the link.
Reply 5 years ago
https://www.instructables.com/id/Arduino-Mousetrap/