Detect smoke and air quality with the MQ-135 Sensor and Arduino.

You may create a high-efficiency alarm system that will notify when harmful gases are discharged into the air or when the air is polluted with them by using the MQ-135 Sensor. Let's take a closer look at how to use and program the MQ-135 Sensor with the Arduino.


Overview


A sound sensor can be used for that like the one in the picture below. This sensor works by simple firing a signal at the D0 pin once the sound level is above a certain level. This level can be tuned by rotating the golden screw left and right until it's suitable for your surrounding.

Getting the items


For this project you will need the following components: 

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Air Quality Sensor (MQ-135)
19 QAR
Sale Off
Voltaat Active Buzzer - 5V
2 QAR

Make sure you also have a breadboard and jumper wires to connect everything together

Sale Off
Voltaat Full-size Breadboard
20 QAR
Sale Off
Voltaat Jumper Wires - Male to Male (40 Pack)
10 QAR

Wiring it up


Follow the instructions below as well as the image that shows how to connect the MQ-135 sensor to the Arduino to get the right data.


Connections from the Arduino to the breadboard:

• Arduino pin GND → Breadboard ground line

• Arduino pin 5V → Breadboard 5V line


Connections from the MQ135 sensor:

• VCC pin→ Breadboard 5V line

• GND pin→ Breadboard ground line

• A0 pin→ Arduino pin A0


Connections from the buzzer:

• Negative pin→ Breadboard ground line

• Positive pin→ Arduino pin 11


Coding


The function of this sketch is to obtain a reading from the MQ135 sensor output via the Arduino's analog input, which expresses the purity of the air and the concentration of various harmful gases in it, then compare it to the clean air value specified in the code, which is 200, and then perform a logical comparison operation if the sensor reading exceeds 200, the alarm is activated, otherwise it turns off.


You can change this value slightly based on the quality of the air around you so that the code works well for you, if the alarm works when the circuit is initially connected, then you have to increase the value.


This code is basic and straightforward, relying just on reading the analog value from the sensor, thus no libraries are required.


  1. /*
  2.   Voltaat learn (https://www.voltaat.com)
  3.   Link for full tutorial: https://www.voltaat.com/blogs/tutorials/detect-smoke-and-air-quality-with-the-mq-135-sensor-and-arduino
  4.   Link for libraries: There are no libraries
  5.  
  6.   The function of this sketch is to Detect smoke and air quality using MQ-135 Sensor and Arduino.
  7.  
  8.   Connections from the Arduino to the breadboard:
  9.   • Arduino pin GND → Breadboard ground line
  10.   • Arduino pin 5V → Breadboard 5V line
  11.  
  12.   Connections from the MQ135 sensor:
  13.   • VCC pin→ Breadboard 5V line
  14.   • GND pin→ Breadboard ground line
  15.   • A0 pin→ Arduino pin A0
  16.  
  17.   Connections from the buzzer:
  18.   • Negative pin→ Breadboard ground line
  19.   • Positive pin→ Arduino pin 11
  20.  
  21. */
  22.  
  23.  
  24. // define MQ135 variable with A0 value that represent the input analog pin in the arduino
  25. int MQ135 = A0;
  26.  
  27. // define buzzer variable with 11 value that repesent the output digital pin in the arduino
  28. int buzzer =11;
  29.  
  30. // define standardAir variable with 200 value
  31. int standardAir =200;
  32.  
  33.  
  34. // Commands inside void setup run once
  35. void setup()
  36. {
  37. // MQ135 is defined as an input
  38. pinMode(MQ135, INPUT);
  39.  
  40. // buzzer is defined as an output
  41. pinMode(buzzer, OUTPUT);
  42.  
  43. // Start the serial monitor at 9600 baud rate (9600 bits per second)
  44. Serial.begin(9600);
  45.  
  46. }
  47.  
  48. // Commands inside void loop run forever
  49. void loop()
  50. {
  51. // Read the value of analog input from MQ135 and assign it in the variable sensorRead
  52. int sensorRead = analogRead(MQ135);
  53.  
  54. // print "Sensor Reading: " to the serial monitor at the same line
  55. Serial.print("Sensor Reading: ");
  56.  
  57. // print The sensorRead value to the serial monitor at the same line
  58. Serial.print(sensorRead);
  59.  
  60. // compare the sensor reading value with the standard fresh air value that we set in the code
  61. if(sensorRead > standardAir)
  62. {
  63. // Generates a square wave of the specified frequency (1000) on a pin (buzzer)for a duration of 200 Millisecond to make a tone
  64. tone(buzzer,1000,200);
  65.  
  66. // print " ,Gas is detected" to the serial monitor at new line
  67. Serial.println(" ,Gas is detected");
  68. }
  69. else
  70. {
  71. // stop generating the tone
  72. noTone(buzzer);
  73.  
  74. // print " ,No gas detected" to the serial monitor at new line
  75. Serial.println(" ,No gas detected");
  76. }
  77.  
  78.  
  79. }

Testing it out


When you test the sensor with a lighter, the buzzer will ring because the sensor reacts to both the gases released during the ignition process and the gas created by the lighter without ignition. It lasts for 200 milliseconds after the gas level in fresh air returns to normal.


You may now access the serial monitor on your Arduino IDE by clicking on it.


The serial monitor is an excellent tool for communicating between your computer and Arduino by sending and receiving various instructions and information over the USB cable.


As shown in the image below, the serial monitor now displays the sensor reading value and gas detection status, and the value is updated and printed every second due to the delay we added in our code. You should also ensure that you have selected the correct baud rate (9600) as specified in the code.


Resources 


Arduino Code

Fritzing Wiring file

Related Tutorials


How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

How to become very good 

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et 

Liquid error: Could not find asset snippets/zend_pre_order_theme_snippet.liquid
Liquid error: Could not find asset snippets/uploadkit.liquid