Mq135 with esp8266, ppm

Hello
Everyone, so i have a problem of data that i got from mq135 sensor with ESP8266. I use analogRead(MQPIN) for the code (MQPIN = A0) this is the code

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

//LCD
LiquidCrystal_I2C lcd (0x27, 16, 2);

// Pin sensor
#define DHTPIN 2       
#define DHTTYPE DHT11
#define MQPIN A0

//WIFI
// WiFi credentials
const char* ssid = "...";
const char* password = "...";

// MQTT HiveMQ Cloud
const char* mqtt_server = "..";
const int mqtt_port = ..;
const char* mqtt_user = "...";
const char* mqtt_pass = "....";

// MQTT dan DHT
WiFiClientSecure espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

// Koneksi WiFi
void setup_wifi() {
  Serial.print("Connecting to WiFi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  int retry = 0;
  while (WiFi.status() != WL_CONNECTED && retry < 20) {
    delay(500);
    Serial.print(".");
    retry++;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi connected");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\n Failed to connect to WiFi");
  }
}

// Reconnect MQTT
void reconnect_mqtt() {
  if (!client.connected()) {
    Serial.print("Connecting to MQTT...");
    if (client.connect("ESP8266Client", mqtt_user, mqtt_pass)) {
      Serial.println("MQTT Connected!");
    } else {
      Serial.print("Failed to connect, rc=");
      Serial.println(client.state());
    }
  }
}

void setup() {
  Serial.begin(115200);
  dht.begin();
  pinMode(MQPIN, INPUT); 
  setup_wifi();

  espClient.setInsecure(); 
  client.setServer(mqtt_server, mqtt_port);

//LCD
lcd.begin();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("ESP8266 Starting...");
delay(5000);

}
void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    setup_wifi();
  }

  if (!client.connected()) {
    reconnect_mqtt();
  }

  client.loop(); // panggil dulu untuk setup awal

  float suhu = dht.readTemperature();
  float kelembapan = dht.readHumidity();
  int gas = analogRead(MQPIN);

  if (isnan(suhu) || isnan(kelembapan)) {
    Serial.println("Failed to read DHT11");
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Read Error!");
    return;
  }

  String payload = "{\"suhu\":" + String(suhu) + 
                   ",\"kelembapan\":" + String(kelembapan) + 
                   ",\"gas\":" + String(gas) + "}";

  Serial.print("Sending: ");
  Serial.println(payload);
  client.publish("esp/data", payload.c_str());

  // LCD TAMPILAN
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("S:");
  lcd.print(suhu, 1);
  lcd.print((char)223);
  lcd.print("C");

  lcd.setCursor(8, 0);
  lcd.print("| G:");
  lcd.print(gas);

  lcd.setCursor(0, 1);
  lcd.print("K:");
  lcd.print(kelembapan, 0);
  lcd.print("%");

  lcd.setCursor(8, 1);
  lcd.print("|");

  // Tunggu 15 detik, tapi loop tetap aktif
  unsigned long start = millis();
  while (millis() - start < 15000) {
    client.loop();
    delay(10);
  }
}

So, I've been collecting gas data for 3 days, and I'll use this data to train machine learning, but I'm confused, so here are my questions.

  1. Is this already in PPM? Some say it is, while others say it's not yet in PPM because it's still analog read.
  2. If it's not yet in PPM, can I calculate it in data preprocessing? I don't have time to re-collect the data due to the approaching deadline.

I need the answer, pleaseee

The value you're reading from analogRead(MQPIN) is a raw ADC value, which ranges from 0 to 1023 on the ESP8266 (10-bit ADC). This is simply a representation of the voltage output from the MQ135 sensor.

This raw value corresponds to the voltage across the load resistor (RL) in the MQ135's internal voltage divider.
To get PPM (parts per million) values, you need to convert the analog value to a voltage, then to sensor resistance (Rs), and finally use the sensor’s sensitivity curve (from the MQ135 datasheet) to convert to PPM.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.