Bernie, a crested gecko

This is Bernie, she lives with me in a vivarium, monitored by an Arduino.


You see, Bernie is a Crested Gecko (Crestie), and unlike most lizards that are kept commonly as pets, Crested Geckos need a mixed environment to ensure that they can comfortably survive in their habitat. Most lizards, such as bearded dragons, require sand, a heat lamp, ultraviolet light, and perhaps a small, covered space to hide in to help regulate their temperature.


We do not know a great deal about Cresties, and we are still learning about them, such as how long they live, what their habits are and how to care for them properly. Unfortunately this means that there are a lot of people, including pet store owners, who incorrectly care for Cresties. Meaning that they become deformed and poorly treated because of the lack of information and assumptions that they're the same as other lizards.


This lack of knowledge is caused by the fact that we thought Cresties were an extinct species, until they was rediscovered in 1994 to still exist on an area of land called New Caledonia.


For a period of time, Cresties were exported to America and Europe, where they were bred in captivity and ultimately became pets. Cresties are pretty well established now and their export from New Caledonia was put to an end, but that does not mean you cannot pick up a Crestie as a pet for yourself.


Cool! Or, Not?


New Caledonia has an interesting climate, being south of the equator and near Australia should give you some idea of the type of tropical environment which Cresties live in naturally. It is a place of high humidity, temperatures up to 29 degrees centigrade, and as low as 16 degrees centigrade, depending on the time of day, the time of year, and whether or not you are in a cool breeze and in the shade.


It is this kind of environment which we need to recreate in a vivarium to keep the Crestie happy, so that it is able to clean itself, keep hydrated and to shed its skin when necessary.


There are also other considerations to be made for the little virtual environment, such as ventilation / airflow to ensure that on occasion, the vivarium dries out a little to help prevent mould and other unwanted bacteria from growing. It is also sensible to clean out vivariums from time to time, and some people have their setups created as small ecosystems. With insects living in the substrate (usually consisting of a mixture of soil and/or orchid bark) alongside the crickets and soft fruit mix that the Cresties feed upon when the fancy takes them. So if your vivarium is intended to have living plants and animals inside along with the lizard, they too are something to take into consideration.



Taking Care of the Little Gal, Manually

Monitoring the habitat and maintaining it properly, for a time,  are all aspects of looking after the little gecko that we acted on manually each and every day as best we could. This was only possible to some extent, due to work we were not sure what was happening with the vivarium during the day. There's also the environment outside of the vivarium, depending on which room it resides greatly affects how warm or cold it gets, how quickly it dries out and how long it takes to do each of these.


