MQ-135 Air Quality Detector using Arduino UNO
Detect harmful gases in real time with the MQ-135 sensor. Reads analog concentration, converts to voltage, triggers digital threshold alerts, and controls Red/Green LEDs — simulated in Cirkit Designer.
What You'll Build
A smart environmental monitoring system that reads gas concentration from an MQ-135 sensor, converts raw ADC values to voltage, checks a digital threshold, and instantly lights a Red (Danger) or Green (Safe) LED. All serial readings are viewable in the Arduino IDE Serial Monitor. Designed and simulated using Cirkit Designer — no physical hardware needed to get started.
Difficulty
Beginner
Build Time
45–90 Min
Sensor Output
Analog + Digital
Resolution
0–1023 (10-bit ADC)
About the MQ-135 Gas Sensor
The MQ-135 uses a heated metal oxide sensing element whose resistance changes in the presence of target gases. It detects multiple harmful gases across a wide concentration range, making it ideal for indoor air quality projects.
Detectable Gases
Outputs Explained
📊 Analog Output AOUT → A0
Provides a continuous voltage (0–5V) proportional to gas concentration. Arduino reads this as a 10-bit value (0–1023), then converts to voltage using raw × (5.0 / 1023.0).
⚡ Digital Output DOUT → D2
Snaps LOW when gas concentration exceeds the threshold set by the onboard potentiometer. Reads HIGH when air is safe. Used to trigger the LED alert system instantly.
Components Required
| Component | Qty | Notes |
|---|---|---|
| Arduino UNO (R3) | ×1 | Main controller — runs sensing and alert logic |
| MQ-135 Gas Sensor Module | ×1 | Includes onboard LM393 comparator and potentiometer for DOUT threshold |
| Red LED (5mm) | ×1 | Danger indicator — lights when DOUT is LOW |
| Green LED (5mm) | ×1 | Safe indicator — lights when DOUT is HIGH |
| 220Ω Resistor | ×2 | Current-limiting for each LED |
| Jumper Wires (M-M, M-F) | ×15+ | All signal and power connections |
| Breadboard | ×1 | For prototyping LED circuits |
How the Code Works
Analog Reading — Raw ADC Value
Every 100ms, the Arduino calls analogRead(A0) to sample the MQ-135's analog output. This returns an integer from 0 to 1023 representing the gas concentration level — higher values mean more gas.
Voltage Conversion
The raw value is converted to real voltage using the formula volts = raw × (5.0 / 1023.0). This maps the 10-bit ADC range to 0–5V and is printed to the Serial Monitor for human-readable output.
Digital Threshold Detection
digitalRead(D2) reads the DOUT pin of the MQ-135 module. The onboard LM393 comparator outputs LOW when gas exceeds the threshold (adjustable via the blue potentiometer on the module), and HIGH when air is safe.
LED Alert Logic
If danger == true (DOUT is LOW): Red LED turns ON, Green LED turns OFF. Otherwise: Green LED turns ON, Red LED turns OFF. This gives an instant visual status without needing to check the Serial Monitor.
Serial Monitor Output
Every loop prints the raw AOUT value, voltage in volts (3 decimal places), and the safety status string to Serial at 115200 baud. Use Arduino IDE → Tools → Serial Monitor to view live readings.
Example Serial Monitor Output
AOUT=350 (1.711 V) DOUT=HIGH (SAFE)
AOUT=362 (1.770 V) DOUT=HIGH (SAFE)
// Gas detected — threshold exceeded
AOUT=720 (3.520 V) DOUT=LOW (DANGER)
AOUT=748 (3.657 V) DOUT=LOW (DANGER)
Wiring Diagram
Pin Connection Map
MQ-135 Sensor
Red LED (Danger)
Green LED (Safe)
Step-by-Step Instructions
Gather All Components
Collect your Arduino UNO, MQ-135 gas sensor module, red and green LEDs, two 220Ω resistors, a breadboard, and jumper wires. Verify your MQ-135 module has 4 pins (VCC, GND, AOUT, DOUT) and an onboard blue potentiometer.
Wire the MQ-135 Sensor to Arduino
- VCC → Arduino 5V pin
- GND → Arduino GND pin
- AOUT → Arduino A0 (analog in)
- DOUT → Arduino D2 (digital in)
Connect the Red LED (Danger)
- Red LED anode (+, longer leg) → 220Ω resistor → Arduino D9
- Red LED cathode (−, shorter leg) → Arduino GND
The resistor limits current to ~14mA at 5V, protecting both the LED and the Arduino output pin.
Connect the Green LED (Safe)
- Green LED anode (+) → 220Ω resistor → Arduino D10
- Green LED cathode (−) → Arduino GND
Install Arduino IDE
Download the Arduino IDE from arduino.cc. No additional libraries are required for this project — it uses only built-in Arduino functions (analogRead, digitalRead, Serial).
Upload the Code
Copy the sketch from the Code section below. Connect Arduino via USB, select Board → Arduino UNO and your correct Port, then click the Upload arrow.
Adjust the DOUT Threshold
Once powered, wait 30 seconds for the sensor to warm up. Open Serial Monitor at 115200 baud. Use a small screwdriver to turn the blue potentiometer on the MQ-135 module until the DOUT switches from SAFE to DANGER at your desired gas level.
Test with a Gas Source
Briefly bring a lighter (unlit) near the sensor to expose it to butane, or breathe directly onto it to see CO₂ detection. Observe the Serial Monitor readings increase and the Red LED activate when the threshold is exceeded.
Full Source Code
// ───────────────────────────────────────────────────── // MQ-135 Air Quality Detector — Arduino UNO // MakeMindz.com | Cirkit Designer Simulation Project // Detects: NH₃, NOx, Alcohol, Benzene, Smoke, CO₂ // ───────────────────────────────────────────────────── // ── Pin Definitions ────────────────────────────────── const int PIN_AOUT = A0; // Analog gas concentration output const int PIN_DOUT = 2; // Digital threshold output (active-LOW via LM393) const int LED_RED = 9; // Red LED → DANGER indicator const int LED_GREEN = 10; // Green LED → SAFE indicator // ── setup() ────────────────────────────────────────── // Runs once on power-up or reset void setup() { pinMode(PIN_DOUT, INPUT); // DOUT uses internal LM393 pull-up pinMode(LED_RED, OUTPUT); pinMode(LED_GREEN, OUTPUT); // Ensure both LEDs are off at startup digitalWrite(LED_RED, LOW); digitalWrite(LED_GREEN, LOW); Serial.begin(115200); Serial.println("MQ-135 Air Quality Detector — MakeMindz.com"); Serial.println("Warming up sensor... please wait 30 seconds."); Serial.println("─────────────────────────────────────────────"); } // ── loop() ─────────────────────────────────────────── // Repeats every 100ms — reads sensor, updates LEDs and Serial void loop() { // Step 1: Read raw analog value (0–1023) int raw = analogRead(PIN_AOUT); // Step 2: Convert to voltage (0.0–5.0V) float volts = raw * (5.0f / 1023.0f); // Step 3: Read digital threshold output // LOW = gas concentration ABOVE threshold → DANGER // HIGH = gas concentration BELOW threshold → SAFE int doLevel = digitalRead(PIN_DOUT); bool danger = (doLevel == LOW); // Step 4: Update LED indicators digitalWrite(LED_RED, danger ? HIGH : LOW); digitalWrite(LED_GREEN, danger ? LOW : HIGH); // Step 5: Print to Serial Monitor Serial.print("AOUT="); Serial.print(raw); Serial.print(" ("); Serial.print(volts, 3); Serial.print(" V) DOUT="); Serial.println(danger ? "LOW (DANGER ⚠️)" : "HIGH (SAFE ✅)"); delay(100); // 10 readings per second }
Simulate Without Hardware
Test the full project in your browser — no sensor required
Use Cirkit Designer or Wokwi to virtually build and run the circuit. Import the diagram.json above, paste the Arduino code, and run the simulation to observe LED switching and Serial Monitor output in real time.
Key Features
Real-Time Monitoring
Reads and displays gas concentration 10 times per second via Serial Monitor.
Analog Detection
Reads full 0–1023 ADC range with voltage conversion for precise concentration tracking.
Digital Threshold Alert
Instant DOUT-based alerting — no floating thresholds or software debounce needed.
LED Safety Indicator
Red/Green visual status — immediately obvious even without a Serial Monitor open.
Simulation Friendly
Designed for Cirkit Designer — test all logic before buying a single component.
Beginner Code
Zero external libraries — only built-in Arduino functions used throughout.
.png)
Comments
Post a Comment