MH-Z19B CO2 sensor with ESP32

Supply 5 V Bus GPIO Measures co2
VINGNDTXRXPWMHDVO

Pins and ESP32 wiring

PinRoleESP32 pinNotes
VINPowerVINNeeds 4.5 to 5.5 V: use VIN
GNDGroundGND
TXOutput (the part drives it)D25UART: the part sends data here. Connect to an ESP32 RX pin (9600 baud, 3.3 V level)
RXInput (the ESP32 drives it)D26UART: the part receives data here. Connect to an ESP32 TX pin
PWMOutput (the part drives it)D27PWM output, optional
HDNot wired to the ESP32—Zero calibration: pulling LOW for 7 s calibrates to 400 ppm. Leave unconnected
VOAnalog outputD34Analog output, optional (not on all versions)

Suggested wiring for the 30-pin ESP32 DevKit, avoiding flash, input-only and strapping pins. BoardPilot checks it for you as you wire.

This module is sold for 5 V. Its outputs may be 5 V: use a level shifter or a voltage divider on ESP32 inputs.

Starter code (Arduino)

void setup() { Serial.begin(115200); }

void loop() {
  int mv = analogReadMilliVolts(34);  // VO on GPIO 34
  Serial.println(mv);
  delay(200);
}

Sources

Try it in 3D in BoardPilot

BoardPilot Part data (JSON)

Related parts

Part data: CC BY 4.0, free to reuse with credit to BoardPilot.