Trying to read voltage with MCP3002 but all pins read source voltage
After successfully getting my LM34 working with my ADC0831 thanks to help from the forum I have moved onto the 2 channel MCP3002. I attempted to get it working with a simple proof of concept by reading voltage off the first channel. I hooked everything up as I saw it on the datasheet with a reference and source voltage of 5volts as supplied by BS2 BOE.
After first loading the code and running it, I had a debug screen which showed me 5volts. Seeing as I was measuring 5volts, I assumed it was working fine. However, I quickly found out that it read EVERY value as 5volts. After unhooking my voltage sources from the channels and getting my meter I saw both channels and the Vref pin are all putting out ~5volts when disconnected. I believe it is caused by a problem with the initialization code and testing with a second MCP3002 gets me the same readings on my meter.
I was able to find examples for the MCP3008 and the MCP3208, but only really able to find examples for hooking the MCP3002 to a Raspberry Pi which was confusing at best for me. I found one bit of code which I used for initialization process that had no real documentation and could very well be bad.
Input?
LM37_MCP3002-FORUM.BS2
After first loading the code and running it, I had a debug screen which showed me 5volts. Seeing as I was measuring 5volts, I assumed it was working fine. However, I quickly found out that it read EVERY value as 5volts. After unhooking my voltage sources from the channels and getting my meter I saw both channels and the Vref pin are all putting out ~5volts when disconnected. I believe it is caused by a problem with the initialization code and testing with a second MCP3002 gets me the same readings on my meter.
I was able to find examples for the MCP3008 and the MCP3208, but only really able to find examples for hooking the MCP3002 to a Raspberry Pi which was confusing at best for me. I found one bit of code which I used for initialization process that had no real documentation and could very well be bad.
Input?
' ============================================================================== ' ' ' {$STAMP BS2} ' ' ' Notes: This code was pieced together from bits found on google and Stamp Works experiment #27 found at ' URL: http://www.parallax.com/dl/docs/books/sw/exp/sw27.pdf ' The scavenged code from google was mostly for the initialization process(Which I assume is incorrect). ' I used the Stamp Works code to read and display the result as a voltage for a proof of concept. I realize ' I'm pulling less data that the ADC is capable of, but I believed it simplified things for testing. ' ' ' ' ------------------------------------------------------------------------------ ' I/O Definitions ' ------------------------------------------------------------------------------ A2DdataO CON 6 ' A/D data OUTPUT line A2DdataI CON 3 ' A/D data INPUT line for selecting channels. A2Dclock CON 1 ' A/D clock A2Dcs CON 2 ' A/D chip select (low true) ' ------------------------------------------------------------------------------ ' Variables ' ------------------------------------------------------------------------------ result VAR Byte ' result of conversion a2dchan VAR Nib ' A2D channel selector mVolts VAR Word ' convert to millivolts ' ------------------------------------------------------------------------------ ' Program Code ' ------------------------------------------------------------------------------ Main: a2dchan = 0 ' Sets channels to 0 ? GOSUB InitADC GOSUB ReadData GOSUB PrintData PAUSE 1024 GOTO Main ' ------------------------------------------------------------------------------ ' Subroutines ' ------------------------------------------------------------------------------ ' Prepare the ADC for data acquisision '=-=-=-=-=-=-= InitADC: '=-=-=-=-=-=-= HIGH A2Dcs ' Pull CS high to ensure it's reset PULSOUT 1,A2Dclock ' Use clock pulse to advance communication ' Start bit LOW A2Dcs ' Pull CS (Chip Select) pin low to initiate communication SHIFTOUT A2DdataI, A2Dclock, 0, [1\1] ' Send '1' to DIN and a CLK pulse for start bit ' SGL/DIFF bit ' 0: 1 chan differential mode ' 1: 2 chan singled ended mode SHIFTOUT A2DdataI, A2Dclock, 0, [1\1] ' ODD/SIGN bit ' 0: for chan 0 ' 1: FOR chan 1 SHIFTOUT A2DdataI, A2Dclock, 0, [a2dchan\1] ' MSBF bit (Most Significant Bit Flag) ' 0: MSB data followed by LSB data ' 1: just output MSB data ' Although testing seems to indicate these bit settings are reversed SHIFTOUT A2DdataI, A2Dclock, 0, [0\1] 'LOW DIN 'PULSOUT 1,CLK ' Use clock pulse to advance communication ' Null Bit ' SHIFTIN DOUT, CLK, 1, [DigBit\1] ' Actually grab the null bit ' Send a clock pulse to activate the null bit, but don't ' bother grabbing it PULSOUT 1,A2Dclock 'DEBUG BIN1 DigBit, "-" RETURN ' Get 9 OF 10 data bits using SHIFTIN '=-=-=-=-=-=-= ReadData: '=-=-=-=-=-=-= result = 0 SHIFTIN A2DdataO, A2Dclock, 0, [result\9] HIGH A2Dcs ' Done with that reading. Pull CS pin high RETURN '=-=-=-=-=-=-= PrintData: '=-=-=-=-=-=-= mVolts = result */ $139C ' x 19.6 (mv / unit) DEBUG HOME DEBUG "ADC..... ", DEC result, " ", CR DEBUG "volts... ", DEC mVolts DIG 3, ".", DEC3 mVolts PAUSE 100 ' delay between readings RETURN
LM37_MCP3002-FORUM.BS2
Comments
http://www.parallax.com/Portals/0/Downloads/docs/prod/compshop/MCP3202Demo.zip
I have been so used to digging in the deepest, darkest holes of the internet for code, I completely over looked what was right in front of me. Let me give this a shot and see what happens....
UPDATE:
The code ran fine and is WORLDS simpler than that beast I was messing with. However, I am still reading 5volts on both channels even when they are disconnected from a power source. Any more ideas? Or could I have managed to somehow smoke these 2 chips?
This sounds like a wiring issue.
Could you take a picture of your setup? It might help someone identify the problem.
To sum everything up:
1) Check the connections 600 or so times to be EXTRA sure!
2) Use the code Chris wrote as a starting point (http://www.parallax.com/Portals/0/Do...CP3202Demo.zip)
It's called the "smoke" test. You pray it doesn't smoke when you turn on the power.