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>
  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:
  • 2
    It is far from clear what this convoluted code is supposed to do, but it seems to assume that voltage and resistance are inversely related which is the ultimate cause of the divide by zero error – Milliways Jan 26 '20 at 21:37
  • I skimmed Pauschow's tutorial and code and found them very good. Once catch is that he is using MCP3208 and so the code will NOT work for your MCP3008. Luckily 10-bit MCP3008 and 12-bit 3208 hardware are almost identical except the analog to digital conversion part, and the SPI command code is also almost the "same". If you indeed are using MCP3008, I can try to find the get around details for you to do a "slight" modification to your MCP3208 code to make it work for MCP3008. – tlfong01 Jan 27 '20 at 1:19
  • Your error message "divide by zero" seems to be caused by Rpi getting zero output from the MCP3008. You might like to read the CHAT record of this Q&A for more details: raspberrypi.stackexchange.com/questions/106728/…. – tlfong01 Jan 27 '20 at 1:30
  • In case you found the chat record of the above Q&A messy, here is a clean version: penzu.com/p/dfa36278. Happy reading and learning. Cheers. – tlfong01 Jan 27 '20 at 1:56
  • There are other possible causes of trouble: (1) I did not go into the details of pschow's tutorial and check out if he is using "single ended" or "differential" mode of channel input. If you wired the wrong input mode, you might get unexpected results. See Comments #4, 5 of this Q&A for more details: raspberrypi.stackexchange.com/questions/107340/…. – tlfong01 Jan 27 '20 at 2:09

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.

  • I used piscope and I have flat lines (no readings) for all my GPIO pins. Perhaps I have bad wiring (the only reason I doubt that is I have rewired the circuit many times and tested voltages across my voltage divider). – raspinooblet Jan 27 '20 at 3:31
  • @raspinooblet I suggest you disconnect everything from the extension header and run gpiotest – joan Jan 27 '20 at 9:14

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.

  • The resistance equation comes from the equation to get Vo from a voltage divider. – raspinooblet Jan 27 '20 at 3:18
  • OK.... having derived that equation: I think you've got your constants wrong, and you should factor out the 3.3 volts (while ensuring you don't just do integer division). And the value->volts equation should use 1023, not 1024. – David G. Jan 27 '20 at 4:37
  • Why is that? 2^10 is 1024 – raspinooblet Jan 27 '20 at 15:10
  • True, but the values range from 0 (0 volts) to 1023 (the reference voltage (3.3 volts)). (Actually, you might want to try feeding the ground and reference voltages (at different times) as the input and see what you get. It is possible that feeding in the reference voltage will get you a lower value.) – David G. Jan 27 '20 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.