No response from Grove CO2 MH-Z16 sensor

Hello Tech support,

I ordered a Rapsberry Pi4 and GrovePi+ board + a collection of sensors some weeks ago.
Everything works fine except the Grove CO2 MH-Z16 sensor. The GrovePi installation package includes a Python script for reading the CO2 sensor. However, executing the script results in no response from the sensor, but also no errors. Debugging the Python code shows that executing of the script stops at the serial.Serial.write method from the pySerial library.

The code below summarizes my problem. The sensor is connected to the RPISER port of the GrovePI= board.

import serial
import time
ser = serial.Serial(’/dev/ttyAMA0’, 9600, timeout = 1)
values = bytearray([0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79])
w_return=ser.write(values)
print(“w_return”, w_return)

Executing the code will never reach the print function.

The CO2 sensor requires writing these 9 bytes, before reading the C02 concentration.
Because the RPISER port opens without any errors, my conclusion is that the CO2 sensor is ‘dead’.

Is there any way to test if the CO2 sensor is functioning? The CO2 sensor manual only provides information about reading data form the sensor, but not for testing its correct functioning.

Can anyone help me?

Hi @bwbauke:
I would say the UART has to be enabled if you want to use UART in raspberry pi 4 . this is a guide to enable it as below.

sudo sh -c "echo dtoverlay=pi3-miniuart-bt >> /boot/config.txt"
sudo systemctl stop [email protected] 
sudo systemctl disable [email protected] 
sudo sh -c "echo console=tty1 root=PARTUUID=ea7d04d6-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles > /boot/cmdline.txt" 
sudo reboot

Dear Users of the Grove CO2 sensor,

I found the answer to my problem on the following web page:

In short, if you want to use the CO2 sensor Python code that comes with the GrovePi installation with a Raspberry Pi 4, then you need to fix 2 problems:
(1) Change the CO2 sensor command string into a byte-array (see my code a posted before in this thread)
(2) Change the port name that the RPISER port is pointing to, into /dev/ttyS0 (see link above for more detailed information)

Then the sensor works fine -:slight_smile: