Smart Indoor Air Quality Monitor With Auto Ventilation Using Raspberry Pi 5

by Daniel DSouza in Circuits > Raspberry Pi

71 Views, 0 Favorites, 0 Comments

Smart Indoor Air Quality Monitor With Auto Ventilation Using Raspberry Pi 5

5guw-xKHvirF8zNIumXLbIlxQtr56nLjDxbi5qM2U68hvJRmAuXYbbeBp_Y51dtW2zxEPhnUk92c5AS48EJIZ2B5TuaQsrCrxSCL8MWBPawxdjJWoimnnm9roMwcbLhUfRMNKlNC0z8D_FAHjfxuxjwe-Gps9xjXuoyS3F_Lc3QWr1uCOZisnYPfrZGChGWO.jpg

Indoor air pollution is one of the most overlooked health risks today. From cooking fumes and dust to volatile organic compounds (VOCs), the air inside our homes can often be more polluted than outdoor air. This is where a smart indoor air quality monitor becomes essential.


In this project, you’ll build a smart indoor air quality monitor with auto ventilation using Raspberry Pi 5, capable of detecting harmful gases and automatically controlling a fan when air quality drops.

Why This Project Matters

  1. Real-time indoor AQI monitoring with automation
  2. Automatically triggers ventilation when pollution rises
  3. Helps build a smart home air quality control system
  4. Ideal for homes, labs, offices, and IoT enthusiasts

This is not just a simple sensor project; this is a complete IoT based air quality monitoring system with automation that you can expand into a full smart home solution.

Project Overview

This air quality monitoring system using Raspberry Pi works in a simple but powerful pipeline:

  1. Sensors collect data
  2. Raspberry Pi processes data
  3. AQI is calculated
  4. Decision logic triggers relay
  5. Fan turns ON/OFF automatically

Workflow

Sensor → Raspberry Pi → AQI Calculation → Decision → Relay → Fan

Supplies

BWb0aQOsF2zpDYNKVnYF1X3TFkQ_IxMWrv2TWRPRtuZmfxiQx0IReP-SXAOyANUSWOjOQ7n3GGqMkss3kMM0XmBxolpOvX192mtvZC_P5HXgllbci-NOqq9Y22IQO2hJo7PQTl25jBFK2-pJ_JNd5-fCMJHyCEno-2dInfl8MSXzF4yRC3sAFEqUO7_R7LT4.jpg
BJf9mCIp_eRz8ManYafWuK4wjWSnU7pRwjyRbDW1kSekKyqrRPcQigtgrLlct3kluBGI8GW7Io1_Hi_-xB-dSGldQM0yEAJtIddF-xSGaz8Fs_eyo2QK_G_ThxjHKXRMwXOth09MA-ftWEji_dwzLCpyfPYcbIJvpfPbSZejyoSPoMGYosPJtNxUyInb56Jx.jpg
jFqBlwVd8ruocOkXc4w2jPJaD__THyvcJ4SbcAhoTOx1Spi7iWZwQWv6xnLfiebnsN7zh7Cn42j-i7PsETOivasIoOtorbL4wdvXW2IKUKZVWfEkXlWZ5vGV6ynJJ7204q4iboBKytUoTctrm6P_U745TqeNLp_ucYwoWuJrnHk5wO6p6gk3ZXmdgjLZbOB5.jpg
75w1dhMIdgVTxquF7ZpPjk8aCOq3ovmNznOfqwcaG93qSN9AqQTZ_ViOzk66V8PtjWCoYm7MxRo2-kMe_qNK1ECuG-q7LJ2bKW3_dgWlO21MFIzd2z2QWI8egwYZ9SRZETm0GpU8CoVL9LfIgpwdVnpbFnjMbhWcDCWNXs3la0SHtpeCPSVMAb2vwA2Bo5Rb.jpg

Core Components

  1. Raspberry Pi 5
  2. The brain of the system for processing and automation
  3. MQ135 Gas Sensor
  4. Detects CO₂, NH₃, alcohol, benzene, smoke
  5. BME680 Sensor (Optional but Recommended)
  6. Measures temperature, humidity, pressure, VOCs
  7. Relay Module (5V)
  8. Controls fan automatically
  9. DC Fan / Exhaust Fan
  10. For ventilation

