ephestione
Posts: 31
Joined: Wed Aug 15, 2018 10:46 am

How to connect and use GPIO expansions on CJMCU-2317 with MCP23017?

Fri Aug 16, 2019 3:46 pm

I know there ARE guides out there because I've been spending the last few hours browsing pages and articles from google results.
I didn't find anything linear enough for me to understand though, so I post here in hopes that someone is able to hit me with the "got it!" revelation.

Prologue: I've programmed an alarm system using PHP, I love how you can access anything GPIO under /sys/class/gpio/ and really use that adagio that goes "in linux everything is a file".
I can just fopen() fread()/fwrite() and rewind() to access GPIO in an extremely fast manner.
Now tho I ended using all the proper GPIO pins, and either I use as input/output the other alternate function pins as well (I don't want to because I plan to use those for their own function in the future) or I finally put to use the CJMCU-2317 board that I have lying around.

I understand that I should connect (from expansion board to Pi3):
VCC-3v3
GND-GND
SCL-SCL
SDA-SDA
Reset-(what here? optional?)

But after that my brain simply gets lost.
I mean, I get what is the serial interface, why RX goes to TX and vice-versa, what is the baudrate and all... but I don't know what SCL and SDA are, what INTA and INTB are and if/how they should be connected, how do I set A0-A1-A2 if I need multiple expansions and I do even connect multiple expansions (all in parallel to SCL and SDA?) and most importantly, what is this

Code: Select all

sudo i2cset -y 1 0x20 0x00 0x80
nonsense that I keep reading everywhere?
When those additional pins are functional, shouldn't I be able to access them under /sys/class/gpio/ as well, or something similar, in a nicely numbered fashion, in other words won't I be able to treat them as files to write and read from within PHP?

I really need some guide that makes sense to me.

ephestione
Posts: 31
Joined: Wed Aug 15, 2018 10:46 am

Re: How to connect and use GPIO expansions on CJMCU-2317 with MCP23017?

Fri Aug 16, 2019 3:58 pm

Addendum to my own post: regarding the address, A0-A1-A2 are the "address bits", so being a 3-bit address there can be from 0 to 7, that is maximum 8 expansion boards, each one should have its own address set with those bits by putting them either LOW (0) or HIGH (1), each board having a different combination.
But... do you connect the SCL-SDA of each board in parallel to the SCL-SDA of the Pi?
Still, there's the most important point missing, how do I access those pins from PHP in a way that doesn't involver system calls with exec() but rather file reads with fopen()


EDIT: yes, SCL and SDA are connected in parallel between the Pi and every other I2C device, an each one must have its own unique address set via the An pins.

Now I just miss the final step.

ephestione
Posts: 31
Joined: Wed Aug 15, 2018 10:46 am

Re: How to connect and use GPIO expansions on CJMCU-2317 with MCP23017?

Fri Aug 16, 2019 5:04 pm

Sooo, I just furthered my search with other keywords, and the problem in itself is not accessing the MCP23017 specifically, but rather accessing I2C devices with anything that is not bash's i2ctools or python with its bus read/write functions.
As per this thread:
https://www.raspberrypi.org/forums/view ... &p=1520239
there SHOULD be a way to use purely PHP functions to access the bus as a file and then seek inside it to write/read an address, but while apparently the write part works, the reading doesn't.
The one user who said they had a great solution, ended up not sharing any solution at all :roll:

Basically, since those additional GPIO's on the MCP23017 board are on a totally different hardware interface, the raspi doesn't see them as GPIO's at all, but rather as a generic I2C device attached to it, so there won't be any files anywhere like those in the /sys/class/gpio/ path, but you need to use a completely different method to access them, and even if it was feasible within PHP, that doesn't cope at all with an existing code where pin numbers are associated to sensors in an array, and you use for all of them the same method of reading in /sys/class/gpio/. You would have to differentiate, in the code, between the sensors connected to hardware GPIO, and the sensors on the I2C bus, and use completely different reading code altogheter, which means also no EDGE, probably no way to latch onto stream_select(), hence making the code extremely slow by and in itself.
Better just strip all the SPI and I2C pins off their special functions and use them as GPIO then, that makes for 7 additional inputs/outputs.
I need the serial for when I'll finally get to work the GSM module, otherwise that would be 2 additional pins as well.
Heck I could use the GSM module on a usb-ttl adapter and still keep those 2 pins...

Return to “Interfacing (DSI, CSI, I2C, etc.)”