Posts Tagged ‘AVR’

Butterfly Logger Firmware 0.30C (bugfix)

Saturday, January 22nd, 2011

Last week I released another firmware update for the AVR Butterfly Logger. This was a bugfix to the thermistor routines for calculating negative temperatures from the onboard temperature sensor.

You can download the source code from SourceForge.

The thermistor routines in the logger project were adapted from Martin Thomas’ port of the Atmel AVR Butterfly example code. This routine compares the ADC input from the thermistor sensor to a table of known readings. The table () contains a single entry for each degree celsius from -15°C to +60°C. The routine would simply step through the table until the ADC reading exceeded ( negative temperature coefficient (NTC) device. This means as the temperature increases the resistance drops. In our electrical configuration of the sensor this also applies to the voltage seen at the ADC input.>) the value in the table. The position in the table is then given as the temperature in degrees celsius.

When first developing the data logger in 2004 and 2005 I quickly wrote an interpolation routine that would extend the integer value given in the original with an extra two decimal places. After finishing writing the code I reviewed both the code and the corresponding output. At the time of that brief review everything appeared to make sense. As the original project had other more accurate temperature sensors available, the operation of the onboard temperature sensor was not critical. The onboard temperature sensor was instead intended as a fast secondary sensor for live feedback to the user via the LCD and so the code was not widely tested. Whoops.

This December (being the coldest on record in the UK for the last 100 years) showed up some of the flaws in my code. After some prompting from a couple of users of the project I set aside some time to investigate further. My investigation involved writing a test routine to simulate all possible sensor readings and then evaluating the results. This was an incredibly simple exercise, just a few lines of code and then plotting the results with Gnuplot. The output from the function is shown below in Figure 1.

Figure 1: Thermistor routines test output

This figure shows the full-scale results for the original and fixed routines along with the data points given in the original source. As you can see the interpolated line appears to follow the datapoints quite nicely until it drops below zero degrees.

At this point the linear interpolation is way-off and producing mostly garbage (you can see this clearly in the close-up below in Figure 2)

Figure 2: Close-up of the thermistor routines test output

Looking at the close-up in Figure 2 you can see the awful job the routine does when dealing with negative temperatures. If you look very closely at the positive temperatures you will also note a small error in that calculation as well. After plotting the output and going over the code it was very easy to spot the couple of errors. After the corrections the graph of the output (The green line in Figures 1 and 2) intersects perfectly with the data points given in the source code. If it were not for visual inspection of the results (via the graph) I don’t think I would have noticed this second error at all.

Further changes were also made to the routine so that when the readings are outside the range specified by the data table then they will record an over or under temperature error. This is signified in the log as +++.++ or ---.-- for over temperature or under temperature respectively. On the LCD, +++.+C or ---.-C is displayed. Previously the routine had just returned the maximum or minimum temperature from the table.

It had been at the back of my mind that there was something not quite right about that bit of code for some time. It was so simple in the end to write a routine to test the function with all possible inputs that I could kick myself for not doing it earlier. By plotting the results on a graph it became obvious how the code was misbehaving. The key lessons I’ve learned from this it is that a little testing goes a long way and that some simple visualisation can really help you understand a bug quickly.

More information about the AVR Butterfly Logger project can be found at the project website.

Any questions can be asked in the project forums.