atmospheric carbon dioxide sensing with an Arduino

I was asked by my Lab Manager to investigate whether one of our CO2 incubators was working properly. Apparently there’s some doubt as to whether it was maintaining CO2 levels. CO2 is an easy gas to detect for the same reason that it causes global warming: it absorbs infrared radiation. You can now buy cheap Non-Dispersive InfraRed CO2 detectors for about US$40 from websites such as AliExpress. However, CO2 incubators typically enrich CO2 to about 5% by volume while most of the cheap sensors only measure up to around 0.5%, or 5,000 parts per million [ppm]. I found a sensor from Sandbox Electronics that works up to 10% by volume, or 100,000ppm. Perfect for testing CO2 incubators.

The MH-Z16 NDIR CO2 Sensor costs $67.95 and comes with a UART to I2C adapter. This makes powering it and communicating with it quite simple: just connect four wires to the development board of your choice! I used an Arduino Nano to read the values from the sensor and send them to the laptop over the USB connection that was also powering it. The sensor can be calibrated in atmospheric CO2 conditions at the push of a button. I have reservations about this one-point calibration but “you pays yer money…”, as my dad used to say.

The Nano is sitting on a screw terminal shield with mount holes for easy connections ($4 well spent for making secure, non-permanent connections to the Arduino). On the laptop screen you can see data coming in from the example sketch I copied from the vendor’s website and using the Arduino library they provided. The sketch sends a reading every second. The serial output shows a starting CO2 reading of 458ppm which climbs slowly to >1000ppm when the photo was taken. This is because I was sitting in front of the sensor taking the photos and it’s registering a local CO2 increase as a result of my breath. If I huffed on it it climbed to >10,000ppm, or 1% by volume. The sensor is very slow to adjust to changes. I had to huff on it for a good couple of minutes until it got that high. Wikipedia tells me that exhaled breath is normally 4-5% CO2 so I guess if I’d kept at it for a while it would have got up there. I might have passed out before then though! 😆 When I moved it away from me it took several more minutes to come back down to ambient levels. Not ideal but fine for my application and budget!

The next task was to make an enclosure to protect the sensor, logic boards and delicate wiring. A couple of evenings spent playing with OpenSCAD yielded an alpha version which I cut on the lasercutter today and tried out. I had to correct a few mistakes in the design but ultimately produced this:

Which became reality as this:

I would have taken a picture of the two boards mounted inside but the lasercut bits fitted together so snugly I didn’t want to try and separate them again in case something broke. You can see the lights of the Arduino inside through the ventilation holes  🙂

I’ve put the sensor in an (allegedly) working CO2 incubator and left it for 20 minutes to stabilise. The sensor read about 40,000ppm, or 4% by volume. As the incubator was meant to be maintaining 5% I’m not too sure what to make of this. I will have to find another incubator to make a replicate measurement. Maybe all of our incubators are a bit off! 😵 However, I’m very pleased with how simple the sensor was to get working and seeing as all you need to run it is a laptop with the Arduino drivers loaded and a serial terminal, such as PuTTY, it is very accessible to even the relatively non-technically minded. 😎


Using Adafruit Fona GSM/GPRS breakout to log data to Xively

Collecting and logging sensor data automatically is one of the most useful “sciencey” things you can do with programmable microprocessors like Arduino and Raspberry Pi. One problem with this is what to do with the data you are collecting. You can store it locally to the device, for example Arduinos can log temperatures or movement signals from PIR sensors to SD cards. However, you have to turn the device off and remove the card, put it into a laptop and copy the data off it before you can access it. Alternatively you can send the data over a serial connection or wirelessly to another device which will store the data for you for easier retrieval or even real-time plotting. Recently small GSM/GPRS modules have become available which are essentially the guts of a mobile phone with no screen, battery or keyboard. These enable you to use mobile phone networks to send data to the internet using protocols such as GPRS and 3G. This is very useful for science as field experimental sites rarely sport ethernet ports or wireless routers through which to transmit data from long-term monitoring sites.

The Adafruit Fona is one of these devices and I obtained one with the intent of monitoring the temperatures in some of our more critical freezers and fridges in case one of them broke down, imperilling the irreplaceable scientific samples stored within. I’ve already set up one device using a GPRS shield from Elecrow. You can see the feed from it here. Its logging temperatures from three freezers in a shed on AUT’s North Campus where various valuable samples are stored. The Fona is a little smaller and a little cheaper and I wanted to make another couple of devices to deploy in our labs to keep an eye on the freezers there. These contain not only valuable samples but, in our molecular labs, thousands of dollars worth of reagents for the MiSeq, our next generation sequencing platform. The idea here is not just to keep an eye on the functionality of the freezers but to get notified if the power goes down. Freezers are well insulated so if the power goes off the temperature won’t immediately rise. If the power goes off on Friday evening, however…

In setting up the Fona I ran into problems getting code to work. The Fona code examples for GPRS access were limited to simple website recovery and not sending HTTP PUT requests, which is what is required to log data to Xively. Frustratingly the code I’d adapted from my previous GPRS logger didn’t work and I could find no indication as to why. Even more frustratingly the Arduino Xively libraries didn’t work either, probably because they had been written for use with WiFi or ethernet and not GPRS. Instructions are sent to the GPRS shields from your microprocessor using a special set of text commands, called AT commands. These commands need to be sent to the shield over a serial connection. The HTTP PUT requests sent to Xively must conform to something called REST, which is explained in somewhat inadequate detail in Xively’s API docs. So, to summarise: I had to use my Arduino to send AT commands to the Fona over serial that would instruct it to send PUT requests conveying my data to Xively. Three different communication protocols. Simples!  >_<

To cut a long story short I managed to use a Python library to send updates from my laptop to Xively by HTTP PUT requests. Using more Python I then managed to log the text of those HTTP requests and use that as a template that I could deconstruct and send to the Fona from my Arduino, inserting real-time data from sensor reads. The result is the code linked at the bottom of this post, which logs temperature, pressure and humidity data from an Adafruit BME280 sensor as well as the battery voltage of the Li-Ion cell connected to the Arduino and Fona. Credit to Limor Fried/Ladyada and Chip McLelland, whose example sketches I adapted to create this.

Here’s a quick pic of my setup. Its a Feather 32u4 LoRa, a uFL Fona and a BME280, all from Adafruit Industries. The Fona must have a lithium battery connected to it so I’ve added an 18650 Li-ion cell which is connected to both the Feather, which charges it, and the Fona. If the power does go out at work then the device will run for several hours on this battery, even without any low-power optimisation of the code. The enclosure is lasercut from 3mm acrylic. Its a beta version as I’ve yet to add mounts and an enclosure for the BME280. Ultimately I want this sketch to read three DS18B20 temperature sensors too, which will be in the freezers, but that’s a work in progress.


Download this code: Adafruit_Feather_Fona_BME280.ino

Lasercut electrophoresis gel tray & combs

Heather and I made some more tweaks to the design from my earlier post, such as adding tabs to the comb supports to make removing them easier. We also added some gaps to the slots for the combs as the lasercutter’s tolerances are incredibly fine, making parts fit really snugly together. So snug that they’ll slot together quite securely without glue. It looks very cool.


The 1.5mm acrylic that I had ordered from RS came in so we took ourselves off to go and cut it. The tray is 6mm clear acrylic and the comb supports are 3mm black acrylic from an offcut we found under the lasercutter. It looks pretty good assembled. (Yes, we reversed every other comb.) We had a couple of issues with the fit between the tabs and cutouts of the tray base and sides but 5 minutes’ work with a file sorted that out so that Heather could superglue it together.


Heather specified the dimensions of the comb teeth so that she could use a 12 channel multipipettor to load amplicons straight from her PCR plates. I’m going to have to ask her to remind me why there’s thirteen teeth…. ? I design things to be fixable so if one of the teeth gets broken you can simply undo the bolts to replace it.


