frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

MCP3004 not working/noise on line

Wed Feb 20, 2013 9:01 pm

I just got a MCP3004 ADC and am having a bit of trouble with it. The data bits returned just flicker from 0 to 1023 suggesting there is something wrong with the chip or there is noise on the wire. Below is the wiring diagram I am using and the Python code I'm using to run it. Any help would be greatly appreciated.

Image
http://s19.postimage.org/wv2v3o7n7/net.png
Image
http://s19.postimage.org/50746t0vn/oahigo_iwg_bb.jpg

Code: Select all

import spidev
import time

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

def get_adc(channel):
    if((channel > 3) or (channel <0)):
        return -1

    r = spi.xfer2([1, (4+channel)<<4, 0])
    adcout = ((r[1]&3) << 8) + r[2]
    return adcout

while True:
    print get_adc(0)

frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: MCP3004 not working/noise on line

Fri Feb 22, 2013 1:06 pm

Hello?

User avatar
Grumpy Mike
Posts: 917
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: MCP3004 not working/noise on line

Sat Feb 23, 2013 5:52 pm

Well the schematic and the physical diagram do not match, so there is not much confidence in either.

Where did you get your code from?
Looking at the line
(4+channel)<<4
That looks wrong when you look at the data sheet.
I suspect you might be selecting the wrong channel so wire up all the analogue inputs to the wiper of the pot and see if you get a steady reading.

frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: MCP3004 not working/noise on line

Sat Feb 23, 2013 7:59 pm

Grumpy Mike wrote:Well the schematic and the physical diagram do not match, so there is not much confidence in either.

Where did you get your code from?
Looking at the line
(4+channel)<<4
That looks wrong when you look at the data sheet.
I suspect you might be selecting the wrong channel so wire up all the analogue inputs to the wiper of the pot and see if you get a steady reading.
I made the code by adapting other peoples for the MCP3002/3008. I'm trying to send 0000 0001 (start bit) 1000 0000 (single ended, channel 0) 0000 0000. I realise the schematics probably aren't the same but the fritzing one is a replica of how I wired it. I'll try different wirings next time I get a chance, I'm away at the moment.

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: MCP3004 not working/noise on line

Sun Feb 24, 2013 3:30 am

Giving a quick look at the data sheet and your code, I see two issues.

1. You should use xfer() instead of xfer2(). xfer2() does not release the CSx line between transfers, and it is needed by the MCP3004.

2. For single ended conversion, change:

Code: Select all

    r = spi.xfer2([1, (4+channel)<<4, 0])
to
    r = spi.xfer([1, (8+channel)<<4, 0])
Enjoy!
Bill

frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: MCP3004 not working/noise on line

Sun Feb 24, 2013 7:16 pm

bgreat wrote:Giving a quick look at the data sheet and your code, I see two issues.

1. You should use xfer() instead of xfer2(). xfer2() does not release the CSx line between transfers, and it is needed by the MCP3004.

2. For single ended conversion, change:

Code: Select all

    r = spi.xfer2([1, (4+channel)<<4, 0])
to
    r = spi.xfer([1, (8+channel)<<4, 0])
Enjoy!
Bill
Thanks a lot! On a side note why does it need 8+channel?

frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: MCP3004 not working/noise on line

Sun Feb 24, 2013 8:55 pm

frasmacon wrote:
bgreat wrote:Giving a quick look at the data sheet and your code, I see two issues.

1. You should use xfer() instead of xfer2(). xfer2() does not release the CSx line between transfers, and it is needed by the MCP3004.

2. For single ended conversion, change:

Code: Select all

    r = spi.xfer2([1, (4+channel)<<4, 0])
to
    r = spi.xfer([1, (8+channel)<<4, 0])
Enjoy!
Bill
Thanks a lot! On a side note why does it need 8+channel?
OK, I worked out the 8+channel, and I changed the code but I still get erratic readings but I know the code works now as I tested it just feeding 3.3V into the chip and it read 1023. Could it be noise on the line, or a faulty pot. I don't trust mine as when I just hook it up to an LED, it doesn't really work that well. It's one I borrowed off my school so it wouldn't surprise me. It's 10K also, would that make a difference.

Theoretically, if I left the pin unconnected, shouldn't the readout be zero? Because for me it floats from 0 to around 270

User avatar
bgreat
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm

Re: MCP3004 not working/noise on line

Mon Feb 25, 2013 1:05 am

A faulty pot is a possibility. Usually though ADC stability problems can be traced to noise on the power.

An open connection input will be unstable. If you want a stable test value, just use two equal value resistors in series from Vcc to ground, with the center connected to the ADC input. You should get a reading near the middle of the ADC range -- ~511. As a quick test, I hook one input to Vcc, one to ground and one to a fixed resistor divider, and a final one to a variable input. This lets me confirm the operation with 3 known values and one variable input for testing.

Enjoy!
Bill

frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: MCP3004 not working/noise on line

Wed Mar 06, 2013 7:40 pm

OK, I got it to work!!!! :D :D :D :D :D :D :D :D :D

I used that code that was suggested earlier and I actually soldered wires onto the pot and now it works perfectly! For anyone who wants help with this I have put the code, Fritzing diagram and a video of it working. Thanks a lot guys!

Fritzing:
Image

Code: Select all

# Python Code to communicate with MCP3004 by Fraser May

#Import SpiDev wrapper to enable hardware SPI
import spidev

#Establish SPI connection with Bus 0, Device 0
spi = spidev.SpiDev()
spi.open(0,0)

def get_adc(channel):
    #Check valid channel
    if((channel > 3) or (channel < 0)):
        return -1

    #Perform SPI transaction and store returned bits in 'r'
    r = spi.xfer([1, (8+channel)<<4, 0])
    #Filter data bits from returned bits
    adcout = ((r[1]&3) << 8) + r[2]
    #Return value from 0-1023
    return adcout


while True:
    print get_adc(0)
Video: http://youtu.be/ETLiNCB0lgg

themaniskevin
Posts: 2
Joined: Mon Mar 10, 2014 11:45 pm

Re: MCP3004 not working/noise on line

Mon Mar 10, 2014 11:51 pm

Could someone please explain the following lines:
r = spi.xfer([1, (8+channel)<<4, 0])
#Filter data bits from returned bits
adcout = ((r[1]&3) << 8) + r[2]

I dont understand the 8+channel syntax. I also am lost in the manipulation of adcout. Thank you.

User avatar
joan
Posts: 14472
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MCP3004 not working/noise on line

Tue Mar 11, 2014 8:34 am

themaniskevin wrote:Could someone please explain the following lines:
r = spi.xfer([1, (8+channel)<<4, 0])
#Filter data bits from returned bits
adcout = ((r[1]&3) << 8) + r[2]

I dont understand the 8+channel syntax. I also am lost in the manipulation of adcout. Thank you.
(8+channel)<<4 means add 8 to channel and shift the value 4 places to the left. It's shorthand for multiply 8+channel by 16. Presumably that sets the channel bit for the ADC.

r[1]&3 masks off all but the bottom two bits of r[1]. <<8 multiplies the result by 256. I guess the ADC has 10 bit accuracy. The result seems to be returned in the bottom 2 bits of r[1] and (the 8 bits of) r[2].

themaniskevin
Posts: 2
Joined: Mon Mar 10, 2014 11:45 pm

Re: MCP3004 not working/noise on line

Tue Mar 11, 2014 5:04 pm

Thank you!

Return to “Interfacing (DSI, CSI, I2C, etc.)”