I want to use two CO2 sensor. But it did not works. Help!

I want to make a mini automated plant pot in a plastic box.
What I want to do is, use two CO2 sensors.
At first, I attached one CO2 sensor to the plastic box.
And I attached the other CO2 sensor out of the plastic box.
Then, It will show my students the difference in CO2 gas concentration.

Because the plant uses CO2 in the plastic box, the CO2 concentration will be lower than outside of the box.
I think this is a good idea for understanding photosynthesis students.
However,
I don't know how to code two other CO2 sensors at one Arduino.
I did very well at using just one CO2 sensor.
I used the attached code, but it did not work. And I don't know why.
Help me, please.

I used cm1106 co2 sensor. and use the code in the description.
And I used the code that attached.

cm1106 sensor

And I used Arduino Uno.

#include <SoftwareSerial.h>

SoftwareSerial mySerial_1(13,11);
SoftwareSerial mySerial_2(7,8);
unsigned char Send_data_1[4] = {0x11, 0x01, 0x01, 0xED};
unsigned char Send_data_2[4] = {0x11, 0x01, 0x01, 0xED};
unsigned char Receive_Buff_1[8];
unsigned char Receive_Buff_2[8];
unsigned char recv_cnt_1 = 0;
unsigned char recv_cnt_2 = 0;
unsigned int PPM_Value_1;
unsigned int PPM_Value_2;

void Send_CMD_1(void) {
  unsigned int i;
  for (i = 0; i < 4; i++) {
    mySerial_1.write(Send_data_1[i]);
    delay(1);
  }
}
unsigned char Checksum_cal_1(void) {
  unsigned char count_1, SUM_1 = 0;
  for (count_1 = 0; count_1 < 7; count_1++) {
    SUM_1 += Receive_Buff_1[count_1];
  }
  return 256 - SUM_1;
}

void Send_CMD_2(void) {
  unsigned int i;
  for (i = 0; i < 4; i++) {
    mySerial_2.write(Send_data_2[i]);
    delay(1);
  }
}
unsigned char Checksum_cal_2(void) {
  unsigned char count_2, SUM_2 = 0;
  for (count_2 = 0; count_2 < 7; count_2++) {
    SUM_2 += Receive_Buff_2[count_2];
  }
  return 256 - SUM_2;
}

void setup() {
  pinMode(13,INPUT);
  pinMode(11,OUTPUT);
  pinMode(7,INPUT);
  pinMode(8,OUTPUT);
  Serial.begin(9600);
  while (!Serial) ;
  mySerial_1.begin(9600);
  
  while (!mySerial_1);
  while (!mySerial_2);
}

void loop() {
  Serial.print("Sending...");
  Send_CMD_1();
  while (1) {
      if (mySerial_1.available() and mySerial_2.available()) {
        Receive_Buff_1[recv_cnt_1++] = mySerial_1.read();
        Receive_Buff_2[recv_cnt_2++] = mySerial_2.read();
        if   (recv_cnt_1 == 8 and recv_cnt_2 == 8) {
                recv_cnt_1 = 0;
                recv_cnt_2 = 0;
                break;
         }
            }
         }

  if (Checksum_cal_1() == Receive_Buff_1[7] and Checksum_cal_2() == Receive_Buff_2[7]) {
     PPM_Value_1 = Receive_Buff_1[3] << 8 | Receive_Buff_1[4];
     PPM_Value_2 = Receive_Buff_2[3] << 8 | Receive_Buff_2[4];
     Serial.write("    PPM(Sensor_1) : ");
     Serial.write("    PPM(Sensor_2) : ");
     Serial.println(PPM_Value_1);
     Serial.println(PPM_Value_2);
        }
  else {
     Serial.write("CHECHSUM ERROR");
  }
  delay(1000);
}

Please help the helpers by reading and following the instructions in the "How to get the best out of this forum" post, linked at the head of every forum category.

That would include editing your post to add code tags, and adding links to the sensors.

Thanks for your suggestion. I fixed it. thanks.

On could better describe what "it does not work" mean.

One could get a micro controller with more than 1 hardware serial port.

One could do a searchy thingy on the words "arduino using 2 software serials".

Thanks for your suggestion. I found some 2 software serials post but it that, there is no void_CMD things. they just use values.

Describe what you expected to happen, and what happened instead.

State which Arduino you are using.

When using two Software serials, only one can "listen" at once. You have to switch between them.

Please read that "How to use the forum" post.

I do not understand what you are trying to convey.

