Sistema de segurança residencial Arduino usando módulo Sim900 Gsm, detector de movimento Pir e interruptor de contato de porta magnético

Arduino Home Security System Using Sim900 Gsm Module, Pir Motion Detector and Magnetic Door Contact Switch

In this tutorial I will make an Arduino home security system. You can install this DIY Arduino security system project on the main door of your house. The DIY Arduino security system checks two things. First check if anyone has approached the door? Secondly, do you check if the door was opened by the person? When the first two conditions are met, an sms/text message will be sent to the house owner. The presence/movement of the person in front of the door is detected by the PIR motion detector and the opening of the door is checked by the door contact switch OR the reed switch. Both sensors (PIR motion and reed switch) are connected to the Arduino Uno and any intrusion detected by the sensors is instantly transmitted to the Arduino Uno. Arduino as an intrusion alert sends an sms/text message to the owner's mobile number using the GSM module.

Arduino Gsm Pir and Door Sensor Security System

Arduino Gsm Pir and Door Sensor Security System

PIR Motion Detection Sensor

I used the popular HCSR501 passive infrared motion detection sensor in the DIY project. It can detect movement from a maximum distance of 7 meters. The sensitivity range of the HCSR501 PIR Motion Detector can be set by rotating the variable resistor/potentiometer mounted on the circuit board of the HCSR501 PIR Motion Sensor. It also has a second variable resistor/potentiometer which is used to set the delay time I will talk about this later (No Out pin of the Hcsr501). Hcsr501 pir sensor has 3 pins. Two are power pins. Vcc is connected to the +ve terminal of the power supply and Gnd is connected to the -ve of the power supply. The third pin is the outside pin. It usually stays low. When the PIR sensor detects the presence of an IR emitting body in its range, it sets the output pin high. Now the question is, how long does the PIR output pin remain high when the presence of a person is detected? For this purpose, the second variable resistor/potentiometer is present on the HCSR501 PCB board, you can set the delay time or off pin at high time using the second variable resistor.
So where will the pir sensor be mounted to detect the presence of a person. It must be mounted on the top of the door entry. The diagram below better illustrates the PIR mounting above the door.

Photograph

Pir sensor installation above the door and area coverage angle.

Sim900 Gsm Module (Arduino Gsm)

I am using SainSmart Sim900 GPRS/Gsm module in the project. It is easy to use and its documentation is quite simple. I worked with many other GSM modules built on the SIMCom sim900 chip. I found SaintSmart to be the best of all. Chinese cheap gsm sim900 modules are really difficult to use, and due to poor circuit designs, their power requirements differ from each other. The SaintSmart gsm module works at 5 volts. It has five pins. In five three are power pins. Two ground pins and one +ve power pin. We supply 5 volt power to the +ve pin and ground the Gnd pins. The remaining two pins are Uart pins.
All sim900 gsm modules communicate with external controllers/microcontrollers over Uart protocol. The Uart protocol has two pins. One is TxD (Transmit) and another is RxD (Receive). To send and receive data to the sim900 gsm module, the external microcontroller must also have a Uart port. In simple words, Uart communication takes place between two Uart ports. The sim900 GSM module has a Uart port, so our external microcontroller must also have one to communicate with the GSM module. In Uart communication we connect the TxD pin of the first device to the RxD of the second. Similarly, Rxd of the first is connected with TxD of the second. After connection, we define the communication transmission rate in the software. Baud rate is simply the communication speed between two devices communicating on the Uart port. In the project circuit diagram, you will be enlightened about the UART connections.
The Sim900 gsm module works ON command set. They are a lot of NO commands if you refer to the sim900 chipset datasheet. In our project, we will only use those that are necessary to send an SMS text message. I have another tutorial on how to send SMS with the sim900 gsm module. You can visit it by clicking the button below.

Gsm Arduino: Send an SMS with Sim900

Door Contact Switch/Reed Switch

A reed switch is an electrical device operated via an installed magnet. It consists of two halves. One half contains the actual switch and the other half just contains a magnet. When the magnetic half is brought closer to the switch half, it begins to conduct electricity. So how does this all happen? In fact, half of the switch contains a pair of magnetic rods. Typically, when there is no magnetic field present near the switch, the rods are separated from each other (open circuit – no electrical path). When we bring the magnetic half closer to the switch half, the magnetic field brings the rods closer together and when the magnetic field strength reaches the limit, the rods lock together (making a physical contact – Path for the flow of electricity).
How to install the door reed switch? We generally mount half of the switch on a fixed/immovable surface and the magnetic half is installed on the movable surface. In our case, I installed the magnetic half on the door and the switch half on the wall near where the door closes. Both halves must be in the correct position and at the correct distance to make contact. Below you can see the correct positioning and installation of the door contact sensor.

