RGPIO and PiBoard

How to use RGPIO with PiBoard.
This guide uses a PiBoard with the following components:
  • EA DOG-M LCD (on pins 17,0,11,22,23,24,25) in 3.3V mode)
  • LCD Backlight (on pin 18)
  • Buttons connecting pins 7,8,9,10 to ground when pressed
  • PCF8563 I2C Real time clock (with super-capacitor power backup)
  • DS1621 I2C Temperature device
  • DS18B20 1-Wire Temperature device
To use RGPIO from python, we open the /dev/rgpio device as an unbuffered file in r/w mode:
rgpio=open('/dev/rgpio','r+',0)
# r+ means read/write; 0 means unbuffered
we can then write to and read from this file to control the PiBoard.

LCD – EA-DOG-M

RGPIO does the basic initialization from the C command to get the LCD running in 4-bit mode with the appropriate pins, but the EADOG-M screens need additional initialization according to whether you run in 3.3V or 5V mode.
\C29 – Set 4-bit mode and instruction set 1
\C15 – Set bias for 3-line LCD
\C55 – Booster (for 3.3V) on, Contrast in follower mode
\C6E – Follower ccontrol
\C72 – Set Contrast
\C28 – Set 4-bit more and instruction set 0
rgpio.write('L17,0,11,22,23,24,25C')
rgpio.write('LD\C29\C15\C55\C6E\C72\C28')

LED Backlight (EA LED55x31-G)

The EA LEDs are coordinated with the LCDs but use completely separate pins. The PiBoard lets you choose between a 3.3V or a 5V supply using the appropriate current-limiting resistor.
The LED is controlled through a NPN transisitor driven by pin 18. The Raspberry Pi has hardware PWM enabled on pin 18 and this can be used to control the LED brightness. RGPIO supports both software and hardware (pin 18 only) PWM, but the hardware version is much more efficient.
#Set LED to 50%
rgpio.write('W50,18C')

Buttons (Tyco 6x6mm)

When the buttons are pressed, the GPIO port is shorted to GND. For the buttons to work, they need a pull-up resistor. The PiBoard has space for a separate pull-up resistor, or we can use the CPU’s internal pull-up capability using P7U8U9U10U.
To get the pin settings we use RGPIO’s pin change interrupt to tell us when the pin is grounded.
rgpio.write('P7U1,7L')  # Initialize pull
rgpio.write('P8U1,7L')  # up and get repeated notifications
rgpio.write('P9U1,7L')  # when pin Lo
rgpio.write('P10U1,7L')
To see what is going on, we have two options: read in a polling loop, knowing we may get a blank line because there is nothing waiting:
while True:
    inLine=rgpio.readline()
    # Now process the line
Or we can get RGPIO to send us a signal to tell us something is waiting.
import signal,os
def IOSignal(signum,frame):
    inLine=rgpio.readline()
    # Now process the line

signal.signal(signal.SIGIO,IOSignal) # call IOSignal on SIGIO
pid=os.getpid()                      # Get PID of this process
rgpio.write('%dS'%pid)               # tell RGPIO to send SIGIO here
while True:
    time.sleep(1)                    # just loop asleep waiting...

I2C Clock

To give the Raspberry Pi a clock, you can either be connected to the internet (in which case, the very accurate internet time is available), or you need to provide an external clock. We do this with an I2C Real Time Clock chip.
The I2C bus is a very convenient mechanism to communicate between components. It needs 4 connections: VDD, GND, SCL, SLA. Many CPUs (including the Raspberry Pi’s) have I2C communications built in. On the Raspberry Pi the I2C components are connected to I2C channel 1. Each I2C component has an Id: the DS1307 and DS1337 are on 0x68, the PCF8563 is on 0x51.
There are device drivers for many of the available RTC chips (and other I2C chips), and you can use these if you want to, but they are not always as flexible as you might want. RGPIO provides another interface.
To set the clock on PCF8563 from the system time, use:

t=datetime.datetime.today()
sec=toBCD(t.second)
min=toBCD(t.minute)
hour=toBCD(t.hour)
date=toBCD(t.day)
month=toBCD(t.month)
year=toBCD(t.year % 100)
day=t.weekday()
rgpio.write("I1N0x51D2,%d,%d,%d,%d,%d,%d,%dW"%(sec,min,hour,date,day,month,year))
To get the clock from PCF8563 and set the system time, use:
import re, os
rgpio.write("I1N0x51D2,7R")
reI2C=re.compile('I2C *([^@]*)@([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*) *([^ ]*)')
inLine=rgpio.readline()    # If using signal handling, this code goes there
event=reI2C.search(inLine) # Use Regular expressions to unpack the input
sec=int(g(3))&0x7f         # The PC8563 doesn't leave the unused bits zero
min=int(g(4))&0x7f         # so we have to clear them ourselves
hour=int(g(5))&0x1f
date=int(g(7))&0x3f
month=int(g(8))&0x1f
year=int(g(9))+2000
newDate=datetime.datetime(year,month,date,hour,min,sec)
os.popen("date -s %s"%newDate)
The DS13x7 series are similar but some detail differences. There are other I2C Real-time-clocks that can be used.

I2C Thermometer (DS1621)

The DS1621 needs initializing so it starts measuring the temperature. Thereafter the temperature can be read whenever required.
rgpio.write('I1N0x48D0xEEW') # Command 0xEE starts conversion
rgpio.write('I1N0x48D0xAA,2R') # Read 2 bytes to get the temperature
inLine=rgpio.readline()
event=reI2C.search(inLine) # Use Regular expressions to unpack the input
temp=(event.group(3)+256*event.group(4))/256

This temperature is accurate to 0.5C, and you can greater accuracy (about 1/16C) by reading from locations A8 and A9 and using a formula in the specification.
Note: PiBoard has the DS1621 located next to the Timekeeping Crystal. Time Crystals typically have a fixed frequency that varies according to the temperature. This thermometer can be used to improve the accuracy of the clock.

1-Wire Temperator sensor (DS18B20)

The 1-Wire bus uses VDD, GND and DQ (the signal wire). (And some devices will work without the VDD connection.) Linux has 1-Wire modules available and you can use these, however, there are some problems with usability:

  1. The w1-therm module requires reading a file /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1-slave. The problem is that reading this file takes a long time (about 1 second) – this is because it has to initiate a temperature conversion, and must when way for the conversion to complete – which is between 94ms and 750ms, depending on the conversion accuracy. 1 second is much too long to wait for a system with a user interface.
  2. the w1-gpio module can also be used but writing to and reading from /sys/bus/w1/devices/28-xxxxxxxxxxxx/rw. The problem here is that if the sensor goes off line, file operations to the rw file hang.
  3. The built-in support for 1-wire is only available on pin 4. You can have multiple devices on that one pin, but a single pin is still a little limiting.

RGPIO provides an alternative interface to 1-Wire devices.

rgpio.write('P4U')      # Set pin 4's pullup - essential for 1-wire operation
rgpio.write('O4P')      # Set pin 4 as 1-Wire pin
rgpio.write('O0D')      # Tell RGPIO there is only one 1-Wire device
rgpio.write('O0x44C')   # Start temperature convertion
rgpio.write('O0xBEC2W') # Read the temperature
reW1=re.compile('W1(:[0-9a-fA-F\-\.]*)* *(=*) *([^ ]*) *([^ ]*) *([^ ]*)')
inLine=rgpio.readline() # Get the result
event=reW1.search(inLine)
temp=(int(event.group(3),16)+256*int(event.group(4),16))/16.0

If you have more than one device on the bus, you can do the following:

rgpio.write('P4U')      # Set pin 4's pullup - essential for 1-wire operation
rgpio.write('O4P')      # Set pin 4 as 1-Wire pin
rgpio.write('OL')       # List available devices
inLine=rgpio.readline() # Get the first found device (blank if none)
event=reW1.search(inLine)
w1Device=event.group(1)[1:]  # Get the device name
                             # format is ff-nnnnnnnnnnnn.cc
                             # ff is family, n..n is number, cc is checksum
w1Family=w1Device[0:2]
w1NC=w1Device[3:7]
w1NB=w1Device[7:11]
w1NC=w2Device[11:15]
w1Id="%x,0x%x,0x%x,0x%xD"
rgpio.write("O%s"%w1Id)      # Set this device as the one to use
rgpio.write('O0x44C')        # proceed as above
...

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *