Choose your country and language
Language
MQ135 is a heated semiconductor sensor that responds to several gases. It is useful for investigating relative changes, but it is not a selective CO₂ sensor. A library returning a number called PPM does not turn an uncalibrated reading into a documented CO₂ concentration.
Check both Winsen’s sensor manual and the circuit diagram for your particular breakout module. The heater, measurement circuit and analogue output have different roles. Warm-up, temperature, humidity, load resistance and calibration affect the result.
A module powered from 5 V may have an analogue output exceeding the permitted ESP32 input voltage. Do not connect such an output directly to GPIO34. Use suitable voltage conditioning and a common ground, and check the voltage with a multimeter before connecting it.
As a calculation example, 20 kΩ from the analogue output to the measurement point and 10 kΩ from that point to GND gives a nominal ratio of 1/3: 5 V becomes approximately 1.67 V. This changes the load on the source. Check that your particular module can drive this load, and calculate using the highest possible output voltage. A voltage divider is not galvanic isolation.
This example applies to a classic ESP32 with GPIO34 as an ADC1 input. It logs the voltage at the protected ADC input in millivolts. It does not calculate gas type, CO₂, PPM or a health assessment.
const int GAS_PIN = 34; // Classic ESP32 ADC1; verify your exact board
void setup() {
Serial.begin(115200);
analogReadResolution(12);
analogSetPinAttenuation(GAS_PIN, ADC_11db);
}
void loop() {
unsigned long adcMillivolts = analogReadMilliVolts(GAS_PIN);
Serial.print("ADC input, mV: ");
Serial.println(adcMillivolts);
delay(1000);
}For actual CO₂ measurement, select a documented CO₂ sensor, for example using NDIR or another specified measurement technique. For safety alarms, use approved equipment intended for the purpose. This hobby experiment is not a fire, gas or carbon-monoxide alarm.
See the sensor range, or use the raw signal as your next exercise in data logging and display on a local webpage.
Language