Step #1: How the System Works
- This system is a basic motion activated alarm. It is built around an Arduino microcontroller. It is connected to a PIR motion sensor, a buzzer, a resistor, and a pair of external terminals. The whole system is battery powered so that it is easily portable. The system is activated and deactivated by touching a certain value resistor to the contacts on the housing.
- When a certain value resistor is connected to the terminals for a few seconds, the system arms itself. After a ten second delay, the motion sensor is activated. If it detects motion, it makes a brief warning beep. After this, if the alarm is not deactivated within ten seconds, the siren will go off. To deactivate the alarm, all you have to do is touch the resistor to the contacts for a few seconds.
- The resistor makes a very unique kind of key. The resistance must be within a very small range in order to activate the system. Because the resistance must be within this window for several seconds, you can't disable it with a variable resistor (potentiometer). By covering up the colored stripes on the resistor, other people won't be able to tell what resistance it is by looking at it. This makes the resistor lock very secure.
Step #2: Microcontroller Code
Here is some sample code that you can use to start the project.
You can either copy and paste it into a new sketch or you can download the attached file.
Then upload it to your board.
int alarmPin = 2; // Piezo buzzer connected to digital pin 2
int motionSensorPin = 13; // Motion sensor connected to digital pin 13
int resistanceSensorPin = 5; // Center pin of resistors connected to analog pin 5
int sensorHoldTime = 2000; // Time that resistance must be within range to activate the alarm
int resistanceSetValue = 512; // Setting for resistor value
int resistanceSensorValue = 0; // Input resistance reading
int motionSensorState = LOW; // Store whether motion is detected
int armState = LOW; // Is the alarm armed
int triggerState = LOW; // Has the alarm been triggered
unsigned long currentTime = 0; // current time
int delayTime = 10000; // Delay between detecting motion and alarm
unsigned long activateTime = 0; // stores time that the alarm is triggered
pinMode(alarmPin, OUTPUT); // sets the digital pin 2 as output
pinMode(motionSensorPin, INPUT); // sets the digital pin 13 as input
currentTime = millis();
resistanceSensorValue = analogRead(resistanceSensorPin); // reads resistor value
if (abs(resistanceSensorValue - resistanceSetValue) activateTime + delayTime)) //sound alarm if it is not deactivated in time
Step #3: Prototype the System on a Breadboard
- Once you have the code, you can connect all the external parts. The easiest way to do this is with a breadboard. This will let you make temporary connections to test everything out.
- Connect the motion sensor to the 5V pin, the GND pin, and to digital pin 13. Connect the black wire of the buzzer to GND and connect the red wire to digital pin 2. Lastly, connect one of the resistors to the 5V pin and analog pin 5. Connect the other resistor to GND and analog pin 5.
- Before plugging in the Arduino board remove one of the resistors from the breadboard. Then connect the Arduino board to your computer with a USB connector cable, and upload the code. Once the upload is complete the system should immediately begin functioning.
- To arm the system, insert the resistor back into the board where it was. After it has been securely fit in place, remove it again. As soon as the Arduino detects the resistor, it will begin activating the alarm. After a ten second delay, the motion sensor will be activated. When motion is sensed, there will be a brief warning beep and ten seconds later the alarm will sound. To deactivate the alarm, insert the resistor back into the breadboard. The alarm should stop immediately, and after two seconds, the alarm will be deactivated. You can now either remove the resistor and the alarm will go into standby mode (de-activated) or you can leave the resistor in and the system will reset.