Sensiron SGP41 I2C code for Arduino

bjeanslame
Posts: 2
Joined: Sat Aug 05, 2023 2:45 pm

Sensiron SGP41 I2C code for Arduino

Postby bjeanslame » Sat Aug 05, 2023 2:54 pm

Hello,
I'm having trouble getting the following code from Sensiron working on my ESP32-S2 dev board. https://github.com/Sensirion/arduino-i2c-sgp41.

I've made the following addition to the Sensiron code to setup the correct GPIOs for the I2C bus
- Wire.begin(6, 7,100000);

I believe the hardware is connected correctly for the following reasons.
1) I'm able to see the correct address on the I2C scanner
2) I'm able to retrieve my components' serial number
3) Another Sensirion sensor on the same I2C bus (different sensor model and address) is functioning correctly.

However I get the following error when running the above code
-"executeSelfTest failed with error: Not enough data received" and "Error trying to execute measureRawSignals(): Not enough data received"

Please let me know how I can debug and next steps, thanks for helping me out :)

bjeanslame
Posts: 2
Joined: Sat Aug 05, 2023 2:45 pm

Re: Sensiron SGP41 I2C code for Arduino

Postby bjeanslame » Tue Aug 08, 2023 10:56 pm

Below is my code and the output.

Code:

Code: Select all

#include <Arduino.h>
#include <SensirionI2CSgp41.h>
#include <Wire.h>

SensirionI2CSgp41 sgp41;

// time in seconds needed for NOx conditioning
uint16_t conditioning_s = 10;

void setup() {

  Serial.begin(115200);
  while (!Serial) {
    delay(100);
  }

  Wire.begin(6, 7);

  uint16_t error;
  char errorMessage[256];

  sgp41.begin(Wire);

  uint16_t serialNumber[3];
  uint8_t serialNumberSize = 3;

  error = sgp41.getSerialNumber(serialNumber, serialNumberSize);

  if (error) {
    Serial.print("Error trying to execute getSerialNumber(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
  } else {
    Serial.print("SerialNumber:");
    Serial.print("0x");
    for (size_t i = 0; i < serialNumberSize; i++) {
      uint16_t value = serialNumber[i];
      Serial.print(value < 4096 ? "0" : "");
      Serial.print(value < 256 ? "0" : "");
      Serial.print(value < 16 ? "0" : "");
      Serial.print(value, HEX);
    }
    Serial.println();
  }

  uint16_t testResult;
  error = sgp41.executeSelfTest(testResult);
  Serial.println(testResult);
  if (error) {
    Serial.print("Error trying to execute executeSelfTest(): ");
    errorToString(error, errorMessage, 256);
    Serial.print(errorMessage);
  } else if (testResult != 0xD400) {
    Serial.print("executeSelfTest failed with error: ");
    Serial.println(testResult);
  }
}

void loop() {
  uint16_t error;
  char errorMessage[256];
  uint16_t defaultRh = 0x8000;
  uint16_t defaultT = 0x6666;
  uint16_t srawVoc = 0;
  uint16_t srawNox = 0;

  delay(1000);

  if (conditioning_s > 0) {
    // During NOx conditioning (10s) SRAW NOx will remain 0
    error = sgp41.executeConditioning(defaultRh, defaultT, srawVoc);
    conditioning_s--;
    Serial.println(conditioning_s);
  } else {
    // Read Measurement
    error = sgp41.measureRawSignals(defaultRh, defaultT, srawVoc, srawNox);
  }

  if (error) {
    Serial.print("Error trying to execute measureRawSignals(): ");
    errorToString(error, errorMessage, 256);
    Serial.println(errorMessage);
    Serial.println(srawVoc);
    Serial.println(srawNox);
  } else {
    Serial.print("SRAW_VOC:");
    Serial.print(srawVoc);
    Serial.print("\t");
    Serial.print("SRAW_NOx:");
    Serial.println(srawNox);
  }
}
Output:

Code: Select all

10:18:20.930 -> ESP-ROM:esp32s2-rc4-20191025
10:18:20.930 -> Build:Oct 25 2019
10:18:20.930 -> rst:0x1 (POWERON),boot:0x9 (SPI_FAST_FLASH_BOOT)
10:18:20.930 -> SPIWP:0xee
10:18:20.930 -> mode:DIO, clock div:1
10:18:20.962 -> load:0x3ffe6100,len:0x524
10:18:20.962 -> load:0x4004c000,len:0xa70
10:18:20.962 -> load:0x40050000,len:0x298c
10:18:20.962 -> entry 0x4004c18c
10:18:21.027 -> SerialNumber:0x000003C83069
10:18:21.358 -> 42405
10:18:21.358 -> Error trying to execute executeSelfTest(): Not enough data received9
10:18:22.416 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:22.416 -> 0
10:18:22.416 -> 0
10:18:23.468 -> 8
10:18:23.468 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:23.468 -> 0
10:18:23.468 -> 0
10:18:24.512 -> 7
10:18:24.512 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:24.512 -> 0
10:18:24.512 -> 0
10:18:25.565 -> 6
10:18:25.565 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:25.565 -> 0
10:18:25.565 -> 0
10:18:26.620 -> 5
10:18:26.620 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:26.620 -> 0
10:18:26.620 -> 0
10:18:27.677 -> 4
10:18:27.677 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:27.677 -> 0
10:18:27.677 -> 0
10:18:28.727 -> 3
10:18:28.727 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:28.727 -> 0
10:18:28.727 -> 0
10:18:29.780 -> 2
10:18:29.780 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:29.780 -> 0
10:18:29.780 -> 0
10:18:30.828 -> 1
10:18:30.828 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:30.828 -> 0
10:18:30.828 -> 0
10:18:31.948 -> 0
10:18:31.948 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:31.948 -> 0
10:18:31.948 -> 0
10:18:33.065 -> Error trying to execute measureRawSignals(): Not enough data received
10:18:33.097 -> 0
10:18:33.097 -> 0

Who is online

Users browsing this forum: Baidu [Spider] and 56 guests