Additional Components

  1. Breadboard
  2. Jumper wires
  3. Power supply (5V 3A for Raspberry Pi)
  4. Resistors (if needed)
  5. ADC module (like MCP3008 for MQ135 analog output)

System Architecture

gLSMJA2UNHCKrNNjg8CsCi64Xk_4IGAo-qgnUg-Al0EdT4sI3ZEn2osw81ccFIeTAuesSEq6LEGvxYVHsoYvoeE1I3UfAIGVZ6altuiptO4cjSGTjlUR25k05b5CGZC73578cCqJrYoN-K4zZ1vxv-GlL3LYAO-u9JDdLcu46wbOrSDCjpIeojOv_KDoPYCH.jpg

Before jumping into wiring, it's important to understand how your smart indoor air quality monitor actually works internally. Think of this step as building the “brain map” of your system.

How the System Thinks

Your Raspberry Pi 5 IoT air quality system operates in a continuous loop:

  1. Data Collection Layer
  2. The MQ135 sensor continuously detects gases like CO₂, ammonia, and smoke.
  3. The BME680 (if used) adds environmental data like humidity and VOC levels.
  4. Signal Conversion Layer
  5. MQ135 outputs analog signals.
  6. Raspberry Pi cannot read analog signals directly.
  7. MCP3008 converts analog → digital signals via SPI communication.
  8. Processing Layer
  9. Raspberry Pi reads sensor values every few seconds.
  10. It processes the data and applies AQI logic.
  11. Decision Layer
  12. If air quality crosses a threshold → trigger action.
  13. Actuation Layer
  14. Relay module turns ON/OFF the fan.

This creates an auto ventilation system using Raspberry Pi.

Real-Time Loop

This process repeats every 2–5 seconds, enabling indoor AQI monitoring with automation.

Required Hardware Setup

gJuZ_kOd4lz9a0on5c-PIvCYh8cbLHTN35lgfjCeZBffAtnfYrlGsZ_RmeMgE2_HD7cEc4_8uPfvzNH8oAfBJPViMuolz9OHH4DXXC9zBE9HPPBNGDhgQvQm6E4XgrYgV2tmTdOjHC2Xx2_UPabGtWu9rZoDYjPHccUXm5gAsqIMFsrJ3i5VXQCjusZPkaFA.jpg

Now let’s physically assemble your Raspberry Pi air quality sensor project.

Step-by-Step Assembly

1. Positioning Components

  1. Place Raspberry Pi on a stable surface
  2. Keep breadboard close to minimize wire clutter
  3. Ensure proper ventilation space for testing

2. Power Setup

  1. Use a 5V 3A power adapter
  2. Do NOT power sensors from unstable sources

3. Sensor Placement

  1. Keep MQ135 exposed to air (not enclosed)
  2. Avoid placing it near heat sources

4. Relay Setup

  1. Place relay module away from Pi to avoid electrical noise
  2. Use proper insulation if working with AC loads

Safety Guidelines

  1. If you're a beginner, use a DC fan instead of AC
  2. Never touch exposed wires when powered
  3. Double-check connections before switching ON

Raspberry Pi OS Installation

QMZbK5D_EPLUGE2iO4jb6g5DHPeaLXAmZ0TKGXcimjSB_dHewXXIVCDcv7s-KBkVZPIpso2VaCD6YLDH0RALDY80j2bV7-qSrTnaHtnH-kstjtNDoYCBIQMRZq9inrjRK8WDMmsWKF93izCOfAIYdFVVS_Jy4JMMiD1xUTjlClk61Kc1gPznrospSvspfcRx.jpg

This step sets up the software environment for your IoT based air quality monitoring system.

Full Installation Process

1. Flash OS

  1. Download Raspberry Pi Imager
  2. Select:
  3. OS → Raspberry Pi OS (64-bit recommended)
  4. Storage → Your SD card

2. Advanced Settings (Important)

  1. Enable SSH (optional)
  2. Set WiFi credentials
  3. Set username/password

3. First Boot

  1. Insert SD card into Pi
  2. Connect monitor, keyboard, mouse
  3. Power ON

4. Enable Interfaces

sudo raspi-config

Navigate to:

  1. Interface Options → Enable SPI
  2. Interface Options → Enable I2C

Update System

sudo apt update && sudo apt upgrade -y

Tip

Always update your system before installing libraries to avoid compatibility issues.

Official Guide: https://www.raspberrypi.com/documentation/computers/getting-started.html

Sensor Connections

nCMN3USiRevcbQDdXmAtrHlGrunQKrv0gbAqn94EuMaxltnvEGTCvAeBm0qqHldWpx0SUxZDxhYfUTE74tMpLIVa7510W0Jcu9IObNNDQS6OVtGNMFjIUxgDz7l6s-aoVz8Net8cdzL7ORAytoUWqZpX-xPOsirg0GMBERxIFUrWKRvtAJxfsIse_VEV52ZM.jpg

MQ135 → MCP3008 → Raspberry Pi

MQ135 Connections:

  1. VCC → 5V
  2. GND → GND
  3. AOUT → CH0 (MCP3008)

MCP3008 → Pi:

  1. VDD → 3.3V
  2. VREF → 3.3V
  3. AGND/DGND → GND
  4. CLK → GPIO11
  5. DOUT → GPIO9
  6. DIN → GPIO10
  7. CS → GPIO8

BME680 (Optional but Powerful)

  1. Uses I2C (simpler wiring)
  2. Connect:
  3. SDA → GPIO2
  4. SCL → GPIO3

MCP3008 Datasheet: https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf

Installing Required Libraries

oK-0TYFCC5Ym_iUJnPha51I46txJ6JS3mbWJO9CM7CDwkKyardhB2mHDni-ttrb-52TCGN_XJLpXKiSP0ieCf9k30VNrqevw-hgiAEVKn2y7Eo9ryFxjRiCgr7rhRJs7AhrdGhQceF1CAEOiSNKruyGwNL-4yzRIRAJ2sjD2iY_5AbTd7EsxCsvqy-6TKle4.jpg

Now we prepare the software tools for your Raspberry Pi 5 based smart home air quality control project.

Install Libraries

pip3 install spidev RPi.GPIO adafruit-circuitpython-bme680

Verify SPI

ls /dev/spidev*

You should see:

/dev/spidev0.0

Why These Libraries?

  1. spidev → Reads analog values via MCP3008
  2. RPi.GPIO → Controls relay
  3. bme680 → Reads environmental data

Writing the Python Code

Zhim20h9vkzDtfN_r1HEwil3dkKKgN6E0Bi_v4KdojeMidJrIzkCpWaXPxjBAsAcKR6ZOwVNf852ACRw7R-wFRTKIN5PmQ_3lsqGkHZ9QNFNw8OfrSL0qrb4WvXhg-EIPPZahf4e-NsgiLdbycX0AFXFXkb29qxhvvTBN1miO4evbU6F8p3grbNkCtbj1JiT.jpg

Now we build the logic for your IoT air quality monitoring system with automatic fan control.

Code Breakdown

1. Initialize SPI

  1. Connects to MCP3008

2. Read Sensor Data

  1. Converts analog → digital values

3. Process Data

  1. Converts raw values into AQI

4. Control Output

  1. Turns fan ON/OFF

Key Functions

Read sensor values

Calculate AQI

Control relay

Tip

Test each part individually before combining everything.

AQI Calculation Logic

8r5bf01RsutjwW_4NJOeSAJNfas49AIg53Lblra8rvQctZwQBaMu7TgNr_RW7quNtCmih3lFJSJJzIicXAACOVwlrAfbpYR4KYcnKhngNDiyzihb6_4-Yncf_-ap0P5YJj2qfgJYopsPoElE8nIaeR7YWHw5bFCvOVepz6L2n8h_6EWeFqo6hlI-m6PwBaef.jpg

AQI is the heart of your indoor air pollution monitoring project.


Range Category

0–200 Good

200–400 Moderate

400+ Unhealthy


Tip

  1. Convert MQ135 readings → PPM
  2. Use standard AQI formulas for accuracy

Relay & Fan Automation Setup

0g_MSRwcMfdEIrEU1TLieRzMFYl_G6EiYpLPG8YxwIFz7xBtlJL2rogipxLP4k1xgzZDMmCNyZ0hnQ9OCjpM0N5vy90fxrbyKmgwT86SS3Fp6t-znc1ZjjBnapiG7MM__Z_TSAipGotdG-Z_GcrUnXqd1VjicBtNkrcPoyGpB0Pv1CY9yXXX-OKIo_fQgd4Q.jpg

This is where your smart home air quality control system becomes truly “smart”.

How It Works

  1. AQI = Good → Fan OFF
  2. AQI = Moderate → Optional ON
  3. AQI = Unhealthy → Fan ON

Relay Logic

  1. GPIO HIGH → Relay ON → Fan ON
  2. GPIO LOW → Relay OFF → Fan OFF

Tip

Some relays are active LOW, so test before finalizing.

Testing & Calibration

FmTciv5L4MGXc_F5CZ0U-Y5F1hc5n5F1t0mj_a7Gdoav3djMRJI00uteGsBVYf6SOJYfOqGtw7WAGVT01WH-OMrZjyui8ocIYBHnixClLQp8-kIaYud-HgoBSAadlk4Qw28snG4IU-Njc9e6r6E-vvcta8e7B27lUKDAk-9erpsQqpJfp_EcsR9VbXb_GmZR.jpg

Testing ensures your Raspberry Pi air quality sensor project works reliably.

Testing Methods

  1. Use incense sticks or perfume spray
  2. Observe sensor spikes
  3. Check if fan activates

Calibration Tips

  1. Let MQ135 warm up for 24–48 hours
  2. Record baseline values
  3. Adjust thresholds accordingly

Final Output & Use Case

Ld3N5Tu5WFUUOsuVU87jnytj80fbDSar4nJ--Sy5UorTN3J346zM1Z49nnwlos6Cv8gWt7lZa3xIhLtSfZ07JEQ0c8UCZezCzByjLh_KS302u0ZFaCUgvQ4V1AFibPSP7pd6RRhgkGE5wNEnEmHYqY-qAy7qNMB2TjMlLV8g-mUbRUtCLGDPhUofmb1emZaA.jpg

What You’ll See

  1. Real-time sensor values in terminal
  2. AQI classification updates
  3. Fan automatically turning ON/OFF

Complete Python Code

import spidev
import time
import RPi.GPIO as GPIO

spi = spidev.SpiDev()
spi.open(0,0)

relay_pin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(relay_pin, GPIO.OUT)

def read_channel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data

def calculate_aqi(value):
if value < 200:
return "Good"
elif value < 400:
return "Moderate"
else:
return "Unhealthy"

def control_fan(aqi):
if aqi == "Unhealthy":
GPIO.output(relay_pin, GPIO.HIGH)
else:
GPIO.output(relay_pin, GPIO.LOW)

try:
while True:
gas = read_channel(0)
aqi = calculate_aqi(gas)
print("Gas:", gas, "| AQI:", aqi)
control_fan(aqi)
time.sleep(2)

except KeyboardInterrupt:
GPIO.cleanup()

External Resources

  1. Raspberry Pi Docs: https://www.raspberrypi.com/documentation/
  2. MQ135 Guide: https://components101.com/sensors/mq135-gas-sensor-for-air-quality
  3. BME680 Library: https://github.com/adafruit/Adafruit_CircuitPython_BME680
  4. MCP3008 Datasheet: https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf

Future Improvements

  1. Mobile App (Blynk / Firebase)
  2. Cloud Dashboard (MQTT, Node-RED)
  3. AI-based AQI prediction

Conclusion


You’ve successfully built a smart indoor air quality monitor with auto ventilation using Raspberry Pi 5—a complete smart home air quality control system with real-world applications.

This project is a perfect combination of:

  1. IoT
  2. Automation
  3. Environmental monitoring