Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

📘Gas Detection (Bluetooth Serial Terminal Version)

🎯 Objective of This Lesson

Students will learn:

  • How to enable Bluetooth in ESP32

  • How to send gas sensor data to mobile

  • How to monitor gas levels wirelessly

  • How to detect gas leakage using Bluetooth


📱 Required Mobile App

Install on Android:

  • Serial Bluetooth Terminal
    OR

  • Any Bluetooth Terminal App


🔌 Hardware Reminder

MQ3 Pin ESP32 Pin
VCC 5V
GND GND
AO GPIO 34

No extra components.


🧾 Complete ESP32 Bluetooth Code (Copy–Paste Ready)

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

#define MQ3_PIN 34

int gasValue = 0;
int threshold = 1200; // Adjust after calibration

void setup() {

Serial.begin(115200);
SerialBT.begin("Gas_Detector"); // Bluetooth device name

pinMode(MQ3_PIN, INPUT);

Serial.println("Bluetooth Gas Detection System Started...");
SerialBT.println("Connected to Gas Detection System");

Serial.println("Warming up sensor...");
SerialBT.println("Warming up sensor...");
delay(30000); // 30 seconds warm-up

Serial.println("Sensor Ready!");
SerialBT.println("Sensor Ready!");
}

void loop() {

gasValue = analogRead(MQ3_PIN);

Serial.print("Gas Value: ");
Serial.println(gasValue);

SerialBT.print("Gas Value: ");
SerialBT.println(gasValue);

if (gasValue > threshold) {

Serial.println("⚠ GAS LEAK DETECTED!");
SerialBT.println("⚠ GAS LEAK DETECTED!");

} else {

Serial.println("Environment Safe");
SerialBT.println("Environment Safe");
}

SerialBT.println("----------------------");

delay(1000);
}

🧠 Code Explanation (Step-by-Step)


1️⃣ Include Bluetooth Library

 
#include “BluetoothSerial.h”
 

This enables Bluetooth functionality in ESP32.


2️⃣ Create Bluetooth Object

 
BluetoothSerial SerialBT;
 

This object is used to send and receive Bluetooth data.


3️⃣ Start Bluetooth

 
SerialBT.begin(“Gas_Detector”);
 

This sets the device name visible in mobile Bluetooth list.

Students will see:

👉 Gas_Detector


4️⃣ Warm-Up Delay

 
delay(30000);
 

Sensor needs heating time for stable readings.


5️⃣ Reading Gas Value

 
gasValue = analogRead(MQ3_PIN);
 

Reads analog voltage from MQ3.

Range:

0 – 4095


6️⃣ Sending Data to Mobile

 
SerialBT.println(gasValue);
 

This sends data to the Bluetooth terminal app.

Difference:

  • Serial.print() → Computer

  • SerialBT.print() → Mobile


7️⃣ Gas Detection Logic

 
if (gasValue > threshold)
 

If gas level exceeds threshold:

Display:

⚠ GAS LEAK DETECTED!

Else:

Environment Safe


📱 How to Connect Mobile

1️⃣ Upload code
2️⃣ Turn ON Bluetooth in phone
3️⃣ Open Serial Bluetooth Terminal app
4️⃣ Search for device
5️⃣ Select Gas_Detector
6️⃣ Connect

Now you will see live gas readings.


📊 Example Bluetooth Output

Gas Value: 380
Environment Safe

Gas Value: 1600
GAS LEAK DETECTED!


🔧 Threshold Calibration Reminder

Clean air → Note value
Expose to alcohol vapor → Note value

Set threshold between both values.

Example:

Clean air: 400
Gas present: 1800

Set threshold = 1000


🎓 Learning Outcomes

After this lesson, students understand:

  • Wireless sensor monitoring

  • Bluetooth communication

  • Difference between Serial & SerialBT

  • Real-time alert system

  • Practical IoT foundation

Scroll to Top