#define UART1_TX_PIN 5
#define UART1_RX_PIN 6
#define DATA_BUFF_LEN 9
byte query_co2[9]={0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; // Query Data
// {0xff, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78}; // Zero Calibration Calibration can only be performed after stabilizing for 5 minutes in a 400 ppm environment.
// {0xff, 0x01, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78}; // Span Calibration Select 2000 ppm environment (span calibration needs to be performed first)
int concentration_co2 = 0;
void setup()
{
Serial.begin(115200);
Serial1.begin(9600, SERIAL_8N1, UART1_RX_PIN, UART1_TX_PIN); // UART1
}
void loop()
{
byte receivedData[DATA_BUFF_LEN] = {0};
Serial1.write(query_co2, DATA_BUFF_LEN);
delay(10);
if (Serial1.available())
{
// Read the number of available bytes
int numBytes = Serial1.available();
// Read bytes one by one and store them into a byte array
Serial1.read(receivedData, numBytes);
concentration_co2 = receivedData[2]*256 + receivedData[3];
}
Serial.print(concentration_co2);
Serial.println(" ppm");
delay(1000);
}