The vivarium we use is long, stood vertically, since Cresties prefer to climb up and down trees and, as it turns out, burrow into the substrate to lay their eggs (even if they haven't had super fun times). The enclosure is monitored by two temperature sensors, one at the bottom of the tank, and one at the top as it is usually best to maintain a temperature gradient in the tank from cooler at the base to warmer at the top.


A hygrometer helps to keep track of the humidity in the tank, with a spray bottle simply used to keep the humidity up when it begins to get too low, too often. Eventually we tired of the bottle and found the funds to buy a misting system, which is basically a 12v water pump and a set of piping.


I live in the UK, and it is not always warm here which means some applied heat is necessary. The vivarium came with a light bulb, and we supplemented this with a heat mat and a ceramic bulb pointed at the glass tank. These are all controlled by a thermostat because doing this part manually, is not sensible in any way. Though these heating elements are not connected to the temperature sensors they are used as guides to ensure the settings on the thermostat are correct, it is also time aware because one of the power inputs can be connected to an on/off timer socket which works surprisingly well.


This setup has its limits, in the past few years the ambient temperature at summer time has been increasing, at least in the United Kingdom, and we had some difficult moments when the temperature was trying to exceed 29 degrees centigrade. Bernie didn't like this, as she was yawning a lot to try to cool down, and burying herself in the damp substrate. There's also no active ventilation, and another difficulty is finding a way to maintain the humidity properly as a spray bottle can become very insufficient very quickly, especially if it's a cold day and the heating elements are drying up the tank.


Introducing: Automation

When I finished University in 2010 I spent a lot of time hanging out with friends whom were studying their undergraduate degree courses in Electronic Engineering. It was here where I was introduced to micro-controllers and that sparked an interest in me to learn more about them and do more with them. My friends were mainly using PIC32s in the labs, or some equivalent, soldering on perfboard/protoboard/veroboard with serial interfaces to the computers. I'm not sure what it was that sparked my interest, I guess it was the idea of control and the sense of being able to monitor your surroundings and react to it, my dissertation supervisor also lamented at the time how they wanted to actually do more with them in light of computer networking. My how the times have changed in only a few years.


it wasn't until a couple of years later that I encountered Leeds Hackspace where JonS introduced me to an Arduino Uno and how to do the 'hello world' of programming the device at the time, blinking an LED. From that first example I started to collect together sensors, servos, and focused on getting each individual component working, wondering when I would have a reason to combine them together to create something sensible, and then along came Bernie.


Crested Gecko amongst Wires

Bernie likes hairEvery time Bernie is let out of the vivarium she goes for a wander up and around it, and (yes the power is disconnected) she enjoys crawling through the wires up against the ArduinoArduino that I currently have setup for the start of the monitoring and control project, we think that she would probably enjoy some more vines inside the vivarium as she also love to crawl into/up hair as well.


So the plan, use an Arduino, or a couple of them, to control fans which would help to ventilate and also cool the vivarium when needed, it would decide when to do so by monitoring the temperature with DS18B20DS18B20s and react to temperatures outside of the desired parameters as appropriate.


The next stage of this development would be to also control the misting system. This would be governed by a sensor, which at present is a DHT11, which is attached to the Arduino to detect the humidity level for when it gets too low, I considered misting the vivarium on a timer also, however this was attempted before all of the electronics and the tank was almost flooded (note to self, consider drainage for the vivarium tank). So, a humidity sensor and some intelligence on the actions is better. Since the Arduino can run from 12 volts from the VIN/RAW thanks to its onboard voltage regulator, I can power the fans, the misting system, and use mosfets between the logic input/output pins and the power for the water pump, while also using the 5v sensors. The fans would have to use pulse width modulation (PWM), using typical 120 millimetre fans designed for cooling a computer case works perfectly for this and happens to be the right size for the top of the vivarium, I could even monitor the speed of them as well, and use a potentiometer as a manual over-ride to control the speed if I didn't want to rely on the automatic logic coded into the chip. In fact, here's a handy fritzing:


vivarium monitor

The barrel jack on the left is the 12 volt in, while the barrel jack on the top right is to the misting pump/system, as it is purely a water pump in a box, connected up to a wall wart.


I was surprised to discover that Fritzing was a pain to use. Especially if you want to use parts that do not already exist in its parts library and forcing you to create new parts. To do so you can't create a new one from scratch, you have to repurpose an existing one and edit it. Trying to create a printed circuit board layout from your breadboard designs is even more annoying, to me at least. Fritzing kept crashing when trying to perform its autorouting and lost the circuit components. Good thing I had a backup.


As an extension to this design though, I could also take over the control of the heating elements from the thermostat that's currently in use. I would have to add additional circuitry to make it aware of the time of day and to maintain this without slipping out of synchronisation with real-time, though the current thermostat has been running for so long, reliably, it is a sufficient constant that I can rely on it to maintain heat.


What I really should look into though are methods of cooling the tank, especially since the ambient temperature in the UK has managed to hit temperatures of 32 degrees centigrade. I considered using a peltier, and they are an idea although they are horribly inefficient, plus I would likely have to actually cool something like the water reservoir for the misting system rather than the vivarium itself directly.


It's also worth noting that these components are not all sat on the breadboard like in the fritzing diagram, in fact, I had to find a way to encapsulate the DHT11 humidity sensor so that I could put it into the vivarium safely. So, enter an amount of overlapped heatshrink and a bit of well enveloped hot glue, and also practicing something I have never done, braiding:


IMG_20170808_223341.jpgIMG_20170808_223351.jpgI'm sufficiently pleased with how they turned out, and it's not the type of thing which Bernie will try to eat on, unless it moves in the dark. She'll probably use it to shed against if anything. The DS18B20 is similarly protected (though I bought it like this rather than making it up myself) :




Fritzing aside, the Arduino IDE steps up for programming the device and using the OneWire example as the basis, we have a very rudimentary program that can monitor the temperature input from the DS18B20 and output the PWM signal required to control the fans, here is a snapshot of the code from my github repo:



There's a lot of rough approximations here, and in fact this is only one version of the code and doesn't really take into account the potentiometer properly, yet. I had a hard time using the Arduino 'map' function to try to get it to control the fans in a rational manner (since they're 0-255 output for speed, and the potentiometer is 0-1023) it wasn't quite working properly (never reaching 100% speed and never going slow enough, input welcome).


I also haven't connected up a humidity sensor yet or control for the misting system, this is mainly held back because of my concern for not putting soldered contacts directly into the vivarium, I require a humidity sensor that is properly enclosed for two reasons: 1. to ensure that it doesn't short out and 2. So that Bernie doesn't mistake it for food and try to chomp on it and hurt herself (she jumps at shadows). So I'm still searching, else I'll probably heat-shrink wrap a DHT11.


Trying not to let the Magic Blue Smoke Out

I did once. Well, that's entirely not true for the Arduino, I have actually killed one of the DS18B20s I'm using, and I broke off the micro-usb connector from the Arduino. When I first setup the Arduino to monitor the terrarium's temperature that felt like the easy part. Buying the sensor and using the example OneWire code pretty much set that as 'done'. The hard part was buying powerful and appropriate fans to cool the vivarium and get the air flowing. I also wanted something which wasn't going to make too much noise, basically if you're using fans you're powering motors, and the smaller the fan, the more annoying and high pitched the noise is going to be. Now, I'm not sure what the tolerance to noise is for a Crested Gecko, but I'm sure that I get fed up of them quickly, plus we want to make sure we're moving a good amount of air.


At the top of the vivarium there is a removable panel which has a metallic mesh to allow heat and airflow. This is perfectly designed for the "Exo Terra" range of vivarium additions such as the heating lamp we currently have covering half of the panel. The other side was available and happened to be perfectly sized at approximately 240 millimetre by 120 millimetre, so two fans it is!


The fans have three connections, power, ground and signal, and most importantly they operate at 12 volts. Aside from the signal connection, which offers up the speed of the fan using PWM and I believe has a built in resistor on the fan to provide this at 5 volts.


The arduino however, operates at 5 volts logic, and will happily take 12 volts. So this meant I needed a way to control the fans via PWM at a voltage which the Arduino was happy at. There are some examples out there where you can solely use a 10kohm resistor connected to the power lines of the fans to control the speed. However I didn't want to do this (because learning), there are sensible reasons why not to, part of my concern for example, are power spikes that usually happen from turning motors on and off (diodes can also help with this). Relays are sometimes peoples choice, but they are too slow for PWM, they really don't like being switched on and off quickly. So my choice in this matter was the MOSFET.


The circuit for the MOSFET is relatively simple. I connected the 12v line to the Arduino and also to the 'positive' of the 120mm fans, and the 'negative' (ground) to one side of hte MOSFET. The other side of the ground is connected to the ground/GND of the Arduino. The third, centre leg of the MOSFET, utilising a pull down resistor (to prevent it floating if nothing else) is connected to an input/output logic pin on the Arduino, and it's this PWM pin which controls the fan's speed and overall provides enough protection to not set fire to the Arduino.


I haven't worked out the code properly to read the fan speed signal from the fans, though. I think I haven't quite got the synchronisation or the maths correct to make sense of the signal coming from the fans. It's important to have this feedback though to ensure that the fans are working, and to be able to alert if there's a problem with them.


I did consider having variable fan speeds, that would scale with the temperature detected, so that at lower temperatures there was still some airflow happening, and when it was getting too hot we put the fans at full speed to "yep, this needs cooling down, right now". The code for this was bad, and it was lagging the microcontroller so that the reaction time from temperature to fan speed was ever increasing (this is not a good idea). I will need to refactor the code so that it is interrupt based, and I changed the code so that it merely goes "okay, we're too hot now, turn the fans on max and cool down as quickly as possible", this has in truth worked reasonably well. So I'm not sure of the merit of the other method, aside from prevention of it getting to the 'it's too hot and we've made sure it doesn't get that high at all'. So I may reconsider it.


There is also some concept of circuit protection in the design, you see, when you're controlling motors and they're spinning up/down, you may be pushing electricity to them, however motors are also like miniature generators that can create some power themselves, and this power is known to spike higher in voltage than what they're rated at. So, to protect against this, we put diodes in the circuit to prevent the spikes from going back to the controller circuitry, and instead force it to run across the motor again. Which is somewhat safer.


Simulating Environments and Ensuring Stability

Once I've finally sorted out the code, the sensors, and the temperature control though I considered that I could effectively emulate the exact environment of New Caledonia. Since we can actually pull near real time information about the weather in the environment, we could use intelligence gained from this, historical and otherwise, to simulate the environment that the crested gecko should be experiencing. Creating almost a home away from home.


To do this properly, and also to keep track of whether or not the Arduino has been behaving properly I would likely have to introduce more hardware. Perhaps the illustrious Raspberry PiRaspberry Pi, providing its functionality as a Linux based computer that is connected to the internet. Now with feedback from the Arduino, based upon the fan speeds, humidity, temperature and compared to the weather in New Caledonia, or even the expected temperature in the UK, the system would know to prepare for changes in the environment which are coming up and to adjust accordingly.


Everyone loves graphs, right? Well I should be able to log the data over time and see how well the environment is simulating the environment compared to New Caledonia, for example here's some of the debug output from the code I've written so far:




For a while with the vivarium setup, several years ago in fact, we needed to buy an uninterruptible power supply (UPS) to ensure that the vivarium thermostat was able to maintain the temperature. We had a lot of power cuts. Most UPS devices actually come with USB connectivity, allowing detection of when you're running on UPS power, the state of the batteries, such as their temperature, charge level, whether or not they're running a maintenance cycle. I think this could also fit in well with the combination of a Raspberry Pi with the Arduino setup.


Happy family


Still Work to Do

I'll admit that progress on this has been somewhat slow, but progress has been made all the same. Hopefully this write-up will help to inspire your next Arduino project, or perhaps you've been considering setting up a vivarium system yourself.


I'm open to ideas or improvements to work on this, and I've had suitable motivation to progress because Bernie is practically part of our small, happy family, and at 5 years old will likely still be around for at least another 15.