Hi there,
I made a post recently about the ds-CO2-20 CO2-sensor. I got it to work, but when i connect a second sensor i get the same result from both sensors. Sometimes it differs a tiny bit but the 2 sensor follow each other even when 1 is in a bag and the other in free air. I think the problem lies with the softwareserial library. Does anyone know how to fix it or how to get it working on a hardware tx and rx. I'm working with the Arduino MEGA so i can use 3 TX and RX connections. for connectons see the other toppic. The only difference is pin 10 and 11 are used for the second sensor. Ds-co2-20 CO2 sensor
Here is my code:
/*
coppied from a program by Frank Haelman
of Energy Technology at Odisee University College.
This program reads the CO2-value from the sensor DS-C02-20
Connection DS-CO2-20 (=LEFT) to UNO (= RIGHT):
PIN 1 = Vin => 5V
PIN 2 = MASS => GND
PIN 3 = SDA/TX => pin 12 UNO
PIN 4 = RCL/RX => pin 13 UNO
*/
// Include the library
#include <SoftwareSerial.h>
#include <Wire.h>
#define SOFT_TX1 12 // CO2_1 TX
#define SOFT_RX1 13 // CO2_1 RX
#define SOFT_TX2 10 // CO2_1 TX
#define SOFT_RX2 11 // CO2_1 RX
SoftwareSerial mySerial1(SOFT_TX1, SOFT_RX1); // Object for first ds-CO2-20 sensor (TX and RX)
SoftwareSerial mySerial2(SOFT_TX2, SOFT_RX2); // Object for first ds-CO2-20 sensor (TX and RX)
// Preset values for measurements
const int delayForMeasurements = 1000; //in milliseconds
const int preHeatingTime = 10;
int requestCO2Measurement1() {
// function => measuring CO2 with DS-C02-20 sensor
unsigned char CO2_Read[] = {0x42, 0x4d, 0xe3, 0x00, 0x00, 0x01, 0x72};
unsigned char CO2_Value[24], num = 0;
mySerial1.flush();
mySerial1.write(CO2_Read, 7);
for (unsigned char i = 0; i < 100; i++) {
delay(1);
if (mySerial1.available() == 12) {
while (mySerial1.available()) {
CO2_Value[num] = mySerial1.read();
num++;
}
break;
}
}
int CO2Concentration = int(CO2_Value[4] * 256.0 + CO2_Value[5]);
Serial.print(F("CO2 concentration 1: "));
Serial.print(CO2Concentration);
Serial.println(F(" PPM"));
return CO2Concentration;
}
int requestCO2Measurement2() {
// function => measuring CO2 with DS-C02-20 sensor
unsigned char CO2_Read[] = {0x42, 0x4d, 0xe3, 0x00, 0x00, 0x01, 0x72};
unsigned char CO2_Value[24], num = 0;
mySerial2.flush();
mySerial2.write(CO2_Read, 7);
for (unsigned char i = 0; i < 100; i++) {
delay(1);
if (mySerial2.available() == 12) {
while (mySerial2.available()) {
CO2_Value[num] = mySerial2.read();
num++;
}
break;
}
}
int CO2Concentration = int(CO2_Value[4] * 256.0 + CO2_Value[5]);
Serial.print(F("CO2 concentration 2: "));
Serial.print(CO2Concentration);
Serial.println(F(" PPM"));
return CO2Concentration;
}
void setup() {
// put your setup code here, to run once:
mySerial1.begin(9600); //setting up CO2 communication
mySerial2.begin(9600); //setting up CO2 communication
Serial.begin(9600);
// Configures the reference voltage used for analog input (i.e. the value used as the top of the input range).
analogReference(DEFAULT);
// Print the preaheating time
for (int t = 0; t < preHeatingTime; t++) {
int countdown = preHeatingTime - t;
Serial.println("Preheating: " + String(countdown) + " Seconds");
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
int CO2Concentration1 = requestCO2Measurement1();
delay(delayForMeasurements);
int CO2Concentration2 = requestCO2Measurement2();
delay(delayForMeasurements);
}
Only one SoftwareSerial object can be listening at a single moment. As your sensor seems to send data only if explicitly requested, you can call the listen() method on it to activate listening for that object.
Nevertheless I would suggest to change the wiring to the hardware serial interfaces as they work much more reliable than the software emulation.
Thanks for the response. How do i code it to work for example on tx 1 and RX 1 on thé Arduino MEGA. I don't know a lot about how thé serial system works.
i Still have some issues i think the values are still from the same sensor
/*
coppied from a program by Frank Haelman
of Energy Technology at Odisee University College.
This program reads the CO2-value from the sensor DS-C02-20
Connection DS-CO2-20 (=LEFT) to UNO (= RIGHT):
PIN 1 = Vin => 5V
PIN 2 = MASS => GND
PIN 3 = SDA/TX => pin 12 UNO
PIN 4 = RCL/RX => pin 13 UNO
*/
// Include the library
#include <Wire.h>
// Preset values for measurements
const int delayForMeasurements = 1000; //in milliseconds
const int preHeatingTime = 10;
int requestCO2Measurement1() {
// function => measuring CO2 with DS-C02-20 sensor
unsigned char CO2_Read[] = {0x42, 0x4d, 0xe3, 0x00, 0x00, 0x01, 0x72};
unsigned char CO2_Value[24], num = 0;
Serial1.flush();
Serial1.write(CO2_Read, 7);
for (unsigned char i = 0; i < 100; i++) {
delay(1);
if (Serial1.available() == 12) {
while (Serial1.available()) {
CO2_Value[num] = Serial1.read();
num++;
}
break;
}
}
int CO2Concentration = int(CO2_Value[4] * 256.0 + CO2_Value[5]);
Serial.print(F("CO2 concentration 1: "));
Serial.print(CO2Concentration);
Serial.println(F(" PPM"));
return CO2Concentration;
}
int requestCO2Measurement2() {
// function => measuring CO2 with DS-C02-20 sensor
unsigned char CO2_Read[] = {0x42, 0x4d, 0xe3, 0x00, 0x00, 0x01, 0x72};
unsigned char CO2_Value[24], num = 0;
Serial2.flush();
Serial2.write(CO2_Read, 7);
for (unsigned char i = 0; i < 100; i++) {
delay(1);
if (Serial2.available() == 12) {
while (Serial2.available()) {
CO2_Value[num] = Serial2.read();
num++;
}
break;
}
}
int CO2Concentration = int(CO2_Value[4] * 256.0 + CO2_Value[5]);
Serial.print(F("CO2 concentration 2: "));
Serial.print(CO2Concentration);
Serial.println(F(" PPM"));
return CO2Concentration;
}
void setup() {
// put your setup code here, to run once:
Serial1.begin(9600); //setting up CO2 communication
Serial2.begin(9600); //setting up CO2 communication
Serial.begin(9600);
// Configures the reference voltage used for analog input (i.e. the value used as the top of the input range).
analogReference(DEFAULT);
// Print the preaheating time
for (int t = 0; t < preHeatingTime; t++) {
int countdown = preHeatingTime - t;
Serial.println("Preheating: " + String(countdown) + " Seconds");
delay(1000);
}
}
void loop() {
// put your main code here, to run repeatedly:
int CO2Concentration1 = requestCO2Measurement1();
int CO2Concentration2 = requestCO2Measurement2();
delay(delayForMeasurements);
}
i connected the first sensor to pins 18 and 19 and the second to 16 and 17
Both sensors follow each others readings perfectly even tho 1 sensor is in a almost airtight bag and 1 sensor is not. I would expect that the sensor in the bag stays at a certain CO2 level or at least changes slower because almost no air can get in or out the bag. The levels are so high because i breath on them. But both sensors go up the exact same amount in exactly the same time. Also these sensors need to be calibrated and they are not yet well calibrated, so out of experence with other sensors like this i know they should not be at the same CO2 level.