Build Your Own Breath Monitoring System Using ESP32 and MQ Sensors
🫁 Build Your Own Breath Monitoring System Using ESP32 and MQ Sensors
📅 By Dinesh | LearnWithDTrend Blog | 2026
🚀 Introduction
Have you ever wanted to build a simple breath monitoring system that can read gas changes from your breath and show the values on an OLED display?
In this project, I built a compact breath monitoring setup using:
✅ ESP32 microcontroller
✅ MQ3, MQ135, MQ9, and MQ2 sensors
✅ 128x64 OLED display
✅ I2C communication
✅ Real-time sensor reading on serial monitor and display
This project is useful for learning how gas sensors respond to breath-related changes and how sensor data can be displayed in real time.
Let’s dive in.
🧠 Project Concept
Here’s how the system works:
The ESP32 reads values from four MQ sensors.
Each sensor detects a different gas or air-quality pattern.
The data is printed on the serial monitor.
The same values are shown on the OLED display.
The system refreshes every 2 seconds for live monitoring.
This makes the project simple, interactive, and easy to demonstrate in a lab or project expo.
🧰 Components Used
| Component | Quantity |
|---|---|
| ESP32 DevKit | 1 |
| MQ3 Sensor | 1 |
| MQ135 Sensor | 1 |
| MQ9 Sensor | 1 |
| MQ2 Sensor | 1 |
| OLED Display 128x64 (I2C) | 1 |
| Jumper Wires | As needed |
| Breadboard | 1 |
| USB Cable | 1 |
🔌 Connections
ESP32 Pin Configuration
| Component | ESP32 Pin |
|---|---|
| MQ3 | GPIO 34 |
| MQ135 | GPIO 35 |
| MQ9 | GPIO 32 |
| MQ2 | GPIO 33 |
| OLED SDA | GPIO 22 |
| OLED SCL | GPIO 23 |
| OLED VCC | 3.3V or 5V depending on module |
| OLED GND | GND |
Important Notes
Use a common ground for all modules.
The OLED in this code uses I2C address 0x3C.
ESP32 analog pins are used for MQ sensor readings.
MQ sensors need proper warm-up time for stable readings.
📟 ESP32 Code
The ESP32 reads the MQ sensor values and displays them on the OLED screen.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// MQ Sensor Pins
#define MQ3_PIN 34
#define MQ135_PIN 35
#define MQ9_PIN 32
#define MQ2_PIN 33
void setup() {
Serial.begin(115200);
Wire.begin(22,23); // SDA, SCL
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED not detected");
while(true);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10,20);
display.println("Breath Analysis");
display.setCursor(25,35);
display.println("System Start");
display.display();
delay(2000);
}
void loop() {
int mq3 = analogRead(MQ3_PIN);
int mq135 = analogRead(MQ135_PIN);
int mq9 = analogRead(MQ9_PIN);
int mq2 = analogRead(MQ2_PIN);
Serial.println("------ Sensor Readings ------");
Serial.print("MQ3: ");
Serial.println(mq3);
Serial.print("MQ135: ");
Serial.println(mq135);
Serial.print("MQ9: ");
Serial.println(mq9);
Serial.print("MQ2: ");
Serial.println(mq2);
Serial.println("-----------------------------");
display.clearDisplay();
display.setCursor(0,0);
display.println("Breath Analysis");
display.setCursor(0,16);
display.print("MQ3: ");
display.println(mq3);
display.setCursor(0,28);
display.print("MQ135: ");
display.println(mq135);
display.setCursor(0,40);
display.print("MQ9: ");
display.println(mq9);
display.setCursor(0,52);
display.print("MQ2: ");
display.println(mq2);
display.display();
delay(2000);
}
🧠 How the Code Works
1. Library Imports
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
These libraries are used to communicate with the OLED display.
Wire.hhelps with I2C communicationAdafruit_GFX.hprovides graphics functionsAdafruit_SSD1306.hcontrols the OLED screen
2. OLED Display Setup
Adafruit_SSD1306 display(128, 64, &Wire, -1);
This creates the OLED object for a 128x64 display.
3. Sensor Pin Definitions
#define MQ3_PIN 34
#define MQ135_PIN 35
#define MQ9_PIN 32
#define MQ2_PIN 33
These lines assign analog input pins to each MQ sensor.
4. Setup Function
The setup() function runs only once when the ESP32 starts.
Starts serial communication at 115200 baud
Starts I2C on pins 22 and 23
Checks whether the OLED is connected
Shows a startup message on the screen
If the OLED is not detected, the code stops.
5. Loop Function
The loop() function runs continuously.
Reads analog values from all four sensors
Prints the values to the serial monitor
Clears the OLED screen
Displays all sensor readings on the OLED
Waits 2 seconds before repeating
📊 Sensor Reading Logic
The MQ sensors output analog values depending on the gas concentration near the sensor.
| Sensor | Purpose |
|---|---|
| MQ3 | Alcohol / breath-related gas sensing |
| MQ135 | Air quality / VOC sensing |
| MQ9 | Carbon monoxide and combustible gas sensing |
| MQ2 | Smoke and gas sensing |
These values can change when someone exhales near the sensors.
🛠️ Build Process
First, I connected the MQ sensors to the ESP32 analog input pins and wired the OLED display using I2C.
Next, I uploaded the code and confirmed that the OLED started with the welcome message.
Then, I opened the serial monitor to verify that each sensor value was updating correctly.
Finally, I tested the system by breathing near the sensors and observed the changing values on both the display and the serial monitor.
🎮 Control / Display Features
Real-time sensor value reading
OLED display output
Serial monitor output
Simple breath-monitoring demonstration
Easy ESP32-based hardware setup
📌 Important Notes
MQ sensors need warm-up time before stable readings.
The values are raw analog readings, not calibrated gas concentration.
For a better project, calibration can be added later.
Make sure the OLED I2C address matches your module.
🎉 Final Result
✅ Breath monitoring system built successfully
✅ ESP32 reading multiple MQ sensors
✅ OLED display showing live values
✅ Serial monitor output working
✅ Simple and effective sensor demonstration project
This project is a good example of combining gas sensors, ESP32, and OLED display in one useful embedded system.
❤️ Let’s Connect
This project is useful for learning:
ESP32 programming
Analog sensor reading
OLED display interfacing
I2C communication
Real-time monitoring systems
You can use this as a blog post, mini project report, or project demonstration content for LearnWithDTrend.
LearnWithDTrend Blog Draft — ready for publishing
Comments
Post a Comment