Skip to main content

Meadow.Foundation.ICs.IOExpanders.Mcp23009

Mcp23009
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.ICs.IOExpanders.Mcp23xxx

Code Example

private Mcp23009 mcp;

public override Task Initialize()
{
var interruptPort = Device.CreateDigitalInterruptPort(Device.Pins.D00, InterruptMode.EdgeRising);
var resetPort = Device.CreateDigitalOutputPort(Device.Pins.D01);

mcp = new Mcp23009(Device.CreateI2cBus(), 0x20, interruptPort, resetPort);

return base.Initialize();
}

public override Task Run()
{
while (true)
{
TestBulkDigitalOutputPortWrites(20);
TestDigitalOutputPorts(2);
}
}

private void TestDigitalOutputPorts(int loopCount)
{
var out00 = mcp.CreateDigitalOutputPort(mcp.Pins.GP0);
var out01 = mcp.CreateDigitalOutputPort(mcp.Pins.GP1);
var out02 = mcp.CreateDigitalOutputPort(mcp.Pins.GP2);
var out03 = mcp.CreateDigitalOutputPort(mcp.Pins.GP3);
var out04 = mcp.CreateDigitalOutputPort(mcp.Pins.GP4);
var out05 = mcp.CreateDigitalOutputPort(mcp.Pins.GP5);
var out06 = mcp.CreateDigitalOutputPort(mcp.Pins.GP6);
var out07 = mcp.CreateDigitalOutputPort(mcp.Pins.GP7);

var outputPorts = new List<IDigitalOutputPort>()
{
out00, out01, out02, out03, out04, out05, out06, out07
};

foreach (var outputPort in outputPorts)
{
outputPort.State = true;
}

for (int l = 0; l < loopCount; l++)
{
// loop through all the outputs
for (int i = 0; i < outputPorts.Count; i++)
{
// turn them all off
foreach (var outputPort in outputPorts)
{
outputPort.State = false;
}

// turn on just one
outputPorts[i].State = true;
Thread.Sleep(250);
}
}

// cleanup
for (int i = 0; i < outputPorts.Count; i++)
{
outputPorts[i].Dispose();
}
}

private void TestBulkDigitalOutputPortWrites(int loopCount)
{
byte mask = 0x0;

for (int l = 0; l < loopCount; l++)
{
for (int i = 0; i < 8; i++)
{
mcp.WriteToPorts(mask);
mask = (byte)(1 << i);
Thread.Sleep(5);
}
}
}

Sample project(s) available on GitHub

Wiring Example

To wire a Mcp23009 to your Meadow board, connect the following:

Mcp23009Meadow Pin
GNDGND
VCC3V3
SCLD08 (SCL Pin)
SDAD07 (SDA Pin)

Class Mcp23009

Represent an MCP23009 I2C port expander with open-drain outputs

Assembly: Mcp23xxx.dll
View Source
Declaration
public class Mcp23009 : Mcp23x0x, IDigitalInputOutputController, IDigitalInputController, IDigitalOutputController, ISpiPeripheral, II2cPeripheral, IDigitalInterruptController, IPinController

Inheritance: System.Object -> Meadow.Foundation.ICs.IOExpanders.Mcp23xxx

Implements:
Meadow.Hardware.IDigitalInputOutputController, Meadow.Hardware.IDigitalInputController, Meadow.Hardware.IDigitalOutputController, Meadow.Hardware.ISpiPeripheral, Meadow.Hardware.II2cPeripheral, Meadow.Hardware.IDigitalInterruptController, Meadow.Hardware.IPinController

Methods

CreateDigitalOutputPort(IPin, bool)

Creates a new DigitalOutputPort using the specified pin and initial state

View Source
Declaration
public IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false)
Returns

Meadow.Hardware.IDigitalOutputPort: IDigitalOutputPort

Parameters
TypeNameDescription
Meadow.Hardware.IPinpinThe pin number to create the port on
System.BooleaninitialStateWhether the pin is initially high or low

CreateDigitalOutputPort(IPin, bool, OutputType)

Creates a new DigitalOutputPort using the specified pin and initial state

View Source
Declaration
public override IDigitalOutputPort CreateDigitalOutputPort(IPin pin, bool initialState = false, OutputType outputType = OutputType.OpenDrain)
Returns

Meadow.Hardware.IDigitalOutputPort: IDigitalOutputPort

Parameters
TypeNameDescription
Meadow.Hardware.IPinpinThe pin number to create the port on
System.BooleaninitialStateWhether the pin is initially high or low
Meadow.Hardware.OutputTypeoutputTypeThe output type

Implements

  • Meadow.Hardware.IDigitalInputOutputController
  • Meadow.Hardware.IDigitalInputController
  • Meadow.Hardware.IDigitalOutputController
  • Meadow.Hardware.ISpiPeripheral
  • Meadow.Hardware.II2cPeripheral
  • Meadow.Hardware.IDigitalInterruptController
  • Meadow.Hardware.IPinController