Uncategorized

MCP23s17

how to use MCP23S17 pin expander via SPI

Ask QuestionAsked yesterdayActive yesterdayViewed 23 times1

I’m working on a custom sensor that is wired to a couple of MCP23S17 gpio expanders, which are in term connected to a Raspberry Pi 4 via the SPI pins i.e. SPI_MOSI, SPI_MISO, SPI_SCLK and CE0 pin.

I’m trying to read/write to these expanded pins via some python code, preferably using ‘wiringpi’ but I can’t seem to find any information about using these expander boards with SPI, all the tutorials I can find talk about connecting it via I2C in stead.

More specifically, I’d like to use SPI to address the expander pins and read analog voltages. I’ll include an example of my Python code for clarity’s sake:

selectorPins = [65, 66, 67, 68, 69, 70, 71, 72]
analogReadPins = [89, 90, 91, 92]

i2c1_pin_base = 65
i2c1_address = 0b001
i2c2_pin_base = 81
i2c2_address = 0b000

wiringpi.wiringPiSetup()
wiringpi.mcp23017Setup(i2c1_pin_base, i2c1_address)
wiringpi.mcp23017Setup(i2c2_pin_base, i2c2_address)

for x in selectorPins:
    wiringpi.pinMode(x, 1)
for x in analogReadPins:
    wiringpi.pinMode(x, 0)

# select sensor
wiringpi.digitalWrite(selectorPins[0], 0)
wiringpi.digitalWrite(selectorPins[1], 1)
# ...

sensorValue = wiringpi.analogRead(analogReadPins[2])
# ...

Though this obviously doesn’t work since the expanders are connected via SPI and I’m not using I2C at all. Indeed If i try to run this I just get an error

Unable to open I2C device: No such file or directory

I’m running raspbian buster lite and have SPI set up properly (I hope) as I can do

pi@mySensorPi:~ $ ls -l /dev/spi*
crw-rw---- 1 root spi 153, 0 Oct 15 11:59 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 Oct 15 11:59 /dev/spidev0.1

I’m not sure if I’m missing something, I haven’t worked with gpio expanders before. Any help and info is welcome!

P.S. the wiring is embedded in a custom board for this sensor so changing the pins is a costly option I’d like to avoid if possible.pythonspiwiringpishareedit  follow  closeflag asked yesterdayWARdd4166 bronze badges

  • 1Have you tried to search the web? There is a lot of info around. For analogue inputs you will need an A/D converter and there is loads of info about mc23s17 on Pi when searching for ‘mcp23s17 python raspberry pi’. – Dirk yesterday 
  • 1@Dirk can the MCP23S17 not read analog inputs directly? If not I’m afraid our chip designer has messed up – WARdd yesterday 
  • @WARdd, Some years back, I successfully used Rpi IDLE python to play with SPI MCP23S08 and MCP23S17, using through hole chips. Afterwards I found almost everybody is playing with I2C MCP23017 for the lazy reason that there is cheapy assembled module using I2C MSP23017 but unluckily almost no module for MSP23S17 (very expensive or hard to find). So I switched to I2C MCP23017. As you might have already noticed, S17 and 017 have the same internal architecture, and only difference is the communication interface SPI, or I2C. / to continue, … – tlfong01 yesterday    
  • I forgot if I followed AdaFruit or Mag Pi magazine’s tutorial on MCP23S08 or MCP23S17. A useful trick for newbies is (1) Do python SPI loop back test, to make sure software and hardware (wiring) setup is OK. (2) Use SPI to access some registers, usually address 0x00 config register. One you have have done (1) and (2), then you can borrow Arduino or Rpi’s S17 or 017 library’s functions to do the tasks you want. – tlfong01 yesterday   
  • Another useful or time saving trick is try to write you read/write register functions/methods as high level as possible, because you will find that those functions can be shared between S17 and 017. Now a warning to newbies. MCP23S17/017 has very steep learning curve, because the register naming is very complicated. My advice is NOT to start with S08 or 008, because the S08 functions are very tedious to scale up to 017/S17. So you might like to start one through hole S17 and use a bread board to experiment. – tlfong01 yesterday   
  • I heard that wiring pi is no longer supported on Rpi4. So if you aim for long term, say 5 years, think carefully if you really want to start with wiring pi. AdaFruit MCP23017 library also has its problems, because they are no longer keen to support Rpi. They prefer M0 Circuit Python. – tlfong01 yesterday   
  • I very much agree with you that it is very costly to change wiring of S17 pins. But I don’t think you need to do that, because in the old days before Rpi4B, everybody is using the standard pins. But if your board is using software SPI, and you don’t have enough documentation, then you might be in big trouble. – tlfong01 yesterday   
  • One more thing before I go – in case you could not find S17 modules (through hole version are good for newbies to learn, but very tedious and error prone when you scale up) in your place, you might try AliExpress Aideepen Direct Store:SPI MCP23S17 10MHz Module 16-Bit I/O Expander Module US$1.21 – AliExpress Aideepen Direct Store aliexpress.ru/item/4000158844766.html. Good luck and cheers. – tlfong01 yesterday    

add a comment

Categories: Uncategorized

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.