MCP ExplorerExplorer

Esp Idf I2c Mcp23017

@JoSmith40on 9 months ago
1 Apache-2.0
FreeCommunity
AI Systems
Demonstration app for ESP32 using improved I2C API in ESP-IDF 5.4.

Overview

What is Esp Idf I2c Mcp23017

esp-idf-i2c-MCP23017 is a demonstration application designed for the ESP32 microcontroller using the ESP-IDF framework. It utilizes the I2C protocol to interface with the MCP23017 I2C port expander, allowing for the expansion of GPIO pins.

Use cases

Use cases include expanding the number of inputs and outputs for projects like sensor monitoring systems, controlling LED displays, and managing multiple switches in a compact design.

How to use

To use esp-idf-i2c-MCP23017, customize the configuration by defining the I2C pins and address in the code. You can set the SCL and SDA pins, the MCP23017 address, and the I2C frequency. After configuring, build and flash the application to the ESP32 board.

Key features

Key features include support for multiple ESP32 targets, compatibility with the newer I2C API in ESP-IDF 5.4, and the ability to configure 8 inputs and 8 outputs using the MCP23017.

Where to use

esp-idf-i2c-MCP23017 can be used in various applications requiring additional GPIOs, such as home automation, robotics, and IoT devices that need to control multiple sensors or actuators.

Content

Supported Targets ESP32-C2/C3 ESP32-C5/C6 ESP32-H2 ESP32-P4 ESP32-S2/S3 T-Display-S3/…S3-AMOLED

esp-idf-i2c-mcp23017 new 2025

List of contents

Overview

This is a demonstration application for the ESP32 in the ESP-IDF environment V5.4 with the newer I2C-API i2c_master.
In ESP-IDF 5.4, Espressif has revised and improved the I²C API. Therefore, the header file mcp23017.h had to be adapted considerably.

This API structure with “handles” and configuration objects is typical for the newer ESP-IDF I²C API and differs from the older API:

i2c_master_bus_handle_t bus_handle;
i2c_master_bus_config_t bus_config = { ... };
err = i2c_new_master_bus(&bus_config, &bus_handle);

For more information on the I²C address and other I²C devices, see here:
(https://i2cdevices.org/addresses)

Software Required

This code has been updated and compiled with the following versions:

  • VSCode: March 2025 (version 1.99.1)
  • ESP-IDF 5.4.1
  • CMake version 3.30.2

Hardware Required

  • ESP32 board
  • I2C port expander MCP23017
  • 2x 4K7 resistors
  • USB cable
  • cable for I2C (V+, GND, SDA und SCL)

How to use this project

You can customize this configuration for your project:

#define I2C_SCL 17
#define I2C_SDA 18
#define MCP23017_ADDR 0x20
#define I2C_FREQ_HZ 400000 // 400 kHz (100 ... 800 kHz)

Configuration MCP23017 for this System

  • 8x Inputs
  • 8x Outputs

The 8 inputs (port A) are checked and output at port B.

Build and Flash

In the footer, you need to review and adjust the following settings:

  1. Interface Type
  2. The port where the ESP32 board is connected
  3. the ESP32 type

The I²C bus allows the use of multiple slaves (FRAMs, LCD, sensors, port extensions, etc.).
Some I²C-devices have pins A0, A1 and A2 for address configuration. Other I²C slaves do have enable pins (CS). This is where you can use GPIO pins.

Attention! The hardware addressing must match the software!
This shows how to datasheet: doc/mcp23017.pdf

Show animation:
fusszeile.gif

Example Output

I (276) main.c says: MCP23017 initialized: NO ERROR
I (276) main.c says: MCP23017 Address: 0x20
I (276) main.c says: I2C Speed: 400 kHz
I (286) main.c says: INTCAP-Wert beim Interrupt: 0x7F
I (286) main.c says: MCP23017 config ok NO ERROR
I (296) main.c says: MCP23017 Port A = INPUTS, Interrupts active LOW
I (296) main.c says: MCP23017 Port B = OUTPUTS

I (156386) main.c says: Time measurement: Current iteration: 347 µs, Average: 349 µs
I (158126) main.c says: Port B: 0xBF (Bits: 10111111)

ESP-IDF Application Size Analysis

The ESP-IDF Application Size Analysis tool provides a detailed breakdown of your application’s memory usage, helping developers optimize storage allocation.

Navigate to:
doc/ESP-IDF_ Size.gif

Press: shift + > type: ESP-IDF: Size Analyse of the Binaries





doc/esp-idf-size-analysis.png

Special thanks

to NSBum. He provided the older code that I modified/adapted.
https://github.com/NSBum/

Tools

No tools

Comments

Recommend MCP Servers

View All MCP Servers