User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Circuit Problem

Thu Aug 06, 2015 2:28 pm

Hi guys

I used to display 0 - 3.3V by using MCP3008 and Pi. My sensors are 0 - 10V and I draw a circuit (picture below here) to divide voltage. According to my calculations it should work but it shows wrong. When sensor is 5V, I see 4,5V on the screen. And it should be 0 - 10V but after 8.8V it reach 1023. I dont understand what is problem. If someone has idea plase guys let me know.

The circuit
circuit.png
circuit
circuit.png (20.31 KiB) Viewed 718 times
The code

Code: Select all

import spidev
import time
import os
import urllib
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)


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


def ReadChannel(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data



def ConvertVolts(data,places):
  volts = (data * 10) / float(1023)
  volts = round(volts,places)  
  return volts


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

  
sensor1_channel = 0
sensor2_channel = 1
sensor3_channel = 2
sensor4_channel = 3
sensor5_channel = 4
sensor6_channel = 5
sensor7_channel = 6
sensor8_channel = 7
delay = 5


GPIO.setwarnings(False)
GPIO.setup(5, GPIO.IN)
GPIO.setup(6, GPIO.IN)
GPIO.setup(13, GPIO.IN)
GPIO.setup(19, GPIO.IN)
GPIO.setup(26, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(20, GPIO.IN)
GPIO.setup(21, GPIO.IN)
    
  

while True:


        
  foldtime = time.time()
  fnewtime = time.time()
  fstore = 10.0                                 # second

  while True:  
    fnewtime = time.time()
    fdtime = fnewtime-foldtime


  
    digital1 = GPIO.input(5)
    digital2 = GPIO.input(6)
    digital3 = GPIO.input(13)
    digital4 = GPIO.input(19)
    digital5 = GPIO.input(26)
    digital6 = GPIO.input(21)
    digital7 = GPIO.input(20)
    digital8 = GPIO.input(16)

    if digital1 == 1:
     digital1 = 'ON'
    else:
      digital1 = 'OFF'

    if digital2 == 1:
     digital2 = 'ON'
    else:
      digital2 = 'OFF'

    if digital3 == 1:
     digital3 = 'ON'
    else:
      digital3 = 'OFF'

    if digital4 == 1:
     digital4 = 'ON'
    else:
      digital4 = 'OFF'

    if digital5 == 1:
     digital5 = 'ON'
    else:
      digital5 = 'OFF'

    if digital6 == 1:
     digital6 = 'ON'
    else:
      digital6 = 'OFF'

    if digital7 == 1:
     digital7 = 'ON'
    else:
      digital7 = 'OFF'

    if digital8 == 1:
     digital8 = 'ON'
    else:
      digital8 = 'OFF'


    sensor1_level = ReadChannel(sensor1_channel)
    sensor1_volts = ConvertVolts(sensor1_level,2)
  
    sensor2_level = ReadChannel(sensor2_channel)
    sensor2_volts = ConvertVolts(sensor2_level,2)
  
    sensor3_level = ReadChannel(sensor3_channel)
    sensor3_volts = ConvertVolts(sensor3_level,2)

    sensor4_level = ReadChannel(sensor4_channel)
    sensor4_volts = ConvertVolts(sensor4_level,2)

    sensor5_level = ReadChannel(sensor5_channel)
    sensor5_volts = ConvertVolts(sensor5_level,2)

    sensor6_level = ReadChannel(sensor6_channel)
    sensor6_volts = ConvertVolts(sensor6_level,2)

    sensor7_level = ReadChannel(sensor7_channel)
    sensor7_volts = ConvertVolts(sensor7_level,2)

    sensor8_level = ReadChannel(sensor8_channel)
    sensor8_volts = ConvertVolts(sensor8_level,2)


    if fdtime >= fstore:
      foldtime = fnewtime
      googlepage = "https://script.google.com/macros/s/"
      mygooglekey = "AKfycag8ıtng1231byDLLxRSLBLLqdadekqtjr_0wSgFJKfqRassd526s21QtLeO9oJMhUqzFBs"
      wtime = time.strftime("%d/%m/%Y %H:%M:%S")
      content = urllib.urlopen("%s%s/exec?DATE=%s&Sensor1=%s&Sensor2=%s&Sensor3=%s&Sensor4=%s&Sensor5=%s&Sensor6=%s&Sensor7=%s&Sensor8=%s&Digital1=%s&Digital2=%s&Digital3=%s&Digital4=%s&Digital5=%s&Digital6=%s&Digital7=%s&Digital8=%s" %(googlepage, mygooglekey, wtime, sensor1_volts, sensor2_volts, sensor3_volts, sensor4_volts, sensor5_volts, sensor6_volts, sensor7_volts, sensor8_volts, digital1, digital2, digital3, digital4, digital5, digital6, digital7, digital8)).read()
      print "-----------------------------------------------------------------------------"  
      print(" Sensor-1            Sensor-2         Sensor-3          Sensor-4         Sensor-5            Sensor-6         Sensor-7          Sensor-8")
      print(" {} - ({}V)        {} - ({}V)      {} - ({}V)      {} - ({}V)          {} - ({}V)          {} - ({}V)     {} - ({}V)        {} - ({}V)".format(sensor1_level,sensor1_volts,sensor2_level,sensor2_volts,sensor3_level,sensor3_volts,sensor4_level,sensor4_volts,sensor5_level,sensor5_volts,sensor6_level,sensor6_volts,sensor7_level,sensor7_volts,sensor8_level,sensor8_volts))    
      print(" Digital-1            Digital-2         Digital-3          Digital-4         Digital-5            Digital-6         Digital-7          Digital-8")
      print(" {}                  {}                  {}                 {}                    {}                    {}                {}                 {} ".format(digital1,digital2,digital3,digital4,digital5,digital6,digital7,digital8))    


time.sleep(delay)
cactiapps.blogspot.com

User avatar
coskukoz
Posts: 34
Joined: Sun May 31, 2015 4:01 pm
Location: Balıkesir, Turkey

Re: Circuit Problem

Fri Aug 07, 2015 8:57 am

Guys problem was Grounds. MCP3008 has 2 types of grounds which are called 'digital ground' and 'analog ground'. I connected them together. It's a mistake. Right way is: connect digital ground to Pi and connect analog ground to sensors. That's all. Now it's working. :ugeek:
cactiapps.blogspot.com

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Circuit Problem

Fri Aug 07, 2015 12:19 pm

but actually according
https://www.adafruit.com/datasheets/MCP3008.pdf and
https://learn.adafruit.com/reading-a-an ... -a-mcp3008 see FIGURE 6-5
the grounds should be connected.

but how about Vref? connected to 3v3 gives
10(V) * 1024 * 470/(1000+470) *3.3 = 992 => 9.68V

Return to “Beginners”