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

RFID with the Innovations ID12 RFID Reader and the Raspberry Pi

I have an Innovations ID12 RFID card reader and a Sparkfun RFID USB Reader board. The RFID USB reader board is primarily intended to provide a convenient USB to serial connection for an ID12 or ID20 reader. There is an FTDI driver for the Raspberry Pi that would allow plugging the unit into a USB port on the Pi, but I haven't tried it yet.  The reader board does, however, also have the pins broken out for the power (5V) and TTL serial connection. I was able to use these connections to get the reader to work with an Arduino and Netduino, so I wanted to try it with the Raspberry Pi. 

Since the ID12 in this example communicates with the Raspberry Pi over the serial port, it is assumed that you are not logged in to the Pi using the same serial connection. Use a wireless or ethernet connection to avoid conflicts.

Note (5/3/2013): Sparkfun now has a new version of the ID-12, called ID-12LA, that works with power supplies between 2.8V and 5V.

Note (5/29/13): For information on using the 3.3 volt ID-20LA with a Raspberry Pi, see this post


Connections:


Since the ID12 is a 5V device, it requires a logic level converter to connect it safely to the Raspberry Pi. I should also note that I am using an Adafruit Pi Cobbler Breakout Kit to connect my Raspberry Pi to the breadboard. 

The high voltage (HV) side of the logic level converter is connected to 5V and GND on the Pi (i.e., the row of the breadboard in line with the pin on the cobbler connector). The low voltage side of the level converter is connected to the Pi's 3V3 and GND. The Pi's RXD pin is connected to the RXO on the LV side of the level converter. 

Connect the VCC pin on the USB reader breakout board to the HV pin on the HV side of the logic level converter. The GND pin on the reader board connects to the GND on the HV side of the level converter. The TX pin on the reader board should connect to RXI on the HV side of the logic level converter (opposite the connection for the Pi to RXO on the LV side).

Freeing Serial Port ttyAMA0:


The RXD pin on the Pi connects to the Linux serial device ttyAMA0. By default, though, this device is used for console output. Follow the 3 steps on the Adafruit Learning System page, "Freeing UART on the Pi."

Python Code:


The following very simple Python code reads a 125 kHz card when one is near the reader and prints out the card's number. This code requires the PySerial module.

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

code = ''

while True:
    data = serial.read()
    if data == '\r':
        print(code)
        code = ''
    else:
        code = code + data

About This Blog

This blog is a place for me to put together notes, links, and observations about my experience with the Raspberry Pi. I don't claim any great expertise, but I will work to make sure that what I post is correct to the best of my understanding.  I hope that others out there somewhere may find these postings useful. I have a similar blog with posts about my getting to know the Arduino and Netduino called Brad's *Duino Blog