magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

MQ135 Gas Sensor - How do you read from it?

Sat Jun 20, 2015 9:46 am

I have just purchased the mq135 gas sensor from ebay (http://www.ebay.com.au/itm/251930629656 ... EBIDX%3AIT) It has 4 pins - VCC, GND, DO and AO (which I assume means power, ground, digital output and analog output). Apparently the sensor can detect various gases such as:

ammonia, aromatic compounds, sulfur, benzene vapor, smoke and other gases(?)

Does anyone know how I would go about connecting this to a breadboard and reading the appropriate gas values using python. The most information I could find was from here:

http://www.raspython.org/digital-sensor ... s-example/

But this doesn't print out the gas concentration levels which is what I want. I assume these values can be read directly from the digital output?

User avatar
DougieLawson
Posts: 35784
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: MQ135 Gas Sensor - How do you read from it?

Sun Jun 21, 2015 7:00 am

You should get what you need from the datasheet at: https://www.futurlec.com/Datasheet/Sensor/MQ-135.pdf
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
mikronauts
Posts: 2716
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: MQ135 Gas Sensor - How do you read from it?

Sun Jun 21, 2015 2:45 pm

From a quick glance at the data sheet, you will only get a "ppm detected" output, with various sensitivity for the gasses.

It will not give you a separate reading per different gas, nor does it have a way to select the gas you are interested in.
magearlik wrote:I have just purchased the mq135 gas sensor from ebay (http://www.ebay.com.au/itm/251930629656 ... EBIDX%3AIT) It has 4 pins - VCC, GND, DO and AO (which I assume means power, ground, digital output and analog output). Apparently the sensor can detect various gases such as:

ammonia, aromatic compounds, sulfur, benzene vapor, smoke and other gases(?)

Does anyone know how I would go about connecting this to a breadboard and reading the appropriate gas values using python. The most information I could find was from here:

http://www.raspython.org/digital-sensor ... s-example/

But this doesn't print out the gas concentration levels which is what I want. I assume these values can be read directly from the digital output?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Re: MQ135 Gas Sensor - How do you read from it?

Mon Jun 22, 2015 4:15 am

Ok. So does anyone know how I would read from it in python to print the overall gas concentration? The digital out uses ttl serial??? (Is this something like this: https://electrosome.com/uart-raspberry-pi-python/ ???) For arduino they have this information (http://playground.arduino.cc/Main/MQGasSensors), but being the total noob I am I have no idea how this translates to the pi.

User avatar
mikronauts
Posts: 2716
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: MQ135 Gas Sensor - How do you read from it?

Mon Jun 22, 2015 2:08 pm

You need an analog to digital converter, and read that from python. I normally use MCP3208 12 bit ADC's.

The data sheet for the sensor has charts showing analog output for detected gas concentration.

There is some sample python code in my article below: (uses MCP3208 & Python on the Pi)

http://www.mikronauts.com/raspberry-pi/ ... and-howto/

This other article covers using the ThingSpeak web IoT service with such an ADC:

http://www.mikronauts.com/raspberry-pi/ ... eak-howto/

And of course, you can google "raspberry pi mcp3208" for many more examples.
magearlik wrote:Ok. So does anyone know how I would read from it in python to print the overall gas concentration? The digital out uses ttl serial??? (Is this something like this: https://electrosome.com/uart-raspberry-pi-python/ ???) For arduino they have this information (http://playground.arduino.cc/Main/MQGasSensors), but being the total noob I am I have no idea how this translates to the pi.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Re: MQ135 Gas Sensor - How do you read from it?

Mon Jun 22, 2015 2:12 pm

Ok cheers for the help. Off to ebay land. Thanks.

User avatar
mikronauts
Posts: 2716
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: MQ135 Gas Sensor - How do you read from it?

Mon Jun 22, 2015 2:30 pm

You are most welcome.
magearlik wrote:Ok cheers for the help. Off to ebay land. Thanks.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

Zorac
Posts: 25
Joined: Wed Nov 04, 2015 4:04 am

Re: MQ135 Gas Sensor - How do you read from it?

Wed Dec 09, 2015 8:19 pm

magearlik wrote:Ok cheers for the help. Off to ebay land. Thanks.
did you have any success? an update on your project would be great as im looking at picking one of these sensors too. the wiring seems straight forward, but i haven't found any samples of python code to read the DO pin.

magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Re: MQ135 Gas Sensor - How do you read from it?

Thu Dec 10, 2015 2:26 am

I did this a while ago but from memory what I did was buy an ADC chip for the raspberry pi - the mcp3008. Then I followed the instructions here which also worked for the gas sensor:

https://learn.adafruit.com/reading-a-an ... spberry-pi
http://www.instructables.com/id/Wiring- ... /?ALLSTEPS
http://www.learningaboutelectronics.com ... rry-pi.php

Alternatively you can buy an Arduino and it will read analog out of the box. Instructions for the Arduino are here:

http://www.instructables.com/id/How-to- ... /?ALLSTEPS

This code works for any of the MQ series of sensors.

I hope this helps.

Zorac
Posts: 25
Joined: Wed Nov 04, 2015 4:04 am

Re: MQ135 Gas Sensor - How do you read from it?

Thu Dec 10, 2015 2:58 am

That helps a lot. what about calibration? I assume it outputs some number that doesn't have much meaning until it's calibrated?

magearlik
Posts: 9
Joined: Sat Jun 20, 2015 9:35 am

Re: MQ135 Gas Sensor - How do you read from it?

Thu Dec 10, 2015 3:11 am

Not to sure about this. But I know you need to leave it on for a while before you take measurements so the element inside it can heat up. According to an article I read here:

http://stackoverflow.com/questions/3110 ... th-arduino


" i've found that, if you're using the sensor for the first time, it's a good practice to leave it powered on for about 24 hours before getting a good read. Don't skip this step.

then comes the calibration process. just calibrate it to get about 100-150 from analogRead in a good air condition.

normal air returns ~100-150
alcohol returns ~700
lighter gas returns ~750+ "

Basically just take note of what the value defaults to in good air quality and use that as a reference point.

Zorac
Posts: 25
Joined: Wed Nov 04, 2015 4:04 am

Re: MQ135 Gas Sensor - How do you read from it?

Thu Dec 10, 2015 4:28 am

thanks. i was coming to a similar conclusion at looking at some sample code. I was hoping to get a ppm readout, but i may still give this a try as there doesn't appear to be any other options.

beno_cd
Posts: 2
Joined: Wed Dec 30, 2015 6:24 pm
Location: Concepción, Chile
Contact: Website

Re: MQ135 Gas Sensor - How do you read from it?

Wed Dec 30, 2015 7:37 pm

Zorac wrote:thanks. i was coming to a similar conclusion at looking at some sample code. I was hoping to get a ppm readout, but i may still give this a try as there doesn't appear to be any other options.
Hi everyone, this is my first post :)

I've been working on a project using a Raspberry Pi B+ with an MQ135 sensor and an MCP3008 ADC. Everything is connected and working but I have no idea about what does the readout from the sensor mean. Normally I get some value between 25 - 50 in clean air.
After doing some research I found a similar project that uses a tp-401 sensor which is also for air quality measurement and they use the following table to interpret their data.

Image

Now my idea is the following: since we know that the MQ135 returns a value between 0 and 1023 why not using cross multiplication to convert the numbers, so 1023 would be equal to 500 and then we can use the AQI table to interpret the sensor readout.
Any comments regarding this are welcome ;)

(sorry my english is a bit rusty)

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: MQ135 Gas Sensor - How do you read from it?

Thu Dec 31, 2015 12:39 am

The only data points that you have are clean air and 100ppm of NH4.

Your first problem is that an ADC will not directly read the resistance. The reading will be proportional to RL/(RL+Rs) You know RL (the load resistor) and you want Rs (the sensor resistance). It is not a simple linear relationship; you need to solve the equation.

RL is adjustable, which means that we need to adjust it when the sensor is reading clean air, so that RL = Rs.

The second problem is that it is tricky to get your hands on a tank of 100ppm NH4. These sorts of things are available, if you are a large business willing to buy a lot, but we are probably not. Clean air is also similarly available but we would have to settle for something less perfect such as out in the countryside.

So the only conclusion we can draw is that the sensor gives one reading in clean air and a different one in dirty air. We certainly cannot assume the ADC will ever reach full-scale since that would mean Rs was much bigger or smaller than RL and the graph only shows it going from the same to around a tenth. We can probably make qualitative judgements by calibrating it for clean air in one environment and finding whether another environment was better or worse.

Return to “Beginners”