Expanding Io Ports On Meadow With An Mcp23008

Photo of jorgedevs

Made by jorgedevs

About the project

Sometimes you run out IO ports on your main prototype board. Meadow works seamlessly with different IO expanders, like an MCP23008.

Project info

Difficulty: Easy

Platforms: MicrosoftMeadowWilderness Labs

Estimated time: 1 hour

License: MIT license (MIT)

Items used in this project

Hardware components

Tiny Breadboard Tiny Breadboard x 1
Tiny Breadboard Tiny Breadboard x 1
Jumper Wire Kit - 140pcs Jumper Wire Kit - 140pcs x 1
Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM Raspberry Pi 4 Model B 1GB, 2GB, 4GB or 8GB RAM x 1

Software apps and online services

Microsoft Visual Studio 2019 Microsoft Visual Studio 2019

Story

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 Pinout

MCP23008 Pinout

MCP23008 Pinout

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 ports

Step 1 - Assemble the circuit

For this project, wire up your breadboard and Meadow as shown in the Fritzing diagram:

Connecting an MCP23008 to Meadow

Connecting an MCP23008 to Meadow

Connecting an MCP23008 to Meadow

Step 2 - Create a Meadow Application project

Create a new Meadow Application project in Visual Studio 2019 for Windows or macOS and name it McpLeds.

Step 3 - Add the required NuGet packages

Windows

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 McpLeds

Copy the following code below:

public class MeadowApp : App<F7Micro, MeadowApp>{    List<Led> leds;    Mcp23x08 mcp;    public MeadowApp()    {        Console.Write("Initialize hardware...");        mcp = new Mcp23x08(Device.CreateI2cBus(), true, true, true);        leds = new List<Led>();        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP0)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP1)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP2)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP3)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP4)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP5)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP6)));        leds.Add(new Led(mcp.CreateDigitalOutputPort(mcp.Pins.GP7)));        Console.WriteLine("done.");        CycleLeds();    }    void CycleLeds()    {        Console.WriteLine("Cycle leds...");        while (true)        {            foreach(var led in leds)            {                led.IsOn = true;                Thread.Sleep(500);            }            Thread.Sleep(1000);            foreach (var led in leds)            {                led.IsOn = false;                Thread.Sleep(500);            }        }    }}

In Meadow's Contructor, 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 CycleLeds(), the app steps in an infinite while loop, and turns all LEDs on in one foreach, and the next foreach it turns them off.

Step 5 - Run the project

Click the Run button in Visual Studio. It should look like to the following GIF:

McpLeds project running...

McpLeds project running...

McpLeds project running...

Check out Meadow.Foundation!

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.

References

Schematics, diagrams and documents

McpLeds circuit diagram

Code

McpLeds complete project

You can check the McpLeds project in Meadow.Samples/Source/MeadowSamples/McpLeds

Credits

Leave your feedback...