I2C not sending anything

Hasan Shadi
Posts: 26
Joined: Tue Mar 31, 2020 7:26 pm

I2C not sending anything

Postby Hasan Shadi » Mon May 11, 2020 8:18 pm

I have been trying to get the I2C working properly for the last couple days, the code is very simple, it just initializes the I2C driver, and sends a message frequently in a loop. This is the code:

Code: Select all


#include <esp_event.h>
#include <driver/i2c.h>

void sendMessageTo(const unsigned char registerNumber, const unsigned char command,
     unsigned char write, unsigned char* const readBuffer, size_t readBufferSize) { 

        i2c_cmd_handle_t cmdHandler = i2c_cmd_link_create();
        i2c_master_start(cmdHandler);
        i2c_master_write_byte(cmdHandler, (MPU6050Address << 1) | !write, 1);
        printf("%s\n", !write ? "Reading" : "Writing");
        i2c_master_write_byte(cmdHandler, registerNumber, 1);
        if (write) {
            i2c_master_write_byte(cmdHandler, command, 1);
        } else {
            printf("Going to read!\n");
            for (unsigned char i = readBufferSize ; i != 0 ; i--) {
                printf("Requesting another register with index of %d\n", i);
                i2c_master_read_byte(cmdHandler, readBuffer+(readBufferSize-i), 
                (i-1) == 0 ? I2C_MASTER_NACK : I2C_MASTER_ACK); 
                // try the NACK and last_ACK
            }
        }
        i2c_master_stop(cmdHandler);
        i2c_master_cmd_begin(I2C_NUM_0, cmdHandler, pdMS_TO_TICKS(5));
        i2c_cmd_link_delete(cmdHandler);

}

    inline void init_master() { // this has to be called once!

        i2c_config_t masterConfig;
        masterConfig.mode = I2C_MODE_MASTER;
        masterConfig.master.clk_speed = 100000;
        masterConfig.sda_io_num = (gpio_num_t)12;
        masterConfig.scl_io_num = (gpio_num_t)14;
        masterConfig.scl_pullup_en = (gpio_pullup_t)1;
        masterConfig.sda_pullup_en = (gpio_pullup_t)1;
        ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &masterConfig));
        ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
        
        sendMessageTo(MPU6050ResetRegister, MPU6050DisableTempSensor, 1, 0, 0);
        sendMessageTo(MPU6050AccelConfigRegister, 0, 1, 0, 0);
        sendMessageTo(MPU6050GyroConfigRegister, 0, 1, 0, 0);
        printf("The master has finsihed configuration!\n");

    }

}

void setup(void);
void loop(void);

void app_main() {

    setup();
    for (; ;) {
        loop();
    }

}

struct timeval getCurrentTime() {

    struct timeval t;
    gettimeofday(&t, NULL);
    return t;

}

void setup() {
    
    init_master();

}

void loop() {

    unsigned char* buffer = (unsigned char*)calloc(2, 1);
    sendMessageTo(MPU6050XAccelOutputRegister, 0, 0, buffer, 2);
    free(buffer);
    printf("\n\n");
    vTaskDelay(pdMS_TO_TICKS(100));

}
All you need to take care of is that I am not getting anything from the "Digital MultiMeter" that I am using to monitor the voltage and everything from the assigned pins, which are SCL and SDA. The voltage is 3.3V exactly, it had not changed since I restarted the ESP32. I am using ESP-IDF 4.0 Release version to program it. I am also using Windows 8.1 along with an ESP32-NodeMCU, I have attached a picture of it. the pins are: GPIO 12 as SDA and 14 as SCL. Even If you are wondering that it won't work unless you connect it to slave or something, I have tried that, still getting same results. If I have said some information that is wrong or a misunderstanding from me, I hope you correct me.
Attachments
ESP32_NodeMCU.PNG
ESP32 NodeMCU
ESP32_NodeMCU.PNG (297.54 KiB) Viewed 3562 times

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: I2C not sending anything

Postby ESP_Sprite » Tue May 12, 2020 8:08 am

Note a voltmeter is the wrong tool for the job here: I2C pulses are relatively fast and a normal I2C bus is mostly high: a digital multimeter is likely to average the voltage over a fairly long time and as such 'miss' the I2C traffic. If you have a LA or oscilloscope, you're more likely to diagnose the issue.

User avatar
jgustavoam
Posts: 165
Joined: Thu Feb 01, 2018 2:43 pm
Location: Belo Horizonte , Brazil
Contact:

Re: I2C not sending anything

Postby jgustavoam » Tue Jun 23, 2020 2:44 pm

Hi,
Good tool to verify that the I2C interface is working well is the I2C scanner.

ESP32 - I2C Scanner using Arduino IDE (working)
viewtopic.php?f=18&t=4742

If you want to decode signals, use a cheap logic analyzer.
Retired IBM Brasil
Electronic hobbyist since 1976.

Who is online

Users browsing this forum: No registered users and 348 guests