22CS103 Nithilan Valan Exp 4
22CS103 Nithilan Valan Exp 4
AIM:
To detect the presence of gas or smoke in the air using an Arduino Uno and an MQ2 gas
sensor, with visual alerts through an LED indicator.
PROCEDURE:
1. Reads the gas sensor value using:
i. analogRead(MQ2pin)
2. Compares the sensor value with the threshold (470) to detect gas presence.
3. Turns the LED OFF (LOW) if gas is detected and prints "SMOKE DETECTED" to
the Serial Monitor.
4. Keeps the LED ON (HIGH) if no gas is detected and prints the sensor value to the
Serial Monitor.
COMPONENTS REQUIRED:
1. Arduino Uno (R3)
2. Gas Sensor
3. Breadboard
4. LED
5. 4 kΩ Resistor
6. Jumper Wires
WIRING DIAGRAM:
DEFAULT CODE:
int LED = A1;
const int gas = 0;
int MQ2pin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
float sensorValue,MQ2pin;
sensorValue = analogRead(MQ2pin); // read analog input pin 0
}
else{
digitalWrite(LED,HIGH);
Serial.println("Sensor Value: ");
Serial.println(sensorValue);
}
delay(1000);
}
float getsensorValue(int pin){
return (analogRead(pin));
}
Explanation of the Code
1. The MQ2 gas sensor is connected to analog pin A0, and an LED is connected to A1 to
indicate gas detection status.
2. The setup() function initializes serial communication at 9600 baud, allowing sensor
readings to be displayed on the Serial Monitor.
3. The loop() function continuously reads the gas sensor’s analog value using
analogRead(MQ2pin), storing it in sensorValue.
4. If the sensor value is greater than or equal to 470, it means gas is detected, so the LED is
turned OFF (LOW) and "SMOKE DETECTED" is printed.
5. If the sensor value is below 470, the LED remains ON (HIGH), and the sensor reading is
printed.
6. A delay of 1 second (1000ms) ensures the readings update at regular intervals.
7. The getsensorValue(int pin) function is defined but never used in the main code; it simply
returns an analog reading from the specified pin.
USE CASE:
1. Try to execute the code by changing the gas content near the sensor.
2. Once you are used to it, try to change other gas sensors available in the Tinkercad or use
two LEDs - RED and GREEN. When there is no gas, the green LED should glow. if the
sensor identifies the gas, then the red LED should glow. Added to it connect the buzzer so
that when the sensor detects the gas, the RED LED and buzzer should be triggered.
UPDATED CODE:
Use Case 2:
int LED1 = A1;
int LED2 = A2;
const int gas = 0;
int MQ2pin = A0;
void setup() {
Serial.begin(9600);
}
void loop() {
float sensorValue,MQ2pin;
sensorValue = analogRead(MQ2pin); // read analog input pin 0
}
else{
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
Serial.println("Sensor Value: ");
Serial.println(sensorValue);
}
delay(1000);
}
float getsensorValue(int pin){
return (analogRead(pin));
}
RESULTS:
Use Case 1:
Output 1:
647.00 |SMOKE DETECTED
Use Case 2:
Output 2.1:
Sensor Value:
273.00
Output 2.2:
666.00 |SMOKE DETECTED
EXPLANATION:
Use Case 1:
1. The MQ2 gas sensor was connected to the Arduino and continuously measured the gas
concentration using analogRead(MQ2pin).
2. The initial reading was low, indicating no gas detected.
3. The user increased the gas content near the sensor in Tinkercad by clicking on the sensor,
simulating the presence of smoke.
4. As the gas concentration increased, the sensor value reached 647, crossing the threshold of
470.
5. The Arduino detected the smoke and displayed "647.00 | SMOKE DETECTED" in the
Serial Monitor.
6. This process continued in a loop, allowing the user to observe how the sensor reacts to
different gas levels.
Use Case 2:
1. A green LED (LED2) was connected to pin A2 and a buzzer to pin D8, while the red
LED (LED1) remained connected to A1.
2. The code was modified so that:
3. If no gas was detected (sensor value < 470), the green LED turned ON, the red LED
and buzzer remained OFF.
4. If gas was detected (sensor value ≥ 470), the red LED and buzzer turned ON, and the
green LED turned OFF.
5. The user executed the modified circuit and observed the following:
6. When the sensor value was low (273.00), the green LED was ON, and the Serial Monitor
displayed "Sensor Value: 273.00".
7. When the gas concentration increased (666.00), the red LED and buzzer activated, the
green LED turned OFF, and the Serial Monitor displayed "666.00 | SMOKE
DETECTED".
8. This system continuously checked for gas levels every second, providing real-time alerts
through the LEDs and buzzer.
RESULT:
Thus, the IoT based gas leakage monitoring system with Arduino Uno was implemented
successfully using Tinkercad and the outputs have been verified.