3

I am attempting to make a temperature monitor based on these links:
1. https://bitbucket.org/pschow/rpiadctherm/src/dbfe8101eeb4/basiclogmcp.py?at=master
2. https://www.paulschow.com/2013/08/monitoring-temperatures-using-raspberry.html

Using a MCP3008 and a thermistor. I have enabled the SPI interface, installed spidev, and I have rewired my circuit several times using different wires. I have also used a voltmeter to test connections and I am getting voltage across my voltage divider. I have a wire connected between the thermistor and resistor to Channel 0 on the MCP3008. I get this error when I run my code:

%Run Thermistor.py
Traceback (most recent call last):
  File "/home/pi/Desktop/Thermistor.py", line 54, in <module>
    print(temp_get(0))
  File "/home/pi/Desktop/Thermistor.py", line 23, in temp_get
    ohms = ((1.0/volts)*3300.0)-10000.0 #calculate the ohms of the thermististor
ZeroDivisionError: float division by zero

I want to see if I am even getting any output from the MCP3008 (like you can on arduino). My code is the following:

import spidev
import math
import time

spi = spidev.SpiDev()
spi.open(0, 0)
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def readadc(adcnum):
# read SPI data from MCP3208 chip, 8 possible adc's (0 thru 7)
    if adcnum > 7 or adcnum < 0:
        return -1
    r = spi.xfer2([1, 8 + adcnum << 4, 0])
    adcout = ((r[1] & 3) << 8) + r[2]
    return adcout

def temp_get(adc):

    value = readadc(adc) #read the adc

    volts = (value * 3.3) / 1024.0 #calculate the voltage

    ohms = ((1.0/volts)*3300.0)-10000.0 #calculate the ohms of the thermististor

    lnohm = math.log1p(ohms) #take ln(ohms)

    #a, b, & c values from http://www.thermistor.com/calculators.php

    #using curve Z/D (-4.4%/C @ 25C) Mil Ratio B

    a =  0.001124753301019

    b =  0.000234813406978

    c =  0.000000085355035

    #Steinhart Hart Equation

    # T = 1/(a + b[ln(ohm)] + c[ln(ohm)]^3)

    t1 = (b*lnohm) # b[ln(ohm)]
    c2 = c*lnohm # c[ln(ohm)]
    t2 = math.pow(c2,3) # c[ln(ohm)]^3
    temp = 1/(a + t1 + t2) #calcualte temperature
    tempc = temp - 273.15 - 4 #K to C
    # the -4 is error correction for bad python math
    #print out info

    print ("%4d/1023 => %5.3f V => %4.1f Ω => %4.1f °K => %4.1f °C from adc %d" % (value, volts, ohms, temp, tempc, adc))

    return tempc

while True:
    print(temp_get(0))
    time.sleep(1)
tlfong01
  • 4,847
  • 3
  • 12
  • 24

2 Answers2

3

For testing if you drop the bit rate to 100 kbps or so there are a number of methods.

You could use my piscope to see the data being passed between the ADC and the Pi.

An alternative is to use a script to monitor the SPI protocol.

Or just watch the GPIO change state with monitor GPIO.

joan
  • 71,852
  • 5
  • 76
  • 108
1

First, your equation to get resistance from voltage is probably wrong. Voltage goes on top. The general equation is "E=IR" or "Voltage = Current * Resistance".

Second, your voltage should almost certainly NOT be zero. Investigate that.

David G.
  • 225
  • 1
  • 2
  • 7