Classroom CO2 traffic light
Build it step by step
0 of 4 doneWire the I2C bus
SCD40 and OLED both on 3V3, GND, SDA 21, SCL 22.
Add the traffic light
Green, amber and red LEDs with 220 Ω on GPIO25, 26, 27.
Upload and wait
The first value comes after about 5 s and settles within a few minutes.
Check outdoors
Fresh outdoor air should read about 420 to 450 ppm. The sensor recalibrates itself over a week of normal use (it assumes it sees fresh air sometimes).
How do you build a classroom CO2 traffic light?
An SCD40 measures real CO2 in ppm and a traffic light tells a classroom when to open the windows: green under 800, amber to 1200, red above. You need ESP32 DevKit V1, SCD40 CO2 sensor, SSD1306 OLED Display, LED 5 mm green, LED 5 mm yellow, LED 5 mm red, Resistor 220 Ω × 3, USB A to micro-USB cable (data), 5 V 2 A USB adapter and Jumper wires male to male (40 pcs, 20 cm). It takes 4 steps and about 75 minutes.
- Level
- Intermediate
- Time
- About 75 min
- Parts
- 10
Parts list
- Development BoardsBuyESP32 DevKit V1 × 1
- SensorsBuySCD40 CO2 sensor × 1
- DisplaysBuy
- SemiconductorsBuyLED 5 mm green × 1
- SemiconductorsBuyLED 5 mm yellow × 1
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 3
- Cables & WiresBuy
- PowerBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| SCD40 VIN / GND | ESP32 3V3 / GND | Sensor power |
| SCD40 SDA / SCL | ESP32 GPIO21 / GPIO22 | I2C (address 0x62) |
| OLED SDA / SCL / VCC / GND | GPIO21 / GPIO22 / 3V3 / GND | Same bus (0x3C) |
| ESP32 GPIO25 / GPIO26 / GPIO27 | 220 Ω, green / amber / red LED, GND | Traffic light |
Code
Arduino IDE with the ESP32 board package (Espressif), board "ESP32 Dev Module". Install "SparkFun SCD4x Arduino Library" and "Adafruit SSD1306".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <SparkFun_SCD4x_Arduino_Library.h>
#include <Adafruit_SSD1306.h>
SCD4x co2;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const int G = 25, A = 26, R = 27;
void setup() {
Serial.begin(115200);
Wire.begin();
pinMode(G, OUTPUT); pinMode(A, OUTPUT); pinMode(R, OUTPUT);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
if (!co2.begin()) { Serial.println("SCD40 not found"); while (true) delay(100); } // starts periodic measurement
}
void loop() {
if (!co2.readMeasurement()) { delay(500); return; } // a new value every 5 s
int ppm = co2.getCO2();
float t = co2.getTemperature(), h = co2.getHumidity();
digitalWrite(G, ppm < 800); digitalWrite(A, ppm >= 800 && ppm < 1200); digitalWrite(R, ppm >= 1200);
oled.clearDisplay(); oled.setTextSize(1); oled.setCursor(0, 0); oled.print("CO2 ppm");
oled.setTextSize(3); oled.setCursor(0, 12); oled.print(ppm);
oled.setTextSize(1); oled.setCursor(0, 42); oled.print(ppm < 800 ? "Fresh" : ppm < 1200 ? "Open a window soon" : "Open the windows now");
oled.setCursor(0, 54); oled.print(t, 1); oled.print(" C "); oled.print(h, 0); oled.print(" %");
oled.display();
Serial.printf("%d ppm, %.1f C, %.0f %%\n", ppm, t, h);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
SCD40 not found
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Outdoors or by an open window | 420 to 500 ppm, green |
| Breathe gently towards it from 30 cm | Rises above 1200 within 10 to 20 s, red |
| Closed room, 30 students, 30 minutes | Usually amber or red |
If it does not work
"SCD40 not found"
Check SDA/SCL and 3V3, and that no other device uses address 0x62.
Reads much too high outdoors
Leave it running for a week near a window at times, or run forced recalibration from the library example.
Temperature reads warm
The ESP32 heats nearby air. Keep the sensor a few cm away from the board.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the classroom CO2 traffic light?
ESP32 DevKit V1, SCD40 CO2 sensor, SSD1306 OLED Display, LED 5 mm green, LED 5 mm yellow, LED 5 mm red, Resistor 220 Ω × 3, USB A to micro-USB cable (data), 5 V 2 A USB adapter and Jumper wires male to male (40 pcs, 20 cm).
How long does the classroom CO2 traffic light take to build?
About 75 minutes for a intermediate maker, in 4 steps.
How do I know the classroom CO2 traffic light works?
Outdoors or by an open window: 420 to 500 ppm, green. Breathe gently towards it from 30 cm: Rises above 1200 within 10 to 20 s, red. Closed room, 30 students, 30 minutes: Usually amber or red.
SCD40 not found: what should I check?
Check SDA/SCL and 3V3, and that no other device uses address 0x62.
Reads much too high outdoors: what should I check?
Leave it running for a week near a window at times, or run forced recalibration from the library example.
Temperature reads warm: what should I check?
The ESP32 heats nearby air. Keep the sensor a few cm away from the board.
