In this tutorial, we will learn how to interface MQ-135 Gas Sensor with Arduino Board. We will explore what is a gas sensor, how it works, its pin configuration and how to interface it with Arduino.
What is MQ-135 gas sensor?
MQ-135 sensor belongs to the MQ series that are used to detect different gasses present in the air. The MQ-135 sensor is used to detect gases such as NH3,NOx, alcohol, Benzene, smoke,CO2 ,etc. steel exoskeleton houses a sensing device within the gas sensor module.
how does it work?
Through connecting leads, the sensing element is exposed to current. The gases that come close to the sensing element are ionized and absorbed by the sensing element as a result of this current, which is known as heating current. This affects the resistance of the sensing element, hence changing the value of the current leaving it.
When no gas is present, digital output is 1 and analog output gives 1023 max value. When gas is present, digital output is 0 and analog output is much less than 1023.
Using potentiometer on chip we can control the turning OFF point of digital pin at some value of analog pin. The sensor needs a load-resistor at the output to ground. Its value could be from 2kOhm to 47kOhm. The lower the value, the less sensitive is the sensor. The higher the value, the less accurate is sensor for higher concentrations of gas. If only one specific gas is measured, the load-resistor can be calibrated by applying a known concentration of that gas. If the sensor is used to measure any gas (like in a air quality detector) the load-resistor could be set for a value of about 1V output with clean air. Choosing a good value for the load-resistor is only valid after the burn-in time
MQ-135 Gas Sensor Module Overview
MQ-135 gas sensor module features both analog output fetched from its AO pin and digital output fetched from its DO pin.
The analog output voltage lies between 0-5V where the output voltage increases relatively with the concentration of gas vapors coming in contact with the sensor. Under standard conditions, this output voltage from the sensor is directly proportional to the concentration of CO2 gas in PPM. This output voltage is converted to a digital value (0-1023) via the analog to digital converter in Arduino. This value is equal to the gas concentration in PPM.
Whereas the digital output voltage (0/1) is obtained after passing the analog output from LM393 comparator situated at the backside of the sensor module. The in built potentiometer is manually calibrated to change the sensitivity of the digital output and set a threshold value. This is done with the help of the DOUT LED. When the concentration of gas vapors will be greater than the threshold value set, the digital output will be LOW. This will be easily monitored when the DOUT LED lights up. Additionally, rotating the potentiometer clockwise results in a higher sensitivity.
The MQ-135 gas sensor module’s front side consists of the sensor enclosed in a steel exoskeleton. This is to protect the sensor as it gets heated up over time and will eventually come in contact with the gas vapors.
The backside of the module consists of two LEDs: DOUT LED (lights when DO is LOW) and the power LED (lights when the module is powered on), an LM393 comparator, and a potentiometer.
Specifications
The table below shows some key specifications of the MQ-135 sensor module:
Feature | Description |
---|---|
Operating Voltage | 2.5-5.0V |
Detecting Concentration | 10ppm-300ppm for NH3 10ppm-1000ppm for Benzene 10ppm-300ppm for Alcohol |
Load Resistance | Adjustable |
Heater Resistance | 33Ω ± 5% |
Heater Consumption | less than 800mW |
Operating Temperature | -10 to 45°C |
Pinout
The MQ-135 sensor module consists of four pins namely VCC, GND, DO, and DO. The table below gives a brief description of them.
Pin | Description |
---|---|
VCC | Positive power supply pin that powers up the sensor module. |
GND | Reference potential pin. |
AO | Analog output pin. It generates a signal proportional to the concentration of gas vapors coming in contact with the sensor. |
DO | Digital Output pin. It also produces a digital signal whose limit can be set using the in-built potentiometer. |
Interfacing MQ-135 Gas Sensor with Arduino
The table below shows the connections you need to make between the MQ3 sensor module and Arduino using both the analog output and the digital output pins of the sensor.
MQ-135 Module | Arduino |
---|---|
VCC | 5V |
GND | GND |
AO | A0 |
DO | Pin 2 |
Connect MQ-135 sensor’s VCC pin with 5V terminal of Arduino UNO. This will power up the sensor.
Additionally, we will connect the analog pin AO with A0 and DO with Pin 2 of Arduino UNO. Both the devices will be commonly grounded.
Follow the connection diagram below, to connect your devices accordingly.
MQ-135 Gas Detection Arduino Sketch
Open your Arduino IDE and go to File > New. Copy the code below in that file.
This sketch will read both the analog and digital outputs of the sensor. If the analog output is greater than 400 then an LED connected at Arduino pin 2 will turn ON. Otherwise, turn the LED OFF and print both the analog and digital output readings on the serial monitor.
int sensorValue;
int digitalValue;
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
pinMode(13, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
sensorValue = analogRead(0); // read analog input pin 0
digitalValue = digitalRead(2);
if (sensorValue > 400)
{
digitalWrite(13, HIGH);
}
else
digitalWrite(13, LOW);
Serial.println(sensorValue, DEC); // prints the value read
Serial.println(digitalValue, DEC);
delay(1000); // wait 100ms for next reading
}
How the Code Works?
Create two int variables to hold the analog and digital output readings.
int sensorValue;
int digitalValue;
Inside the setup() function, we will open the serial communication at a baud rate of 9600. Then configure pin2 connected with the DO pin of the sensor as an input and pin13 connected with the LED’s anode pin as an output.
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
pinMode(13, OUTPUT);
pinMode(2, INPUT);
}
In the infinite loop(), we will use analogRead() on the A0 pin and save the value in ‘sensorValue.’ Likewise, we will read the digital output on pin2 using digitalRead() and save the value in ‘digitalValue.’ Next, using an if-else statement we will check if the analog reading is greater than 400 or not. If it is then turn the LED ON. Otherwise, leave the LED OFF and display the current analog and digital readings in the serial monitor.
void loop()
{
sensorValue = analogRead(0); // read analog input pin 0
digitalValue = digitalRead(2);
if (sensorValue > 400)
{
digitalWrite(13, HIGH);
}
else
digitalWrite(13, LOW);
Serial.println(sensorValue, DEC); // prints the value read
Serial.println(digitalValue, DEC);
delay(1000); // wait 100ms for next reading
}
Demonstration
To see the demonstration of the above code, upload the code to Arduino. But, before uploading code, make sure to select the Arduino board from Tools > Board and also select the correct COM port to which the Arduino board is connected from Tools > Port.
On the serial monitor, you can see the values of the analog pin being detected. Currently, in my case, they are around about 150 which indicates normal air.
- Normal air returns approximately 100-150
- Alcohol returns approximately 700
- Lighter gas returns approximately 750
Watch the video demonstration below:
You may also like to read:
How to calibrate MQ 135 Gas Sensor?
Is it necessary to calibrate the gas sensor?
Yes,there is need to do calibration on the MQ 135
What is dec
DEC is to print the number in decimal
How is the data sent by sensor over the lines, using UART protocol or voltage level ?
Please clarify.
It’s an analog value (resistance, charges depending on the gas concentration)
Voltage level
how u know this statement ” Normal air returns approximately 100-150
Alcohol returns approximately 700
Lighter gas returns approximately 750 ” ? any source or else ?
Hi Richi,
You’ll need to calculate the PPM (parts per million). There’s a graph of this sensor from the manufacturer which shows logarithmic decay of each substance.
There are some videos online that derive this formula. Just search for calculate CO2 ppm MQ135
It will convert the arbitrary adc number to a PPM for one of the gasses it can measure.
can I used this sensor just for detecting Co2?
No you can sense a few different gasses (look up the specs)
can I used this sensor just for detecting Co2? please gv me the answer by email
Hi, why is my reading so low ~ 20+ only??
in my case, as i was using a microcontroller without 5v output, i had the sensor connected to A0 and to an external battery…. and was not connecting the ground to the board… once i did that numbers went up… (since i had to modify the connections it got messed up all became loose, and since i probably won’t be using the sensor for anything… i put it away in its bag…)
i must add that i thought it would read 2 different values, one for CO2 and another set for CHx (hydrocarbides? hidrocarburs in my language)
Your schematic does not show the load-resister. Is this an omission or is the resistor not necessary? Thanks for the tutorial!
Can i use this sensor with 3.3V ?
Hi,
Thanks for code and descriptions. Can you tell me what is the output value will come for detection of amonia gas?
If we are using this sensor to detect many gases,then how we will know their percentage level seperately
How do I enter ammonia values
Sir
But you are not described about the digital pin
Plz tell me about how to use digital pin
digital pin only provide high signal and low signal on detection of gas and you can simply use digitalpin mode function to read this high and low signal. it is very easy
how could i differentiate multiple gas level from the same input A0?
I interfaced aurdino with mq135 sensor, I got the output displaying in serial monitor.I Want to push these values to p10 led board using Arduino throw hub12 port How is it possible…?
can I used this sensor for detecting O2?
Is ppm the units of result?