Back to Tutorial

ESP32 with an MQ-135 gas sensor

ESP32 with an MQ_135

Hardware Setup:

  1. Gather the necessary components:
  2. Connect the MQ-135 sensor to the ESP32:
    • Connect the VCC pin of the sensor to the 3.3V or 5V output of the ESP32.
    • Connect the GND pin of the sensor to the ground pin of the ESP32.
    • Connect the output pin of the sensor (usually labeled “OUT”) to an analog input pin on the ESP32 (e.g., A0).
    • Connect one end of the resistor to the output pin of the sensor and the other end to the ground pin of the ESP32.

Software Setup:

  1. Install the Arduino IDE: Download and install the Arduino IDE from https://support.arduino.cc/hc/en-us/articles/360019833020-Download-and-install-Arduino-IDE.  
  2. Install the ESP32 board support package: Open the Arduino IDE, go to File > Preferences, and add the following URL to the Additional Boards Manager URLs field: https://dl.espressif.com/package/esp32/index.json. Then, go to Tools > Board > Boards Manager and install the “ESP32” board.  

Code:

C++

const int sensorPin = A0;
const int loadResistance = 10000; // Resistance of the load resistor in ohms

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

void loop() {
  int sensorValue = analogRead(sensorPin);
  float voltage = sensorValue * (3.3 / 1023); // Assuming 3.3V ADC reference
  float resistance = loadResistance * (5.0 - voltage) / voltage;
  float gasConcentration = pow(10, ((resistance - 11000.0) / 680.0));

  Serial.println(gasConcentration);

  delay(1000);
}

Explanation:

  • The code defines the analog pin connected to the MQ-135 sensor and the load resistance value.
  • In the setup() function, the serial monitor is initialized to print the sensor readings.
  • In the loop() function, the analog value from the sensor pin is read, converted to voltage, and then used to calculate the resistance and gas concentration.
  • The gas concentration is calculated using a specific formula for the MQ-135 sensor.

Uploading the Code:

  1. Connect the ESP32 to your computer.
  2. Select the correct board and port in the Arduino IDE.
  3. Compile and upload the code to the ESP32.

Testing:

  1. Open the serial monitor in the Arduino IDE.
  2. Expose the MQ-135 sensor to different concentrations of the gas you want to measure. You should see the sensor values change accordingly.

Additional Notes:

  • The MQ-135 sensor requires calibration to obtain accurate gas concentration readings. Refer to the sensor’s datasheet for calibration procedures.
  • The sensor’s sensitivity can vary depending on factors like temperature and humidity. Consider compensating for these factors if necessary.
  • You can customize the code to perform different actions based on the sensor readings (e.g., triggering alarms, controlling ventilation systems).
  • Consider using a library or framework like ThingSpeak or Blynk to visualize and analyze the sensor data.

By following these steps, you can effectively use an ESP32 to measure gas concentrations using an MQ-135 sensor.

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Tutorial