MH-Z19B CO2 sensor with ESP32
Supply 5 V
Bus GPIO
Measures co2
Pins and ESP32 wiring
| Pin | Role | ESP32 pin | Notes |
|---|---|---|---|
| VIN | Power | VIN | Needs 4.5 to 5.5 V: use VIN |
| GND | Ground | GND | |
| TX | Output (the part drives it) | D25 | UART: the part sends data here. Connect to an ESP32 RX pin (9600 baud, 3.3 V level) |
| RX | Input (the ESP32 drives it) | D26 | UART: the part receives data here. Connect to an ESP32 TX pin |
| PWM | Output (the part drives it) | D27 | PWM output, optional |
| HD | Not wired to the ESP32 | — | Zero calibration: pulling LOW for 7 s calibrates to 400 ppm. Leave unconnected |
| VO | Analog output | D34 | Analog 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
- Winsen MH-Z19B user manual, Pins Vin, GND, Tx, Rx, PWM, HD, Vo; UART 9600 baud, 3.3 V logic
Try it in 3D in BoardPilot
Related parts
A02YYUW waterproof ultrasonic distance (UART)A3144 hall effect switchAM312 mini PIR motion sensorATGM336H GPS/BeiDou moduleBeitian BN-220 GPS moduleBenewake TF-Luna LiDAR (UART mode)
Part data: CC BY 4.0, free to reuse with credit to BoardPilot.