The PIR SE-10 sensor is an electronic component used to detect motion. It does this by detecting infrared wavelengths that are not visible to the human eye. Any source that emits these type of wavelengths like the human body, or more specifically, its heat, will trigger the sensor.
The sensor has three pins:
Figure 1. Sensor Pinout
Where DC 12V(+B) is the red wire, GND is the white wire, and ALARM(AL) is the black wire. Note that you can power this device with up to 12 VDC. The AL pin is what sends the signal to the sensor when motion is detected. It is important to mention that this specific sensor has an open collector, which means that we have to include a pull up resistor to keep the signal high. The diagram below helps describe what an open collector is:
Figure 2. Open Collector
This is the diagram of an NPN transistor. Transistors pretty much act as switches. In this case whenever the sensor detects motion, it will send a small signal through the base to let current flow from the collector to the emitter. When no current flows through the base, the collector remains open and, thus, no current flows from the collector to the emitter. The reason we need to add a pull up resistor to the collector is so that our Raspberry Pi can detect a change in the signal when the sensor detects motion. Let’s look at the diagram below to see how it works:
Figure 3. Connection Between Sensor and RPi
The Raspberry Pi will read the signal from the alarm pin (collector) at all times. Let’s say our sensor is not detecting any motion. Therefore no current is flowing through the base and no current is flowing from the collector to the emitter. Then we can say that the current in each channel is:
IB = 0
IE = 0
IC = 0
The Raspberry Pi won't detect anything at its input. Now, let’s say that our sensor detects motion. This will make current flow through the base and current will flow from the collector to the emitter. However, since nothing is driving the collector high, no current is flowing through the collector. Now the current in each channel is:
IC = 0
IE = IB
So the Raspberry Pi won't detect anything again.
Now let's add a pull up resistor to see how things change:
Figure 4. External Pull Up Resistor
When no motion is detected by the sensor, then current will only be flowing from the collector to the Raspberry Pi’s input, so the signal will always be high. When the sensor detects motion, the switch will close, and now the current from the collector will flow through the emitter instead. This makes the Raspberry Pi input read a low signal.
In other words, without the pull up resistor, the Raspberry Pi won't be able to tell when the alarm pin has been triggered.
With the Raspberry Pi it is not necessary to attach an external pull up resistor to the circuit. We can use its internal pull up resistor that can be enabled through code. We will show you how to do this in the Code section of this tutorial.
Assuming that you have your Raspberry Pi ready to go, go ahead and follow the instructions below to make the connections. Otherwise, follow the instructions under Operating System for the Raspberry Pi and USB Wi-Fi Module from this tutorial to setup your Raspberry Pi:
Solder the headers to the T-Shifter Breakout Board
Place the T-Shifter Breakout Board into the protoboard
Connect one end of the ribbon cable to the T-Shifter Breakout board and the other end to the Raspberry Pi
Place the sensor’s wire into the breadboard using the JST connector
Use jumper wires to make the following connections:
DC 12V (+B)
Note: You need to turn on access for less secure apps in your Google account. Otherwise, the email won't go through.
In the Raspberry Pi go to Menu > Accessories > Text Editor. Write the following code and save it as a .py file. Once it is saved, in the Text Editor window, go to Run > Run Module to execute the program.
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
These are the libraries required to send emails using Raspberry Pi.
import RPi.GPIO as GPIO
These are the libraries required to use the GPIOs from the Raspberry Pi and enable delays.
SENSOR = 16
Here we select the addressing mode for the inputs and outputs. The two options are “BOARD” or “BCM.” The first one refers to physical numbering while the second refers to GPIO numbering. Let’s look at the diagram below to identify each one:
Figure 5. Raspberry Pi’s GPIOs
Each numbering method has a different number to identify each pin. Depending on the method you select, the pin number will change. In our case, we will be using the physical numbering (BOARD), and the pin that is highlighted in the diagram above. Thus, we will use the number 16 to refer to this pin. If we were using the GPIO numbering (BCM), then we would have to use number 23 instead.
In the next line of code, we assign pin 16 the name “SENSOR” to identify it as a sensor.
The third line of code declares the pin as an input and enables the Pi’s internal pull up resistor.
fromaddr = "YourEmailAddress@gmail.com"
toaddr = "EmailAddressOfRecipient@gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Motion sensor has been activated"
body = "Someone is coming!"
Here we create two strings that have our email address and the email address of the person to whom we want to send the email. In our case we will be sending an email to ourselves, so both emails are the same. Then we use MIMEMultipart to fill the email’s sections “To,” “From,” and “Subject” with the corresponding strings. Next, we create another string that has the content of the email, and use MIMEText to attach the text as the body of the email. Lastly, we wait 3 seconds to let the sensor stabilize.
if not GPIO.input(SENSOR):
print "Motion Detected"
server = smtplib.SMTP('smtp.gmail.com','587')
text = msg.as_string()
The while loop serves to check every 0.1 seconds if motion is detected. The if not statement serves to send the email if the sensor detects motion. Remember that the AL pin has a pull up resistor. This means that whenever motion is detected the signal will go low. Thus, we want to know when the pin’s state is not true to send the email, hence the use of if not.
Inside the if not statement we store Gmail’s server location and the port we are going to use to communicate with it in the variable server. Then we use starttl () as a security function to protect our password when we connect to the Gmail server. Next we use login() to login in to our Gmail account. Subsequently we store the strings from MIMEMultipart() and MIMEText() in the variable text. Now text contains all the parts of our email: “To,” “From,” “Subject,” and “Body.” Lastly we send the email to the corresponding address, close the connection with the server, and wait 2 seconds.
In the video we use an alternate way of executing the python code without having to use the monitor and keyboard (via SSH). In your phone download the following app:
Before you can use this app, you need to know you Pi’s IP address. In the terminal window of the Raspberry Pi type the following command:
Take note of this number since you are going to need it to connect through your phone.
Now launch the app, go to options, click on SSH Settings and type the IP Address of your Pi. Leave the SSH Port as 22. Click on OK, open options again, and go to Connect. You will be asked to type in a username and a password. Fill each section with the following:
Once you are connected, type the following command:
In our case the python file is named “motion_sensor_test.py” so we type:
This will make the python code run.