AS7265X transferring read data slow

I am currently using a SparkFun AS7265X spectral sensor, using an Adafruit Feather esp32s3 board to get data from the sensor over I2C. My goal is to get the data as close to real-time as possible, but the read rate is much slower than I need. I have tried setting the integration cycles to a lower value, but for some reason, it actually makes the read rate slower. I have also tried a different sensor of the same type, but it yeilded the same results. I am creating an object in my code that contains a method to read the data, and setup the sensor. The setup code is being ran in setup(), and the sensor readd method is being ran in loop(). No other configuration for the sensor is being done in setup() or loop(), just calling the methods. Below is the code that I have:

Setup code

 void setupSensor()
    {

        while (sensor.begin() == false)
            Serial.println("Sensor does not appear to be connected. Please check wiring. Freezing...");

        sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_6CHAN_CONTINUOUS); // Continuous measurements
        sensor.disableIndicator();
        //sensor.setIntegrationCycles(5);
        Wire.setClock(400000);
        Wire.begin();

        if (this->LEDsOn == true)
        {
            // Turn on LED bulbs
            this->sensor.enableBulb(AS7265x_LED_WHITE);
            this->sensor.enableBulb(AS7265x_LED_IR);
            this->sensor.enableBulb(AS7265x_LED_UV);
        }
    }

Read code

void scanAllChannels(bool calibrated = false)
{
        // Check if calibrated readings are set to true or not
        if (calibrated == false)
        {
            this->channelValues[0] = sensor.getA(); // Get the channel reading
        }
        else if (calibrated == true)
        {
            this->channelValues[0] = sensor.getCalibratedA();
        }
}

Any help getting this working is greatly appreciated!

You have a lot of options…consider the following adjustments:

  1. Integration Time:
    Reducing the integration time can significantly increase the read rate. However, setting it too low might actually slow things down due to increased I2C traffic. Try experimenting with different values:
sensor.setIntegrationCycles(1); // Minimum integration time (2.78ms)
  1. Measurement Mode:
    You’re currently using 6-channel continuous mode. For faster readings, consider using 4-channel mode:
sensor.setMeasurementMode(AS7265X_MEASUREMENT_MODE_4CHAN_2);
  1. Gain Setting:
    Lower gain can lead to faster readings but may reduce sensitivity:
sensor.setGain(AS7265X_GAIN_1X);
  1. I2C Clock Speed:
    You’ve already set it to 400kHz, which is good…you can also try 800KHz and see if it’ll run at that speed

  2. Polling vs Interrupt:
    Instead of continuously polling, use the interrupt pin to know when data is ready:

sensor.enableInterrupt();
attachInterrupt(digitalPinToInterrupt(interruptPin), dataReadyISR, FALLING);
  1. Optimize Reading Function:
    Your scanAllChannels function is only reading one channel. To read all channels efficiently:
void scanAllChannels(bool calibrated = false)
{
    if (calibrated)
    {
        channelValues[0] = sensor.getCalibratedA();
        channelValues[1] = sensor.getCalibratedB();
        // ... repeat for all channels up to R
    }
    else
    {
        channelValues[0] = sensor.getA();
        channelValues[1] = sensor.getB();
        // ... repeat for all channels up to R
    }
}
  1. Use Non-blocking Reads:
    Instead of waiting for measurements, check if data is available:
void loop()
{
    if (sensor.dataAvailable())
    {
        scanAllChannels();
        // Process data
    }
    // Do other tasks
}
  1. Raw Data:
    If absolute calibrated values aren’t necessary, use raw data for faster processing:
uint16_t rawA = sensor.getA();
  1. Timing Analysis:
    Use micros() to measure the time between readings and adjust the portions of code where it is hanging

Remember, the maximum theoretical read rate is limited by the sensor’s integration time and I2C communication speed. Balancing speed with measurement quality is crucial