UART communication code is not working

i am interfacing the sensor with stm32 nucleo 152re board using uart communication but code is error free and not working .

if (response[1] != 0x86)
printf(" Invalid response from co2 sensor! \n\r");
return -1;

if (response[8] == checksum(response)) {
int responseHigh = (int) response[2];
int responseLow = (int) response[3];
int ppm = (256 * responseHigh) + responseLow;
return ppm;
// printf(“CO2= %d.\n\r”, ppm);
} else
printf(“CRC error!\n\r”);
return -1;

int main() {
uart4.baud(115200); // Setting baud rate to 115200
int i = 1;
printf(“Hello World !”);

while(1) {
unsigned long currentMillis = now;
if (co2ppm > 10000) {
printf(“Zero was calibrated \n\r”);
else if ((currentMillis - previousMillis) > INTERVAL) {
previousMillis = currentMillis;
co2ppm = readCO2();
printf(“CO2= %d.\n\r”, co2ppm);
uart4.printf(“UART1: %d.\n\r”, co2ppm);

myled = !myled;

If using MBED OS 6, look at BufferedSerial documentation.

There is a code example at the bottom of the page for “Printing to the console”, which does work on that board.


it is not necessary to split your code into 2-3 posts, you can edit already existing post.
Be so kind and use the following procedure to format the code

//your code here


Also you must provide more information about your project and its issue.
“The sensor” tell us nothing and information about a version of MbedOs (probably not 6+) or tool what you use, is missing.

Anyway, I recommend firstly try the communication of your uart4 with a TTL convertor (or something similar) and a serial terminal app, without the sensor. By this way you can check if your uart4 works and and its output is in correct format.
Then you can connect sensor and try it again.

BR, Jan