Health
Air quality monitor
Shows how stuffy a room is. The light is green when the air is fresh, yellow when it is time to open a window, and red when it is stale.
- Hard
- about 60 min
- ESP32-S3
- 7 parts
- 12 wires
- 71 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The sensor does not count CO₂ molecules. It measures fumes and estimates CO₂ from them, so treat the number as a guide for when to air the room, not as a laboratory reading.
What you learn
Two I2C devices on one bus, thresholds, and mixing colour with three PWM outputs.
How it works
input
SGP30
decides
ESP32-S3
output
OLED 0.96 in
RGB LED
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 7 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 12
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
air-monitor.ino
// Air quality monitor
// Shows estimated CO2 and fumes in the room. The light is green when the air
// is fresh, yellow when it is time to open a window, red when it is stuffy.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_SGP30.h>
const int PIN_SGP_SDA = 8;
const int PIN_SGP_SCL = 9;
const int PIN_OLED_SDA = 8;
const int PIN_OLED_SCL = 9;
const int PIN_RGB_R = 15;
const int PIN_RGB_G = 16;
const int PIN_RGB_B = 18;
const int CO2_FRESH_BELOW = 800; // ppm
const int CO2_STUFFY_ABOVE = 1500; // ppm
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
Adafruit_SGP30 sgp;
bool sensorOk = false;
void light(int r, int g, int b) {
analogWrite(PIN_RGB_R, r);
analogWrite(PIN_RGB_G, g);
analogWrite(PIN_RGB_B, b);
}
void setup() {
pinMode(PIN_RGB_R, OUTPUT);
pinMode(PIN_RGB_G, OUTPUT);
pinMode(PIN_RGB_B, OUTPUT);
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
sensorOk = sgp.begin(&Wire);
light(0, 0, 40); // blue while the sensor warms up
}
void loop() {
oled.clearDisplay();
if (!sensorOk || !sgp.IAQmeasure()) {
oled.setTextSize(1);
oled.setCursor(0, 24);
oled.print("SGP30 not found");
oled.display();
delay(1000);
return;
}
int co2 = sgp.eCO2;
oled.setTextSize(1);
oled.setCursor(0, 0);
oled.print("CO2 (estimate)");
oled.setTextSize(3);
oled.setCursor(0, 14);
oled.print(co2);
oled.setTextSize(1);
oled.print(" ppm");
oled.setCursor(0, 50);
oled.print("Fumes ");
oled.print(sgp.TVOC);
oled.print(" ppb");
oled.display();
if (co2 < CO2_FRESH_BELOW) light(0, 120, 0);
else if (co2 < CO2_STUFFY_ABOVE) light(140, 90, 0);
else light(160, 0, 0);
delay(1000); // the SGP30 wants one reading a second
}
Libraries
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit SSD1306: install it from Sketch > Include Library > Manage Libraries.
- Adafruit SGP30 Sensor: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.