I'm trying to use MCP3002 for the first time, and can't seem to get the basics to work

I've been following this guide and am pretty certain that all my connections are right (updated photo below).

Running the program below, I can't make sense of the figures i'm getting - using the RPis 3.3V and with this as the potentiometer, I get either 1024 or 0 values irrespective of the position of the potentiometer.

I have added a led to check that the potentiometer works. I've tested all the wires to make sure the crimping is ok.

What am I getting wrong and/or how can I try and troubleshoot to look for any potential issues?

enter image description here


# botbook_mcp3002.py - read analog values from mcp3002
# (c) BotBook.com - Karvinen, Karvinen, Valtokari

# Installing spidev: 
#  sudo apt-get update
#  sudo apt-get -y install git python-dev
#  git clone https://github.com/doceme/py-spidev.git
#  cd py-spidev/
#  sudo python setup.py install

import spidev   # installation help in botbook_mcp3002.py comments
import time

def readAnalog(device = 0,channel = 0):
    assert device in (1, 0)
    assert channel in (1, 0)
    #open spi
    spi = spidev.SpiDev()
    #spi.open(0, device)
    spi.open(0, device)
    Protocol start bit (S), sql/diff (D), odd/sign (C), MSBF (M)
    Use leading zero for more stable clock cycle
    0000 000S DCM0 0000 0000 0000
    Sending 3 8bit packages so xpi.xfer2 will return the same amount.
    start bit = 1
    sql/diff = 1 SINGLE ENDED MODE  (2 channel mode) 
    odd/sign = channel 0/1
    MSBF = 0
    command = [1, (2 + channel) << 6, 0]
    #2 + channel shifted 6 to left
    #10 or 11 << 6 = 1000 0000 or 1100 0000
    reply = spi.xfer2(command)

    Parse right bits from 24 bit package (3*8bit)
    We need only data from last 2 bytes.
    And there we can discard last two bits to get 10 bit value 
    as MCP3002 resolution is 10bits
    Discard reply[0] byte and start from reply[1] where our data starts
    value = reply[1] & 31   
    #31 = 0001 1111 with & operation makes sure that we have all data from XXXX DDDD and nothing more. 0001 is for signed in next operation.
    value = value << 6  #Move to left to make room for next piece of data.
    #000D DDDD << 6 = 0DDD DD00 0000
    #Now we get the last of data from reply[2]
    value = value + (reply[2] >> 2)
    #Here we discard last to bits
    #DDDD DDXXX >> 2 = 00DD DDDD
    #0DDD DD00 0000 + 00DD DDDD = 0DDD DDDD DDDD
    return value

def main():
    #read channel 0 on device 0
    value = readAnalog(0, 0)

if __name__ == "__main__":
  • We need to see the code you are using and a clear photo of the connections you have made. – joan Jun 6 '18 at 20:54
  • Thanks for the comment @joan - that's why I referred to the article I followed so that I do not have to publish a large post. Having said that - I have now added a link to a photo of the circuit, I hope it's clear enough. the code I used is the one from the article, completely unchanged. – Yossi Dahan Jun 6 '18 at 21:27
  • 3
    It's impossible to know what is going on with the cyntech and the ribbon cable between the breadboard and the Pi. A common mistake is having the ribbon cable back to front. It is much simpler to diagnose when jumper leads are used directly between the Pi expansion header and the breadboard – joan Jun 6 '18 at 21:57
  • I've changed the connection to go the the Pi directly and added the code used to the question. thank you. – Yossi Dahan Jun 10 '18 at 21:23
  • i noticed you have executed readAnalog() only once per execution. could you put it in a loop (with a short delay) and print out the sequence of values you get while you vary the pot setting ? – Shreyas Murali Jun 13 '18 at 16:22

To fault find: Remove the connection to the potentiometer and get two identical resistors (say between 4.7k and 47k). Connect the data in line to each, with the other ends of the resistors connected to 0v and 5v. This should give you a 2.5v voltage in on the analog line in. It should read around 512. If still not, switch to the other data in line (in case the code is reading the other one). It would help also if you have a voltmeter to measure the voltage on the input line.

If that all works step through attaching the potentiometer. But get rid of the LED. You definitely need a pull up (or down) resistor on the other side so that you get a changing voltage.

Also, have you enabled the SPI in raspi-config?

pi@raspberrypi:~ $ ls -l /dev/spi*
pi@raspberrypi:~ $ lsmod | grep spi

Enable it in raspi-config, doesn't appear to need a reboot with the latest HW/OS.

pi@raspberrypi:~ $ sudo raspi-config
pi@raspberrypi:~ $ ls -l /dev/spi*
crw-rw---- 1 root spi 153, 0 Jun 16 09:00 /dev/spidev0.0
crw-rw---- 1 root spi 153, 1 Jun 16 09:00 /dev/spidev0.1
pi@raspberrypi:~ $ lsmod | grep spi
spidev                 16384  0
spi_bcm2835            16384  0

I think the method in the doc you reference may be old, try the above.

  • I connected as you instructed and got @2.5v going in, which is what I assume you intended, and I'm still getting 0s, so I'll have to assume the MCP3002 is burnt. thanks for suggesting this approach. I'll look to get a new one and re-test, which might take me a bit of time, but I'll come back to this when I got one and tick. voted-up in the meantime. One question though - when you say pull up (or down) on the other side - what does 'other side' mean? – Yossi Dahan Jun 15 '18 at 19:11
  • Data in, out and clock look okay. It may be nothing - but move the 0v and +5v to the 2 and 6 pin. It looks like you 3.3v - okay - but I always use 5v. – PaulS Jun 15 '18 at 22:26
  • 1
    Thanks @PaulS - SPI was enabled (but I checked again today); I have already moved the voltage to +5v to cater for your initial answer :-( – Yossi Dahan Jun 16 '18 at 8:11
  • 1
    I've got new ADC on order, but as they will arrive after the bounty expires, I'll award it now for all the help @PaulS and will circle back once I am able to re-test with a new component – Yossi Dahan Jun 19 '18 at 6:54
  • 1
    Sadly not a lot of luck, and I can't explain why. tried with a brand new MCP3002; whilst ordering that, I also ordered MCP3008 and followed this - learn.adafruit.com/raspberry-pi-analog-to-digital-converters/… which did work; I tried the 'software route' which worked a treat when using GPIO with 'software SPI'; trying to use SPI resulted in 0 values. I have doubled checked that SPI is enabled and followed codementor.io/sheena/… with no luck. I have a working alternative, but am really baffled!!! – Yossi Dahan Jul 6 '18 at 13:13

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.