Saturday, June 30, 2012

Update

Hi. Thanks for all the success with my first post, I can't believe it actually made it to Hack a Day and Adafruit, you can imagine the shock I had when I was looking at my google reader and see a photo of my desk on it :D. Over the next week or two, I'll be doing a few more projects with my RasPi, hopefully adding a nice ftdi chip on board to make my life a bit easier and keep working on my PyLCD  project to make it as portable as possible. Of course, this all depends on how fast UPS wants to be. Thanks for all the comments and views.

Saturday, June 23, 2012

Running a HD44780 LCD over I2C (On the Raspberry Pi)

First Post! ... Now that that's over, I'm one of the lucky few/many people who has been able to get the Pi, so time to start spitting out libraries and tutorials. Today I'm going to start by focusing on getting an HD44780 lcd to work with the RasPi, since these are incredibly common, and some of the cheapest and easiest character lcds to work with.

One of the major problems plaguing the RasPi is the lack of GPIO, and the weird placement and random numbered GPIO pins broken out, making any code using them much more disorganized than if consecutive  GPIO pins had been used. In addition to the inconvinience of using the GPIO, the LCD uses 5v, while the RasPi is strict 3.3v. In order to prevent any chance of damage to my Pi, and to solve the problem, I opted to use a PCF8574 I2C 8 bit IO expander. This makes it easy enough to use the bottom four bits of the expander for LCD data, and the next 3 bits for RS, R/W, and EN. To control the LCD this way, I wrote up a nice library to get things working on the lcd.  In addition to this, I added a few extra bits to the library so that anyone can add more devices to it, and wrote a class for the tmp102 so we can have some live data to display.
The Raspberry Pi wired up to a serial bridge, PCF8574 Port Expander,
and a tmp102 12bit temperature sensor

'''
Copyright (C) 2012 Matthew Skolaut
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import smbus
from time import *
# General i2c device class so that other devices can be added easily
class i2c_device:
def __init__(self, addr, port):
self.addr = addr
self.bus = smbus.SMBus(port)
def write(self, byte):
self.bus.write_byte(self.addr, byte)
def read(self):
return self.bus.read_byte(self.addr)
def read_nbytes_data(self, data, n): # For sequential reads > 1 byte
return self.bus.read_i2c_block_data(self.addr, data, n)
class lcd:
#initializes objects and lcd
'''
Reverse Codes:
0: lower 4 bits of expander are commands bits
1: top 4 bits of expander are commands bits AND P0-4 P1-5 P2-6
2: top 4 bits of expander are commands bits AND P0-6 P1-5 P2-4
'''
def __init__(self, addr, port, reverse=0):
self.reverse = reverse
self.lcd_device = i2c_device(addr, port)
if self.reverse:
self.lcd_device.write(0x30)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_device.write(0x20)
self.lcd_strobe()
sleep(0.0005)
else:
self.lcd_device.write(0x03)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_strobe()
sleep(0.0005)
self.lcd_device.write(0x02)
self.lcd_strobe()
sleep(0.0005)
self.lcd_write(0x28)
self.lcd_write(0x08)
self.lcd_write(0x01)
self.lcd_write(0x06)
self.lcd_write(0x0C)
self.lcd_write(0x0F)
# clocks EN to latch command
def lcd_strobe(self):
if self.reverse == 1:
self.lcd_device.write((self.lcd_device.read() | 0x04))
self.lcd_device.write((self.lcd_device.read() & 0xFB))
elif self.reverse == 2:
self.lcd_device.write((self.lcd_device.read() | 0x01))
self.lcd_device.write((self.lcd_device.read() & 0xFE))
else:
self.lcd_device.write((self.lcd_device.read() | 0x10))
self.lcd_device.write((self.lcd_device.read() & 0xEF))
# write a command to lcd
def lcd_write(self, cmd):
if self.reverse:
self.lcd_device.write((cmd >> 4)<<4)
self.lcd_strobe()
self.lcd_device.write((cmd & 0x0F)<<4)
self.lcd_strobe()
self.lcd_device.write(0x0)
else:
self.lcd_device.write((cmd >> 4))
self.lcd_strobe()
self.lcd_device.write((cmd & 0x0F))
self.lcd_strobe()
self.lcd_device.write(0x0)
# write a character to lcd (or character rom)
def lcd_write_char(self, charvalue):
if self.reverse == 1:
self.lcd_device.write((0x01 | (charvalue >> 4)<<4))
self.lcd_strobe()
self.lcd_device.write((0x01 | (charvalue & 0x0F)<<4))
self.lcd_strobe()
self.lcd_device.write(0x0)
elif self.reverse == 2:
self.lcd_device.write((0x04 | (charvalue >> 4)<<4))
self.lcd_strobe()
self.lcd_device.write((0x04 | (charvalue & 0x0F)<<4))
self.lcd_strobe()
self.lcd_device.write(0x0)
else:
self.lcd_device.write((0x40 | (charvalue >> 4)))
self.lcd_strobe()
self.lcd_device.write((0x40 | (charvalue & 0x0F)))
self.lcd_strobe()
self.lcd_device.write(0x0)
# put char function
def lcd_putc(self, char):
self.lcd_write_char(ord(char))
# put string function
def lcd_puts(self, string, line):
if line == 1:
self.lcd_write(0x80)
if line == 2:
self.lcd_write(0xC0)
if line == 3:
self.lcd_write(0x94)
if line == 4:
self.lcd_write(0xD4)
for char in string:
self.lcd_putc(char)
# clear lcd and set to home
def lcd_clear(self):
self.lcd_write(0x1)
self.lcd_write(0x2)
# add custom characters (0 - 7)
def lcd_load_custon_chars(self, fontdata):
self.lcd_device.bus.write(0x40);
for char in fontdata:
for line in char:
self.lcd_write_char(line)
class tmp102:
def __init__(self, addr, port):
self.sensor = i2c_device(addr, port)
# read a register
def read_reg(self, reg):
return self.sensor.read_nbytes_data(reg, 2)
# read the current temp in celsius
def read_temp(self):
tempraw = self.read_reg(0)
return tempraw[0] + (tempraw[1] >> 4) * 0.0625
view raw pylcd.py hosted with ❤ by GitHub
eLinux has a guide on how to build and install the kernel, to get a updated kernel with the drivers use this git repository: https://github.com/bootc/linux
This library makes it very simple to use both these devices with the RasPi. Before using the library, you must load the i2c-dev module. At the time at which I started this project, the default Raspberry Pi linux tree was outdated and did not include spi and i2c userspace drivers.

After you get your new kernel installed, load the i2c-dev module, and place pylcd.py in whichever directory your working in, then we can move on to a simple sample program.


'''
Copyright (C) 2012 Matthew Skolaut
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''
import pylcd
# Create instances of the tmp102 temp sensor and io
# expander controlling lcd
tmp102 = pylcd.tmp102(0x48, 0) # address, bus#
lcd = pylcd.lcd(0x38, 0)
while 1:
lcd.lcd_puts(" Temp C: " + str(tmp102.read_temp()), 2)
view raw lcd_temp_rpi.py hosted with ❤ by GitHub


This is just a simple temperature display program using both the lcd and the tmp102. Despite being hard to see, the RasPi is connected through the i2c0 port with GPIO0 as SDA, and GPIO1 as SCL. Hopefully this library can help a few people get further with the RasPi and be part of some great projects. In the following weeks I plan on posting more content centered around the Raspberry Pi and other projects. If you use this in your project, please contact me, I would like to see how this libraries being put to use. Good Luck!




Links:

https://gist.github.com/2962884 The libraries somewhat regularly updated gist