ESPIDF I2C: Cannot send the correct address

RobotDragon0
Posts: 4
Joined: Thu Dec 19, 2024 2:16 pm

ESPIDF I2C: Cannot send the correct address

Postby RobotDragon0 » Thu Jun 26, 2025 3:48 pm

Hello,
I am using my EPS32S3 to communicate with a MCP23017-E/SO via I2C. My code is shown below, and this is what I see on my logic analyzerlogic analyzer. The data doesnt make sense. I would expect to at least see it transmit the device address correctly (which is 0x20).

The datasheet says the address should be 0x20 if I am shorting A0,A1, and A2 to ground. This is how my circuit is connected.

What should I change below? Thanks.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "driver/i2c_master.h"
#include "driver/i2c.h"
#include "esp_log.h"
#include "esp_adc_cal.h"
#include "driver/adc.h"
#include "hal/i2c_types.h"
#include "soc/i2c_periph.h"
#include "hal/i2c_hal.h"

#define PIN_NUM_SCL 1
#define PIN_NUM_SDA 2
#define I2C_MASTER_NUM I2C_NUM_0
#define MCP23017_ADDR 0x20
#define GPIOB 0x13
#define I2C_CMD_SIZE_MAX 256

esp_err_t i2c_init(void){
    esp_err_t ret;
    i2c_master_bus_config_t bus_config = {
        .i2c_port = I2C_MASTER_NUM,
        .sda_io_num = PIN_NUM_SDA,
        .scl_io_num = PIN_NUM_SCL,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .flags.enable_internal_pullup = false
    };


    i2c_master_bus_handle_t i2c_bus_handle;


    ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle );
    ESP_LOGI("I2C", "i2c_new_master_bus returned %s", esp_err_to_name(ret));
    if(ret!= ESP_OK)
        return -1;

     i2c_device_config_t dev_config_MCP23017= {
        .dev_addr_length = I2C_ADDR_BIT_LEN_7,
        .device_address = MCP23017_ADDR,
        .scl_speed_hz = I2C_MASTER_FREQ_HZ
    };

    ret = i2c_master_bus_add_device(i2c_bus_handle, &dev_config_MCP23017, &i2c_MCP23017_handle);
    ESP_LOGI("I2C", "i2c_master_bus_add_device %s", esp_err_to_name(ret));
    if(ret!= ESP_OK)
        return -1;
        
    return ESP_OK;
}

esp_err_t i2c_register_write(i2c_master_dev_handle_t dev_handle, uint8_t reg_addr, uint8_t *data, size_t len)
{
    esp_err_t ret;
    ESP_LOGI("I2C", "Transmitting to device");
    uint8_t buffer[I2C_CMD_SIZE_MAX];
    memcpy(buffer, &reg_addr, len);
    memcpy(buffer+1, data, len);
    ret = i2c_master_transmit(dev_handle, buffer, len+1, portMAX_DELAY);
    ESP_LOGI("I2C", "i2c_register_write %s", esp_err_to_name(ret));
    return ret;                 
} 

void app_main() {
    if(i2c_init() == -1){
        ESP_LOGI("I2C", "i2c_init failed");
    }
     while(1){
        ESP_LOGI("I2C", "Writing to GPIOB");
        vTaskDelay(1000);
        uint8_t data = 0xFF;
        i2c_register_write(i2c_MCP23017_handle, GPIOB, &data, 1);
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
}

nopnop2002
Posts: 347
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: ESPIDF I2C: Cannot send the correct address

Postby nopnop2002 » Sun Jun 29, 2025 7:06 am

You can use this to verify that your circuit is correct.

https://github.com/UncleRus/esp-idf-lib ... 7/mcp23017

eriksl
Posts: 199
Joined: Thu Dec 14, 2023 3:23 pm
Location: Netherlands

Re: ESPIDF I2C: Cannot send the correct address

Postby eriksl » Mon Jul 21, 2025 11:09 am

So you're using some library or what? I don't see the problem, really.

Who is online

Users browsing this forum: Bytespider, Google [Bot], PerplexityBot, Qwantbot and 3 guests