# PIR on RPi



## balanga (Jun 29, 2019)

How do I detect motion using a PIR on a RPi using FreeBSD?

Which wires do I connect to where and can I get any sample code from anywhere? I only want to switch on a LED if motion is detected. Would I need a breadboard to make this work?


----------



## Phishfry (Jun 29, 2019)

balanga said:


> Would I need a breadboard to make this work?


No they make all kinds of little expermenter boards with PIR sensor. Just two wires. One to a ground and one to a GPIO pin.
You must set the GPIO pin for the correct mode (input).
To switch on a LED when PIR is triggered might take a small script. You need to poll the PIR as it is nothing more than a on/off switch.


----------



## Phishfry (Jun 29, 2019)

I just had a look at ebay and I see that PIR sensors use 3 wires.
Power(VCC), Ground, Signal.
https://www.ebay.com/itm/223556416939All you need is a board like this and some Pi jumper wires (female to female).








						40 Pin Dupont Cables M-F, M-M, F-F Jumper Breadboard Wire GPIO Ribbon Pi Arduino  | eBay
					

10 Different Colors, 120units copper cable in total, for making prototypes with arduino boards, stepper motor 3D printer etc, The combinations provide a jumper for whatever you need. Including 40 pin male to female jumper wires,40 pin male to male jumper wires, 40 pin female to female jumper wires.



					www.ebay.com
				



I have found these to be most useful even on motherboards.
I had a 1U case I was re-purposing and the power switch leads were too short.
Pi Male to Female jumper wires were the perfect extension cord!


----------



## Phishfry (Jun 29, 2019)

It sounds like you have not yet used a switch with GPIO.
I suggest you start there. First bring up an LED with the jumper wires. Then try a conventional toggle switch.
You will find that a switch alone is really touchy. If you flip the switch back and forth quickly FreeBSD GPIO will lose some switch cycles.
This phenomenon is called debounce. When you flip a switch it drives a GPIO pin low or high. Since the switch is dumb there is no feedback as to the actual position of the switch. So commonly to hold a switch at a state a resistor is used to drive a pin state. Beaglebone has some resistors built in. Pi does not.
Using the PIR ciruit board you may notice some circuitry. This is time delay and sensativity.
The time delay should help you with GPIO pin driving. On PIR triggering the sensor would send hundreds of on-off cycles. So the circuity helps smooth out the switch signal. Remember PIR is a switch too.


----------



## balanga (Jun 30, 2019)

Not entirely sure what you mean by 'conventional toggle switch'.... 

As for PIR, I see it has three pins, gnd, output and Vcc. I have the RPI2-B  so assume gnd can use  any of the pins 6,9,14,20,25,30,34,39....  Also assume Vcc should connect to either 2 or 4. Not sure if the PIR uses 3.3v or 5v...
No idea where the output pin should be connected...


----------



## Phishfry (Jun 30, 2019)

What you could do is study the default pin states. `gpioctl -lv`
Some RPi pins are inputs and some are outputs.
Start with your LED Pin and find an OUT pin to use with it and the same with PIR. Look for a IN pin.
That way on startup it is already the right direction. Otherwise you must define pins (IN/OUT) with a script or the dtb.


----------

