Build your own CO₂ traffic light with Arduino

FunduinoPublished on

Luftqualität messen und als Ampelfarbe sichtbar machen

Wie gut ist die Luft in einem Raum? Der CO₂-Anteil gibt darauf eine brauchbare Antwort: Je mehr ausgeatmete Luft sich ansammelt, desto höher steigt er – und desto nötiger wird das Lüften. In Schulen wurde außerdem festgestellt, dass die Konzentrationsfähigkeit mit steigendem CO₂-Anteil nachlässt.

Eine CO₂-Ampel macht diesen Wert sichtbar. In dieser Anleitung baust du eine solche Ampel mit dem Arduino: Zuerst liest du den Sensor aus und zeigst die Werte im seriellen Monitor an, danach steuerst du damit drei LEDs in Ampelfarben.

Arduino UNO with MH-Z19B sensor and three LEDs on a breadboard
The setup with a CO₂ sensor and LED traffic light

You need this

MH-Z19C CO2 sensor - NDIR infrared, 400-5000 ppm, UART and PWM MH-Z19C CO2 sensor - NDIR infrared, 400-5000 ppm, UART and PWM 35,19 € *Item no. F23106440About the product — MH-Z19C CO2 sensor - NDIR infrared, 400-5000 ppm, UART and PWM
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm
LED 5 mm - 100 Pieces, 5 Colors, 2 V LED 5 mm - 100 Pieces, 5 Colors, 2 V 1,97 € *Item no. F23107316About the product — LED 5 mm - 100 Pieces, 5 Colors, 2 V

All 6 parts together 54,66 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Which CO₂ levels are critical?

In a 2008 announcement by the German Environment Agency titled Health Assessment of Carbon Dioxide in Indoor Air you'll find scientific guidance on which CO₂ levels are normal and which are critical. A level of 400 ppm (parts per million) is considered a completely natural CO₂ concentration in outdoor air. It has been found that students' ability to concentrate decreases as CO₂ levels rise.

The DIN EN 13779 standard divides indoor air quality into four categories:

  • IDA 1 – high indoor air quality: less than 400 ppm above outdoor levels, under 800 ppm absolute
  • IDA 2 – medium indoor air quality: 400 to 600 ppm above outdoor levels, 800 to 1000 ppm absolute
  • IDA 3 – moderate indoor air quality: 600 to 1000 ppm above outdoor levels, 1000 to 1400 ppm absolute
  • IDA 4 – low indoor air quality: over 1000 ppm above outdoor levels, over 1400 ppm absolute

A CO₂ traffic light measures the CO₂ concentration in the surrounding air and displays it as a traffic-light color: it changes from green to yellow to red as soon as concerning levels are reached. This lets you see at a glance when you should ventilate.

You only need a few components: an Arduino UNO, an MH-Z19B CO₂ sensor, a traffic-light LED, and a breadboard with a few wires.

Assembly and wiring

In the first attempt, you only display the sensor readings in the serial monitor. The traffic light will be added afterward.

The MH-Z19B provides its reading as a PWM signal. You connect this output to pin 5 of the Arduino. The sensor’s power supply and shared ground are also connected.

Arduino UNO with MH-Z19B sensor and three LEDs on a breadboard
The setup with a CO₂ sensor and LED traffic light
CO2 sensor MH-Z19B with connected jumper wires
The MH-Z19B up close
1 / 2

Read measured values

The sketch uses pulseIn() to measure how long the sensor’s PWM signal stays HIGH and calculates the ppm value from that.

After uploading, open the serial monitor at 9600 baud. In the example, the values are below 800 ppm - this corresponds to indoor air quality category IDA 1, meaning high indoor air quality.

Serial monitor with CO2 values in ppm output every second
The readings in the serial monitor
Read and display the CO₂ value
// Funduino - CO2-Messung mit dem Sensor MH-Z19B
// Die Messwerte werden über das PWM-Signal des Sensors ermittelt.

// Der PWM-Pin des Sensors kommt an Pin 5 des Mikrocontrollers. Bei anderen
// Boards muss dieser Pin PWM-fähig sein.
int SensorPin = 5;

// Der voreingestellte Messbereich (0 bis 5000 ppm). Der MH-Z19B kann auch auf
// einen Höchstwert von 2000 ppm vorkonfiguriert sein.
int Messbereich = 5000;

unsigned long ZeitMikrosekunden;  // Dauer des PWM-Signalpegels in Mikrosekunden
unsigned long ZeitMillisekunden;  // Dauer des PWM-Signalpegels in Millisekunden

int PPM = 0;        // CO2-Messwert in ppm (parts per million, Anteile pro Million)
float Prozent = 0;  // prozentuale Länge des PWM-Signals

void setup()
{
  pinMode(SensorPin, INPUT);  // Pin 5 wird als Eingang definiert.
  Serial.begin(9600);         // Serielle Verbindung für den seriellen Monitor
}

