0

I'm learning how to read an analog thermistor using an MCP3008 and a Raspberry Pi Zero W, along with a Smraza T Type GPIO Breakout board.

Specifically, I'm following the Analogue Sensors On The Raspberry Pi Using An MCP3008 tutorial on Raspberry Pi-Spy.

I am consistently getting values like this:

--------------------------------------------
Light: 972 (3.14V)
Temp:  183 (0.59V) 9.03 deg C
--------------------------------------------

The light value seems fine but the temperature is dead wrong (it's not 9C in my living room, even in a Kansas City winter). This is the second time I've tried a tutorial with the MCP3008 and the TMP36 and the result seems to be the same.

Here's my code:

#!/usr/bin/python

import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
    adc = spi.xfer2([1,(8+channel)<<4,0])
    data = ((adc[1]&3) << 8) + adc[2]
    return data

# Function to convert a voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data, places):
    volts = (data * 3.3) / float(1023)
    volts = round(volts, places)
    return volts

# Function to calculate temperature from 
# TMP36 data, rounded to specified number
# of decimal places.
def ConvertTemp(data, places):

    # ADC Value
    # (approx)  Temp    Volts
    # 0         -50     0.00
    # 78        -25     0.25
    # 155       0       0.50
    # 233       25      0.75
    # 310       50      1.00
    # 465       100     1.50
    # 775       200     2.50
    # 1023      280     3.30

    temp = ((data * 330) / float(1023)) - 50
    temp = round(temp, places)
    return temp

# Define sensor channels
light_channel = 0
temp_channel = 1

# Define delay between readings
delay = 5

while True:
    # Read the light sensor data
    light_level = ReadChannel(light_channel)
    light_volts = ConvertVolts(light_level,2)

    # Read the temperature sensor data
    temp_level = ReadChannel(temp_channel)
    temp_volts = ConvertVolts(temp_level, 2)
    temp = ConvertTemp(temp_level, 2)

    # Print out results
    print "--------------------------------------------"
    print("Light: {} ({}V)".format(light_level, light_volts))
    print("Temp:  {} ({}V) {} deg C".format(temp_level, temp_volts, temp))

    # Wait before repeating loop
    time.sleep(delay)

Here are some (admittedly difficult-to-look-at) pictures of my connections:

sensors and mcp breakout board overall view

Is there anything immediately apparent that seems wrong? The light sensor seems to work (admittedly, it's harder to tell just how bright the room is in a quantifiable way like it's possible to do with temperature) but the temperature sensor is off.

EDIT: Changed pin to 3.3v and adjusted values accordingly

2 Answers 2

1

I'm afraid it all looks fine to me.

I would try two test readings on the TMP36 channel.

  1. If you connect ground as an input it should return -50 degrees centigrade.
  2. If you connect 3V3 as an input it should return 280 degrees centigrade.

If it does that shows the software is correct which suggests either a problem with the TMP36 or the wiring.

4
  • Well, the values are as you said they'd be with those wiring configurations. I tried switching out the TMP-36 for the others I have and all gave the same temperature range. I also swapped out the MCP3008 with the same result. Could this be a problem with the breakout board I'm using?
    – nerdenator
    Commented Jan 22, 2018 at 0:07
  • Does the temperature vary if you heat up the TMP36? Perhaps a hair dryer for a few seconds.
    – joan
    Commented Jan 22, 2018 at 8:16
  • it definitely changes if i squeeze my thumb and forefinger around it.
    – nerdenator
    Commented Jan 22, 2018 at 14:59
  • The only thing I have to suggest is to double check that the voltage being fed to the MCP3008 is the same as the voltage you expect (3V3).
    – joan
    Commented Jan 22, 2018 at 16:47
0

As it turns out, putting a .01uF capacitor between the ground and vout of the TMP36 resolved this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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