Problem with Adafruit sensor reading

I have a Sparkfun Redboard Plus connected to an Adafruit SCD-30 (temp, humidity & C02 sensor) and an Adafruit VEML7700 (light sensor) via the Qwiic connector. I am trying to get these 2 sensors to read the data once every 5 seconds, and then put the data into one JSON packet which is sent out via the Serial monitor.

For some reason, when either sensor is used individually, the data gets sent with no problem. When I try to combine the code together by using the begin() command on both sensors, empty JSON packets get sent out like {} {} {} instead of JSON packets with the data inside. I have absolutely no idea what is going on. I am pretty bad at Arduino so if anyone could take a look at the code and tell me what I did wrong, I would really appreciate it!

#include <Wire.h>
#include <ArduinoJson.h>
#include "Adafruit_VEML7700.h"
#include <Adafruit_SCD30.h>

// Declare Sensors
Adafruit_SCD30 scd30;
Adafruit_VEML7700 veml = Adafruit_VEML7700();

// Declare other Variables
const int Interval = 5000;
unsigned long lastSCD30Time = 0;

// Setup code here runs once:
void setup() {

  // Initialize Serial output
  Wire.begin();
  Serial.begin(115200);
  while (!Serial) delay(10);

  // Try to initialize Sensors
  if (!scd30.begin()) {
    Serial.println("Failed to find chips");
    while (1) { delay(10); }
  }

  if (!veml.begin()) {
    Serial.println("Sensor not found");
    while (1);
  }

    Serial.print(F("Gain: "));
  switch (veml.getGain()) {
    case VEML7700_GAIN_1: Serial.println("1"); break;
    case VEML7700_GAIN_2: Serial.println("2"); break;
    case VEML7700_GAIN_1_4: Serial.println("1/4"); break;
    case VEML7700_GAIN_1_8: Serial.println("1/8"); break;
  }

  Serial.print(F("Integration Time (ms): "));
  switch (veml.getIntegrationTime()) {
    case VEML7700_IT_25MS: Serial.println("25"); break;
    case VEML7700_IT_50MS: Serial.println("50"); break;
    case VEML7700_IT_100MS: Serial.println("100"); break;
    case VEML7700_IT_200MS: Serial.println("200"); break;
    case VEML7700_IT_400MS: Serial.println("400"); break;
    case VEML7700_IT_800MS: Serial.println("800"); break;
  }

  veml.setLowThreshold(10000);
  veml.setHighThreshold(20000);
  veml.interruptEnable(true);

}

void loop() {

  if (millis() - lastSCD30Time >= Interval) {

    // Create output json document
    DynamicJsonDocument root(1024);

    if (scd30.dataReady()) {
    
      if (!scd30.read()){ Serial.println("Error reading sensor data"); return; }

      root["Temperature (Celsius)"] = scd30.temperature;
      root["Relative Humidity (%)"] = scd30.relative_humidity;
      root["CO2 Concentration (ppm)"] = scd30.CO2;
      root["Light Level (lux)"] = round(veml.readLux());

      
      uint16_t irq = veml.interruptStatus();
      if (irq & VEML7700_INTERRUPT_LOW) {
        Serial.println("** Low threshold");
      }
      if (irq & VEML7700_INTERRUPT_HIGH) {
        Serial.println("** High threshold");
      }
    
      serializeJson(root, Serial);
      Serial.println();
    
    } else { Serial.println("No data"); }

    // Assign to new millis()
    lastSCD30Time = millis();

  }

}

1024 bytes is half of the available memory on the Redboard. As you don't need that much memory for your output, decrease that value significantly (p.e. 200).
You should also think about using a full-fledged JSON library to print 4 values to the serial interface.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.