Heather ran some gels on it today and she was very happy, although it turns out the distance between the combs isn’t quite optimal for her samples so in future she’s going to use every other row to give more distance for the bands to separate. Pics to follow tomorrow.

I will post the laser files here when I get a chance too. Or maybe I should upload them to thingiverse??

Designing and lasercutting custom electrophoresis gel trays and combs.

I’ve been approached by my squid scientist friend, Heather, to make a custom tray and combs for casting agarose electrophoresis gels so that she can run 96 PCR products at a time. Gel combs are pieces of plastic which are plunged into the liquid agarose before it sets to form wells in the solid slab that you can load with the product of your PCR run.


This is the kind of lab gear which costs fantastic sums to purchase from vendors, particularly with any sort of bespoke customisation. Have a look at Thermo Scientific or Sigma Aldrich’s web site, where you will find these little pieces of plastic on sale for around NZ$100 each.

This type of thing is a very straightforward object to design and make for anyone with a few CAD skills and access to a lasercutter. I knocked up a design in about 5 minutes to demonstrate this to Heather and she got very excited and set off to measure up exactly what she wanted.

squid gel comb screenshot from 2016-08-30 20-42-50

Here’s the OpenSCAD code for this object:

translate([0, 0, 13 /2])
cube([133, 1.5, 13], center=true);

translate([0, 0, -3 /2])
cube([122, 1.5, 3], center=true);

