Introduction

MQ sensors are physicochemical sensors used to detect a wide variety of gases, pollutants, and smoke in the atmosphere. The sensor contains a heating element whose electrical resistance changes depending on the presence of a pollutant in the air.

Before functioning and providing accurate readings, some sensors need to be preheated.

Here is a summary table of the different types of MQ gas sensors, their warm-up times for proper operation, the gases they can detect, and their sensitivity:

For the rest of the course, we have chosen the MQ-4 sensor as an example. However, all the circuits we present work for all the sensors listed in the table. Indeed, all MQ sensors have the same pins and are controlled in the same way.

The sensor’s pins

Here are the pins of the gas sensor:

  • GND: Connect to the ground (GND) of the Arduino

  • DigitalWrite: The digital output can be used to control the gas sensor using a potentiometer, for example

  • AnalogWrite: The analog output allows you to read the sensor’s gas value

  • VCC: Must be connected to the 5V or 3.3V pin of the Arduino

Display values on the serial monitor

Now let’s see how to display your gas sensor’s readings on the serial monitor:

const int Gas_Pin = A1; // Gas sensor pin

void setup() {
    Serial.begin(9600); // Initialize serial communication
}

void loop() {
    int value = analogRead(Gas_Pin); // Read the sensor value
    value = map(value, 300, 750, 0, 100); // Convert the value to a percentage
    Serial.print("Gas level: "); // Display the value
    Serial.print(value);
    Serial.println("%");
  
    delay(250); // Pause between readings
}

Gas Detection LEDs

Now let’s see how to create a gas detection system using LEDs. We’ll use 4 LEDs: green, yellow, orange, and red. The program will first turn on the green LED, indicating little or no gas detected, then progressively light up the other LEDs based on the gas percentage measured by the sensor.

Here are the gas percentage thresholds for lighting the LEDs:

  • Green LED: Always on

  • Yellow LED: Turns on at 30% gas detected

  • Orange LED: Turns on at 50% gas detected

  • Red LED: Turns on at 80% gas detected

const int Gas_Pin = A1; // Gas sensor pin
// LED pins
int Green_LED = 7;
int Yellow_LED = 6;
int Orange_LED = 5;
int Red_LED = 4;

void setup() {
  // Set LED pins as outputs
  pinMode(Green_LED, OUTPUT);
  pinMode(Yellow_LED, OUTPUT);
  pinMode(Orange_LED, OUTPUT);
  pinMode(Red_LED, OUTPUT);
}

void loop() {
  int value = analogRead(Gas_Pin); // Read the gas sensor value
  value = map(value, 300, 750, 0, 100); // Convert the value to percentage
  
  digitalWrite(Green_LED, HIGH); // Green LED always on
  digitalWrite(Yellow_LED, value >= 30 ? HIGH : LOW); // Yellow LED on if >= 30%
  digitalWrite(Orange_LED, value >= 50 ? HIGH : LOW); // Orange LED on if >= 50%
  digitalWrite(Red_LED, value >= 80 ? HIGH : LOW); // Red LED on if >= 80%
  
  delay(250); // Pause for 250 milliseconds
}

Smoke Detector

Now let’s see how to create a real smoke detector using a buzzer. The buzzer will turn on when the sensor detects more than 80% gas.

const int gasSensorPin = A0; // Gas sensor pin
const int buzzerPin = A1;    // Buzzer pin

void setup() {
  pinMode(gasSensorPin, INPUT);   // Initialize gas sensor pin as input
  pinMode(buzzerPin, OUTPUT);     // Initialize buzzer pin as output
}

void loop() {
  int value = analogRead(gasSensorPin);           // Read gas sensor value
  value = map(value, 300, 750, 0, 100);           // Convert to percentage (0-100%)
  
  if (value > 80) {                               // If value exceeds 80%
    tone(buzzerPin, 494);                         // Turn on buzzer (tone frequency 494Hz)
    delay(500);                                   // Wait half a second
    noTone(buzzerPin);                            // Stop buzzer
  } else {
    digitalWrite(buzzerPin, LOW);                 // Keep buzzer off
  }
  
  delay(100);                                     // Small delay before next reading
}