New domain and blog

New domain and blog
Please head over to my new domain to view my blog and current projects

Sunday, 2 June 2013

Raspberry Pi: MCP23008 Port Expander

I have been wanting to get an MCP23008 I2C port expander connected to my Raspberry Pi for quite a while. I finally got one and during my breaks from LabVIEW CLD exam preparation, made the circuit on some strip board. 


Using the Quick2Wire Python I2C library makes getting this working really quick and easy. Just make sure you place your LED's the correct way around. I spent a bit of time debugging the I2C until I thought of checking the obvious. 

Currently I only have the outputs working as I didn't have any switches with me. I'll add two switches and get that going next. 

Here is the code that I used. I set up a little menu to select which LED to turn on or off. 

#!/usr/bin/python3.2
## Show menu ##
import os
import quick2wire.i2c as i2c
import time
address = 0x20
iodir_register = 0x00
gpio_register = 0x09
choice = 8 # All LED's off
outputStatus = 0x00
with i2c.I2CMaster() as bus:
# set all i/o's to output
bus.transaction(i2c.writing_bytes(address, iodir_register, 0x00))
i = 0
while (i < 10):
bus.transaction(i2c.writing_bytes(address, gpio_register, 0x00))
time.sleep(0.05)
bus.transaction(i2c.writing_bytes(address, gpio_register, 0x3F))
time.sleep(0.05)
i += 1
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
while (choice != 9):
os.system('clear')
print (30 * '-')
print (" M A I N - M E N U")
print (30 * '-')
print ("1. Toggle LED 1")
print ("2. Toggle LED 2")
print ("3. Toggle LED 3")
print ("4. Toggle LED 4")
print ("5. Toggle LED 5")
print ("6. Toggle LED 6")
print ("7. All On")
print ("8. All Off")
print ("9. Quit Program")
print (30 * '-')
## Get input ###
choice = input('Enter your choice [1-9] : ')
### Convert string to int type ##
choice = int(choice)
### Take action as per selected menu-option ###
if choice == 1:
outputStatus = outputStatus ^ 0x01 # 0b00000001
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 2:
outputStatus = outputStatus ^ 0x02 # 0b00000010
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 3:
outputStatus = outputStatus ^ 0x04 # 0b00000100
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 4:
outputStatus = outputStatus ^ 0x08 # 0b00001000
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 5:
outputStatus = outputStatus ^ 0x10 # 0b00010000
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 6:
outputStatus = outputStatus ^ 0x20 # 0b00100000
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 7:
outputStatus = 0x3F
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 8:
outputStatus = 0x00
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
elif choice == 9:
outputStatus = 0x00
bus.transaction(i2c.writing_bytes(address, gpio_register, outputStatus))
print ("Closing program")
else: ## default ##
print ("Invalid number. Try again...")
view raw mcp23008.py hosted with ❤ by GitHub

Next I plan to get the software PWM working so that I can connect up to Cheerlights. Need to do some more studying and then will get that going. Also need some RGB LED's first.

Until next time, happy coding.

Greg

No comments:

Post a Comment