I am using this VOC CJMCU-1100 sensor:
This code works for my TVOC and TPH sensor, but how do I understand TVOC values and programme to identify formaldehyde and other VOC values it is measuring??
The code outputs the TVOC as an analogue value, where the value is of course higher with the more total volatile organic compounds detected. But I can't find any specifications which explain how to compare the values? I also can't work out how to programme the sensor to log the values of the individual gases it is reading. Does anyone know how to go about this? Would help my data credibility immensely! TIA!
Code for reading VOC:
/*********GET VOC ************/
int a=analogRead(Aout);
int b=digitalRead(Dout);
Serial.print("D0:");
Serial.print(b);
Serial.print(" A0:");
Serial.println(a);
Full code for my VOCxTPH sensor:
//********************Start of code for TPH sensor***********************
#include <Wire.h>
#include "RTClib.h"
#include <BME280I2C.h>
#include <SPI.h>
#include <SD.h>
BME280I2C bme;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
RTC_DS1307 rtc;
uint32_t delayMS;
const int chipSelect = 10;
const int pingPin = 7;
unsigned int rtcd;
unsigned int rtcm;
unsigned int rtcy;
unsigned int rtcs;
unsigned int rtcmin;
unsigned int rtchr;
//This sensor measures changes in VOC gas levels//
#define Aout A0
#define Dout 2
void setup() {
pinMode(Aout,INPUT);
pinMode(Dout,INPUT);
// Open serial communications and wait for port to open:
Serial.begin(9600);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
// following line sets the RTC to the date & time this sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2020, 12, 16, 10, 36, 0));
}
while(!bme.begin())
{
Serial.println("Could not find BME280 sensor!");
delay(1000);
}
// bme.chipID(); // Deprecated. See chipModel().
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
}
Serial.print("Initializing SD card...");
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");
}
void loop() {
/*****************Get Temp, Pressure and Humidity********************/
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pres, temp, hum, tempUnit, presUnit);
float preshpa = (pres/100); //create space for conversion of pascals to hectopascals (hPa) which is more usual measure
float corr_temp = (temp-0.0); //create space for correction of temperature according to unique sensor validation (will be different for each sensor)
float corr_hum = (hum+0); //create space for correction of humidity according to unique sensor validation (will be different for each sensor)
float corr_preshpa = (preshpa+10); //create space for correction of pressure according to unique sensor validation (will be different for each sensor)
Serial.print("Temp: ");
Serial.print(corr_temp);
Serial.print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
Serial.print("\t\tHumidity: ");
Serial.print(corr_hum);
Serial.print("% RH");
Serial.print("\t\tPressure: ");
Serial.print(corr_preshpa);
Serial.print(" hPa ");
/*********GET VOC ************/
int a=analogRead(Aout);
int b=digitalRead(Dout);
Serial.print("D0:");
Serial.print(b);
Serial.print(" A0:");
Serial.println(a);
/*********GET TIME ************/
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
DateTime now = rtc.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
//Serial.print(" since midnight 1/1/1970 = ");
//Serial.print(now.unixtime());
//Serial.print("s = ");
//Serial.print(now.unixtime() / 86400L);
//Serial.println("d");
// DateTime now = rtc.now();
rtcs = now.second();
rtcmin = now.minute();
rtchr = now.hour();
rtcy = now.year();
rtcm = now.month();
rtcd = now.day();
/*********STORE DATA TO SD CARD ************/
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
//dataFile.print(cm); dataFile.print(",CM,");
dataFile.print(rtchr); dataFile.print(":");
dataFile.print(rtcmin); dataFile.print(":");
dataFile.print(rtcs); dataFile.print(",TIME,");
dataFile.print(rtcd); dataFile.print("/");
dataFile.print(rtcm); dataFile.print("/");
dataFile.print(rtcy); dataFile.print(",DATE,");
dataFile.print(corr_temp); dataFile.print(",Temperature,");
dataFile.print(corr_preshpa); dataFile.print(",Pressure,");
dataFile.print(corr_hum); dataFile.print(",Humidity,");
dataFile.print(b); dataFile.print(",TVOC Digital,");
dataFile.print(a); dataFile.println(",TVOC Analogue");
delay(1000);
dataFile.close();
delay(10);
// print to the serial port too:
//Serial.println(cm);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.csv");
}
delay(20);
}
// ***********************End of Code -- you are DONE!*******************************