Hi i'm working with a Arduino MEGA 2560 and i'm trying to get the ds-CO2-20 CO2 sensor working but i don't know if i'm using the right code or even connecting the right cables. Does anyone have experience with this sensor and can u give me a simple code with wich it works. I've been searching on the internet but theres not allot of information on the sensor. Thanks in advance.
Hi,
Can you please post links to data/spec of the sensor?
Post the code you have.
Post a circuit diagram you are currently using.
Thanks.. Tom..
Hi,
here is the code i got from chat gpt that always gives 0:
// Define the RX pin for the hardware serial communication
#define CO2_SENSOR_RX 17
void setup() {
// Initialize the serial communication for debugging purposes
Serial.begin(9600);
// Initialize the hardware serial communication with the CO2 sensor
Serial2.begin(9600);
}
void loop() {
// Request the CO2 data from the sensor
Serial2.write("Z\r\n");
// Wait for the response from the sensor
delay(10);
// Read the response from the sensor
String response = "";
while (Serial2.available()) {
char c = Serial2.read();
response += c;
}
// Extract the CO2 value from the response
int co2Value = response.toInt();
// Print the CO2 value to the serial monitor
Serial.print("CO2 value: ");
Serial.println(co2Value);
// Wait for some time before taking another reading
delay(5000);
}
this is a datasheet i found:
But this setup doesn't work so any code and connection is good as long as it works with the Arduino MEGA. I'm truly lost here how hard can it be to just get readings from this sensor.
If the connections aren't clear on the picture here they are:
|ds-co2-20 sensor|Arduino Mega |
|VCC | 5V|
|GND |GND|
|RX |TX2 (pin 17)|
|TX |RX2 (pin 16)|
Hi,
Thanks for the info.
To add code please click this link;
Tom..
Hi,
Sorry for the inconvenience I fixt my mistake.
Thank you for taking your time with me.
For others looking at this thread that in future could be helped, what was the mistake?
Thanks.. Tom..
UPDATE Now I really fixed it here are all the details!
connections:
DS-CO2-20 --- (wire collor)--- Arduino MEGA
VCC (pin 1) --- (RED) --- 5V
GND (pin 2) --- (BLACK) --- GND
TX (pin 3) --- (YELLOW) --- pin 12
RX (pin 4) --- (GREEN) --- pin 13
datasheet for ds-CO2-20:
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_TX 12 // CO2_1 TX
#define SOFT_RX 13 // CO2_1 RX
SoftwareSerial mySerial(SOFT_TX, SOFT_RX); // 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 requestCO2Measurement() {
// 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;
mySerial.flush();
mySerial.write(CO2_Read, 7);
for (unsigned char i = 0; i < 100; i++) {
delay(1);
if (mySerial.available() == 12) {
while (mySerial.available()) {
CO2_Value[num] = mySerial.read();
num++;
}
break;
}
}
int CO2Concentration = int(CO2_Value[4] * 256.0 + CO2_Value[5]);
Serial.print(F("CO2 concentration: "));
Serial.print(CO2Concentration);
Serial.println(F(" PPM"));
return CO2Concentration;
}
void setup() {
// put your setup code here, to run once:
mySerial.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 CO2Concentration = requestCO2Measurement();
delay(delayForMeasurements);
}
Picture:
I hope this helps. If there are any further questions about this you can just ask me.
Thanks to TomGeorge for responding and trying to help.
GOOD LUCK... CheesLover13
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.