adafruit / Adafruit_AS7341

Other
29 stars 18 forks source link

getTINT() can potentially return incorrect values #27

Closed caternuson closed 1 year ago

caternuson commented 1 year ago

Re this thread: https://forums.adafruit.com/viewtopic.php?t=203313

Behavior is platform specific. RP2040 does not show it. UNO does.

Example sketch from thread to demonstrate the issue:

/* This example will read all channels from the AS7341 and print out reported values */

#include <Adafruit_AS7341.h>

Adafruit_AS7341 as7341;

void setup() {
  Serial.begin(9600);

  // Wait for communication with the host computer serial monitor
  while (!Serial) {
    delay(1);
  }

  if (!as7341.begin()) {
    Serial.println("Could not find AS7341");
    while (1) { delay(10); }
  }

  as7341.setATIME(250);
  as7341.setASTEP(600);
  as7341.setGain(AS7341_GAIN_1X);
}

void loop() {

  for (int i = 0; i <= 250; i += 25) {
    as7341.setATIME(i);  // Set ATIME
    delay(1000);

    uint16_t readings[12];

    if (!as7341.readAllChannels(readings)) {
      Serial.println("Error reading all channels!");
      return;
    }

    Serial.print("Current ATIME for sensor ");
    Serial.print(": ");
    Serial.println(as7341.getATIME());

    Serial.print("Current ASTEP for sensor ");
    Serial.print(": ");
    Serial.println(as7341.getASTEP());

    Serial.print("Current GAIN for sensor ");
    Serial.print(": ");
    Serial.println(as7341.getGain());

    Serial.print("Current TINT (ms) for sensor ");
    Serial.print(": ");
    Serial.println(as7341.getTINT());

    long TINT_calc = ((as7341.getATIME() + 1L) * (as7341.getASTEP() + 1L) * 2.78L / 1000L);
    Serial.print("TINT calculated ");
    Serial.println(TINT_calc);

    Serial.print("Reading ");
    Serial.print("ADC4/Clear    : ");
    Serial.println(readings[10]);

    Serial.print("Basic counts ");
    Serial.print("ADC4/Clear    : ");
    Serial.println(as7341.toBasicCounts(readings[10]));

    float basic = (readings[10] / (as7341.getGain() * as7341.getTINT()));
    Serial.print("Basic counts semi manual for sensor ");
    Serial.print(": ");
    Serial.println(basic);

    float basic2 = (readings[10] / (as7341.getGain() * TINT_calc));
    Serial.print("Basic counts manual for sensor ");
    Serial.print(": ");
    Serial.println(basic2);
  }
}

The issue being that the value returned from getTINT() can vary from one computed locally using essentially same math.

caternuson commented 1 year ago

Fixed with #28.