Raspberry Pi Temperature Sensing
2016-11-21 10:07 By Jason Birch
Using the RPiSPi Multi SPI device driver to measure temperature via an MCP3008 A-D converter. Measuring temperature with an MCP9701A temperature sensor and 100K thermistor.
HARDWARE
A two channel A-D converter, MCP3002, is used to provide an interface between the two types of temperature sensors used and a Raspberry Pi.
There are a few items to note about the hardware set up. The fist is that the CS pin on the MCP3002 is connected to GPIO 7 (CE1), when the RPiSPi driver is used without a multiplexer, the CE1 pin is used as opposed to the CE0 pin. And the CE0 pin is left unconnected.
A 1K resistor is used in the MISO line, as when the Raspberry Pi is not configured to use the SPI interface, the MISO pin could be an output, as the MISO pin on the MCP3002 is an output, this could cause damage without a resistor placed in the line.
A 10nF capacitor is placed from 0V to the signal output pin on the MCP9701A and another 10nF is placed across the 100K Termistor. This is to remove any noise which may be present when reading the sensors.
A 100K resistor is placed in series with the 100K Termistor and 3V3. This forms a potential divider, and provides a varying voltage to the A-D converter, relative to the current temperature.
SOFTWARE
In order to access the MCP3002 A-D converter from Python on the Raspberry Pi, an SPI device driver is used. The device driver is available to download and install here: RPiSPi Device Driver. The documentation at the link describes how to install and use the device driver.
The example Python source code is available at this link: GIT Source Code Examples. The Python source code is described at the end of this page.
ANALYSIS
I needed to use a number of temperature sensors in a project, so I wanted to do an isolated experiment to get to know temperature sensors. Using a MCP3002 A-D converter via my previous SPI driver project, to provide two A-D channels and compare a temperature sensor IC with a thermistor type of temperature sensor.
Using Python to put together a simple graphing application and compare the two devices. The MCP9701A temperature sensor IC is fairly accurate and very linear. The linearity is it's most valuable attribute. In comparison the thermistor is not accurate and very non linear. This immediately puts the IC into the use of accurate measurements and thermistor for very considered use where the accurate measurement is not important. The IC is a better option for many reasons, faster response to temperature change, more accurate reading, easier to use, accurate from device to device and batch to batch. The thermistor on the other hand has the advantage of being very cheap, in my case £0.38 for an MCP9701A and £0.20 for a 100K thermistor.
The termistor however is not beyond use. It's reading accuracy can be improved immensely in several ways. However, even after correction, the readings can still vary between device, manufacturer and batch.
So some of the correction methods which can be used are:
Using a look up table to convert the readings made from the termistor, into accurate readings. This is probably the most accurate method for correction. The reason not to use this method might be, when using a micro-controller memory is very limited.
Using a look up table of points at which the thermistor curve is mid corner and points at each end of the curve. Interpolating the result between the closest points to the measurement. Not so accurate as a full table of values.
Using a mathematical curve to convert the values. This is pretty accurate, but you have to be very good at mathematics to achieve this fully. In the example images, to the right, this is the method I used as an experiment. In the top right of the graphs is my attempt to create a curve using a sin wave. The red line at the top is the curve I generated; The blue line is the uncorrected thermistor values per degree; The green value is the MCP9701A readings per degree, using as reference values; The red line at the bottom is the difference between the green and blue lines; And the white line is the corrected values. As you can see at the bottom of the white line my curve does not correct the values very well. I am certainly no mathematician. The main part of the graph has three lines; The green is the MCP9701A reading; The dark blue is the uncorrected thermistor and the light blue is the corrected thermistor.
In micro-controllers I would probably use multipoint interpolation. And in the Raspberry Pi I would use a full table of look up values.
EXAMPLE
The following example has been written for the example configuration of the RPiSPi driver provided, Device.cfg. Before running the example issue the following command to configure the RPiSPi driver and verify the configuration. Alternatively the /etc/RPiSPi/RPiSPi.ini configuration file can be updated if the change is to be permanent:
cat Device.cfg > /dev/RPiSPi
cat /proc/RPiSPi
Download examples in a command terminal:
wget https://github.com/BirchJD/Raspberry-Pi-Python-Interfacing/archive/master.zip
unzip master.zip
cd Raspberry-Pi-Python-Interfacing-master/RPiSPi/RPiTempSensor
The example is written in Python demonstrating monitoring both temperature sensors and displaying the temperature change over time. Near the top of the source code are the constants PLOT_HOURS and PLOT_CELCIUS, which can be altered to change the graph axis max scale:
sudo ./RPi_MCP3002_TempSense.py
|
|
|
|