ADS1115 with espress-idf and esp32

casmeiron
Posts: 1
Joined: Thu Dec 07, 2017 7:39 pm

ADS1115 with espress-idf and esp32

Postby casmeiron » Thu Dec 07, 2017 7:54 pm

Hi guys, this is my first post, so I'm sorry if any guide rules are being broken, I will learn fast how to respect them.

I'm tryin' to read sensor data from channel AIN0 of ADS1115 using my esp32 where AIN0 is connected with 5V of a power source and the ADS is grounded to this source.

I found the i2c a bit complex (maybe I didnt understand it correctly), I hope you guys could help me.

I'm using I2C_NUM_0 (as a guess, but tried I2C_NUM_1 too) and after using I2C Scanner, I see the ADS sensor is present at address 0x48.

The pins are correct, I've doubled checked it.

So after installing using the follow code:

Code: Select all

#define SDA_PIN 21
#define SCL_PIN 22
#define SENSOR_ADDRESS   0x48
#define ACK_VAL                            0x0              /*!< I2C ack value */
#define AIN0_CHANNEL      0x4000
#define WRITE_BIT                          I2C_MASTER_WRITE /*!< I2C master write */
#define READ_BIT                           I2C_MASTER_READ  /*!< I2C master read */

void install() {
    if(!ic2DriverInstalled) {
      ESP_LOGI(tag, "Installing...");
        i2c_config_t conf;
        conf.mode = I2C_MODE_MASTER;
        conf.sda_io_num = SDA_PIN;
        conf.scl_io_num = SCL_PIN;
        conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
        conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
        conf.master.clk_speed = 100000;
        i2c_param_config(I2C_PORT, &conf);
       i2c_driver_install(I2C_PORT, I2C_MODE_MASTER, 0, 0, 0);
        ic2DriverInstalled = true;
      ESP_LOGI(tag, "Installed!");
    }
}

First question: Do I have to use GPIO_PULLUP_ENABLE for this sensor? (SDA AND SCL)
When to use and when not to use ACK wasn't understood by me too.

Ok, after installing I run the code to retrieve sensor data. It's something like this:

Code: Select all

esp_err_t read_sensor(uint8_t* data) {
    int ret;
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, SENSOR_ADDRESS << 1 | WRITE_BIT, ACK_VAL);
    i2c_master_write_byte(cmd, AIN0_CHANNEL, ACK_VAL);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    ESP_LOGD(tag, "RET X1: %d", ret);

    if (ret != ESP_OK) {
        return ret;
    }

    vTaskDelay(30 / portTICK_RATE_MS);
    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, SENSOR_ADDRESS << 1 | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, data, ACK_VAL);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(I2C_PORT, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);

    ESP_LOGD(tag, "RET X2: %d", ret);
   
    return ret;
}


I'm using the i2c example code (from esp-idf github) but modified a little bit with the values that I THINK are correct, like I just get one value from sensor not two [well I think] (like the example, humidity and temperature).

The first RET print is 0 the second is 263, but I don't have a clue what I'm doing, really. :oops:
Is this the correct flow?
1) Write to I2C the address that I want to communicate
2) Write to I2C the address/channel of the sensor that I want to read
3) Read from I2C (in the address that I first used [SENSOR ADDRESS]) the stored value

I really appreciate any help guys. ;)

God Bless.

Who is online

Users browsing this forum: No registered users and 7 guests