The internal Arduino temperature sensor...

OK so I've probably already surprised many of you.  Didn't know there's a temperature sensor built into an arduino?

Regular readers will be familiar with the chip in the Arduino UNO, the basis of the Swift for Arduino product, being the Atmel Atmega328p. This contains an internal temperature sensor that's hooked up to the same MUX that feeds the analog to digital converter, that allows you to read analog voltages on pins A0-A5.  In practical terms, that means you can read the temperature from this sensor and by extension roughly the temperature of the arduino by programming it correctly.

On some platforms this would be a complex process, on S4A... not so much...

Here's the code...

let temperature = slowReadTemperatureCentigrade(tsOffset: 335, tsGain: 1.15)

Yup.  That's it!

What are the magic numbers for? What are the caveats? Why can't I do this on other Arduino platforms or why is it so difficult??

Well, the magic numbers and the caveats... I'll come to in a minute (see the small print after the video).

Why it's not on other platforms... do you really need to ask this now? You know I've got your back. ;-)


There are a few things to bear in mind about temperature sensing and these apply a lot to any analog read too. As far as you and I are concerned it looks pretty instant... something like 0.1ms to 0.2ms. But in microcontroller terms that's important. So the name warns you it will be "slow" and as an alternative there's an interrupt callback async version if you really need it (ask us and we'll give you the details).

Also as an extra caveat on S4A, the "reference voltage source" is different between analog reads and temperature reads, meaning if you do one then the other, you might get a gnarly reading or two as it transitions.  The advice is usually if you don't want to get into the details, do one or two readings of the thing you want first, discarding the result, before the reading you want.

So if you're super paranoid you might do...

let _ = slowReadTemperatureCentigrade(tsOffset: 335, tsGain: 1.15)
let _ = slowReadTemperatureCentigrade(tsOffset: 335, tsGain: 1.15)
let temperature = slowReadTemperatureCentigrade(tsOffset: 335, tsGain: 1.15)

And you should be safe.

Here's the video.  Any questions, contact us on Twitter or write feedback here.  Have fun!







The small print...
What are the magic numbers? What are the caveats?
There are a few! Firstly, the sensor will probably not be much use to you unless you calibrate it! This is a pretty big caveat. Differences between manufactured chips are fairly large and Atmel do not provide strictly calibrated sensors. To calibrate, change tsGain to 1.0 and measure the temperature that's output compared to the actual temperature of the chip (using a laser temperature reader from Amazon is a good approach, $10 or so).  Then use linear regression to figure out tsOffset and tsGain.  That's what the magic values are. I've put in the values I got from calibrating one of my own Arduino UNOs just to give you a rough start.

Simpler, less accurate approach to calibration... just use the values I've provided and see how the temperature read compares to the real temperature a bit, then add a "fudge factor" by eye.  It will probably be good enough for most uses.

If you want a more accurate temperature, I'd suggest you get an external I2C sensor or similar, e.g. https://www.banggood.com/buy/i2c-sensor.html

Comments

Popular posts from this blog

Halloween LED lights on a plastic trick or treat cauldron

All about bootloaders

Pushing the limits