0
votes

Trying to read and convert an NTC 10k B=3950 sensor with an AD/DA PCF8591 converter using Raspberry PI using python. The wiring as it shown in this picture (except the AD converter, couldn't find one for my PCF8591) : wiring

Using the following python code :

[12:45:03] openhabian@openHABianPi:~$ cat read_pcf8591.py
#!/usr/bin/env python
import math

from smbus import SMBus
bus = SMBus(1)

print("Read the A/D")
print("Control C to stop")

bus.write_byte(0x48, 0) #set control register to channel
last_reading =-1

while(0 == 0):
    reading = bus.read_byte(0x48)
    if(abs(last_reading - reading) >2):
        milivolts = reading*(3300/1024)
        degC = (milivolts - 500) / 10
        print("Temp: " + str(degC) + " Reading : " + str(reading) + " milivolts: " + str(milivolts))
        last_reading = reading

This giving me the following results (I'm heating the sensor with my hand) :

[12:43:23] openhabian@openHABianPi:~$ ./read_pcf8591.py
Read the A/D
Control C to stop
Temp: -10 Reading : 134 milivolts: 402
Temp: -9 Reading : 137 milivolts: 411
Temp: -10 Reading : 134 milivolts: 402
Temp: -11 Reading : 131 milivolts: 393
Temp: -12 Reading : 128 milivolts: 384

Apparently the converting to Celsius is wrong, how I can convert properly to Celsius ? Have read about Steinhart-Hart formula, but have no idea (also my knowledge is limited, I'm a beginner) how to use ... Also some docs are pointing to calibration, again no clue about ... Would be very appreciated if someone can help me or point to a right direction.

Thanks! Best regards,

1

1 Answers

0
votes

The math here is not straightforward -- it might be worth searching for whether somebody has already done it, and given you an equation in the form "T=blah(V)", where T is the temperature you want, and V is the voltage you measure. It will be a nasty equation, if it exists, because the potential divider you have creates a non-linear relationship between resistance and voltage, and the thermistor has a logarithmic relationship between temperature and resistance. You can write an expression using just ohms law that gives the ADC input voltage in terms of the thermistor resistance, and you can use the Steinhar-Hart expression to get the thermistor resistance in terms of temperature. So it's easy enough to substitute the second in the first, and get an (ugly) expression of the form "V=blah(T)"; but you want "T=blah(V)"

If I couldn't find somebody who had already done this, my inclination would be to enter the V=blah(T) formula into something like Maple, and let the magic of computer algebra give me the T=blah(V) version, if it exists. It might turn out to be a calculation you could do with pencil and paper -- but I haven't tried, and I wouldn't bet on it.

An easier approach, since you'll probably have to calibrate the system yourself anyway, is to accept that you will need a solution based on measurement. So my approach would be to dangle the thermistor in a small container of water, and heat it, whilst at the same time measuring the true temperature using a thermometer and the number read from the ADC device by the Pi. I would do this over as wide a range of temperature I care about -- it's easy to do from zero celcius to 100 celcius, just starting with crushed ice and gradually boiling it. You'll end up with a table relating temperate at intervals of, say, 2 degrees celcius, to Pi register reading, over the whole temperature range of interest. You can then write some code that interpolates between nearby points in your chart, when the Pi register reading does not correspond to an exact point in your table (it rarely will). There are various ways to do this interpolation, depending on the accuracy required.

This is really a question about electrical engineering and math, and you might get a better answer from a forum that specializes in these things -- the actual programming is trivial when you have the math sorted out.