Hello, I have built a temperature-humidity-VOC Gas monitoring system with the extra sensors laying around. My problem is that two different SHT31 stopped working after couple of tests. The Adafruit test code stops without giving errors.
The system worked perfectly at first, except the Battery voltage reading part, Arduino couldn't measure battery voltage from the voltage divider, it gave 0.7V for actual 2.5V and gave 1.2V for 3.75V. I was doing some tests like printing the reading value on the screen and trying to figure out what was wrong but then screen was gone completely, it is because code stops working at sht31.begin(0x44) part, when I remove the SHT31 connection, the test code works for sgp30 and OLED display. Am I doing something wrong or is my dealer sending me low quality sensors.
Schematic:
Note: Single BC548 wasn't enough to switch the sensor, I removed it and shorted the collector and emitter. I'll ask about it later in another thread.
Code:
#include <Wire.h>
#include "Adafruit_SGP30.h"
#include "U8glib.h"
#include "Adafruit_SHT31.h"
#include <EEPROM.h>
Adafruit_SGP30 sgp;
Adafruit_SHT31 sht31 = Adafruit_SHT31();
bool HeaterState = false;
int8_t counter = 0;
float t;
float h;
float Vread;
bool SGPenabled = EEPROM.read(0);
/* return absolute humidity [mg/m^3] with approximation formula
* @param temperature [°C]
* @param humidity [%RH]
*/
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}
void setup() {
// Serial.begin(9600);
preparesecreen();
sht31.begin(0x44);
if (SGPenabled){
sgp.begin();
sgp.setIAQBaseline(0x99AF, 0x99AF); // (CO2_base , TVOC_base)
}
attachInterrupt(0, Clap, FALLING);
sht31.heater(false);
}
unsigned long t1 = 0; // holds info of rapid claps
unsigned long t2 = 0; // prevents wrong trigger of single clap
unsigned long currenttime;
void Clap(){
currenttime = millis();
if (currenttime - t2 < 100) {t2 = currenttime; return;}
if (!HeaterState){
HeaterState = true;
counter = 80;
} else if (HeaterState){
counter = 0;
}
if (currenttime - t1 <= 1000) {
EEPROM.write(0,!SGPenabled);
void(* resetFunc) (void) = 0;
resetFunc();
} else t1 = currenttime;
t2 = currenttime;
}
void loop() {
t = sht31.readTemperature();
h = sht31.readHumidity();
if (SGPenabled){
sgp.setHumidity(getAbsoluteHumidity(t, h));
sgp.IAQmeasure();
}
displayMEAS(); delay(500);
while (HeaterState){
if (sht31.isHeaterEnabled() == 0) sht31.heater(true);
displayHEAT();
counter--;
if (counter<=0) HeaterState = false;
if (counter<=40) {if (sht31.isHeaterEnabled()) sht31.heater(false);}
delay(1000);
}
}
//bool chargedisplayed = false;
void displayMEAS(){
// chargedisplayed = false;
u8g.firstPage();
do {
if (SGPenabled){
u8g.setFont(u8g_font_6x10);
u8g.drawStr( 0, 10, "Temp:"); u8g.drawStr( 70, 10, "Hum:");
u8g.drawStr( 0, 45, "TVOC(ppb):"); u8g.drawStr( 70, 45, "eCO2(ppm):");
u8g.setFont(u8g_font_7x14B);
u8g.drawStr( 122, 30, "%"); // Hum
u8g.setFont(u8g_font_fub14); //u8g_font_tpssb u8g_font_9x18B u8g_font_courB12 u8g_font_fub17
u8g.setPrintPos(0, 30);
u8g.print(t);
u8g.setPrintPos(70, 30);
u8g.print(h);
u8g.setPrintPos(0, 64);
u8g.print(sgp.TVOC);
u8g.setPrintPos(70, 64);
u8g.print(sgp.eCO2);
u8g.setFont(u8g_font_fub11);
u8g.drawStr( 52, 30, "C"); // Temp
} else{
u8g.setFont(u8g_font_fub11);
u8g.drawStr( 0, 30, "Temp: *C"); u8g.drawStr( 10, 57, "Hum: %");
u8g.setFont(u8g_font_fub14);
u8g.setPrintPos(52, 30);
u8g.print(t);
u8g.setPrintPos(52, 57);
u8g.print(h);
}
// if (!chargedisplayed) {displayCHARGE(); chargedisplayed = true;}
} while( u8g.nextPage() );
}
void displayHEAT(){
u8g.firstPage();
do {
u8g.setFont(u8g_font_fub11);
u8g.drawStr( 15, 15, "Evaporating"); u8g.drawStr( 10, 35, "Condensation");
u8g.setPrintPos(60, 60);
if (counter != 0) u8g.print(counter);
else u8g.drawStr( 15, 60, "CANCELLED!");
} while( u8g.nextPage() );
}
void preparesecreen(){
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
// void displayCHARGE(){
// Vread = analogRead(A0);
// Serial.println(Vread);
// if (Vread > 900) return;
// u8g.setFont(u8g_font_u8glib_4);
// u8g.setPrintPos(103, 4);
// u8g.print(Vread);
//if (Vread > 799) u8g.drawStr( 103, 4, "B: ||||");
//else if (Vread > 758) u8g.drawStr( 103, 4, "B: |||-");
//else if (Vread > 716) u8g.drawStr( 103, 4, "B: ||--");
// else if (Vread > 675) u8g.drawStr( 103, 4, "B: |---");
// else u8g.drawStr( 103, 4, "B: ----");
// }
Note: I removed the "displayCHARGE()" function, because I just want my sensor back, I can live without battery indictor.
I need help on three topics I will ask for two here with the priority order:
1. Why is SHT31 malfunctioning after a while?
2. Why doesn't Arduino read Analog values correctly from A0 pin?