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.

Friday 25 December 2020

Advent of Code 2020 (ii)

 


This year I managed to complete 43 of the 49 Advent of Code tasks (on Xmas day, i.e. today, you only get the 50th star if you have completed all previous 49 tasks). Really proud and happy that I participated every day for almost a full month because I have practised and learned a lot.

A few things that come to my mind:

  • I finally felt compelled to set up WSL2 on my laptop, which involved tweaking something on the bios. 
  • I cloned and tweaked a framework to keep some order on the solutions.
  • I used VS Code (instead of pycharm) and learned about passing arguments to the debugger.
  • I published my code to github, struggling with git commands from time to time (is there anyone who doesn´t??)
  • I googled my way through relative imports in python and vs code setup for projects, eventually dropping the framework in favour of a more simple solution.
  • Library of the month: Collections, I made heavy use of defaultdict, used Counter a few times and thought it was a pity that deque is O(n) for searches and not O(1).
  • Surprised I did not use pandas at all. Not surprised that I used networkx and numpy. I still need to get a hold on regular expressions though.
  • Got reasonably good at parsing files, however a colleague has suggested that I have a look at lark parser   
  • Found out that sadly John Conway passed away this last April.
  • Following another colleague´s advice, I borrowed Python Cookbook from our mini department library but only found time to go through first chapter. 
  • Chinese remainder theorem and Bézout's indentity
  • Needless to say I went into several rabbit holes.
  • Best rank position for Part 1: 1874 on Day 20, which ironically is one of the few days I didn´t manage to complete part 2.
  • Best rank position for Part 2: 2912 on Day 21
  • Quickest Part 1: 00:17:50 on Day 6 (but only rank ~6200)
  • Quickest Part 2: 00:33:40 also on Day 6 (but only rank ~6000)
  • Recurring issue: typing "for i in intVarName:" instead of "for i in range(intVarName)":
The whole experience was made even more enjoyable because a few colleagues joined a private leaderboard and a whatsapp group just for AoC, so this has been a running theme all this month (we were careful with spoilers, as not everyone was doing the puzzles at the same time).

I may try to finish the rest of the puzzles over the holidays. If you want to try AoC, you can join any time and do the puzzles at your own pace.

Friday 11 December 2020

Advent of Code 2020

For the second year running, I am giving a try to Advent of Code. Last year I got 18 stars in the first 10 days. So far, so good as this year I am already at 22 in the first 11 days, but I assume this is where it will get more tricky and I will run out of time (and more likely brain power) to solve the daily puzzles.

This year I did not get to do much coding at work, so this is like an end of year private treat to myself. Advent of Code is a superb learning experience and even if I am far away from the real pros, I think about it in the same way I do about running from time to time.

The one other thing that dawned on me is that I probably approach reading other people´s code as other people may approach reading poetry or sheet music.