A PIR Sensor can tell when something nearby moves. The sensor detects the pattern of infrared energy in its surroundings. If the pattern changes, the sensor outputs a high signal. The Propeller microcontroller only needs to monitor this signal with an I/O pin to know if something is moving around nearby.
(1) PIR Sensor Rev B (#555-28027) [1]
(1) 220 ohm resistor
(3) jumper wires
The PIR sensor connects to 3.3 V, ground and a single Propeller microcontroller I/O pin. (The 220 ohm resistor isn’t strictly necessary, but it could help protect the sensor from an accidental signal sent from the Propeller I/O pin, which could happen from wiring or coding errors.)
Note: The circuit and example code in this tutorial also work with the PIR Mini (#28033). Connect its Out pin to P5, its VDD pin to 3.3 V, and its GND pin to ground.
The following program continuously checks and prints the output state of the PIR sensor.
If you haven't already installed the latest USB driver, SimpleIDE, or Learn folder, go to Propeller C - Set up SimpleIDE. [2]
/* PIR Rev B.c */ #include "simpletools.h" // Include simpletools library int main() // Main function { while(1) // Main loop { int state = input(5); // Check sensor (1) motion, (0) no motion print("%c state = %d%c\n", // Display sensor HOME, state, CLREOL); pause(200); // Pause 1/5 second before repeat } }
First, this program includes the simpletools library for its print, pause, and input functions.
The code is entirely inside an infinite while(1) loop. The statement int state = input(5); means “check I/O pin P5 (which is connected to the PIR sensor) and store the result in the local int variable named state.”
If the I/O pin detects 3.3 V, the PIR sensor detected motion and a 1 is stored in state. If the I/O pin detects 0 V, no motion was detected and a 0 is stored in state.
Next, a print statement displays the value of state in the SimpleIDE Terminal. The %c formatter in the string is needed for the HOME and CLREOL control characters. HOME returns the cursor to the upper left position before printing the text and value of state. CLREOL means “clear to end of line” which erases any other characters that may be on the same line in the terminal. Together, these let the display use just one line and update cleanly.
Finally, pause(200) slows down the loop a little bit.
What’s that glow? — A faint red glow from two LEDS inside the PIR Sensor Rev B’s white dome indicates that the sensor is triggered. It is typical for the sensor to be triggered when it is first powered up and getting an infrared reading from its environment. Once triggered by start-up, or by a moving object, the sensor stays in that state and sends a high signal for several seconds.
Passive Infra-Red — That’s what PIR stands for. The sensor is passively relying on infrared light/heat naturally emitted from surrounding objects. It is not actively sending out infrared signals and looking for a reflection, as some other types of infrared object sensors and line followers do.
Automatic Lights and Alarm Systems — If you have an night light that comes on automatically when you walk near it, there’s likely a PIR sensor inside. Also, PIR motion sensors are sometimes used alone or with other types of sensors in security systems to detect intruders.
You may noticed that if you make just a quick movement, the sensor stays in its triggered state for several seconds even if everything is still again. The example code above checked the sensor’s output about every 200 ms. That is more frequent than needed once the sensor is triggered, and maybe not frequent enough if you want your system to respond immediately to a detected motion. Also, it might be helpful to see how many times the sensor has been triggered. The changes below take care of all these issues.
Now, the outer while(1) loop repeats faster, since pause(200) was reduced to pause(5). But, if the sensor is triggered, a pause(4000) in the if... statement keeps the message from printing repeatedly while waiting for the sensor to clear from its high state. You can adjust this pause time if your sensor is still printing more than once from a quick motion.
Make your Activity Board (original or WX version) an audible motion alarm disconnected from your computer. Here are some suggested hardware and software changes:
Now, your battery-powered motion sensor is ready set up and turn on to stand guard any place you want to take it!
Links
[1] http://www.parallax.com/product/555-28027
[2] http://learn.parallax.com/propeller-c-set-simpleide
[3] https://learn.parallax.com/propeller-c-simple-circuits/piezo-beep
[4] https://learn.parallax.com/propeller-c-simple-devices/sd-card-data
[5] https://learn.parallax.com/propeller-c-simple-devices/play-wav-files