← Back to Suite Hub

💨 MQ-135 Air Quality Sensor & Relay Exhaust Fan

ADC Voltage Sensing, 10s Cooldown Hysteresis, Dynamic Particles, & Pinout

🏠 Living Room Gas Particle Visualizer

Exhaust Fan: OFF
🌀
Exhaust Port
Smoke / Gas Source Generation Rate (PPM): 120 PPM (Clean Air)
RP2040 ADC0 (GP26):
0.40 V
Relay Control (GP16):
LOW (De-energized)

🐍 RP2040 MicroPython Logic (With 10s Hysteresis Cooldown)

from machine import ADC, Pin
import time

adc = ADC(Pin(26)) # MQ-135 Analog Output on GP26
relay = Pin(16, Pin.OUT) # Exhaust Fan Relay on GP16

HIGH_THRESHOLD = 400 # Trigger Fan ON
LOW_THRESHOLD = 200 # Trigger Fan OFF
COOLDOWN_DELAY = 10 # 10s hysteresis before re-arm

while True:
raw_val = adc.read_u16()
voltage = (raw_val / 65535) * 3.3
ppm = int((voltage / 3.3) * 1000)

if ppm > HIGH_THRESHOLD:
relay.value(1) # Trigger Fan ON
elif ppm < LOW_THRESHOLD and relay.value() == 1:
relay.value(0) # Stop Fan OFF
time.sleep(COOLDOWN_DELAY) # 10s delay allows particles to regenerate

🔌 RP2040 Hardware Wiring Schematic

1. MQ-135 Gas Sensor Module
VCC Pin→ 3.3V (Out) / 5V
GND Pin→ Ground (GND)
AO (Analog Out)→ GP26 (ADC0)
2. 5V Optocoupled Relay Module
VCC / DC+ Pin→ VBUS (5V Rail)
GND / DC- Pin→ Ground (GND)
IN (Control Pin)→ GP16 (Digital Out)
3. Exhaust Fan Load Wiring
Relay NO (Normally Open) $\rightarrow$ Fan (+) Wire
Relay COM (Common) $\rightarrow$ External DC Power (+)

📚 Key Concepts

  • ADC Conversion: RP2040 reads 0–3.3V analog input on GP26 mapped into 16-bit registers (0–65535).
  • Hysteresis Gap: Prevents rapid relay chattering when air quality fluctuates around the threshold.
  • 10-Second Cooldown: Forces a 10s pause after fan shutoff, allowing smoke particles to slowly re-accumulate into the living space.

📝 Mini-Quiz Module

1. Why is dual-threshold hysteresis used in environmental relay controls?

2. What GP pin on RP2040 natively supports ADC0 input?