Door contact reed switch installation

Door contact reed switch installation
The block and flow diagram of aduino home security system is given below. The Pir motion detector and magnetic door switch send their respective status to the Arduino. The Arduino then decides what to do next with the pir and door sensor inputs. If the Arduino finds both sensors have transmitted positive status. It alerts the GSM module to activate and send a text SMS on a single encoded SIM number.

Arduino home security system block diagram

Arduino home security system block diagram

Arduino Home Security – Project Circuit Diagram

Coming to the circuit diagram of the project. Only four I/O pins of Arduino Uno are used in the project. Two pins are from the Uart channel Pin#0 and 1 of the Arduino Uno. The other two are pin no. 8 and 9. Pin no. 8 of arduino uno is connected to PIR motion sensor output pin and pin no. 9 of arduino uno is connected to door contact sensor. The Pir motion detector and door contact sensor can be powered by the +5 volt output of the Arduino. But I prefer to use an external power supply to power the pir and door sensor.
The door contact switch can be powered from +5v to +12v. I powered it with 5v power supply. If you insert +12 volts into the reed switch, you may need to insert a resistor in series with the reed switch because the Arduino pins are tolerant of 5 volts and +12v (when contact is made) may destroy the Arduino input pin . When contact is made by the reed switch, the input voltage appears on the Arduino digital pin. In our case, in Door (Pin nº 9) pin. If the voltage is above 5V, it may destroy the Arduino input pin. Therefore, be careful before turning on the reed switch.
Note: I write +12 v due to the fact that the door sensor is installed far from the main microcontroller (Arduino, in our case) and after contact the voltage drops on the wire due to the length of the wire.

GSM module needs extra power while sending SMS. Use an adopter that can supply 1 continuous Ampere of current at 5 volts to power the sim900 gsm module. The 1 amp adopter is enough to power all components (Arduino, Pir, Reed switch and Gsm module) used in the project circuit.

Arduino home security system with pir, gsm and door contact switch

Arduino home security system with pir, gsm and door contact switch
Coming to the code part of the security project. First I defined the Arduino digital pins that will interface with the PIR motion detector and door contact switch (Arduino pins 8 and 9). In the configuration function I declared both pins (Arduino Pins #8 and 9) as input. Since we are reading the status of the sensors, we must declare them as input. After declaring Arduino sensor interface pins as input. I switched to the Arduino Uart module. The Arduino Uart module is present on pins 0 and 1 of the Arduino. To declare and start the arduino uart module in arduino ide, we use Serial.begin command. To define the communication baud rate, we enter the baud rate value in circular brackets after the word begins. In our case I set the communication rate to 9600 bits per second and the command used to initialize it is Serial.begin(9600) .

In the loop function two variables are declared S1 and S2 . Both variables are used to store the status of the PIR sensor and the port. The statement S1=digitalRead(door) is checking the status of the door sensor and storing the status in the variable S1. The statement S2 = digital readout (output) is checking the status of the pir and storing its status in variable S2 . Then, an if statement compares the status of both sensors/variables ( S1 and S2 ). If the PIR output is high and the contact sensor is low, control enters the body of the IF statement. In the body of the statement if NO commands are used to send a text message on a specific mobile number.

To send commands by Arduino on its Uart port Serial.println instruction is used in arduino ide. First “AT+CMGF=1” is to send. This command sets the sim900 gsm module into sms text mode. So “AT+CMGS=” is sending with “\”+923425500809\”” . In fact, it is the recipient's SIM number or the number to which we want to send the SMS. The backslash in the last instruction prevents the double quotes from being executed by the Arduino IDE. Sim900 requires the mobile number to be enclosed in double quotation marks. After the number comes the body/text of the SMS. I am sending my website name www.microcontroller-project.com as text. You can send whatever you want. Then comes the most important statement Serial.write(26) . To finalize the message and send it, sim900 requires Ctrl+Z command. If we translate Ctrl+Z into ascii code, it will be translated as '26'. So at the end of the code we are sending Ctrl+Z command to sim900 to send the text message.

Future work

The project can be updated by controlling other appliances like lights and alarms, etc., if any threats are detected. Arduino relay board containing multiple relays can be used to turn peripherals (lights, alarm) on and off. Alongside an SMS alert message, an email can also be sent to the house owner using the GPRS function of the sim900 gsm/gprs module.

code/files

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.