Build your own CO2 sensor with Homeyduino [English version]

Build your own CO2 sensor with Homeyduino [English version]

The CO2 sensor had been on the wish list for quite some time. Why you ask? The importance of a healthy indoor climate and, in particular, the amount of carbon dioxide that accumulates indoors has gotten more important with people staying in more because of the Corona virus.

CO2, or Carbon dioxide is a gas that occurs naturally (increasingly), the outside concentration of CO2 has been set at 417 ppm (‘parts per million’) in 2020. This is what is considered healthy fresh air. Indoors, however is a different story, because homes are becoming better insulated, there is less and less natural ventilation, which means that harmful substances can accumulate. All the more reason to monitor this via a Co2 sensor and to (automatically) change the air when necessary.

Adverse consequences of too high CO2 concentration 

The adverse effects of too high a CO2 concentration manifest themselves in different ways, but some examples of this are feeling sleepy or sluggish. This is because when the amount of CO2 in your blood rises, this is at the expense of the amount of oxygen you absorb.

All-in-one Co2 sensor on Amazon.com

Scientas.nl has written a nice and clear article about this called: ‘Accumulated CO2 can drastically affect our thinking ability’   . We had already written an article about the importance of good ventilation, which you can read here .

Indoor CO2 concentrations
CO2 concentrations in ‘parts per million’

According to the above schedule, 600-800 ppm would be a healthy to acceptable indoor climate, but how do you measure this? And how do you improve this when necessary?

DIY Smart home Co2 sensor (MH-Z19B)

However, on Aliexpress you will find the MH-Z19B sensor for around $ 30, combine that with a Wemos D1 of a few euros and for an amount between $ 35 and $ 40 you have a CO2 sensor that can communicate with you. smart home. Of course we do this again through Homeyduino , the handy link between Arduino and Homey .

This way you can monitor the course of the CO2 concentration via Homey Insights and you can use your DIY sensors as a trigger for your flows. For example, by automatically controlling your mechanical ventilation .

Insight CO2 sensor through Homey Insights
Insight CO2 sensor through Homey Insights

The necessities on Amazon or Aliexpress:

If you prefer to buy on Aliexpress, you can use these links:

Also necessary, but only to be configured once, a computer or laptop with installed:

Connection diagram

Follow the diagram and image below to connect the sensor to the Wemos D1 Mini.

MH-Z19BWemos D1 Mini pin
PWMD2
5V5V
GG
Connection diagram MH-Z19 on the Wemos D1 Mini, coupled with Homeyduino
Connection diagram MH-Z19 on the Wemos D1 Mini, coupled with Homeyduino

Code Co2 sensor

Below is the code, just like the other Homeyduino projects , uploading is done via Arduino IDE, if you are not familiar with this, read here for an extensive manual . Make sure you have selected the correct board in settings in Arduino IDE and that you upload the code at 9600 baud. The sketch also contains som code to read the measurements via UART. However, we do not use this, so we will only read the sensor using PWM.


// Code for Homeyduino made by Smart Home Blog https://huisvanvandaag.nl. 
// Take a look at my site for more Homeyduino en other Smart Home projects.

 
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <SoftwareSerial.h>
#include <MHZ.h>
#include <Homey.h>

// pin for pwm reading
#define CO2_IN D2 // D2 19

// pin for uart reading
#define MH_Z19_RX D6 // RX 21 D6 >In this sketch we will only be using the pwm reading, however when we delete the uart part of the code, the sketch stops working, so leave it in.

#define MH_Z19_TX D5  // TX 22 D5

MHZ co2(MH_Z19_RX, MH_Z19_TX, CO2_IN, MHZ19B);


void wifi() {
  if (WiFi.status() != WL_CONNECTED) {
    WiFi.begin("<SSID>", "<PASSWORD>"); // Fill in Wifi and password
    uint8_t timeout = 30;
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      if (timeout<1) break;
    }
    if (WiFi.status() == WL_CONNECTED) {
      //Print IP address
      Serial.print("Verbonden met WiFi! (");
      Serial.print(WiFi.localIP());
      Serial.println(")");
    }
  }
}
 
void setup() {
  Serial.begin(9600);
  Homey.begin("Co2 sensor");
  Homey.setClass("sensor");
   Homey.addCapability("measure_co2");
  pinMode(CO2_IN, INPUT); 
  delay(100); 
  } 

void loop() {
  wifi();
  Homey.loop();

  int ppm_pwm = co2.readCO2PWM();
  Serial.print("PPMpwm: ");
  Serial.print(ppm_pwm);
  Homey.setCapabilityValue("measure_co2", (int) ppm_pwm); 

Serial.println("\n------------------------------");
  delay(5000);
}

Testing the Co2 sensor

Now that you’ve successfully uploaded the code, we will try and test it. We do this by opening the Serial Monitor in Arduino IDE, click on Tools and then Serial Monitor or use the keyboard shortcut CTRL+SHIFT+M.

A new screen will then open on which you can read the values ​​from the sensor. The first time you start up the CO2 sensor, it can take at least 3 minutes or more before it gets warm, so don’t panic if something doesn’t appear right away. Another tip I would like to give you is to also have the sensor measure outside, preferably when it is well above freezing, so that you will have an idea whether the values ​​you read are somewhat correct. As indicated earlier in this article, the outside value for Co2 is around 417 ppm , with my own measurements I got out around 530 ppm outside.

Read Homeyduino Co2 sensor via the Arduino serial monitor
Read Homeyduino Co2 sensor via serial monitor.
Easily connect your Homeyduino projects to Homey
Easily connect your Homeyduino projects to Homey

Conclusion

Using this project you can build a well-functioning Co2 sensor with limited resources. Normally you would probably pay several hundred euros for such a sensor, while it only has a fraction of the possibilities.

Homey Co2 flow that automatically starts when the Co2 concentration is too high
Homey Co2 flow that automatically starts when the Co2 concentration is too high

As indicated earlier, you can use this for countless things, as an example in the setup here at home, the Co2 sensor is linked to the mechanical ventilation of Itho Daalderop , linked to Homey via the Spider thermostat and gateway . This can also be operated via Homey thanks to the Spider app . The mechanical ventilation is always switched on at set times, but if it is not switched on and the Co2 value exceeds 1000 ppm , it will still switch on, in the ‘Boost mode’, ie for 30 minutes on the highest setting.

Spider thermostat and Gateway from Itho Daalderop, with which you can easily connect the mechanical ventilation with Homey
Spider thermostat and Gateway from Itho Daalderop, with which you can easily connect the mechanical ventilation with Homey

Spider thermostat and Gateway from Itho Daalderop, with which you can easily connect the mechanical ventilation with Homey

Mechanical ventilation from Itho Daalderop, easy to link with Homey for a healthier indoor climate.
Mechanical ventilation from Itho Daalderop, easy to link with Homey for a healthier indoor climate.

More projects coming soon! Sign up here so you don’t have to miss anything!

Mike | Huisvanvandaag

Geef een reactie

Het e-mailadres wordt niet gepubliceerd.

Aanbevolen
This time we've build a new Multi sensor, a combination between a CO2-, light-, temperature-…
Translate »