Arduino Meet Raspberry Pi
While at the electronics store the other day, I noticed they had motion detectors on sale for only $4. I decided with my latest obsession of electronic tinkering, picking up a OSEEP Passive Infrared Sensor (PIR) Module might be fun.
I guess I should have done a little more reading on the packaging; by the time I was home, I noticed this sensor reported in analog , not digital. This was an issue as the Raspberry Pi only reads digital input.
Lucky for me, I also picked up an Arduino UNO Starter Kit awhile back. I decided this would be a great time to learn more about converting analog signals to digital (one great thing about the UNO is that it has both digital and analog input/output pins).
As an extra, I learned the Nexcon Solar Charger 5000mAh I bough for hiking and camping works great as a Raspberry Pi
power source, in theory I can have a portable motion detector :D
The wiring is rather basic, there is no need for resistors or capacitors, just direct connections.
* Connect motion sensor to the Adruino’s
5v power
and ground. * Connect motion sensor’s signal pin to Analog
A0 pin
on Adruino * Connect Adruino’s Digital
2 pin
to Raspberry Pi’s
GPIO 18
* Connect Andruino’s ground to Raspberry Pi’s Ground Once we are wired up, we can compile and upload the Arduino UNO code using Arduino Studio . Arduino
/*
OSEPP Motion detector analog to digital convertor
https://nessy.info
*/
int analog = A0;
int digital = 2
void setup(){
// set our digital pin to OUTPUT
pinMode(digital, OUTPUT);
}
void loop()
{
// read value from analog pin
int analog_value = analogRead(analog);
// send digital signal when motion detected
if (analog_value > 0) {
digitalWrite(digital, HIGH);
} else {
digitalWrite(digital, LOW);
}
delay(100); // slow down the loop just a bit
}
This Arduino code will read analog input from our motion detector, and any time more than 0v is detected it sends a signal to digital pin 2. Raspberry Pi (Python)
import time
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
def detect():
while True:
if GPIO.input(18):
print '[%s] Movement Detected!' % datetime.now().ctime()
time.sleep(1)
detect() # run movement detection
On the Raspberry Pi side we will listen for signal on GPIO pin 18, and print out a little message, and timestamp. From here we can do all sort of things, Happy Hacking!