This project shows you how to use a MCP23008 I/O port expander to extend peripheral support seamlessly. We'll control eight (8) LEDs as an example for using all pings as Output Ports using only two (2) control wires. You'll code as if the LEDs are connected directly to Meadow using Meadow.Foundation's Unified GPIO Architecture.
The MCP23008 chip is an 8-bit (8 port) digital I/O expander chip that communicates over I2C. It provides eight (8) IP ports and can be used to add additional digital input and output ports to an MCU. It can also be combined with up to seven (7) additional MCP23008 chips, providing up to sixty four additional ports.
To connect the MCP23008 chip, consider the Pin Datasheet below.
MCP23008 is a common integrated circuit in the hardware world and is the typical interface chip for hardware such as I2C LCD backpacks. You can read more here.
Meadow.Foundationa platform for quickly and easily building connected things using.NET on Meadow. Created by Wilderness Labs, it's completely open source and maintained by the Wilderness Labs community.
If you're new working with Meadow, I suggest you go to the Getting Started w/ Meadow by Controlling the Onboard RGB LEDproject to properly set up your development environment.
Using MCP23008 pins as output portsStep 1 - Assemble the circuitFor this project, wire up your breadboard and Meadow as shown in the Fritzing diagram:
Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it McpLeds.
Step 3 - Add the required NuGet packagesWindows
Right-click on your McpLeds project and click Manage NuGet Packages. In the Browse tab, search for Meadow.Foundation.ICs.IOExpanders.Mcp23x08 and click Install to add it to your project.
macOS
Alt-click on your McpLeds project in the Solution Explorer, and click Add => Add Nuget Package to open the NuGet Package window. Search for Meadow.Foundation.ICs.IOExpanders.Mcp23x08 and click Install to add it to your project.
Step 4 - Write the code for McpLedsCopy the following code below:
// public class MeadowApp : App<F7FeatherV1> <- If you have a Meadow F7v1.*
public class MeadowApp : App<F7FeatherV2>
{
List<Led> leds;
Mcp23008 mcp;
public override Task Initialize()
{
var onboardLed = new RgbPwmLed(
redPwmPin: Device.Pins.OnboardLedRed,
greenPwmPin: Device.Pins.OnboardLedGreen,
bluePwmPin: Device.Pins.OnboardLedBlue);
onboardLed.SetColor(Color.Red);
mcp = new Mcp23008(Device.CreateI2cBus());
leds = new List<Led>
{
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP0)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP1)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP2)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP3)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP4)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP5)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP6)),
new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP7))
};
onboardLed.SetColor(Color.Green);
return base.Initialize();
}
public override async Task Run()
{
while (true)
{
foreach (var led in leds)
{
led.IsOn = true;
await Task.Delay(500);
}
await Task.Delay(1000);
foreach (var led in leds)
{
led.IsOn = false;
await Task.Delay(500);
}
}
}
}
In Meadow's Initialize
method, you can see how the Mcp23x08
gets initialized, passing all Address pins true since they're all connected to 3V3, and then we populate a list of LEDs using all the general IO ports calling mcp.CreateDigitalOutputPort()
In the the Run()
task, the app steps into an infinite while loop, and turns all LEDs on in one foreach, and the next foreach it turns them off.
Click the Run button in Visual Studio. It should look like to the following GIF:
This project is only the tip of the iceberg in terms of the extensive exciting things you can do with Meadow.Foundation.
- It comes with a huge peripheral driver library with drivers for the most common sensors and peripherals.
- The peripheral drivers encapsulate the core logic and expose a simple, clean, modern API.
- This project is backed by a growing community that is constantly working on building cool connected things and are always excited to help new-comers and discuss new projects.
Comments