Quantcast
Channel: Sensors - Arduino Forum
Viewing all articles
Browse latest Browse all 1062

I2C communication. Code works with Arduino Uno R4 Wifi but not with ESP32S3

$
0
0

Hi everybody,

i have the following problem. I have a simple example project that works perfectly fine with an Arduino Uno R4 wifi but more or less the same code is not working with an ESP32S3.

I think it has something to do with

wire.endTransmission(false); is somehow handled differently between the Uno R4 and the ESP32 compiler. I think the compiler for the ESP32 ignores the "false" option.

The following code example for the Arduino works perfectly fine. I get plausible sensor read outs:

/*
Test Code to read out the IR temperature measurements from a Heimann Thermopile sensor of type HID-L1
with an Arduino R4Wifi via I2C
*/

#include "Wire.h"             // standard library

// start your defines for pins for sensors, outputs etc.
#define HIDL1_ADDR 0x5a           // I2C address of the HIDL1 IR-Sensor

// Input from serial interface
String inputString = "";
// just some buffer holder for char operations
char buf[32];

int i2cdelay_value = 10;

void setup() {

  Serial.begin(9600);
  int i2cdelay_value = 10;

  Wire.begin();
  Serial.println("End of Setup()");
  delay(500);
}

void loop() {
  
  double hidl1_temperature;
  
  Serial.print("i2cdelay = ");
  Serial.println(i2cdelay_value);

  //Serial.println("Begin of loop()");

  // check for serial commands
  if (Serial.available() > 0)
  { 
    inputString = Serial.readStringUntil('\n');
    Serial.println(inputString);
    if (inputString.startsWith("delay"))
    {
      String i2cdelay = inputString.substring(5, inputString.length());
      Serial.println("delay = "+i2cdelay);
      i2cdelay_value = i2cdelay.toInt();
    }
    else
    {
      i2cdelay_value = 10;
    }
  }
  else
  {
    i2cdelay_value++;
  }
  
  Wire.beginTransmission(HIDL1_ADDR);   // Begin transmission with given device on I2C bus
  Wire.write(0x07);
  Wire.endTransmission(false);
  delayMicroseconds(i2cdelay_value);
  Wire.beginTransmission(HIDL1_ADDR);
  Wire.requestFrom(HIDL1_ADDR, 3);      // Request 3 bytes 
  
  // Read the bytes if they are available
  // The first two bytes are humidity the last two are temperature
  if(Wire.available() == 3) {
    //Serial.println("3 Bytes available");              
    int b0 = Wire.read();
    int b1 = Wire.read();
    int b2 = Wire.read();

    Wire.endTransmission();           // End transmission and release I2C bus
    
    int rawTemperature = 256 * b1 + b0;
    hidl1_temperature = rawTemperature * 0.02 - 273.15;
    
    Serial.print("HID-L1 temperature: ");
    Serial.println(hidl1_temperature);
  }
  else {
      Serial.println("hidl1 not available on wire!");
  }

  delay(500);
  //Serial.println("End of loop()");
}

// end of code

But more or less the same code on the ESP32 is not working correctly. The sensor always returns three bytes with 0xFF. If I run the Arduino code with
the line:

wire.endTransmission();

I have the same problem.

This is the code for the ESP32S3:

/*
Test Code to read out the IR temperature measurements from a Heimann Thermopile sensor of type HID-L1
with an ESP32S3 via I2C
*/

#include "Wire.h"             // standard library

// definitions
#define SDA_1 35
#define SCL_1 36
#define HIDL1_ADDR 0x5a           // I2C address of the HIDL1 IR-Sensor

// Input from serial interface
String inputString = "";
// just some buffer holder for char operations
char buf[32];

int i2cdelay_value = 10;

void setup() {

  // standard stuff here
  Serial.begin(115200);
  int i2cdelay_value = 10;

  Wire.begin(SDA_1, SCL_1, 100000);
  Serial.println("End of Setup()");
  delay(500);
}

void loop() {
  
  double hidl1_temperature;
  double hyt271_humidity;
  double hyt271_temperature;
  double bmp585_pressure;
  double bmp585_temperature;

  Serial.print("i2cdelay = ");
  Serial.println(i2cdelay_value);

  //Serial.println("Begin of loop()");

  // check for serial commands
  if (Serial.available() > 0)
  { 
    inputString = Serial.readStringUntil('\n');
    Serial.println(inputString);
    if (inputString.startsWith("delay"))
    {
      String i2cdelay = inputString.substring(5, inputString.length());
      Serial.println("delay = "+i2cdelay);
      i2cdelay_value = i2cdelay.toInt();
    }
    else
    {
      i2cdelay_value = 10;
    }
  }
  else
  {
    i2cdelay_value++;
  }
  
  Wire.beginTransmission(HIDL1_ADDR);   // Begin transmission with given device on I2C bus
  Wire.write(0x07);
  Wire.endTransmission(false);
  delayMicroseconds(i2cdelay_value);
  Wire.beginTransmission(HIDL1_ADDR);
  Wire.requestFrom(HIDL1_ADDR, 3);      // Request 3 bytes 
  
  // Read the bytes if they are available
  // The first two bytes are humidity the last two are temperature
  if(Wire.available() == 3) {
    //Serial.println("3 Bytes available");              
    int b0 = Wire.read();
    int b1 = Wire.read();
    int b2 = Wire.read();

    Wire.endTransmission();           // End transmission and release I2C bus
    
    int rawTemperature = 256 * b1 + b0;
    hidl1_temperature = rawTemperature * 0.02 - 273.15;
    
    Serial.print("HID-L1 temperature: ");
    Serial.println(hidl1_temperature);
  }
  else {
      Serial.println("hidl1 not available on wire!");
  }

  delay(500);
  //Serial.println("End of loop()");
}

// end of code

I already tried different nus speeds and I introduced a delay after the wire.endTransmission(false); because this was a suggestion I found on the internet, but the delay makes no difference.

Has someone experienced similar problems?

I already run 2 other I2C sensors simultaneously on the same I2C of the ESP32 and they work just fine, but they don't require the wire.endTransmission(false)
after the wire.write ...

Greetings BumbleBeeFPV

2 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 1062

Trending Articles