简体   繁体   中英

Temperature sensor /w Raspberry Pi 3

I am trying to make an temperature sensor.The problem is:I connected the sensor (VCC at 5v , GND at Ground and OUT at the advanced use (that is for the sensor I guess). Now the problem is, i made the python file for it to work , but instead of the real temperature and the voltage it shows me -50 continuously , even if I try to heat him still -50.This is the python code:

import spidev
import time
spi = spidev.SpiDev()
spi.open(0,0)

def readadc(adcnum):
    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

while True:
    for adcInput in range(0,8):
        value = readadc(adcInput)
        voltage = value * 3.3
        voltage /= 1024.0
        tempCelsius = (voltage-0.5)*100
        print "---------------------------"
        print "ADC(", adcInput,")= ", value
        print "---------------------------"
        print "Voltage: ", voltage
        print "---------------------------"
        print "Temp: ", tempCelsius
    time.sleep(1)

Now the thing is , what could possibly cause this problem;the setup of the sensor or the code ? I put the #coding UTF-8 and still -50.I am new in coding and I really can't find how to fix it.

PS : I am using Raspberry Pi 3 2015

You said you connected OUT to pin 28. As I see in your code you are using an ADC interfaced with SPI. SPI is Synchronous, you only connect an OUT digital PIN : a Clock pin misses.

Do you have such shield (ADC with SPI interface)? It is mandatory to have your raspberry reading some analog values (no adc on raspberry).

In fact, it is not possible to read the analog input in the raspberry pi, and you need to use the electronic circuitry to read the information. for example, The MCP3008 is a low cost 8-channel 10-bit analog to digital converter. https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008 and https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/overview

You're trying to read an analog signal from the Pi - as another use said: this is not possible without an ADC. I recommend using an Arduino board and reading the values from the Arduino via the serial port. Arduino has a vast database for analog signal processing, so I always recommend it first. I wrote a blog article on how to do this, check it out (if you're interested):

Datalogging with Arduino and pySerial on a Raspberry Pi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM