How to initialize I2C in Arduino

kaitlinburke
Posts: 2
Joined: Fri Feb 19, 2021 4:54 pm

How to initialize I2C in Arduino

Postby kaitlinburke » Wed Feb 24, 2021 4:24 pm

Hi all,

I'm using the ESP32-S2 on Arduino using the idf-release/v4.2 branch of the arduino-esp32 repository. I'm struggling to read data from an ambient light sensor (Adafruit VEML7700) using the I2C bus. I believe I'm setting up the I2C bus incorrectly, but I'm having difficultly finding answers on the proper way to do this.

If I try to initialize the I2C bus using the example initialization (i2c_master_init) from the ESP-IDF https://github.com/espressif/esp-idf/bl ... ple_main.c, I don't get any errors but all of the sensor readings are 0.

If I try to set up my own Wire instance like in this example https://randomnerdtutorials.com/esp32-i ... duino-ide/, I get this error and all my sensors readings are 0:

Code: Select all

E (6161) i2c: i2c_param_config(644): i2c clock choice is invalid, please check flag and frequency
Same thing if I just try Wire.begin(8,9) as in this post: https://github.com/espressif/arduino-esp32/issues/4375. I've tried setting clk_flags = 0 but it doesn't change anything.

What's the correct way to initialize the I2C bus?

Here's my code, with some of the methods I mentioned above commented out.

Code: Select all

#include "Adafruit_VEML7700.h"
#include "Wire.h"
#include "esp_log.h"
#include "driver/i2c.h"

#define _I2C_NUMBER(num) I2C_NUM_##num
#define I2C_NUMBER(num) _I2C_NUMBER(num)

#define DELAY_TIME_BETWEEN_ITEMS_MS 1000 /*!< delay time between different test items */

#define I2C_MASTER_SCL_IO 9               /*!< gpio number for I2C master clock */
#define I2C_MASTER_SDA_IO 8               /*!< gpio number for I2C master data  */
#define I2C_MASTER_NUM I2C_NUMBER(1)      /*!< I2C port number for master dev */
#define I2C_MASTER_FREQ_HZ 100000        /*!< I2C master clock frequency */
#define I2C_MASTER_TX_BUF_DISABLE 0       /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0       /*!< I2C master doesn't need buffer */

#define WRITE_BIT I2C_MASTER_WRITE              /*!< I2C master write */
#define READ_BIT I2C_MASTER_READ                /*!< I2C master read */
#define ACK_CHECK_EN 0x1                        /*!< I2C master will check ack from slave*/
#define ACK_VAL 0x0                             /*!< I2C ack value */
#define NACK_VAL 0x1                            /*!< I2C nack value */

TwoWire I2CWire = TwoWire(0);
Adafruit_VEML7700 veml = Adafruit_VEML7700();

void setup() {
  
  Serial.begin(115200);
  delay(3000);
  Serial.println("Adafruit VEML7700 Test");

  Serial.println("Start I2C setup...");
  ESP_ERROR_CHECK(i2c_master_init()); // https://github.com/espressif/esp-idf/blob/master/examples/peripherals/i2c/i2c_self_test/main/i2c_example_main.c
  //Wire.begin(8,9);
  //I2CWire.begin(I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO, 100000); //https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
 
  if (!veml.begin()) { // use when ESP_ERROR_CHECK or Wire.begin is uncommented
  //if (!veml.begin(&I2CWire)) { // use when I2CWire.begin is uncommented
    Serial.println("Sensor not found");
    while (1);
  }
  Serial.println("Sensor found");

  veml.setGain(VEML7700_GAIN_1);
  veml.setIntegrationTime(VEML7700_IT_800MS);
 
  veml.setLowThreshold(10000);
  veml.setHighThreshold(20000);
  veml.interruptEnable(true);
}

/**
 * @brief i2c master initialization from https://github.com/espressif/esp-idf/blob/master/examples/peripherals/i2c/i2c_self_test/main/i2c_example_main.c
 */
static esp_err_t i2c_master_init(void)
{
    int i2c_master_port = I2C_MASTER_NUM;
    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = I2C_MASTER_SDA_IO;
    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    conf.scl_io_num = I2C_MASTER_SCL_IO;
    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
    conf.clk_flags = 0;
    i2c_param_config(i2c_master_port, &conf);
    return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}


void loop() {

  Serial.print("Lux: "); Serial.println(veml.readLux());
  Serial.print("White: "); Serial.println(veml.readWhite());
  Serial.print("Raw ALS: "); Serial.println(veml.readALS());
 
  uint16_t irq = veml.interruptStatus();
  if (irq & VEML7700_INTERRUPT_LOW) {
    Serial.println("** Low threshold"); 
  }
  if (irq & VEML7700_INTERRUPT_HIGH) {
    Serial.println("** High threshold"); 
  }
  Serial.println("\n");
  delay(500);
}


Who is online

Users browsing this forum: No registered users and 66 guests