void loop()
{
  // pulseIn misst, wie lange das Signal am Pin auf HIGH bleibt. Ohne den dritten
  // Wert endet die Messung nach 1.000.000 Mikrosekunden, also nach einer Sekunde.
  // Das Signal des Sensors kann aber bis zu 1004 ms lang sein - deshalb setzen
  // wir diesen "Timeout" höher.
  ZeitMikrosekunden = pulseIn(SensorPin, HIGH, 2000000);

  // Umrechnen von Mikrosekunden in Millisekunden
  ZeitMillisekunden = ZeitMikrosekunden / 1000;

  // Laut Datenblatt ist das PWM-Signal des MH-Z19B höchstens 1004 ms lang. Wir
  // teilen die gemessene Dauer durch diesen Höchstwert und bekommen so den Anteil
  // des aktiven Pegels am Gesamtsignal.
  Prozent = ZeitMillisekunden / 1004.0;

  // Aus diesem Anteil und dem Messbereich ergibt sich der ppm-Wert.
  PPM = Messbereich * Prozent;

  Serial.print("CO2 Anteil in der Luft in PPM: ");
  Serial.println(PPM);

  delay(1000);  // Eine Sekunde bis zur nächsten Messung
}

Complete the traffic light

Now you'll use the measured values to control a traffic light. Also connect three traffic-light-colored LEDs to digital pins 6, 7, and 8 - or a ready-made LED traffic light module. Later, relays could be connected there instead of the LEDs to control a real traffic light system.

The three LEDs represent the four indoor air quality categories:

  • IDA 1 - green LED
  • IDA 2 - green and yellow LEDs
  • IDA 3 - yellow LED
  • IDA 4 - red LED

The calculation in the sketch is deliberately kept simple: it divides the measured signal duration by the maximum value of 1004 milliseconds. This is sufficient for a traffic light that needs to distinguish between green, yellow, and red. For more precise results, the MH-Z19B datasheet provides a more detailed formula that also takes the signal’s pause time into account.

CO₂ traffic light: Display the measured value as a traffic light color
// Funduino - CO2-Ampel mit dem Sensor MH-Z19B

int ROT  = 6;  // Rote LED an Pin 6
int GELB = 7;  // Gelbe LED an Pin 7
int GRUN = 8;  // Grüne LED an Pin 8

// Der PWM-Pin des Sensors kommt an Pin 5 des Mikrocontrollers.
int SensorPin = 5;

// Der voreingestellte Messbereich (0 bis 5000 ppm).
int Messbereich = 5000;

unsigned long ZeitMikrosekunden;  // Dauer des PWM-Signalpegels in Mikrosekunden
unsigned long ZeitMillisekunden;  // Dauer des PWM-Signalpegels in Millisekunden

int PPM = 0;        // CO2-Messwert in ppm
float Prozent = 0;  // prozentuale Länge des PWM-Signals

void setup()
{
  pinMode(SensorPin, INPUT);  // Pin 5 wird als Eingang definiert.
  Serial.begin(9600);

  pinMode(ROT, OUTPUT);   // Pin 6 ist ein Ausgang.
  pinMode(GELB, OUTPUT);  // Pin 7 ist ein Ausgang.
  pinMode(GRUN, OUTPUT);  // Pin 8 ist ein Ausgang.
}

void loop()
{
  ZeitMikrosekunden = pulseIn(SensorPin, HIGH, 2000000);
  ZeitMillisekunden = ZeitMikrosekunden / 1000;
  Prozent = ZeitMillisekunden / 1004.0;
  PPM = Messbereich * Prozent;

  Serial.print("CO2 Anteil in der Luft in PPM: ");
  Serial.println(PPM);

  // Ab hier wird die Ampel angesteuert.
  if (PPM < 800)  // Messwert kleiner als 800: hohe Raumluftqualitaet
  {
    digitalWrite(GRUN, HIGH);  // gruene LED an
    digitalWrite(GELB, LOW);
    digitalWrite(ROT, LOW);
  }
  if (PPM >= 800 && PPM <= 1000)  // 800 bis 1000: mittlere Raumluftqualitaet
  {
    digitalWrite(GRUN, HIGH);  // gruene und gelbe LED an
    digitalWrite(GELB, HIGH);
    digitalWrite(ROT, LOW);
  }
  if (PPM > 1000 && PPM <= 1400)  // 1000 bis 1400: maessige Raumluftqualitaet
  {
    digitalWrite(GRUN, LOW);
    digitalWrite(GELB, HIGH);  // nur die gelbe LED an
    digitalWrite(ROT, LOW);
  }
  if (PPM > 1400)  // ueber 1400: niedrige Raumluftqualitaet
  {
    digitalWrite(GRUN, LOW);
    digitalWrite(GELB, LOW);
    digitalWrite(ROT, HIGH);  // rote LED an
  }

  delay(1000);  // Eine Sekunde bis zur nächsten Messung
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.