Trouble understanding I2C examples

DylanWork
Posts: 32
Joined: Wed Jun 02, 2021 4:27 am

Trouble understanding I2C examples

Postby DylanWork » Tue Jul 20, 2021 4:32 am

Hi,

I am trying to use an accelerometer via I2c however I'm finding it difficult to understand what I need to do, I am using this guide to get the accelerometer started (I have done this successfully previously using Arduino Wire() function for standard I2C stuff) and this is the code I've got so far:

Init Function (Based off this example)

Code: Select all

static esp_err_t i2c_master_sensor_init(i2c_port_t i2c_num, uint8_t *data_h, uint8_t *data_l)
{
    int ret;
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    // i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x1b, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    i2c_master_start(cmd);
    // i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x1b, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0xC0, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    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, BH1750_SENSOR_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
    // i2c_master_read_byte(cmd, data_h, ACK_VAL);
    // i2c_master_read_byte(cmd, data_l, NACK_VAL);
    // i2c_master_stop(cmd);
    // ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    // i2c_cmd_link_delete(cmd);
    // return ret;
}
Read function:

Code: Select all

static esp_err_t i2c_master_sensor_read(i2c_port_t i2c_num, uint8_t *data_h, uint8_t *data_l)
{
    int ret;
    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    // i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x06, ACK_CHECK_EN);
    //i2c_master_write_byte(cmd, 0x00, ACK_CHECK_EN);
    i2c_master_stop(cmd);
    // i2c_master_start(cmd);
    // // i2c_master_write_byte(cmd, BH1750_SENSOR_ADDR << 1 | WRITE_BIT, ACK_CHECK_EN);
    // i2c_master_write_byte(cmd, 0x0f, ACK_CHECK_EN);
    // i2c_master_write_byte(cmd, 0x1b, ACK_CHECK_EN);
    // i2c_master_write_byte(cmd, 0xC0, ACK_CHECK_EN);
    // i2c_master_stop(cmd);
    // ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    // i2c_cmd_link_delete(cmd);
    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, BH1750_SENSOR_ADDR << 1 | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, 0x0f, ACK_VAL);
    // i2c_master_read_byte(cmd, data_l, NACK_VAL);
    // i2c_master_stop(cmd);
    // ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    // i2c_cmd_link_delete(cmd);
    // return ret;
}
I just hope that I'm heading in the right direction but I'm still not sure how to finish up the read function so that I actually can print out the data?

I'm posting this as unfinished code as it's been hurting my head for a while now and I'd just be grateful for any pointers in the correct direction.

Cheers

ESP-Marius
Posts: 74
Joined: Wed Oct 23, 2019 1:49 am

Re: Trouble understanding I2C examples

Postby ESP-Marius » Wed Jul 21, 2021 1:51 am

Hi,

We know some of our examples can be a bit complex when you want to get started with just some basic functionality.

To remedy this we actually recently added a "simple I2C" example just to show case basic functionality like this. Unfortunately it hasnt been synced to Github yet, but i'll attach the code here.

Take a look and see if this can help you get started.
Attachments
i2c_simple.zip
(4.88 KiB) Downloaded 319 times

Who is online

Users browsing this forum: No registered users and 190 guests