If you aren't familiar with the MCP230xx series of chips, there's a great guide that describes their usage with a Raspberry Pi. Note that you don't need to install the library or code from the guide, it's only provided for reference.
Wiring
To use an MCP230xx chip with a character LCD you will need to wire the MCP chip to your board's I2C pins, and then wire the LCD to the MCP chip. Below are examples of wiring an MCP23017 to the Raspberry Pi or BeagleBone Black.If you'd like to use an MCP23008 instead of the MCP23017 the wiring is similar, however consult the MCP23008 datasheet to see which pins are for power, ground, I2C, and GPIO.
Raspberry Pi
Wire your MCP23017 and LCD as follows:-
Connect Pi 3.3V power to the power rail of the breadboard, and connect the MCP VDD and RESET to 3V power. Be careful to connect the 3.3 volt and not 5 volt power to these pins!
- Connect Pi ground to the ground rail of the breadboard, and connect the MCP VSS and address pins, one outer lead of the potentiometer, the LCD ground, and the LCD R/W pin to the ground rail.
- Connect Pi 5V power to the other outer lead of the potentiometer, LCD power, and LCD backlight+ pins. Note that 5 volt and not 3.3 volt power is used to power the LCD!
- Connect the middle pin of the potentiometer to the LCD contrast/V0 pin.
- Connect Pi I2C SCL to MCP I2C SCL.
- Connect Pi I2C SDA to MCP I2C SDA.
- Connect MCP GPIOA0 to LCD RS.
- Connect MCP GPIOA1 to LCD EN/clock enable.
- Connect MCP GPIOA2 to LCD DB4.
- Connect MCP GPIOA3 to LCD DB5.
- Connect MCP GPIOA4 to LCD DB6.
- Connect MCP GPIOA5 to LCD DB7
- Connect MCP GPIOA6 to LCD -R/red backlight.
- Connect MCP GPIOA7 to LCD -G/green backlight.
- Connect MCP GPIOB0 to LCD -B/blue backlight.
Make sure you've enabled I2C on the Raspberry Pi if you haven't done so already.
- Connect BeagleBone Black 3.3V power pin P9_3 to the power rail of the breadboard, and connect the MCP VDD and RESET to 3V power. Be careful to connect the 3.3 volt and not 5 volt power to these pins!
- Connect BeagleBone Black ground pin P9_1 to the ground rail of the breadboard, and connect the MCP VSS and address pins, one outer lead of the potentiometer, the LCD ground, and the LCD R/W pin to the ground rail.
- Connect BeagleBone Black 5V power pin P9_7 to the other outer lead of the potentiometer, LCD power, and LCD backlight+ pins. Note that 5 volt and not 3.3 volt power is used to power the LCD!
- Connect the middle pin of the potentiometer to the LCD contrast/V0 pin.
- Connect BeagleBone Black I2C SCL pin P9_19 to MCP I2C SCL.
- Connect BeagleBone Black I2C SDA pin P9_20 to MCP I2C SDA.
- Connect MCP GPIOA0 to LCD RS.
- Connect MCP GPIOA1 to LCD EN/clock enable.
- Connect MCP GPIOA2 to LCD DB4.
- Connect MCP GPIOA3 to LCD DB5.
- Connect MCP GPIOA4 to LCD DB6.
- Connect MCP GPIOA5 to LCD DB7
- Connect MCP GPIOA6 to LCD -R/red backlight.
- Connect MCP GPIOA7 to LCD -G/green backlight.
- Connect MCP GPIOB0 to LCD -B/blue backlight.
Note that the BeagleBone Black has two I2C interfaces and this wiring will use the/dev/i2c-1 interface. Make sure there aren't any device tree overlays loaded which use these I2C pins for other purposes. The default BeagleBone Black device tree configuration with no overlays loaded will expose the necessary I2C interface for the wiring above.
Usage
The char_lcd_mcp.py file in the library's examples folder demonstrates the usage of a character LCD (non-RGB version) with an MCP23017 IO extender. Run the script by executing this command inside the examples directory:sudo python char_lcd_mcp.py
import math import time import Adafruit_CharLCD as LCD import Adafruit_GPIO.MCP230xx as MCP
# Define MCP pins connected to the LCD. lcd_rs = 0 lcd_en = 1 lcd_d4 = 2 lcd_d5 = 3 lcd_d6 = 4 lcd_d7 = 5 lcd_backlight = 6 # Optionally if the backlight is not controllable then set: # lcd_backlight = None # Define LCD column and row size for 16x2 LCD. lcd_columns = 16 lcd_rows = 2 # Alternatively specify a 20x4 LCD. # lcd_columns = 20 # lcd_rows = 4
# Initialize MCP23017 device using its default 0x20 I2C address. gpio = MCP.MCP23017() # Alternatively you can initialize the MCP device on another I2C address or bus. # gpio = MCP.MCP23017(0x24, busnum=1)
Also note if you're using an MCP23008 chip you can instead create an instance of the MCP23008 class. This class is exactly the same as the MCP23017 class, but only supports 8 GPIO pins.
# Initialize the LCD using the pins lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight, gpio=gpio)
The rest of the example code is exactly the same as the non-MCP character LCD example. You only need to change the setup and initialization of the character LCD class to use the MCP IO extender with an LCD!
Note you can also use an RGB character LCD with an MCP IO extender, however the MCP IO extender does NOT support PWM control of the backlight!