Using K30 CO2 Sensor with Wemo D1 on Arduino IDE
Posted: Sat Mar 05, 2016 3:57 am
Hey friends!
I'm a super i2c newb, and am just trying to get this fancy K30 Co2 sensor (http://www.co2meter.com/products/k-30-co2-sensor-module) code that is working well with my UNO to work on this sweet, sweet little Wemos D1 Mini via arduino IDE (1.6....5?) And, for the record, the Mini is taking code from the arduino IDE superbly.
I was able to get an HTU21D sensor working pretty easily on pins D1/D2 so I decided to give it a try with the CO2 sensor. This is the 'provided' code from the company, and I've changed the pins to reflect D1/D2 (though I would prefer to use other pins if possible)
Any thoughts on how to make this work? It IS spitting out 'Co2 ppm = 65535' every 3 seconds in Serial, but the value doesn't ever change.
I'm a super i2c newb, and am just trying to get this fancy K30 Co2 sensor (http://www.co2meter.com/products/k-30-co2-sensor-module) code that is working well with my UNO to work on this sweet, sweet little Wemos D1 Mini via arduino IDE (1.6....5?) And, for the record, the Mini is taking code from the arduino IDE superbly.
I was able to get an HTU21D sensor working pretty easily on pins D1/D2 so I decided to give it a try with the CO2 sensor. This is the 'provided' code from the company, and I've changed the pins to reflect D1/D2 (though I would prefer to use other pins if possible)
Any thoughts on how to make this work? It IS spitting out 'Co2 ppm = 65535' every 3 seconds in Serial, but the value doesn't ever change.
Code: Select all
/*
Basic Arduino example for K-Series sensor
Created by Jason Berger
Co2meter.com
*/
#include "SoftwareSerial.h"
SoftwareSerial K_30_Serial(5,4); //Sets up a virtual serial port
//Using pin 12 for Rx and pin 13 for Tx
byte readCO2[] = {0xFE, 0X44, 0X00, 0X08, 0X02, 0X9F, 0X25}; //Command packet to read Co2 (see app note)
byte response[] = {0,0,0,0,0,0,0}; //create an array to store the response
//multiplier for value. default is 1. set to 3 for K-30 3% and 10 for K-33 ICB
int valMultiplier = 3;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600); //Opens the main serial port to communicate with the computer
K_30_Serial.begin(9600); //Opens the virtual serial port with a baud of 9600
}
void loop()
{
sendRequest(readCO2);
unsigned long valCO2 = getValue(response);
Serial.print("Co2 ppm = ");
Serial.println(valCO2);
delay(2000);
}
void sendRequest(byte packet[])
{
while(!K_30_Serial.available()) //keep sending request until we start to get a response
{
K_30_Serial.write(readCO2,7);
delay(50);
}
int timeout=0; //set a timeoute counter
while(K_30_Serial.available() < 7 ) //Wait to get a 7 byte response
{
timeout++;
if(timeout > 10) //if it takes to long there was probably an error
{
while(K_30_Serial.available()) //flush whatever we have
K_30_Serial.read();
break; //exit and try again
}
delay(50);
}
for (int i=0; i < 7; i++)
{
response[i] = K_30_Serial.read();
}
}
unsigned long getValue(byte packet[])
{
int high = packet[3]; //high byte for value is 4th byte in packet in the packet
int low = packet[4]; //low byte for value is 5th byte in the packet
unsigned long val = high*256 + low; //Combine high byte and low byte with this formula to get value
return val* valMultiplier;
}