Interfacing of CO2 sensor module with Arduino ATMega328 through ttl

hello everyone,
I am trying to interface CO2 module with arduino. As per the instructions of the data, to get the data on serial port through TTL. I am suppose to follow the following instruction:

  1. Initialize UART as: baud rate - 19200, data - 8 bits, stop bit - 1 bit, parity- none.
  2. Send command 80h 02h 00h 7Eh to CO2 system until uplink response with type being 80h

Anyone can help me establish a arduino code to retrieve the data from CO2 module in serial using TTL cable.

See

Serial Begin
Serial Write
Serial Available
Serial Read

If you can't use the hardware serial , consider using SoftwareSerial

(What CO2 module do you have? some have high level libraries doing all the work for you)

Dear Sir,
I am using CO2CGM Module. I have included softwareserail.h library. As per the guideline, i need to send a command 80h 02h 00h 7Eh to co2 module to get the data. How could i write the mentioned code in arduino. As it is written in assembly, if i m not wrong.
I would really appreciate if u can provide me the arduino code for mentioned one (80h 02h 00h 7Eh).

thanks,
Om

it's not written in assembly but in Hexadecimal which they denote with the h after the number 80h 02h 00h 7Eh

which traditionally is represented with 0x before the number in programing - so you would do

CO2CGM_Serial.write(0x80); // sends 80h
CO2CGM_Serial.write(0x02); // sends 02h
CO2CGM_Serial.write(0x00); // sends 00h
CO2CGM_Serial.write(0x7E); // sends 7Eh

where CO2CGM_Serial is the Software Serial object you declared

Than you so much, I will try with this.

thanks,

Om

i am unable to get any output at the end: i m looking for the data on serial port. pls correct me if anything worng.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX
String val= ""; //holds the string of the value
double co2 =0; // holds the actual value
uint8_t buffer[128];
uint8_t ind =0;
byte initPort[] = {0x80,0x02,0x00,0x7E};
void setup() {
// initialize serial:
Serial.begin(19200);
// print the string when a newline arrives:
mySerial.begin(19200);
Serial.write(initPort, sizeof(initPort));
int hexIn = Serial.read();

}

void loop() {
//print the value on serial
while(buffer[ind-1] != 0x80)
{
if(mySerial.available())
{
buffer[ind] = mySerial.read();
ind++;
}
}
Serial.print( "Co2 = ");
Serial.print(co2);
Serial.println(" mmHg");
ind=0; //Reset the buffer index to overwrite the previous packet
}

attached is the guideline.

UN-medical Internal sidestream CO2 User Guide .pdf (230 KB)

Are you sure your device is set up at 19200?

At the end of setup

Serial.write(initPort, sizeof(initPort));
  int hexIn = Serial.read();

Where are you sending the initPort?? Is that your device on Serial?? What are you reading back?

In the loop()

uint8_t buffer[128]; 
uint8_t ind =0; 
....

while(buffer[ind-1] != 0x80)  // <-- first Time you arrive here what do you think ind-1 is?

Rethink your code structure. The loop should not need a while

If something arrives on serial
handle it and add to buffer at index position
If what I just recieved is the end of the communication
set a '\0' at the end of the buffer
print the buffer
Reset the buffer index to zero
Else increase index and check for buffer end/ overflow
End if
End if

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

I'm not sure if SoftwareSerial works well at 19200 baud. And it cannot listen while it is transmitting. Maybe have a look at NeoSWSerial

If that does not work I suggest you get an Arduino with 2 or more HardwareSerial ports - such as a Mega or Micro.

...R

19200 is no pb for software serial. Yes can't do both at the same time but in his case it's not needed so should be fine. His issue is that he is confusing his hardware Serial port with the software one and sending commands to the wrong place most likely.

Dear Sir, I have written the general code and getting output as a ~€.
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); // RX, TX
byte GetData[] = {0x80,0x02,0x00,0x7E};
void setup() {
// initialize serial:
Serial.begin(19200);
}
void loop()
{
Serial.write(GetData,sizeof(GetData));
}

while compiling the code, i am also getting overbaudrate 115200.

Please youse code tags so your code looks like

#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 5); // RX, TX
byte GetData[] = {0x80,0x02,0x00,0x7E};
void setup() {
  // initialize serial:
  Serial.begin(19200);
}
 void loop()
{
  Serial.write(GetData,sizeof(GetData));   
}

Is that the complete program?
It does not use SoftwareSerial at all.
You need a delay() - perhaps delay(300); - in loop() to slow it down so it does not overwhelm the serial interface.
Are you viewing the output in the Serial Monitor?
0x7E is the only one of your values that matches a printable character - what are you expecting to see?

...R

i think so, i want to get the output as 0x80.

I would really appreciate to you if u can modify or give your suggestion. i may not be getting the data.

dear friend,
i m able to receive the data as per need. now i need to formulate. on serial port, i m getting 5 values for example A B C D E and i wanna use in formula 4 and 5.
the formula for this as following:
((128*D+ E)-1000)/100
I would really appreciate you guys. if u can give some solution.

thanks

bioom85:
I would really appreciate you guys. if u can give some solution.

Have you tried the examples (perhaps the 2nd example would be best for you) I linked to in Reply #8?

If so, please post an example of what it produces.

...R

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // RX, TX
byte GetData[5]={0x80,0x04,0x00,0x7C};
void setup() {
  // initialize serial:
  Serial.begin(19200);
  Serial.println("Co2");
  mySerial.begin(19200);
  mySerial.write(GetData,sizeof(GetData));
}
 void loop()
{
    if(mySerial.available())
    Serial.println(mySerial.read());
    
}

the above code i have used to extract the data. now the problem is that as per the command i need to get only 5 bytes. I am getting frequently the same but in rare i am getting 4 and 7 bytes which i am suppose to discard.
I would really appreciate to you if u can provide some idea.

newdata.txt (51.4 KB)

bioom85:
I would really appreciate to you if u can provide some idea.

Read Replies #15 and #8

...R