I am trying to write code in lua to control this relay board. I have written working code that is able to switch each relay when given a command. I am having trouble figuring out how to read the values of the relays after I’ve commanded them. This functionality is important for my application.
For context, I am currently using the API codec to send read commands that look like this: AA 03 BC 41 12 BC
According to the documentation, this should read the I2C address 0x20, bank A. The response should look something like: AA 01 00 AB
I’m not getting any response like this. The responses seem sort of random. Can someone help explain what I’m doing wrong and where my misunderstanding is?
Sorry, we don’t have any examples in Lua, but in general, you are working with the MCP23017 chip for relay control, which a quick google search indicated some sample code for Lua. The important thing to remember about the MCP23017 is that it has two types of read functions:
It has the ability to convert the Outputs (for relay control) into inputs, and then perform a read. You don’t want to do this, as it will never return the correct status.
It has the ability to read the status of the Outputs, and this is what you want to focus on as this will give you the readings you are looking for.
Also keep in mind that I2C works by either Writing data to the port or Writing and Reading data to the port. There is not a way to read without a write function, so keep that in mind. You will need to always specify the I2C address in either function. I suggest reading the MCP23017 datasheet as this will have all the details you need for I2C communications. The only thing specific to this relay board is the MCP23017 must be set to Output, which it looks like you have working, and all read operations need to be focused on reading the status of those outputs without converting them to inputs. Hope this helps point you in the right direction.