-1

I have a problem with reading data from adc convertor MCP3008 10bit.

I found a python script from adafruit that give data value from all channels (CHO TO CH7). I added a picture with my results and the from adafruit site "https://learn.adafruit.com/assets/30459".

You can see that mine results are jumping while from the adafruit is stable on zero. So I think there is something wrong with some of one configuration file.

Any help would be appreciate

# Simple example of reading the MCP3008 analog input channels and printing
# them all out.
# Author: Tony DiCola
# License: Public Domain
import time

# Import SPI library (for hardware SPI) and MCP3008 library.
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

# Hardware SPI configuration:
SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))


print('Reading MCP3008 values, press Ctrl-C to quit...')
# Print nice channel column headers.
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)
# Main program loop.
while True:
    # Read all the ADC channel values in a list.
    values = [0]*8
    for i in range(8):
        # The read_adc function will get the value of the specified channel (0-7).
        values[i] = mcp.read_adc(i)
    # Print the ADC values.
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
    # Pause for half a second.
    time.sleep(0.5)

enter image description here

  • 2
    There is nothing wrong when a pin is unconnected it is called floating and can have any value between 0 and 1023 in this case. Evidence for this is when you connect 3.3 volts on channel 0 you get 1023 constantly. – Steve Robillard Jul 31 '17 at 5:42
0

Many thanks Steve,

I checked, and indeed when I connect at voltage between 0 to 3.3v the value are stable. But now I have a other problem. I have (found) a python script to read the adc value. I get a value around 303 by a voltage of 1.552. When calculating it should be 1.552V*(4096/3.3V)=1926.

!/usr/bin/python3

import spidev spi = spidev.SpiDev() spi.open(0, 0) channel = 1 result = spi.xfer2([1, 8 + channel << 4, 0]) adcValue = ((result[1] & 15) << 8) + result[2] print(adcValue) spi.close()

Running this script the adc value is correct. The most scipts I found are like the one above.

#!/usr/bin/python

#

MCP3204/MCP3208 sample program for Raspberry Pi

#

how to setup /dev/spidev?.?

$ suod modprobe spi_bcm2708

#

how to setup spidev

$ sudo apt-get install python-dev python-pip

$ sudo pip install spidev

# import spidev import time

class MCP3208: def init(self, spi_channel=0): self.spi_channel = spi_channel self.conn = spidev.SpiDev(0, spi_channel)

self.conn.max_speed_hz = 1000000 # 1MHz

            self.conn.max_speed_hz = 1 # 1Hz

    def __del__( self ):
            self.close

    def close(self):
            if self.conn != None:
                    self.conn.close
                    self.conn = None

    def bitstring(self, n):
            s = bin(n)[2:]
            return '0'*(8-len(s)) + s

    def read(self, adc_channel=0):
            # build command
            cmd  = 128 # start bit
            cmd +=  64 # single end / diff
            if adc_channel % 2 == 1:
                    cmd += 8
            if (adc_channel/2) % 2 == 1:
                    cmd += 16
            if (adc_channel/4) % 2 == 1:
                    cmd += 32

            # send & receive data
            reply_bytes = self.conn.xfer2([cmd, 0, 0, 0])

            #
            reply_bitstring = ''.join(self.bitstring(n) for n in reply_bytes)
            # print reply_bitstring

            # see also... http://akizukidenshi.com/download/MCP3204.pdf (page.20)
            reply = reply_bitstring[5:19]
            return int(reply, 2)

if name == 'main': spi = MCP3208(0)

    count = 0
    a0 = 0
    a1 = 0
    a2 = 0
    a3 = 0

    while True:
            count += 1
            a0 += spi.read(0)
            a1 += spi.read(1)
            a2 += spi.read(2)
            a3 += spi.read(3)

            if count == 10:

print "ch0=%04d, ch1=%04d, ch2=%04d, ch3=%04d" % (a0/10, a1/10, a2/10, a3/10)

                    print (a0/10)
                    count = 0
                    a0 = 0
                    a1 = 0
                    a2 = 0
                    a3 = 0

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.