Sketch for MQ135 air quality control sensor

In this post I'm going to share the Arduino sketch for MQ135 (air quality control sensor). I've tried the sketch and it gets reading pretty well. I've tried in my room, on a gas cooker and in the bin. I've found the highest reading on gas cookers, as you probably already know why. So this sensor can be very useful in many places and with this sketch you will be able to control many things too. Over here I've just used a LED, but obviously you can control other stuff aswell instead of LED, it's just for an example.

The sketch

int led = 5;
void setup() {
// initialize serial communication at 9600 bits per second:
 pinMode(led, OUTPUT);

void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);

if (sensorValue >=150){
    digitalWrite(led, HIGH);
  else {
    digitalWrite(led, LOW);

  • When the reading value goes above 150 it will turn the led on otherwise the LED will be be turned off. You can change value "if (sensorValue >=150){" as you wish.
  • As you can see in the diagram (at the top) that the VCC from the sensor going to 5V of Arduino, and GND of sensor going to GND of Arduino, and Aout of the sensor going to analog pin 0 of Arduino. You can change the pin but you have to change in the sketch "analogRead(A0);" aswell if you change it.
* This sketch takes 10 readings in a second "delay(100);" you can take more or less readings just by increasing or decreasing the number.

  • This sensor draws lot of power so you might have to connect to external power if you need to.
  • To get the actual reading you have to wait a bit, because you will get high reading at the beginning and then when it comes down until it starts showing the readings which doesn't go down any more.

That's all for this post I thing, if you didn't understand anything you can let me know by leaving a comment below. Thanks

No comments:

Post a Comment

Newly posted:
Reason why rich getting richer and poor getting poorer