docs/lua-modules/mcp23017.md
| Since | Origin / Contributor | Maintainer | Source |
|---|---|---|---|
| 2020-04-10 | Marcel P. | Marcel P. | mcp23017.lua |
This Lua module provides access to the MCP23017 I²C I/O Expander.
The MCP23017 is a port expander and provides 16 channels for inputs and outputs. Up to 8 devices (128 channels) are possible by the configurable address (A0 - A2).
Due to the 16 channels, 2 bytes are required for switching outputs or reading input signals. These are A and B. A single pin can be set or a whole byte.
The numbering of the individual pins starts at 0 and ends with 7. The numbers are for each register GPIO A and GPIO B.
!!! important
The module requires i2c and bit C module built into firmware.
mcp = require "mcp23017"
The example script can be found here
Configures the address of the module and tests the connection to the i2c bus.
The i2c id is required for an existing i2c interface, alternatively the sda and scl pins can be specified.
Then this function will establish the connection.
Automatically resets the device state (see mcp23017:reset())
mcp23017:setup(address, i2c_id)
address address for MCP23017, default: 0x20 (should be between 0x20 and 0x27)i2c_id id for the i2c bus connection (remember to call i2c.setup before)true if device found, otherwise false.
MCP23017 device on address not foundMCP23017 address is out of rangelocal mcp23017 = require "mcp23017"
local address = 0x20
local cSCL = 1
local cSDA = 2
local i2c_instance = 0
-- setup i2c bus and create instance for mcp23017 (assigned to mcp)
i2c.setup(i2c_instance, cSDA, cSCL, i2c.SLOW)
local mcp = mcp23017(address, i2c_instance)
Set the mode of a single channel. This can be OUTPUT or INPUT.
mcp23017:setMode(register, pin, mode)
register the side of channels (GPA or GPB)pin the number to be set for the channel (0-15)mode the mode for the channel. This can be mcp23017.INPUT or mcp23017.OUTPUTtrue, in case of error nil.
-- set pin 7 and 8 to output (GPA7 and GPB0) and GPB1 to input
mcp:setMode(mcp.GPA, 7, mcp.OUTPUT)
mcp:setMode(mcp.GPB, 0, mcp.OUTPUT)
mcp:setMode(mcp.GPB, 1, mcp.INPUT)
Set the state of a single channel. This can be HIGH or LOW.
mcp23017:setMode(register, pin, state)
register the side of channels (GPA or GPB)pin the number to be set for the channel (0-15)state the state for the channel. This can be mcp23017.HIGH or mcp23017.LOWtrue, in case of error nil.
-- set pin 7 to high (GPA7)
mcp:setPin(mcp.GPA, 7, mcp.HIGH)
-- set pin 8 to low (GPB0)
mcp:setPin(mcp.GPB, 0, mcp.LOW)
get the state for a single channel. This can be HIGH or LOW.
mcp23017:getPinState(register, pin)
register the side of channels (GPA or GPB)pin the number for which a state is to be queried (0-15)true for HIGH, false for LOW, in case of error nil.
-- get the state for pin 9 (GPB1)
print(mcp:getPinState(mcp.GPB, 1))
By calling this function, a safe state is established. All channels are set to input. This function can be used for a panic program.
mcp23017:reset()
None
None
-- reset the mcp23017 to startup defaults
mcp:reset()
Enable or disable the internal pullup resistors.
mcp23017:setInternalPullUp(register, byte)
register the side of channels (GPA or GPB)byte byte to set the pullup resistorsNone
-- enable all pullup resistors for GPA
print(mcp:setInternalPullUp(mcp.GPA, 0xFF))
-- disable all pullup resistors for GPA
print(mcp:setInternalPullUp(mcp.GPA, 0x00))
Setup the mode of the channels with a whole byte.
mcp23017:writeIODIR(register, byte)
register the side of channels (GPA or GPB)byte byte to set the mode for all channels for this registerNone
-- set all GPA to input
print(mcp:writeIODIR(mcp.GPA, 0xFF))
-- set all GPA to output
print(mcp:writeIODIR(mcp.GPA, 0x00))
Setup the output state of the channels with a whole byte.
mcp23017:writeGPIO(register, byte)
register the side of channels (GPA or GPB)byte byte to set the state for all channels for this registerNone
-- set all GPA to HIGH
print(mcp:writeGPIO(mcp.GPA, 0xFF))
-- set all GPA to LOW
print(mcp:writeGPIO(mcp.GPA, 0x00))
Read the input states of the channels with a whole byte.
mcp23017:readGPIO(register)
register the side of channels (GPA or GPB)byte with states
-- get states for GPA
print(mcp:readGPIO(mcp.GPA))