MH-Z19B Reading problem


I tried multiple way to connect the MH-Z19B using Mycodo interface. First of all, using the mh-z19 3.1.1 project, I can easily retrieve data from the /dev/ttyS0 over a SSH connection to the pi with sudo python -m mh_z19 --all. That means, the sensor is working.

Now when I configure Mycodo mh-z19b input, there is nothing happening when trying to read the values. I had configured it to use /dev/ttyS0 and I also tried using a UART-USB converter instead of the TX-RX GPIO pins, not much success.

I don’t know what’s happening, but here is the log about the moment when mycodo tries to acquire data.

2022-01-29 10:40:31,407 - INFO - mycodo.controllers.controller_input_bd84bcbf - Activated in 403.7 ms
2022-01-29 10:40:31,407 - DEBUG - mycodo.daemon - Input controller with ID bd84bcbf-2890-45c0-bd69-9d9f0e392c39 activated.
2022-01-29 10:40:32,420 - DEBUG - mycodo.inputs.mh_z19b_bd84bcbf - No response
2022-01-29 10:40:32,456 - DEBUG - mycodo.controllers.controller_input_bd84bcbf - Adding measurements to InfluxDB with ID bd84bcbf-2890-45c0-bd69-9d9f0e392c39: {}
2022-01-29 10:40:37,407 - ERROR - mycodo.controllers.controller_input_670a2d3b - StopIteration raised 3 times. Possibly could not read input. Ensure it’s connected properly and detected.
2022-01-29 10:40:40,998 - ERROR - mycodo.controllers.controller_input_8a9cea18 - Mycodo is attempting to acquire measurement(s) from an Input that has already critically errored. Review the log lines following Input Activation to investigate why this happened.

Also, I did use a 5k resistor between my VCC and Tx, I really don’t know what to do from know since I can get reading through over ssh.

Thanks for you help.

Are you sure you configured the Input correctly?

i have (hopefully) the same problem with the mh-z19b. or at least one aswell.

mycodo gives this error:

2022-05-13 14:44:23,172 - ERROR - mycodo.inputs.mh_z19b_a7e10268 - Error initializing, trying again in 5 seconds: Write timeout
Traceback (most recent call last):
  File "/var/mycodo-root/mycodo/", line 54, in try_initialize
  File "/home/pi/Mycodo/mycodo/inputs/", line 153, in initialize
  File "/home/pi/Mycodo/mycodo/inputs/", line 201, in abcoff
    self.ser.write(bytearray([0xff, 0x01, 0x79, 0x00, 0x00, 0x00, 0x00, 0x00, 0x86]))
  File "/var/mycodo-root/env/lib/python3.9/site-packages/serial/", line 636, in write
    raise SerialTimeoutException('Write timeout')
serial.serialutil.SerialTimeoutException: Write timeout

i also tried using this one here directly, which worked out of the box

git clone
sudo python3 -m mh_z19
~/mh-z19 $ sudo python3 -m mh_z19
{"co2": 537}

thank you

Providing just an error is not enough information to replicate your environment or diagnose the issue. You need to provide details about the physical connections as well as the software settings used to be able to investigate the issue.

Also, are you sure you have a MH-Z19B and not a MH-Z19? It looks like it’s failing on ABC, which is only present in the B version.

the label says MH-Z19B.
just tried the MH-Z19 (without B) input, but still the same

2022-05-13 17:23:21,402 - ERROR - mycodo.inputs.mh_z19_3a5b0e1b - Error initializing, trying again in 5 seconds: Write timeout
Traceback (most recent call last):
File “/var/mycodo-root/mycodo/”, line 54, in try_initialize
File “/home/pi/Mycodo/mycodo/inputs/”, line 141, in initialize
File “/home/pi/Mycodo/mycodo/inputs/”, line 194, in set_measure_range
self.ser.write(bytearray([0xff, 0x01, 0x99, 0x00, 0x00, 0x00, 0x13, 0x88, 0xcb]))
File “/var/mycodo-root/env/lib/python3.9/site-packages/serial/”, line 636, in write
raise SerialTimeoutException(‘Write timeout’)
serial.serialutil.SerialTimeoutException: Write timeout

i guess the problem is not the physical connection, as i get the correct values outside of mycodo ?

what software settings exactly?

thank you

Physical connections of the hardware, any and all settings you configured in software… all of these are needed to diagnose an issue. None should be assumed.

i have it exactly like this
direct connection, no breadboard or anything

VIN → 5V

mh-z19b settings are all to default. /dev/ttyAMA0 - baudrate 9600

what settings are you mean? i have nothing configured yet. only turned everything to “on” in mycodo-rpi-settings

I2C Enabled: True
1-Wire Enabled: True
Serial Enabled: True
SPI Enabled: True
SSH Enabled: True
Pi Camera Enabled: False

The Mycodo Input is configurable as well.

yes. all to default.

Is /dev/ttyAMA0 the location of your serial device connected to the sensor?

Have you searched similar issues on github? Here are some:

Is /dev/ttyAMA0 the location of your serial device connected to the sensor?

i guess so. isn’t AMA0 the default location? because again, it was working via CLI - all set to default. only one UART device connected.
how would i found out if AMA0 was correct?

anyway; i now tried a cp210x usb converter with /dev/ttyUSB0 and was working directly :confused:

1 Like