I2C 2 sensors read back to back problem.

Shoyur
Posts: 3
Joined: Tue Nov 07, 2023 6:25 pm

I2C 2 sensors read back to back problem.

Postby Shoyur » Tue Nov 07, 2023 7:13 pm

I hope its not the wrong forum for this question.
Anyone has an idea why i cant read 2 in a row :

Code: Select all

main.c :

////////////////////////////////////////////
// get time from rtc 0x68 
// temp from 0x48
// temp from 0x49
// display on 0x3C
////////////////////////////////////////////

#include <stdio.h>
#include <stdbool.h>
#include "ssd1306.h"
#include "lm75.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_sleep.h"
#include <time.h>
#include <sys/time.h>

#define PRECOMPILED_TIMESTAMP __TIME__

#define I2C_MASTER_SCL_IO 1
#define I2C_MASTER_SDA_IO 2
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_FREQ_HZ 100000

#define TMP1075_1_I2C_ADDR 0x48
#define TMP1075_2_I2C_ADDR 0x49

static ssd1306_handle_t ssd1306_dev = NULL;

void app_main(void) {

    // I2C master
    i2c_config_t conf = {
        .mode = I2C_MODE_MASTER,
        .sda_io_num = I2C_MASTER_SDA_IO,
        .sda_pullup_en = GPIO_PULLUP_ENABLE,
        .scl_io_num = I2C_MASTER_SCL_IO,
        .scl_pullup_en = GPIO_PULLUP_ENABLE,
        .master.clk_speed = I2C_MASTER_FREQ_HZ,
        .clk_flags = I2C_SCLK_SRC_FLAG_FOR_NOMAL
    };
    i2c_param_config(I2C_MASTER_NUM, &conf);
    i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);


    // I2C display
    i2cdev_init();
    ssd1306_dev = ssd1306_create(I2C_MASTER_NUM, SSD1306_I2C_ADDRESS);
    ssd1306_refresh_gram(ssd1306_dev);
    ssd1306_clear_screen(ssd1306_dev, 0x00);


    // I2C temperatures
    i2c_dev_t lm75_in, lm75_out;
    i2cdev_init();
    lm75_init_desc(&lm75_in, TMP1075_1_I2C_ADDR, I2C_MASTER_NUM, I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO);
    i2cdev_init();
    lm75_init_desc(&lm75_out, TMP1075_2_I2C_ADDR, I2C_MASTER_NUM, I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO);

    float temp_in = 0.0;
    float temp_out = 0.0;
    char string_temp_in[20] = "I 00.0C"; // °
    char string_temp_out[20] = "O 00.0C"; // °

    
    // RTC
    struct tm tm = {
        .tm_sec = 0, .tm_min = 0, .tm_hour = 12,
        .tm_mday = 1, .tm_mon = 0, .tm_year = 123, .tm_wday = 0, .tm_yday = 0
    };
    strptime(PRECOMPILED_TIMESTAMP, "%H:%M:%S", &tm);
    struct timeval tv = {
        .tv_sec = mktime(&tm),
        .tv_usec = 0
    };
    settimeofday(&tv, NULL);

    time_t current_time;
    struct tm *timeinfo;
    char string_time[16];


    while (1) {

        // time
        current_time = time(NULL);
        timeinfo = localtime(&current_time);
        snprintf(string_time, sizeof(string_time), "%02d:%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
        ssd1306_draw_string(ssd1306_dev, 32, 13, (const uint8_t *)string_time, 16, 1);
        ssd1306_refresh_gram(ssd1306_dev);


        // temp #1
        // lm75_read_temperature(&lm75_in, &temp_in);
        snprintf(string_temp_in, sizeof(string_temp_in), "I%5.1fC", temp_in);
        ssd1306_draw_string(ssd1306_dev, 32, 29, (const uint8_t *)string_temp_in, 16, 1);
        ssd1306_refresh_gram(ssd1306_dev);


        // // temp #2
        lm75_read_temperature(&lm75_out, &temp_out);
        snprintf(string_temp_out, sizeof(string_temp_out), "O%5.1fC", temp_out);
        ssd1306_draw_string(ssd1306_dev, 32, 45, (const uint8_t *)string_temp_out, 16, 1);
        ssd1306_refresh_gram(ssd1306_dev);

        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}
If i comment out a
lm75_read_temperature
it works, so i can comment 1 and other works, and vice versa, but not the 2 at the same time, they have different addresses, a I2C scanner shows them both (0x48 and 0x49).
I tried with a 100ms pause between them.. nope.
If i add a print with esp_err_t everywhere, no errors at all

but whenever i remove one of the 2 commented out read
i get

␛[0;31mE (17752) i2c: i2c_master_cmd_begin(1490): i2c driver not installed␛[0m
␛[0;31mE (18752) i2c: i2c_master_cmd_begin(1490): i2c driver not installed␛[0m
␛[0;31mE (18752) i2c: i2c_driver_delete(451): i2c driver install error␛[0m
␛[0;31mE (18752) i2c: i2c_master_cmd_begin(1490): i2c driver not installed␛[0m
␛[0;31mE (18762) i2cdev: Could not read from device [0x48 at 0]: 259 (ESP_ERR_INVALID_STATE)␛[0m
␛[0;31mE (18762) lm75: lm75_read_temperature(): read_register16() failed: register: 0x0␛[0m

␛[0;31mE (18782) i2c: i2c_master_cmd_begin(1490): i2c driver not installed␛[0m
␛[0;31mE (18782) i2c: i2c_driver_delete(451): i2c driver install error␛[0m
␛[0;31mE (18792) i2c: i2c_master_cmd_begin(1490): i2c driver not installed␛[0m
␛[0;31mE (18802) i2cdev: Could not read from device [0x49 at 0]: 259 (ESP_ERR_INVALID_STATE)␛[0m
␛[0;31mE (18802) lm75: lm75_read_temperature(): read_register16() failed: register: 0x0␛[0m

please give me a clue or maybe another place to find the info.

thank you. Mic.

User avatar
thezcx
Posts: 16
Joined: Mon May 01, 2023 12:21 am

Re: I2C 2 sensors read back to back problem.

Postby thezcx » Thu Nov 09, 2023 2:09 am

May be it's not the best way to do it but you use it like,
Wire.begin()// as default i2c
Wire1.begin(anypin,anypin) // as any other pin as second i2c

Who is online

Users browsing this forum: VinayDand and 363 guests