Thanks for your suggestion. I have some developments. I fix the code. and they just show 0 for all sensor values.


#include <SoftwareSerial.h>

SoftwareSerial mySerial_1(13,11);
SoftwareSerial mySerial_2(7,8);
unsigned char Send_data_1[4] = {0x11, 0x01, 0x01, 0xED};
unsigned char Send_data_2[4] = {0x11, 0x01, 0x01, 0xED};
unsigned char Receive_Buff_1[8];
unsigned char Receive_Buff_2[8];
unsigned char recv_cnt_1 = 0;
unsigned char recv_cnt_2 = 0;
unsigned int PPM_Value_1;
unsigned int PPM_Value_2;

void Send_CMD_1(void) {
  unsigned int i;
  for (i = 0; i < 4; i++) {
    mySerial_1.write(Send_data_1[i]);
    delay(1);
  }
}
unsigned char Checksum_cal_1(void) {
  unsigned char count_1, SUM_1 = 0;
  for (count_1 = 0; count_1 < 7; count_1++) {
    SUM_1 += Receive_Buff_1[count_1];
  }
  return 256 - SUM_1;
}

void Send_CMD_2(void) {
  unsigned int i;
  for (i = 0; i < 4; i++) {
    mySerial_2.write(Send_data_2[i]);
    delay(1);
  }
}
unsigned char Checksum_cal_2(void) {
  unsigned char count_2, SUM_2 = 0;
  for (count_2 = 0; count_2 < 7; count_2++) {
    SUM_2 += Receive_Buff_2[count_2];
  }
  return 256 - SUM_2;
}

void setup() {
  Serial.begin(9600);
  while (!Serial) ;{}
  mySerial_1.begin(9600);
  mySerial_2.begin(9600);
}

void loop() {
  Serial.print("Sending...");
  mySerial_1.listen();
  Send_CMD_1();
  while(mySerial_1.available()>0) {
        Receive_Buff_1[recv_cnt_1++] = mySerial_1.read();
        if   (recv_cnt_1 == 8) {
                recv_cnt_1 = 0;
                break;
         }
            }
         

  if (Checksum_cal_1() == Receive_Buff_1[7]) {
     PPM_Value_1 = Receive_Buff_1[3] << 8 | Receive_Buff_1[4];
     Serial.write("    PPM_sensor_1 : ");
     Serial.println(PPM_Value_1);
        }
  else {
     Serial.write("CHECHSUM ERROR");
  }

  mySerial_2.listen();
  Send_CMD_2();
  while (mySerial_2.available()>0) {
        Receive_Buff_2[recv_cnt_2++] = mySerial_2.read();
        if   (recv_cnt_2 == 8) {
                recv_cnt_2 = 0;
                break;
         }
            }
         

  if (Checksum_cal_2() == Receive_Buff_2[7]) {
     PPM_Value_2 = Receive_Buff_2[3] << 8 | Receive_Buff_2[4];
     Serial.write("    PPM_sensor_2 : ");
     Serial.println(PPM_Value_2);
        }
  else {
     Serial.write("CHECHSUM ERROR");
  }  
delay(1000);
}

How can I swiching between them?? Plaese. let me know.

This is from the documentation of software serial .listen()

listen()
Enables the selected SoftwareSerial object to listen. Only one SoftwareSerial object can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() function (unless the given instance is already listening).

Try to complete the command/response for each instance before advancing through loop()

Serial.print("Sending mySerial_1...");
  send_CMD_1();
  delay(500);//time for response
  mySerial_1.listen();
 if (mySerial_1.isListening()) {
        Serial.println("mySerial_1 is listening!");
    } else {
        Serial.println("mySerial_1 is not listening!");
    }
  //Send_CMD_1();
  while(mySerial_1.available()>0) {

Thanks for your suggestion. however, I did not works.
the sensor show me only 0 values when I using two senor.
But using one sensor, It works.

Try without the delay()

send_CMD_1();
 // delay(500);//time for response
  mySerial_1.listen();

You may want to try AltSoftSerial for one of the ports
https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

It would also be good to consider alternative Arduino's, like the Mega, or other mcus using the Arduino IDE which have multiple hardware serial ports.

I am very thanks for your help. I will try what you suggested. After that, I'll leave the results. Thanks so much. You are my and my student's hero!! When I ask about some other problem again, I believe you can help me.

Hi,
Can you please post a complete circuit diagram of you project?
Include both sensors and all connections and power supply.
Make sure you note component names and pin labels for clarity?

Can you please post image(s) of your project so we can see your component layout?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

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