MCP230xx IO Expander

One problem with using a character LCD is that you give up a lot of GPIO pins to talk to the LCD. Four pins are used to send data, two pins are used for write and clock signals, and another pin or three are used for the backlight for a total of ~6-9 total pins! On a platform like the Raspberry Pi model A/B with only a dozen or so GPIO pins you can quickly run out of pins for other parts of your project. However with chips like the MCP23008 or MCP23017 you can easily add extra GPIO pins to your development board through an I2C interface!

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.

BeagleBone Black

Wire your MCP23017 and LCD as follows:
  • 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
You should see a simple demo of the LCD displaying text, cursors, and scrolling just like the char_lcd.py demo described on the previous page.
If you open char_lcd_mcp.py in a text editor you can see how to use the character LCD library with the MCP chip. Below is an overview of the file:
import math
import time

import Adafruit_CharLCD as LCD
import Adafruit_GPIO.MCP230xx as MCP
First the required modules are imported. Notice the addition of the Adafruit_GPIO.MCP230xx module (imported under the name MCP). This MCP230xx class will be used to talk to the MCP chip and interface with the LCD.
# 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
Next the LCD pins are defined in the script. Note that these pin numbers are the MCP chip GPIO pin numbers, and NOT Raspberry Pi/BeagleBone Black pin numbers!
# 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)
Next an instance of the MCP23017 class is created. By default this class will use the I2C address 0x20 (default for MCP chips) and appropriate I2C bus for the running development board. However if you need to override the address or bus number you can see in the commented line how these are passed as parameters.

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)
Now the Adafruit_CharLCD class instance is created. The big difference with this line and previous examples is that the MCP23017 class (created with the name 'gpio') is passed in as the gpio parameter. By passing in an explicit gpio parameter, the char LCD class will use that GPIO class for talking to the LCD instead of the default development board GPIO pins.

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!
Last updated on 2015-05-04 at 04.27.56 PM Published on 2014-07-22 at 01.35.21 PM