Edited by nopnop2002 at 2017-9-15 06:51
I tried python-periphery using 2G-IOT.
https://github.com/vsergeev/python-periphery
You can expand digital INPUT/OUTPUT port to at most 16.
When using a cascade, it's possible to expand to at most 128 ports.
/dev/i2c-0 & /dev/i2c-2 work fine.
But /dev/i2c-1 don't work.
These pins(#27/#28) are assigned as GPIO.
--- mcp23017.py ---
- '''
- python-periphery mcp23017 sample
- 2G_IOT---MCP23017
- -----------------
- Pin#3----SDA
- Pin#5----SCK
- ---------GPA0--- LED1
- ---------GPA1--- LED2
- ---------GPA2--- LED3
- ---------GPA3--- LED4
- ---------GPA4--- LED5
- ---------GPA5--- LED6
- ---------GPA6--- LED7
- ---------GPA7--- LED8
- GND------A0
- GND------A1
- GND------A2
- 3.3V-----RESET
- 3.3V-----VDD
- GND------VSS
- '''
- from periphery import I2C
- import time
- import sys
- argv = sys.argv
- argc = len(argv)
- if (argc == 1):
- device = "/dev/i2c-0"
- if (argc == 2 and int(argv[1]) == 0):
- device = "/dev/i2c-0"
- if (argc == 2 and int(argv[1]) == 1):
- device = "/dev/i2c-1"
- if (argc == 2 and int(argv[1]) == 2):
- device = "/dev/i2c-2"
- print device
- # Open i2c-0 controller
- #i2c = I2C("/dev/i2c-0")
- i2c = I2C(device)
- # Write data to MCP23017
- msgs = [I2C.Message([0x00]), I2C.Message([0x00], read=False)]
- i2c.transfer(0x20, msgs)
- msgs = [I2C.Message([0x01]), I2C.Message([0x00], read=False)]
- i2c.transfer(0x20, msgs)
- msgs = [I2C.Message([0x12]), I2C.Message([0x00], read=False)]
- i2c.transfer(0x20, msgs)
- msgs = [I2C.Message([0x13]), I2C.Message([0x00], read=False)]
- i2c.transfer(0x20, msgs)
- byte=0
- for var in range(0,8):
- byte=byte << 1
- byte=byte+1
- print "byte=",byte
- msgs = [I2C.Message([0x12]), I2C.Message([byte], read=False)]
- i2c.transfer(0x20, msgs)
- time.sleep(1.0);
- for var in range(0,8):
- byte=byte >> 1
- print "byte=",byte
- msgs = [I2C.Message([0x12]), I2C.Message([byte], read=False)]
- i2c.transfer(0x20, msgs)
- time.sleep(1.0);
- i2c.close()
EDIT:I tried python smbus library.
It's work fine.
That's quite easier.