Gas Sensor
Gas Sensor
SOFTWARES USED
Arduino UNO (Arduino IDE)
APPARATUS/ COMPONENTS REQUIRED
1
Koneru Lakshmaiah Education Foundation
22SC1209 IOT WORKSHOP
PRE-LAB
THEORY:
In this Experiment , you will read the sensor analog output voltage and when the smoke
reaches a certain level, it will make sound a buzzer and a red LED will turn on.
When the output voltage is below that level, a green LED will be on.
MQ-2 Smoke Sensor
The MQ-2 smoke sensor is sensitive to smoke and to the following flammable gases:
• LPG
• Butane
• Propane
• Methane
• Alcohol
• Hydrogen
The resistance of the sensor is different depending on the type of the gas.
The smoke sensor has a built-in potentiometer that allows you to adjust the sensor sensitivity
according to how accurate you want to detect gas.
2
Koneru Lakshmaiah Education Foundation
22SC1209 IOT WORKSHOP
The voltage that the sensor outputs changes accordingly to the smoke/gas level that exists in
the atmosphere. The sensor outputs a voltage that is proportional to the concentration of
smoke/gas.
In other words, the relationship between voltage and gas concentration is the following:
Working Mechanism
The output can be an analog signal (A0) that can be read with an analog input of the Arduino
or a digital output (D0) that can be read with a digital input of the Arduino.
3
Koneru Lakshmaiah Education Foundation
22SC1209 IOT WORKSHOP
Pin Wiring
The MQ-2 sensor has 4 pins.
Pin-------------------------------------Wiring to Arduino Uno
A0-------------------------------------Analog pins
D0-------------------------------------Digital pins
GND-----------------------------------GND
VCC------------------------------------5V
So, before jumping into the coding part, let's check whether we've assembled all the
necessary hardware components.
PROCEDURE:
1. Give the connections to the Arduino board as shown in the connection diagram.
2. Open Arduino IDE in the computer
3. Create new file File_-- New
4. Type your program and Save it in appropriate location in your computer.
5. Compile your program by clicking Verify option in the menu.
6. Once the program compiled successfully, connect the Arduino board to the computer using USB
cable.
7. After connecting, go to Tools ---- Board --- Select Arduino/Genuino Uno option
8. After selecting board, go to Tools ---- Port --- Select Arduino Uno COM port 3 (name may
appear differently for other computers).
**Note that this port option will be displayed only when board is connected to
computer
9. Now upload the program to the Arduino board by clicking Upload option.
10. Once the program uploaded successfully, open serial monitor window in Arduino IDE to see
the value of Gas Sensor. When you change the Smoke content of MQ2, observe the buzzer beeps
when Sensor values reaches threshold “300” and according to that LED glove.
4
Koneru Lakshmaiah Education Foundation
22SC1209 IOT WORKSHOP
pinMode(buz, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop()
{
int sensValue=analogRead(sensor);
Serial.println(sensValue);
if (sensValue > 300)
{
digitalWrite(redled, HIGH);
tone(buz,10000,10);
digitalWrite(greenled, LOW);
}
else
{
digitalWrite(greenled, HIGH);
noTone(buz);
digitalWrite(redled, LOW);
}
}
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to
calling function
int readSensor() {
unsigned int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
unsigned int outputValue = map(sensorValue, 0, 1023, 0, 255); // map the 10-bit data to 8-bit data
if (outputValue > 65)
analogWrite(ledPin, outputValue); // generate PWM signal
5
Koneru Lakshmaiah Education Foundation
22SC1209 IOT WORKSHOP
else
digitalWrite(ledPin, LOW);
return outputValue; // Return analog moisture value
INFERENCES/ANALYSIS
POST-LAB
Results
6
Koneru Lakshmaiah Education Foundation