Sunday, 27 December 2020

A rant about measuring CO2, temp and humidity with Arduino

Before Christmas, I had a look at a couple of CO2 sensors and was surprised to see how unnecessarily complex our life is made. At which point did someone decide that the best way to check the value is to do it through an app in your mobile and not through a simple screen on the device? Honestly, I was also quite surprised at the prices. 150 euros reduced from 210 euros? Electronic components are quite cheap, so either high quality sensors are very expensive or someone is making a lot of profit (or both). Maybe I could make one myself despite my very limited knowledge of electronics?

Anyway, since I already happen to have an arduino, a few Raspberry Pi´s and a Calliope Mini, I thought I may as well buy a few sensors and google my way through. The internet is full of successful examples so I thought it may be worth explaining where I struggled. Things never seem to be so straight forward for me. I remember watching one of the Star Wars films many years ago and how I smiled to myself when young Anakin built C-3PO as a pet project when he was ten years old. But then, he was a Jedi and I am not.

Jedi or not Jedi, I bought the following:

SSD1306 OLED mini screen => 6.50 euros

MQ135 gas sensor => 5 euros

DHT11 temp and humidity sensor => 4.50 euros


Except the MQ135, you can buy everything you need and a lot more for under 30 euros with a starter kit, with arduino breadboard, etc included.

I found this video, which seems to be what I wanted, except things are never so easy. After I did all the connections, installed the Arduino IDE, downloaded the Adafruit libraries mentioned in the video and typed the program I got a very blank screen. Not quite what I wanted but not totally unexpected either. Ok, so is it the sensor or the screen? Or both? The good news, time to go into a rabbit hole and learn about OLED´s, I2C addresses and more watching this great video from DroneBot Workshop.

Eventually I had a look at the webpage of the people selling the screen and noticed there was an Arduino library mentioned in the Downloads and Links section! I tried it and still got a blank screen. Looked like a dead end but I also noticed that there is a free ebook which comes with each component. I got the ebook and, surprise, surprise, they mention a different library, U8g2. Even with this one, I could not make it work with the template program provided. My screen is 128x64 but the way to instantiate the display provided in the book, U8G2_SSD1306_128X64_UNIVISION_F_HW_I2C, threw and error. However I managed to make it work using U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C (note the change from 128x64 to 128x32). That means I lose half the screen resolution, but since I only need to display three numbers, it is still ok.

At that point I could display some text, just not the values from the gas sensor. After a lot of looking around, I noticed that the connection to ground in the breadboard was in the wrong place. Once that was fixed, I got the CO2 reading! One small step in the right direction.

From the little I have learned so far, the sensor needs to be left connected for 24 hrs to burn in and then calibrated (read the actual value outside and adjust the reading to be the expected normal CO2 values of 350 to 450 ppm). The calibration can be done with quite some detail.  Right now I get a reading of 250 to 300 ppm. Since the accuracy of this sensor is not that great, it is probably best to go for a traffic light rather than actual values. Good if below 400, amber if below 800 and red otherwise. Not yet implemented, but I happen to have the ideal traffic light module which came as part of this 35 in 1 kit. Otherwise, there is a great opportunity for more rabbit holes, here and here for a start.

Enough of the gas sensor, what about the temperature and humidity sensor? That should be a lot easier, plus I do know which values to expect. Well, it turns out nothing is easy and straight forward. The reading right now is that I am sitting at home at 13 °C and 146% humidity (!). 

As by now was to be expected, there are differences between the connection instructions in the ebook and the datasheet, which actually mentions a pull-up resistor

Plus of course, who says there is only one type of DHT11 sensor? The eagle-eyed will have noticed that one has three pins and the other has 4 pins. The one included in the sensor kit happens to have 3 pins. And, never mind what the ebook says, it also mentions a pull-up resistor in the data sheet. In one place a 5.1K resistor is mentioned and in another a 10K resistor. Would this explain the wrong readings? I happen to have 10K resistors but no 5.1K ones. So the easiest thing is to try with the 10K first just to fins out that the readings are still wrong. Next is to put two 10K resistors in parallel and give it a try. Fingers crossed and, surprise, surprise, readings still wrong. In fact not temperature reading is -14 °C. Humidity has somewhat improved down to 148%.

I will keep prodding the internet and report back when and if I manage to get some decent temp and humidity readings. I have a suspicion that this has to do with the data transfer. One thing is for sure, the more you learn, the more you know that you don´t know.

UPDATE 28th December: so after some more reading about bus data transfer, trying to understand the data sheet and a few more youtube channels and even considering whether to redo the whole thing using a Calliope, I noticed that I had not initialized the temp sensor! So a simple dht.begin() did the trick! Temperature is within two degrees of the reading of an analog thermometer. Humidity currently showing 42%, which seems reasonable.