Cant get a value from mcp3008 please help.
Moderators: adafruit_support_bill, adafruit

Forum rules
Talk about Adafruit Raspberry Pi® accessories! Please do not ask for Linux support, this is for Adafruit products only! For Raspberry Pi help please visit: http://www.raspberrypi.org/phpBB3/

Cant get a value from mcp3008 please help.

by IslandMedic on Thu Feb 28, 2013 3:44 pm

I have been struggling trying to get this simple circuit to work. When you look at the logic analyzer output you will see the Dout line has nothing. I have the input channel for the ADC set to 2.73 volts. the code out put is all zeros. I just don't know what I am doing wrong. One question I have is, do I have to cross over din and dout. Meaning do I wire din -> SPIMISO or should it go to din -> SPIMOSI. Any thoughts or ideas would be very much appreciated.

thanks
Brad

Wiring:

MCP3008
Pin 16,15 -> rpi 3.3v
Pin 14,9 -> RPi Gnd
Pin 10 CS -> rpi 25
Pin 11 din -> rpi 24
Pin 12 dout -> rpi 23
Pin 13 clk -> rpi 22

Logic Analyzer:
spec.png
spec.png (45.68 KiB) Viewed 208 times


Channel 0 -> mcp3008 pin 11 Din
Channel 1 -> mcp3008 pin 12 Dout
Channel 2 -> mcp3008 pin 10 CS
Channel 3 -> mcp3008 pin 13 CLK

Test code:
Code: Select all | TOGGLE FULL SIZE
#!/usr/bin/env python
import time
import os
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
DEBUG = 1

# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
        if ((adcnum > 7) or (adcnum < 0)):
                return -1
        GPIO.output(cspin, True)

        GPIO.output(clockpin, False)  # start clock low
        GPIO.output(cspin, False)     # bring CS low

        commandout = adcnum
        commandout |= 0x18  # start bit + single-ended bit
        commandout <<= 3    # we only need to send 5 bits here
        for i in range(5):
                if (commandout & 0x80):
                        GPIO.output(mosipin, True)
                else:
                        GPIO.output(mosipin, False)
                commandout <<= 1
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)

        adcout = 0
        # read in one empty bit, one null bit and 10 ADC bits
        for i in range(12):
                GPIO.output(clockpin, True)
                GPIO.output(clockpin, False)
                adcout <<= 1
                if (GPIO.input(misopin)):
                        adcout |= 0x1

        GPIO.output(cspin, True)

        adcout >>= 1       # first bit is 'null' so drop it
        return adcout

# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 22
SPIMISO = 23
SPIMOSI = 24
SPICS = 25

# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)


print "| #0 \t #1 \t #2 \t #3 \t #4 \t #5 \t #6 \t #7\t|"
print "-----------------------------------------------------------------"
while True:
        print "|",
        for adcnum in range(8):
                ret = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
                print ret,"\t",
        print "|"




IslandMedic
 
Posts: 9
Joined: Fri Feb 01, 2013 4:19 pm

Re: Cant get a value from mcp3008 please help.

by IslandMedic on Thu Feb 28, 2013 3:47 pm

I should also mention that I am using the Adafruit Prototyping Pi Plate Kit for Raspberry Pi to make my connections. I also have one of the original RPI's

thanks

Brad
IslandMedic
 
Posts: 9
Joined: Fri Feb 01, 2013 4:19 pm

Re: Cant get a value from mcp3008 please help.

by mwilson on Fri Mar 01, 2013 1:31 pm

Random idea: could you zoom in on the clock signal and make sure that a pulse lasts at least 125nsec, as required in the MCP3008 datasheet? One often thinks that bit-banging a clock in Python will guarantee suitably long pulse lengths, but RPis are fast. If the MCP3008 didn't even notice it had been clocked ...
mwilson
 
Posts: 46
Joined: Sun Oct 23, 2011 11:17 am
Location: Maynooth, Canada

Re: Cant get a value from mcp3008 please help.

by IslandMedic on Fri Mar 01, 2013 6:22 pm

Thanks I hadn't considered that idea. I will play with the timing and see if that helps.

Brad
IslandMedic
 
Posts: 9
Joined: Fri Feb 01, 2013 4:19 pm