AS7343 with ESP32, Arduino IDE

I am on my wit’s end.

I’ve got my hands on AS7343 board, even though full know that there are no AS7343 libraries for ESP32/Arduino available yet, thought that I would give it a try.

My code shown below, it gave out data of all 14 channels just fine but when I read 0xBC register it told me that the FIFO buffer is full and Wtime is too short.

I tried to increase Wtime to its maximum and reduce integration time but 0XBC register does not change.

I have no idea how to deal with this FIFO thing, it said some data might be lost but the sensor responds fine and gives out data as expected. Red channel increases while showing red items, VIS increases when light increases etc.

I tried to follow AS7341 example but can’t see what is behind the hood of those codes.

Any suggest or advice? Please,

#include <Wire.h>

#define AS7343_ADDR 0x39

// Register map based on the Python library
#define REG_ENABLE 0x80
#define REG_ATIME  0x81
#define REG_WTIME  0x83
#define REG_CONFIG 0x70
#define REG_GAIN   0xC6 // AGAIN in CFG1
#define REG_CH0_DATA 0x95
#define REG_CFG20  0xD6 // Configuration register for auto_smux
#define FIFO_MAP  0xFC //Set the FIFO map
#define REG_ASTEP  0xD4 // Astep register (low and high bytes at 0xD4 and 0xD5)
#define REG_RESET  0xFA // Software reset register

class AS7343 {
private:
    uint8_t i2c_addr;

public:
    AS7343(uint8_t addr = AS7343_ADDR) {
        i2c_addr = addr;
    }

    bool begin() {
        Wire.begin();

        if (!writeRegister(0x80, 0x1B)) { // Enable power and sensor
            return false;//0b01011011
        }

        if (!writeRegister(REG_RESET, 0b00000010)) { // Perform software reset
            return false;
        }

        if (!writeRegister(0xD6, 0x60)) { // Set auto_smux to 3
            return false;//0b01100000
        }

        if (!writeRegister(REG_WTIME, 0x5E)) { // Set WTIME to 94 (0x5E in hex)
            return false;
        }

        if (!writeRegister(REG_ATIME, 0x1D)) { // Set ATIME to 29 (0x1D in hex)
            return false;
        }

        if (!writeRegister(REG_ASTEP, 0x57)) { // Set ASTEP low byte to 599 (0x57 in hex)
            return false;
        }

        if (!writeRegister(REG_ASTEP + 1, 0x02)) { // Set ASTEP high byte to 599 (0x02 in hex)
            return false;
        }
        return true;
    }

    bool readAllChannels(uint16_t *data) {
        for (int i = 0; i < 3; i++) {
            if (!writeRegister(FIFO_MAP, 0x7F)) { // Write FIFO_MAP three times
                return false;
            }
        }

        Wire.beginTransmission(i2c_addr);
        Wire.write(REG_CH0_DATA);
        if (Wire.endTransmission() != 0) {
            return false;
        }

        Wire.requestFrom(i2c_addr, 36);
        if (Wire.available() < 36) {
            return false;
        }

        for (int i = 0; i < 18; i++) {
            uint8_t low = Wire.read();
            uint8_t high = Wire.read();
            data[i] = (high << 8) | low;
        }

        return true;
    }

    void displayIntegrationSettings() {
        uint8_t atime = readRegister(REG_ATIME);
        uint16_t astep = readRegister(REG_ASTEP) | (readRegister(REG_ASTEP + 1) << 8); // REG_ASTEP is 0xD4 and 0xD5
        float integrationTime = (atime + 1) * (astep + 1) * 2.78; // Integration time in microseconds

        Serial.print("ATIME: ");
        Serial.println(atime);
        Serial.print("ASTEP: ");
        Serial.println(astep);
        Serial.print("Integration Time: ");
        Serial.print(integrationTime, 2); // Show two decimal places for accuracy
        Serial.println(" microseconds");

        uint8_t regBC = readRegister(0xBC); // Read register 0xBC
        Serial.print("Register 0xBC: ");
        Serial.println(regBC, BIN); // Display in binary format

        uint8_t wtime = readRegister(REG_WTIME); // Read WTIME
        Serial.print("WTIME: ");
        Serial.println(wtime);

        // Add an extra line for easier readability
        Serial.println();
    }

    uint8_t readRegisterPublic(uint8_t reg) { // Expose readRegister as public
        return readRegister(reg);
    }

private:
    bool writeRegister(uint8_t reg, uint8_t value) {
        Wire.beginTransmission(i2c_addr);
        Wire.write(reg);
        Wire.write(value);
        return (Wire.endTransmission() == 0);
    }

    uint8_t readRegister(uint8_t reg) {
        Wire.beginTransmission(i2c_addr);
        Wire.write(reg);
        if (Wire.endTransmission() != 0) {
            return 0;
        }

        Wire.requestFrom(i2c_addr, 1);
        if (Wire.available()) {
            return Wire.read();
        }

        return 0;
    }
};

AS7343 sensor;

const char* spectrumNames[14] = {
    "Violet1", "Violet2", "Blue", "Blue/Cyan", "Cyan", "Green",
    "Yellow/Green", "Orange", "Orange/Red", "Red2", "Red3", "NIR", "VIS", "Flicker"
};

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

    if (!sensor.begin()) {
        Serial.println("AS7343 initialization failed!");
        while (1);
    }

    Serial.println("AS7343 initialized successfully!");

    sensor.displayIntegrationSettings();
}

void loop() {
    uint16_t spectralData[18] = {0};

    if (sensor.readAllChannels(spectralData)) {
        uint16_t averagedData[14] = {0};

        // Average the duplicated VIS and Flicker data
        averagedData[0] = spectralData[12]; // Violet1
        averagedData[1] = spectralData[6];  // Violet2
        averagedData[2] = spectralData[0];  // Blue
        averagedData[3] = spectralData[7];  // Blue/Cyan
        averagedData[4] = spectralData[8];  // Cyan
        averagedData[5] = spectralData[1];  // Green
        averagedData[6] = spectralData[15]; // Yellow/Green
        averagedData[7] = spectralData[2];  // Orange
        averagedData[8] = spectralData[9];  // Orange/Red
        averagedData[9] = spectralData[13]; // Red2
        averagedData[10] = spectralData[14]; // Red3
        averagedData[11] = spectralData[3]; // NIR
        averagedData[12] = (spectralData[4] + spectralData[10] + spectralData[16]) / 3; // VIS
        averagedData[13] = (spectralData[5] + spectralData[11] + spectralData[17]) / 3; // Flicker

        for (int i = 0; i < 14; i++) {
            Serial.print(spectrumNames[i]);
            Serial.print(": ");
            Serial.println(averagedData[i]);
        }

        // Read and display 0xBC register value
        uint8_t regBC = sensor.readRegisterPublic(0xBC);
        Serial.print("Register 0xBC: ");
        Serial.println(regBC, BIN); // Display in binary format

        // Read and display WTIME
        uint8_t wtime = sensor.readRegisterPublic(REG_WTIME);
        Serial.print("WTIME: ");
        Serial.println(wtime);

        // Add an extra line for easier readability
        Serial.println();
    } else {
        Serial.println("Failed to read spectral data.");
    }

    delay(1000);
}

this is what its output lookalike

Violet1: 27

Violet2: 53

Blue: 186

Blue/Cyan: 270

Cyan: 351

Green: 491

Yellow/Green: 115

Orange: 607

Orange/Red: 659

Red2: 605

Red3: 143

NIR: 253

VIS: 1210

Flicker: 17998

Register 0xBC: 10000100

WTIME: 94