Saturday, January 19, 2013

Zilog ePIR Motion Detector and the Raspberry Pi

Here is a simple example of using an ePIR to detect motion. 

The Zilog ePIR functions in several modes. In this case, the ePIR runs in solicited mode (the code prompts the sensor to report whether or not it has detected motion). The sensor connects to the Pi via a serial connections.

The code sends the command to check for motion every half second and returns a 'Y' if motion detected, otherwise 'N'.

This code requires the PySerial module. Follow the 3 steps on the Adafruit Learning System page, "Freeing UART on the Pi," to make sure the serial port ttyAMA0 is free for the program to use. 

Connections:


ePIR  -->  RPi 
Pin 1      GND
Pin 2      3V3
Pin 3      TXD
Pin 4      RXD  (with 100k pull-up resistor)
Pin 6      3V3

Python Code:


import serial
import time

serial = serial.Serial("/dev/ttyAMA0", baudrate=9600)

STATUS_CMD = 'a'
DELAY = 0.5

while True:
    serial.write(STATUS_CMD)
    data = serial.read()
    print data
    time.sleep(DELAY) # pause


Here is a second version of the code that puts the ePIR in "unsolicited mode." In this mode, the sensor returns the letter 'M' when when motion is detected without being prompted by the code to make a reading.


import serial

serial = serial.Serial("/dev/ttyAMA0", baudrate=9600)

CMD = 'M'
DELAY = 0.5

serial.write(CMD)
# ePIR returns unsolicited mode status
rc = serial.read()
# Turn unsolicited mode on
serial.write('Y')
while True:
    if serial.inWaiting > 0:
        data = serial.read()
        print data

3 comments:

  1. Thank you for posting this; I would like a full fledged Python driver for the Zilog Motion Detector module. Do you know if anyone has written one?

    ReplyDelete
  2. Thanks for responding! I'm almost done writing it; check out http://github.com/griffegg/zilog_ZDMII if you would like to take a look.

    ReplyDelete