为了让大家能在项目中更简单的使用LCD屏幕, 我们设计开发了这块扩展板, 支持16x2 LCD屏幕, 3个背光引脚和5个按键引脚, 却只占用树莓派上的两个I2C引脚! 无需布线就能添加显示屏的绝佳方案.
这板子在树莓派 - Model A和B上都可用.
套件中一共有三个电阻. 板子上标记RED和BLUE的地方需要用1/4W 5% 220欧姆的电阻 (红, 红, 棕, 金). 标记GREEN的需要用1/4W 5% 330欧姆的电阻 (橙, 橙, 棕, 金).
There is one of these MCP23017 i2c (16 input/output) port expander chips in the kit. This is how we are able to only use 2 R-Pi pins to run the entire LCD and buttons. Click here for more info on this chip.
5) Male Header Pins
There is one strip of 36 male header pins in the kit. These will be used to attach the LCD to the PCB.
6) Printed Circuit Board
There will be one PCB in the kit as shown above.
7) Raspberry Pi Plate Header
There will be one extra-tall 26 pin female header for plugging into the Pi
There will be one rubber bumper as shown above
You can also use 16x2 LCDs or even OLEDs that are the same size that do not have an RGB backlight, or have no backlight at all.
The backlights will not turn on until you have the code running so don't be alarmed if nothing lights up when you plug in the plate! If you have a 'positive' style LCD, you may see some squares on the first line, that's normal
For a more basic introduction to setting up I2C on your Pi then you may wish to take a diversion to this Adafruit tutorial: http://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c
If you're using Occidentalis, you can skip this next step, but for Raspbian users, edit /etc/modules ( sudo nano /etc/modules) and add:
to the end of the file. Then save and reboot to enable the hardware I2C driver.
Before you can get started with I2C on the Pi, you'll need to run through a couple quick steps from the console. Just enter the following commands to add SMBus support (which includes I2C) to Python:
sudo apt-get install python-smbus sudo apt-get install i2c-tools
i2c-tools isn't strictly required, but it's a useful package since you can use it to scan for any I2C or SMBus devices connected to your board. If you know something is connected, but you don't know it's 7-bit I2C address, this library has a great little tool to help you find it:
sudo i2cdetect -y 0 (if you are using a version 1 Raspberry Pi) sudo i2cdetect -y 1 (if you are using a version 2 Raspberry Pi)
This will search /dev/i2c-0 or /dev/i2c-1 for all address, and if an Adafruit LCD Plate is connected, it should show up at 0x20
Once both of these packages have been installed, you have everything you need to get started accessing I2C and SMBus devices in Python.
The LCD Pi Plate Python code for Pi is available on Github at https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code
The easiest way to get the code onto your Pi is to hook up an Ethernet cable, and clone it directly using 'git', which is installed by default on most distros. Simply run the following commands from an appropriate location (ex. "/home/pi"):
sudo apt-get install git git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git cd Adafruit-Raspberry-Pi-Python-Code cd Adafruit_CharLCDPlate
You will also need to install RPi.GPIO, the python library for Pi that allows easy GPIO access. On Raspbian, just run
sudo apt-get install python- dev sudo apt-get install python-rpi. gpio
Once the code has be downloaded to an appropriate folder, and you have your LCD Plate properly connected, you can start with the basic demo display, which is run by simply executing the library file:
sudo python Adafruit_CharLCDPlate.py
If you have a rev 2 (512MB) Pi, or if you're not getting anything displaying, it might be due to the I2C bus number change in the Pi hardware. Edit Adafruit_CharLCD.py using a command like "nano Adafruit_CharLCD.py" and change the line
lcd = Adafruit_CharLCDPlate(busnum = 0)
lcd = Adafruit_CharLCDPlate(busnum = 1)
The plate uses a character LCD with an external contrast potentiometer. The first time you use it, adjust the potentiometer in the bottom right until you see the text clearly. If you don't upload code to the Pi, some boxes may appear instead, or you may see nothing at all.
Using the library code
Interfacing with the python example code is fairly easy! Inside the Adafruit_CharLCDPlate folder you'll find a testLCD.py python script. The script does a few things, at the top it imports all the sub-modules. You'll need to have the Adafruit_I2C.py, Adafruit_MCP230xx.py and Adafruit_CharLCDPlate.pypython files to be in the same directory so be sure to copy them to your final destination.
Next it initializes the plate with lcd = Adafruit_CharLCDPlate() - this creates the lcd object and starts communicating with the plate to set up the LCD and buttons
After initialization, you can clear the display with lcd.clear() and write text with lcd.message("text goes here") don't forget that its only 16 characters per line, and it does not automatically wrap lines. To insert a newline use the special '\n' character such as shown in the command: lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
Next you can set the backlight with lcd.backlight(lcd.COLORNAME) where COLORNAME is RED, YELLOW, GREEN, TEAL, BLUE, VIOLET for RGB LCD's or for monochrome LCDs, just use ON and OFF
Finally, you can ask the plate which buttons are pressed with buttonPressed(lcd.BUTTONNAME) where BUTTONNAME is LEFT RIGHT UP DOWN or SELECT. This is not an interrupt-driven library so you can't have an interrupt go off when a button in pressed, instead you'll have to query the button in a loop.
That's it! If you want to make detailed messages, use the python string formatting commands to create a string and then write that string with message()
Adafruit's Pi Python codebase
Check the Usage page for how to install the example python code directly from your Pi using git.