Saturday, May 25, 2013

RFID via USB for the Raspberry Pi Using the Innovations ID-20LA & Sparkfun RFID USB Board

The Innovations ID-20LA is a 125 kHz RFID tag reader that works with input voltages from 2.8 to 5 volts. The Sparkfun RFID USB Reader (Board) provides a socket for the ID-20LA (with its 2mm pin spacing) a mini-USB (B) connector, and broken out pins for a serial connection.

Using the mini-USB connection, it is very easy to connect it to the Raspberry Pi.  The Linux distribution that comes installed on the Raspberry Pi includes the FTDI-SIO driver/kernel module, so no additional drivers are needed. When connected to the RPi, the RFID reader is mapped to the serial device /dev/ttyUSB0.

For a Python script to read from a serial device, you do need to install the Python Serial package (apt-get install python-serial).

Python Code


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

code = ''

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

Note (11/12/2013):  You will need to free up the serial port on the Pi.  For help doing this, see the Adafruit tutorial at http://learn.adafruit.com/adafruit-nfc-rfid-on-raspberry-pi/freeing-uart-on-the-pi.

Saturday, May 18, 2013

TCS34725 RGB Color Sensor & the Raspberry Pi

The TCS34725 RGB color sensor board from Adafruit works with the Raspberry Pi using an I2C connection. You can find the datasheet here.

The Python example below reports the clear, red, green, and blue light levels for a given sample (printing them to the console).

Connections


TCS34725  RPi
SDA       P3 GPIO 0 (SDA)
SCL       P5 GPIO 1 (SCL)
3V3       P1 3V3
GND       P9



Python Code


import smbus
import time
bus = smbus.SMBus(1)
# I2C address 0x29
# Register 0x12 has device ver. 
# Register addresses must be OR'ed with 0x80
bus.write_byte(0x29,0x80|0x12)
ver = bus.read_byte(0x29)
# version # should be 0x44
if ver == 0x44:
print "Device found\n"
bus.write_byte(0x29, 0x80|0x00) # 0x00 = ENABLE register
bus.write_byte(0x29, 0x01|0x02) # 0x01 = Power on, 0x02 RGB sensors enabled
bus.write_byte(0x29, 0x80|0x14) # Reading results start register 14, LSB then MSB
while True:
data = bus.read_i2c_block_data(0x29, 0)
clear = clear = data[1] << 8 | data[0]
red = data[3] << 8 | data[2]
green = data[5] << 8 | data[4]
blue = data[7] << 8 | data[6]
crgb = "C: %s, R: %s, G: %s, B: %s\n" % (clear, red, green, blue)
print crgb
time.sleep(1)
else: 
print "Device not found\n"


To keep this example simple, I have left the timing/power consumption for the analog to digital converters on the sensor board and sensor gain at their default values. For more information about these (and other settings) see the datasheet.