Digital display using an MCP23017

I wanted to test the I2C bus on the RPi so I needed a project.  I had an MCP23017 chip lying around from when I bought a starter kit from SK Pang Electronics, so I hooked it up to some breadboard.  The MCP23017 is a 16-bit I/O port expander that is driven by I2C (http://ww1.microchip.com/downloads/en/devicedoc/21952b.pdf).  I took an old VCR apart recently and pulled out the 4 digit LED display, I sat down with a multimeter and worked out the functions of the 12 input pins.  It turns out that it is a common anode type, there are 5 pins to control each separate digit and semi-colon and 7 pins for the segments and so I hooked it up to the MCP23017 on the breadboard.

LED Display

The picture shows my RPi connected to a Gertboard (it is a simple way of accessing the pins without taking the lid off my case).  The MCP23017 is powered by the 5V0 line from the RPi (bottom right of the Gertboard) and pins GPIO0 (SDA) and GPIO1 (SCL) are controlling the chip.  I used 5V to give the LEDs a bit more brightness.

To code the display I needed to light each digit up and then switch it off again quickly to light up the next digit.  The theory is described by Gordon Henderson in his blog. I coded the display using Python using the WiringPi2 wrapper.  Here’s the code (it’s not pretty but it works):


import wiringpi2

pin_base = 100
i2c_addr = 0x20
delay = 1
pins = [100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115]
wiringpi2.wiringPiSetup()
wiringpi2.mcp23017Setup(pin_base,i2c_addr)

def displayDigit(digit):
 if digit == 0:
 binaryDigit = '0000001'
 elif digit == 1:
 binaryDigit = '1001111'
 elif digit == 2:
 binaryDigit = '0010010'
 elif digit == 3:
 binaryDigit = '0000110'
 elif digit == 4:
 binaryDigit = '1001100'
 elif digit == 5:
 binaryDigit = '0100100'
 elif digit == 6:
 binaryDigit = '0100000'
 elif digit == 7:
 binaryDigit = '0001111'
 elif digit == 8:
 binaryDigit = '0000000'
 elif digit == 9:
 binaryDigit = '0000100'
 for i in range (0, len(binaryDigit)):
 wiringpi2.digitalWrite(100 + i, int(binaryDigit[i]))

def displayDigits(digits):
 digits = str(digits).zfill(4)
 for i in range (0, len(digits)):
 displayDigit(int(digits[i]))
 wiringpi2.digitalWrite(108 + i, 1)
 wiringpi2.delay(delay)
 wiringpi2.digitalWrite(108 + i, 0)

for pin in pins:
 wiringpi2.pinMode(pin,1)
 wiringpi2.digitalWrite(pin, 0)

number = int(input("Enter a 4 digit number\n"))
while number >= 0:
 for i in range (0, 54):
 displayDigits(number)
 number = number - 1
print "Finished"

Here’s a picture of it working:

LEDs

And finally here’s a (very messy) Fritzing diagram of the wiring.

LED Display_bb

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s