MQ-135 Gas Sensor Module - EN
MQ-135 Gas Sensor Module - EN
Thank you for purchasing our AZ-Delivery MQ-135 Gas Sensor Module. On
the following pages, you will be introduced to how to use and set up this
handy device.
Have fun!
Table of Contents
Introduction 2
2
Introduction
The MQ-135 gas sensor module is a device that is used for sensing and
measuring the concentration of gases in the air. It can detect such gases as:
ammonia, sulfide, LPG, propane, methane, hydrogen, alcohol, smoke and
carbon monoxide and other harmful gasses. Though it can detect those
gases, it is not able to distinguish the difference between them.
The sensor is enclosed within two layers of fine stainless steel mesh called
Anti-explosion network. As a result of that, it is able to detect flammable
gases without incidents. Likewise, it provides protection for the sensor, and
it filters out suspended particles. That way, only gases are able to pass
inside the sensing chamber.
The module has an on-board LM393 comparator chip which converts the
readings into digital and analog signals. There is also a potentiometer which
is used to calibrate detection sensitivity.
3
Specifications
Operating voltage: 5V
Operating current: 150mA
Power consumption: 900mW
Load resistance: 20kΩ
Heater resistance: 31Ω+5%
Sensing resistance 2kΩ - 20kΩ
Preheat time: 24h
Concentration scope: 200 – 10000ppm (parts per million)
Output: analog, digital
Dimensions: 33x21x22mm (1.3x0.8x0.9in)
For the best detecting results, gas sensor has to be preheated. The best
preheat time for the sensor is above 48 hours. For the detailed information
about the sensor specifications, refer to the datasheet.
4
The pinout
The gas sensor module has four pins. The pinout is shown on the following
image:
NOTE: The Raspberry Pi does not have a digital-analog converter and can
not be used to read analog voltages.
5
How to set-up Arduino IDE
If the Arduino IDE is not installed, follow the link and download the
installation file for the operating system of choice.
For Windows users, double click on the downloaded .exe file and follow the
instructions in the installation window.
6
For Linux users, download a file with the extension .tar.xz, which has to be
extracted. When it is extracted, go to the extracted directory and open the
terminal in that directory. Two .sh scripts have to be executed, the first
called arduino-linux-setup.sh and the second called install.sh.
To run the first script in the terminal, open the terminal in the extracted
directory and run the following command:
sh arduino-linux-setup.sh user_name
user_name - is the name of a superuser in the Linux operating system. A
password for the superuser has to be entered when the command is
started. Wait for a few minutes for the script to complete everything.
The second script, called install.sh, has to be used after the installation of
the first script. Run the following command in the terminal (extracted
directory): sh install.sh
After the installation of these scripts, go to the All Apps, where the Arduino
IDE is installed.
7
Almost all operating systems come with a text editor preinstalled (for
example, Windows comes with Notepad, Linux Ubuntu comes with Gedit,
Linux Raspbian comes with Leafpad, etc.). All of these text editors are
perfectly fine for the purpose of the eBook.
Next thing is to check if your PC can detect an Arduino board. Open freshly
installed Arduino IDE, and go to:
Tools > Board > {your board name here}
{your board name here} should be the Arduino/Genuino Uno, as it can be
seen on the following image:
The port to which the Arduino board is connected has to be selected. Go to:
Tools > Port > {port name goes here}
and when the Arduino board is connected to the USB port, the port name
can be seen in the drop-down menu on the previous image.
8
If the Arduino IDE is used on Windows, port names are as
follows:
For Linux users, for example port name is /dev/ttyUSBx, where x represents
integer number between 0 and 9.
9
How to set-up the Raspberry Pi and Python
For the Raspberry Pi, first the operating system has to be installed, then
everything has to be set-up so that it can be used in the Headless mode.
The Headless mode enables remote connection to the Raspberry Pi,
without the need for a PC screen Monitor, mouse or keyboard. The only
things that are used in this mode are the Raspberry Pi itself, power
supply and internet connection. All of this is explained minutely in the
free eBook:
Raspberry Pi Quick Startup Guide
10
Connecting the module with the microcontroller
D0 D2 Blue wire
A0 A0 Green
wire
11
Sketch example
#define DIGITAL_PIN 2
#define ANALOG_PIN 0
uint16_t gasVal;
boolean isgas = false;
String gas;
void setup() {
Serial.begin(9600);
Serial.println("The sensor is warming up...");
delay(30000);
pinMode(DIGITAL_PIN, INPUT);
}
void loop() {
gasVal = analogRead(ANALOG_PIN);
isgas = digitalRead(DIGITAL_PIN);
if (isgas) {
gas = "No";
}
else {
gas = "Yes";
}
gasVal = map(gasVal, 0, 1023, 0, 100);
Serial.print("Gas detected: ");
Serial.println(gas);
Serial.print("Gas percentage: ");
Serial.print(gasVal);
Serial.print("%\n");
delay(2000);
}
12
Upload the sketch to the microcontroller and open Serial Monitor (Tools >
Serial Monitor). The result should look like as on the following image:
13
The sketch starts with defining and creating two macros called
DIGITAL_PIN, ANALOG_PIN.
The module data can be read in two ways. One is by reading the analog
output pin of the module, and the other is by reading the digital output pin of
the module. To read the analog output pin of the module, the variable called
gasVal is used to store return value from the analogRead() function. The
return value is an integer number in the range from 0 to 1023. To convert it
into a percentage, the map() function is used. This is a built-in function of
the Arduino IDE. It has five arguments and returns an integer value.
14
For example:
gasVal = map(input, in_min, in_max, out_min, out_max)
First argument is the input value, which is in the range from the in_min to
in_max. The return value is an integer number in the range from out_min to
out_max. This function maps one number in the input range, to other
number which is in the different range.
To read the digital output pin of the module, the isGas variable is used to
store the return value of the digitalRead() function.
At the end of the loop() function, the data is displayed in the Serial Monitor.
Between two measurements there is 2 seconds pause: delay(2000);
15
Raspberry Pi and the ADC
The Raspberry Pi cannot read analog inputs, and because of that, we will
need to use an analog-to-digital converter (ADC). An ADC is a device that,
as its name implies, converts analog voltages into digital values. We will be
using a device called “ADS1115", which uses I2C bus to send data to
microcontroller. So first we will have to enable I2C on the Raspberry Pi.
16
Connecting the MQ-135 with the Raspberry Pi
17
18
Enabling the I2C interface
To enable the I2C interface on Raspberry Pi, in your Raspbian, go to: Start
> Preferences > Raspberry Pi Configuration.
This will open up a new window, go to its second tab, “Interfaces”, and
enable the I2C radio button. Once you've done so, press the "ok" button like
shown on the image below:
19
Doing so enables the I2C interface on the "GPIO2" and "GPIO3" pins. Next,
we have to install the library for the ADC device. The library is called
“Adafruit_Python_ADS1x15”. To do so, open the terminal app in your
Raspbian and run these commands one by one:
20
Python code
Create a new file called "mq135.py", and insert the following python code:
import time
import Adafruit_ADS1x15
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
digitalPin = 4
GPIO.setup(digitalPin, GPIO.IN)
adc = Adafruit_ADS1x15.ADS1115()
GAIN = 1
print("[press ctrl+c to end the script]")
try: # Main program loop
while True:
analogReading = adc.read_adc(0, gain=GAIN)
digitalReading = GPIO.input(digitalPin)
print("Analog read: {:>6}\t- Digital read: {}"
.format(analogReading, digitalReading))
time.sleep(0.5)
21
At the begining of the script, we import the appropriate libraries. Next, we
set up our variables and turn off the warnings. The loop is set up to do both
analog and digital readings and then output them. The analog values are
shown as six-digit numbers. Note that in the case of zero being the first
digit, zeros do not get displayed. And finally, we configure the keyboard
interrupt so that we can stop the script at any point by pressing "CTRL + C".
After the previous steps are done, run the script with the following
command:
python3 mq135.py
22
As you can see on the image, we read the sensor state every half a
second. The sensor is detecting natural gases found in the air, so the
default analog values are around 12000. But when exposed to butane
gas from a lighter, the digital readings turn to mostly zeros and the
analog values spike from the 12000 up to the 25000. When digital values
are mostly zeros, this means that a gas concentration is high, while the
analog values indicate the gas concentration value.
23
Now it is time to learn and make the Projects on your own. You can do that
with the help of many example scripts and other tutorials, which you can
find on the internet.
https://az-delivery.de
Have Fun!
Impressum
https://az-delivery.de/pages/about-us
24