I am trying to get the following co2 sensor to work:
I have hooked up scl to d1 and sda to d0. Beneath is my code. When i run the sensor by itself, it runs fine. the problem comes when I integrate additional sensors. For example, the system i am trying to build has light, noise, temperature, acceleration and motion sensors. Running the code with all the sensors, i always get an error with the co2 sensor. Take away all the sensors and just run the co2 code, and it’s fine.
I was also trying to integrate an sdcard in there, but that problem is an whole new challenge in itself.
Co2 code:
// Example usage for Adafruit_SGP30 library.
// This library was modified/wrapped by SJB (https://github.com/dyadica)
// in order to work with Particle Photon & Core.
#include "Particle.h"
#include "Adafruit_SGP30.h"
Adafruit_SGP30 sgp;
void setup() {
Serial.begin(9600);
Serial.println("SGP30 test");
if (! sgp.begin()){
Serial.println("Sensor not found :(");
while (1);
}
Serial.print("Found SGP30 serial #");
Serial.print(sgp.serialnumber[0], HEX);
Serial.print(sgp.serialnumber[1], HEX);
Serial.println(sgp.serialnumber[2], HEX);
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
// sgp.setIAQBaseline(0x8F3A, 0x8D74); // Will vary for each sensor!
}
int counter = 0;
void loop() {
if (! sgp.IAQmeasure()) {
Serial.println("Measurement failed");
return;
}
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
delay(1000);
counter++;
if (counter == 30) {
counter = 0;
uint16_t TVOC_base, eCO2_base;
if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
Serial.println("Failed to get baseline readings");
return;
}
Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
}
}
Combined code:
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_SGP30.h>
// This #include statement was automatically added by the Particle IDE.
//#include <TinyGPS++.h>
#include "Particle.h"
Adafruit_SGP30 sgp;
#define DHTPIN A1 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
/* Vibration */
const int PIEZO_PIN = A0; // Piezo output
/* PIR motion */
int inputPin = D2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
//int value = 0; // variable for reading the pin status
/* Sound*/
#include<math.h>
/* Light */
int lightPin = B5;
/* accel */
int accel_x = B2;
int accel_y = B3;
int accel_z = B4;
/* gps
static const uint32_t GPSBaud = 9600;
TinyGPSPlus gps;
*/
/* load switch */
int loadSwitch = D6;
void setup(void)
{
Serial.begin(9600);
/* gps */
// Serial1.begin(GPSBaud);
dht.begin();
/* PIR motion sensor*/
pinMode(inputPin, INPUT); // declare sensor as input
/* light sensor */
pinMode(lightPin, AN_INPUT);
/*acceleration sensor */
pinMode(accel_x, AN_INPUT);
pinMode(accel_y, AN_INPUT);
pinMode(accel_z, AN_INPUT);
/* air sensor*/
Serial.println("SGP30 test");
if (! sgp.begin()){
Serial.println("Sensor not found :(");
while (1);
}
Serial.print("Found SGP30 serial #");
Serial.print(sgp.serialnumber[0], HEX);
Serial.print(sgp.serialnumber[1], HEX);
Serial.println(sgp.serialnumber[2], HEX);
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
// sgp.setIAQBaseline(0x8E77, 0x92AE); // Will vary for each sensor!
/* load switch */
pinMode(loadSwitch, OUTPUT);
}
int counter = 0;
void loop(void)
{
// go into operating mode here
digitalWrite(loadSwitch, HIGH);
delay(7000); // gather data for set period of time
sensor();
digitalWrite(loadSwitch, LOW); // turn power to sensors off
// enter into standby mode here.
delay(3000); // around 10 to 15 minutes?
}
void sensor(){
String results = ""; // collected data for each sample
/* co2 sensor*/
String co2data = "";
if (!sgp.IAQmeasure()) {
Serial.println("CO2 measurement failed");
co2data = " CO2 measurement failed ";
// return;
} else {
Serial.print("TVOC "); Serial.print(sgp.TVOC); Serial.print(" ppb\t");
Serial.print("eCO2 "); Serial.print(sgp.eCO2); Serial.println(" ppm");
// delay(1000);
co2data = " TVOC: " + String(sgp.TVOC) + " ppb " + " eCO2: " + String(sgp.eCO2) + " ppm ";
}
counter++;
if (counter == 30) {
counter = 0;
uint16_t TVOC_base, eCO2_base;
if (! sgp.getIAQBaseline(&eCO2_base, &TVOC_base)) {
Serial.println("Failed to get baseline readings");
return;
}
Serial.print("****Baseline values: eCO2: 0x"); Serial.print(eCO2_base, HEX);
Serial.print(" & TVOC: 0x"); Serial.println(TVOC_base, HEX);
}
/* light sensor*/
double lightValue = (((analogRead(lightPin)*(3.3/4095.0)/10000.0)*1000000)/0.2);
Serial.println("light: " + String(lightValue));
//Serial.println(((analogRead(lightPin)*(3.3/4095.0)/10000.0)*1000000)/0.2);
//String lightdata = " Light (lux): " + String(analogRead(((lightPin)*(3.3/4095.0)/10000.0)*1000000)/0.2);
String lightdata = " Light (lux): " + String(lightValue);
/* location sensor
if (!Serial1.available() > 0)
{
Serial.println("no gps data currently available");
}
while (Serial1.available() > 0) {
if (gps.encode(Serial1.read())) {
char buf[128];
if (gps.location.isValid()) {
snprintf(buf, sizeof(buf), "%f,%f,%f", gps.location.lat(), gps.location.lng(), gps.altitude.meters());
}
else
{
strcpy(buf, "no location");
}
Serial.println(buf);
}
}
*/
/* acceleraition sensor*/
double x = analogRead(accel_x);
double y = analogRead(accel_y);
double z = analogRead(accel_z);
Serial.print( "x = ");
Serial.print(convertToG(x));
Serial.print( "y = ");
Serial.print(convertToG(y));
Serial.print( "z = ");
Serial.println(convertToG(z));
String acceldata = " Accel (g) x: " + String(convertToG(x)) + " y: " + String(convertToG(y)) + " z: " + String(convertToG(z));
/* Vibration sensor*/
/*Vibration*/
// Read Piezo ADC value in, and convert it to a voltage
int piezoADC = analogRead(PIEZO_PIN);
double piezoV = piezoADC / 4095.0 * 3.3; // particle electron 12 bit , 3.3V
Serial.println(piezoV); // Print the voltage.
String vibrationdata = " Vibration: " + String(piezoV);
// delay(250);
/*temp_Humadity sensor*/
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a
// very slow sensor)
double h = dht.getHumidity();
// Read temperature as Celsius
double t = dht.getTempCelcius();
// Read temperature as Farenheit
double f = dht.getTempFarenheit();
String tempdata = "";
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
tempdata = " Failed to read from temperature sensor ";
// return;
} else {
// Compute heat index
// Must send in temp in Fahrenheit!
double hi = dht.getHeatIndex();
double dp = dht.getDewPoint();
double k = dht.getTempKelvin();
Serial.print("Humid: ");
Serial.print(h);
Serial.print("% - ");
Serial.print("Temp: ");
Serial.print(t);
Serial.print("*C ");
Serial.print(f);
Serial.print("*F ");
Serial.print(k);
Serial.print("*K - ");
Serial.print("DewP: ");
Serial.print(dp);
Serial.print("*C - ");
Serial.print("HeatI: ");
Serial.print(hi);
Serial.println("*C");
tempdata = " Humidity: " + String(h) + "% Temp: " + String(t) + "*C DewP: " + String(dp) + "*C HeatI: " + String(hi) + "*C ";
}
/* motion sensor*/
int value = digitalRead(inputPin); // read input value
String motiondata = "";
if (value == HIGH) { // check if the input is HIGH
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
motiondata = " Motion detected ";
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
motiondata = " Motion ended ";
// We only want to print on the output change, not state
pirState = LOW;
}
}
/* sound sensor*/
int val;
int max = 0;
int counter = 0;
while(counter < 1000){
val=analogRead(A7); //connect mic sensor to Analog MK
if(val > max){
max = val;
}
counter++;
}
//Serial.println(val,DEC);//print the sound value to serial
Serial.println(16.44*log(max) - 19.455 ,DEC);//print the sound value to
String sounddata = " Sound(db): " + String(16.44*log(max) - 19.455 ,DEC);
// Publsih to the cloud
results = co2data + " " + lightdata + " " + acceldata + " " + vibrationdata + " " + tempdata + " " + motiondata + " " + sounddata;
Particle.publish ("Data", results);
}
double convertToG(double num){
return (num - 2048) / 400;
}