Uncategorized

MCP3008 / MCP3208 Programming Notes

Asked 
Active today
Viewed 27 times
2

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)
 New contributor
  • 1
    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 4 hours ago
  • 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 39 mins ago
  • 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 28 mins ago
  • 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 3 mins ago   Edit

2 Answers

1

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.

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.

 New contributor
.END

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: