Image of eBits Academy airquality illustration

MQ135 and ESP32: understand the raw signal before calculating PPM

  • June 23, 2023
  • |
  • Jonas Rothausen

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.

Start with the datasheet

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.

Protect the ESP32 input

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.

Look at the raw signal first

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);
}

Make a repeatable test

  1. Let the sensor stabilise according to the manufacturer’s instructions. A short startup delay is not a full calibration.
  2. Record the supply, warm-up time, temperature and humidity alongside the raw reading.
  3. Look for changes under ordinary, safe conditions. Do not expose yourself or others to harmful gases to create a test signal.
  4. Compare with appropriate reference equipment if you need a quantitative measurement. A single RZERO value in a library does not replace a documented calibration.

Choose the sensor for the question

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.

Leave a comment

Please note, comments need to be approved before they are published.