arduinosensors

Attiny841 trouble with using SGP40 sensor


Brief Synopsis: I wanted to try my hand at a custom printed circuit board assembly which included a microcontroller and a peripheral sensor, the SGP40, and some LED's which would light up determined by the VOC index obtained from the sensor over some I2C data lines. I started this project a while ago and assembled the boards and tested the microcontroller with the LED's and that was as far as I got before I started going back to school and working.

SGP40 Datasheet: https://sensirion.com/media/documents/296373BB/6203C5DF/Sensirion_Gas_Sensors_Datasheet_SGP40.pdf

Script:

#include "SparkFun_SGP40_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_SGP40
#include <Wire.h>

SGP40 mySensor; 

void setup()
{
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  Wire.begin();

  if (mySensor.begin() == false)
  {
    while (1)
    digitalWrite(3, HIGH);
    digitalWrite(2, HIGH);
      ;
  }
}

void loop()
{
  int32_t vocIndex = mySensor.getVOCindex();


  if (vocIndex > 0 && vocIndex <= 165) {
    digitalWrite(1, LOW);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
  } 
  else if (vocIndex > 165 && vocIndex <= 330) {
    digitalWrite(1, LOW);
    digitalWrite(3, LOW);
    digitalWrite(2, HIGH);
  } 
  else if (vocIndex > 330) { 
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(1, HIGH);
  } 
  else if (vocIndex == -100){
    digitalWrite(1, HIGH);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
  }
  else {
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
  }


  delay(1000); 
}

Schematic:

Schematic of the custom board

Where I'm at: Currently I'm having an issue where I'm not sure how to fix or know what I'm doing wrong. Using the below script it appears the value the microcontroller is getting is -100 which i think i remember being an error code from the SGP40, however its been a while since i looked at this sketch and that bit of info about -100 and sensor error might not be accurate, but atleast in my if else if statement that is the output im seeing where two LED's are lit up and the value being read for vocIndex is -100.

What I've done: I've moved the Attiny841 around to different boards using a "burn-in" socket and i get the same output with the two LED's, 1 and 3, being lit. I've made edits to the script but it doesn't seem to have worked.

Goal State: Ideally this little board will do the simple job of getting the VOC index and depending on the VOC index set an LED high which corresponds to a range within that index.


Solution

  • Because of your setup where you are trying to glean data about the state of your setup from 3 LEDs, it is hard to diagnose the problem. I would suggest that you breadboard the simplest setup without LEDs, and use this code, which is SparkFun's example code for the board. It will send output to the serial monitor, and this will give you something concrete to work from.

    #include "SparkFun_SGP40_Arduino_Library.h" // Click here to get the library: http://librarymanager/All#SparkFun_SGP40
    #include <Wire.h>
    
    SGP40 mySensor; //create an object of the SGP40 class
    
    void setup()
    {
      Serial.begin(115200);
      Serial.println(F("SGP40 Example"));
    
      Wire.begin();
    
      //mySensor.enableDebugging(); // Uncomment this line to print useful debug messages to Serial
    
      //Initialize sensor
      if (mySensor.begin() == false)
      {
        Serial.println(F("SGP40 not detected. Check connections. Freezing..."));
        while (1)
          ; // Do nothing more
      }
    }
    
    void loop()
    {
      Serial.print(F("VOC Index is: "));
      Serial.println(mySensor.getVOCindex()); //Get the VOC Index using the default RH (50%) and T (25C)
    
      delay(1000); //Wait 1 second - the Sensirion VOC algorithm expects a sample rate of 1Hz
    }
    

    If this works, you could try YOUR code on the breadboard setup (after adding 3 LEDs). You could also add serial output to your original code so that it can display a full response.

    If the code above gives you nothing useful when run on a breadboard setup, I would be very tempted to run it on something like an Arduino UNO where there is no ambiguity over pin usage etc. If you get no joy from that, I would run an I2C scanner program on the Arduino to check that your SGP40 is connected to the correct pins, and is responding on the I2C bus.