Saturday, May 23, 2015

JAVA Analog - Digital Thermistors with Raspberry Pi

Hello there,

One more lesson from the "Sunfounder 37 modules Sensor Kit for Raspberry Pi Model B". This one was a bit more interesting. Digging down into what is a Thermistor

Analogue Thermistor KY-013
This kit comes with two thermistor sensors, one is the module KY-013 and the other is a Thermistor Analog + Digital Electronic Temperature Sensor. There is a difference in the value generated if connected to 5V. For the formula that I used to calculate the ambient temperature I found it most suitable to use 3.3V.

Well here the fun was in getting an actual temperature value. The original C code from the files "analogTempSensor.c" and "analogTempSensor2.c" is working fine, however I still do not understand what the values represent. Neither in the code or in the documentation explains what is going on. All what this C code does is to read the value from the ADC0832 and substract 60 or 140. Well, as I said, I am still scratching my head around this code.

Thermistor analog + digital
So I decided to do a little bit of research to understand what is a thermistor and after a bit of testing I find out that the KY-013 module is rated as 10K resistor. The module acts as a voltage divider and the ADC0832 value will represent a voltage value between 0 and 255, due to it is an 8-bit IC Analog to Digital Converter. The key part to understand is that this value can be mathematically approximated to a temperature value by using the Steinhart-Hart equation.



Analog pin reading running Ex11_01

Based on a couple of forum discussions here and here and the NTC thermistor datasheet from Murata, I managed to get temperature readings quite close to my home thermometer. For full details on the mathematical calculations and values used in the Steinhart-Hart equation please see the code in the Ex11_01_AnalogTempSensor. Or write a comment below if you have any question and I will do my best to remember what I did.

This sensor is very sensitive so I let it run for a few second or minutes to test if the temperature reading was actually adjusting to variations. So a lot of window open and close events happen during testing ;-)

The Ex11_02_AnalogTempSensor is a simple implementation to detect the pin high or low value. The threshold is adjusted using the blue potentiometer. Running the first exercise code  Ex11_01_AnalogTempSensor is an easy way to adjust the potentiometer to the desired threshold value, because the temperature readings are printed on the console.


Digital pin reading running Ex11_02


For full details about how to set up your Raspberry Pi and how to run these exercises please check out my old post Raspberri Pi with Pi4j and Junit testing mockups. Or leave me a comment if you have any issue or question.

That is all, I hope it helps
Thanks for reading
Keep on hacking

Posted by Marc Andreu.

No comments: