# Ds-co2-20 CO2 sensor

**URL:** <https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097>\
**Category:** Sensors\
**Created:** [April 19, 2023, 12:46pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097 "2023-04-19T12:46:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cheeslover13](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cheeslover13](https://forum.arduino.cc/u/cheeslover13)\
**Post date:** [April 19, 2023, 12:46pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/1 "2023-04-19T12:46:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 19, 2023, 1:00pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/2 "2023-04-19T13:00:55Z")

</div>

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.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![cheeslover13](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cheeslover13](https://forum.arduino.cc/u/cheeslover13)\
**Post date:** [April 20, 2023, 1:29pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/3 "2023-04-20T13:29:52Z")

</div>

Hi,  
here is the code i got from chat gpt that always gives 0:

```cpp
// 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:

> **[DS-CO2-20%20series%20data%20manual\_English\_V2.9.pdf](https://www.icbanq.com/icdownload/data/ICBShop/Board/DS-CO2-20%20series%20data%20manual_English_V2.9.pdf)**
>
> 746.35 KB

![image](https://europe1.discourse-cdn.com/arduino/original/4X/0/7/4/0748ac942bdef9c5a72f8aa884a6799a8850a111.png)

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)|

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 20, 2023, 2:07pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/4 "2023-04-20T14:07:43Z")

</div>

Hi,  
Thanks for the info.  
To add code please click this link;

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code-and-common-code-problems):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

Tom.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![cheeslover13](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cheeslover13](https://forum.arduino.cc/u/cheeslover13)\
**Post date:** [April 20, 2023, 2:30pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/5 "2023-04-20T14:30:51Z")

</div>

Hi,  
Sorry for the inconvenience I fixt my mistake.

Thank you for taking your time with me.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [April 20, 2023, 6:13pm UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/6 "2023-04-20T18:13:58Z")

</div>

> [@cheeslover13](#):
>
> 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.. 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![cheeslover13](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@cheeslover13](https://forum.arduino.cc/u/cheeslover13)\
**Post date:** [April 21, 2023, 9:58am UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/8 "2023-04-21T09:58:17Z")

</div>

**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:**

> **[DS-CO2-20%20series%20data%20manual\_English\_V2.9.pdf](https://www.icbanq.com/icdownload/data/ICBShop/Board/DS-CO2-20%20series%20data%20manual_English_V2.9.pdf)**
>
> 746.35 KB

**code:**

```plaintext
/*
  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:**  
 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/f/1/c/f1c46663783c712d52863f34c4c1038ab2674afb.jpeg)

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [October 18, 2023, 9:58am UTC](https://forum.arduino.cc/t/ds-co2-20-co2-sensor/1117097/9 "2023-10-18T09:58:32Z")

</div>

Closed on October 18, 2023, 9:58am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
