Problem with UART reading (MH-Z19B sensor)

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
fdufnews
Posts: 76
Joined: Mon Jul 25, 2016 11:31 am

Re: Problem with UART reading (MH-Z19B sensor)

Post by fdufnews » Mon May 24, 2021 7:44 am

Is this garbage only occuring during the first exchange or for every other ones?

If it is only on the first one, it is surely a problem linked to the setup of the sensor. In that case, maybe you can add a few seconds delay before entering the flush loop I suggested. The delay shall be longer than the time it takes for the sensor to make its internal calibration and end its setup.

KUMO
Posts: 7
Joined: Sat May 22, 2021 6:25 pm

Re: Problem with UART reading (MH-Z19B sensor)

Post by KUMO » Mon May 24, 2021 6:54 pm

fdufnews wrote:
Mon May 24, 2021 7:44 am
Is this garbage only occuring during the first exchange or for every other ones?

If it is only on the first one, it is surely a problem linked to the setup of the sensor. In that case, maybe you can add a few seconds delay before entering the flush loop I suggested. The delay shall be longer than the time it takes for the sensor to make its internal calibration and end its setup.
The garbage comes with each reading no matter how long I waited. Really strange.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Problem with UART reading (MH-Z19B sensor)

Post by Roberthh » Mon May 24, 2021 6:57 pm

If I look at your screen copies, the garbage comes at the first read, causing an offset of 3 bytes. That offset continues through the following reads of 9 bytes each and results in bad results. Again: every time before sending out a command to the sensor you have to ensure that nothing is already in the receive buffer.

KUMO
Posts: 7
Joined: Sat May 22, 2021 6:25 pm

Re: Problem with UART reading (MH-Z19B sensor)

Post by KUMO » Tue May 25, 2021 9:04 pm

Roberthh wrote:
Mon May 24, 2021 6:57 pm
If I look at your screen copies, the garbage comes at the first read, causing an offset of 3 bytes. That offset continues through the following reads of 9 bytes each and results in bad results. Again: every time before sending out a command to the sensor you have to ensure that nothing is already in the receive buffer.
Thanks again for your advice, Robert!
I connected the sensor now to Arduino. Quite interesting, there is no such problem, the reading for the first two minutes shows zero, after correct value was displayed.

Post Reply