for(x = [-6:1:6]) {
translate([x *9, 0, -6.5])
cube([4.5, 1.5, 10], center=true);

Pretty simple, huh?

I have some clear 6mm thick acrylic sheet which I can use for the gel-casting tray and I’m going to order some 1.5mm acrylic sheet from RS to cut the combs from. In exchange for my help I get to post the design here on my blog so win-win!



I’ve sketched out the design and its looking great. I’m going to do a test cut in MDF first to see how it fits together.

Screenshot from 2016-09-03 23-20-20

Bee hive mass logger

11-03-2016: This project hit a problem. See update at the bottom.

Following on from my bathroom scales hack I spent this evening sawing and screwing together the platform for it to sit on. This was an urgent task as the bee hive is being moved to a new location tomorrow and so this would be an ideal opportunity to get the scales in place without having to arrange another movement of the hive. Bees are incredibly precise creatures, I am discovering. If you move their hive a just a few meters away when foragers are out, they will return to the location they are familiar with and won’t be able to find their way home! If beekeepers have to move their hives such a short distance they’ll move them a couple of kilometers away for a week and then move them to the new spot.

The scales will sit on a plywood deck raised off the roof’s surface to provide a thermal insulation gap for the hive. I drew up the rough schematic below to illustrate this.

beehive mass monitor schematic

My final design was slightly different, with the pallet being a little smaller than the plywood shelf. This is because, as well as the four strips of ply that keep the shelf securely fitted over the scales, I also added a plywood curtain around the edge of the shelf that hung down below the pallet to prevent rain being driven in to the scales. The ply is 12mm and very solid. I sized it to be ever so slightly smaller than the footprint of the hive so that it will fit perfectly on top. Here’s some build pics:

shelf (upside down), scales, pallet


scales on the pallet


This next one is upside down but illustrates how the four strips of ply keep the top shelf secure. You can also see the curtains. The notches at the back are for wires to exit.


final assembly



Looking forward to testing this!


Update, 11-03-2016

Unfortunately there is a major flaw with my design. When Matt went to put the bee hive on top of it it turned out to be unstable due to the small footprint of the scales relative to the hive. The hive is only one box at the moment but Matt intends to add another soon and that would definitely make it vulnerable to being blown over by a gust of wind! Not a desirable situation. I am going to have to look at designing a larger platform to the corners of which I can attach the load cells. This is contrary to the advice on the MakeZine blog that inspired this project, however I cannot see any other solution besides buying an industrial-size set of scales. I want to try the DIY option first before I spend a couple of hundred dollars.

Here’s a longer video showing details of the stepper motor I’m using as an alternator mounted in its housing, the hubs and arms and the coupling to the spindle. I’ve left the third side off the housing to show this. Obviously the whole thing is going to need a coat of paint before I can mount it outside.

I’m really pleased with how much of this turned out. The frame, the spindle and the arms all look just like the CAD design. 


The only difficulty I had was with the design of the foils. My initial design was a Darrieus type VAWT with an airfoil at the end of each arm. I cut the foil profile out of the same 6mm MDF I used for the arms and hubs and intended to cover them with corflute. I happened to have a bunch of Green Party electoral billboards left over from last year and wanted to upcycle them for this purpose. 

The first design I had was for a simple symmetrical foil made from the intersection of two elipses. 


Once I’d cut the parts I realised they were far too big so I decided I had to cut a scaled down version. Out of curiosity I found myself Googling “formula for an aerofoil” and stumbled across a Wikipedia page with an equation to produce a proper NACA aerofoil profile. 


This seemed like a much more pleasing solution so, with an eye to minimising waste, I cut them out of the first foils. You can see how big the first ones were in comparison.


The two holes are 6mm diameter ones which I thought would be useful to help me align the foil profiles between the two arms. I envisaged running 6mm dowels between them to stabilise the foils, which are 0.3m long and would flex if they were just corflute, and to help keep the arms in alignment.

As it turned out 6mm dowel was pretty whippy too and so I bought 8mm dowel thinking I could just drill the holes out. When I got home I had a go at wrapping the corflute around the foil profiles and quickly realised that corflute doesn’t like being bent to smooth, small-radius profiles such as those in the new design. To be frank, I’d probably made the new profile too small! There was no way I was going to get five consistent foils and consistency is pretty important in something that you hope is going to rotate at hundreds of rpm. Imbalances will result in vibration and damage, paticularly in a kludged-up design like this. 

So I swallowed my pride, dug around in the garage for a bit and turned up a piece of 55mm PVC drainpipe that I could cut into half-tube sections to turn my elegant Darrieus VAWT design into a clumsy and inefficient Savonius VAWT. Feel free to read up on the differences in the two designs. In brief, the Darrieus type relies on lift from aerofoils, whereas Savonius types utilise drag from buckets to produce torque. The former produces higher tip speeds and faster rotation, which is more efficient for generating power. As a result Darrieus turbines usually capture more energy from the wind than Savonius types, although each have their advantages. The 55mm pipe is probably too small for this and I’d see a lot more power from a bigger section but it was all I had to hand. For this reason the buckets are only zip tied on so I can easily replace them with larger ones in the future.

Anyway, this at least allowed me to finish assembling the pieces and to pop it out on the deck to grab the passing breeze. I’m really happy that the thing turns smoothly on the two 608 bearings as these are meant to be rotational bearings, not thrust bearings. For this reason I left the second set of bolts out of the hubs to minimise weight. They did not seem necessary for structural integrity, thanks to the close fit of the lasercut pieces. 

The stepper motor lined up beautifully and, after a little grinding with the Dremel the coupling was a perfect fit. I haven’t checked the voltage coming off the stepper yet. I used the smaller NEMA16 rather than the NEMA23 as the bigger stepper just seemed to require too much torque to turn the shaft. These aren’t really very well suited to this application but this is just a fun project and I’m not aiming to generate any significant power. My main goal here is to get an idea of the potential of this design. 

One challenge left, besides the big question of where to mount it, is what to do with the output. The stepper will produce 4-phase AC which will need to be rectified and fed into a battery. I’m contemplating building my own rectifier from Schottky diodes scavenged from old PC power supplies but it is probably quicker, more reliable (i.e. smarter!) to just buy four bridge rectifiers. I’d also like to use an Arduino to log things like the rotation speed- easily derived from the AC frequency- and the voltage and power generated. Although this thing is essentially a great big anemometer it would be interesting to get another proper one to ground truth my measurements. All fun for the future!