intepreting ADC results

I am using an MCP3204 to convert voltage value between 1.6V to 2.5V. My Vref is 2.5V. I'm using the code on the website for 3208. I'm assuming there's no modification to use with a 3204 other than i only have 4 channels as opposed to 8 on the ADC.

How do we decipher the values we get from DATAIN? I'm currently getting 4095 for 2.5V input, and 1137 for 1.6V input. What do these values mean?

Thanks.

Hmm, if your reference is 2.5 volts, then 2.5 volts should read 4095.
It sounds like thats what you are getting.
Each unit of reading in millivolts is 2500/4096 or 0.61mv.
Therefore, a reading of 1137 should be 1137 * 0.61 or 693 millivolts.

Thanks, another question about the ADC.

for the mode selection, does single ended input means that the voltage i'm putting in is positive and with reference to gnd?

for the pin selection, if i'm reading pin one of the ADC (channel 0), would i say read_adc(0) or read_adc(1)?

does single ended input means that the voltage i'm putting in is positive and with reference to gnd

yes.

if i'm reading pin one of the ADC (channel 0), would i say read_adc(0) or read_adc(1)

they are labelled from 0 to 5 so reading pin one is not channel 0. The software goes with the pin labelling.

Thanks, but i still don't get it. pin labelling for the arduino or for the mcp3204? if my input is at pin one of the mcp3204, how do i read it?

Pin 1 is Channel 0 on the MCP2304.
Sorry I thought you were talking about the Arduino.

so i would say read_adc(0) if my input is at pin 1 of the mcp3204?

Yes :slight_smile:

Thanks a lot. However, I am new to the board and the ADC so I have more questions.

I am trying to understanding the code for interfacing the ADC mcp3204.

void loop() {
readvalue = read_adc(0);
Serial.println(readvalue,DEC);
delay(250);
}

Running the above code causes the Arduino to conitinuously toggle the SPI pins to read pin zero from the ADC, right?

How come value it prints from "Serial.println(readvalue, DEC)" isn't consistent even though the same voltage? (to test I used 2.5 V which is the same as my Vref)

Running the above code causes the Arduino to conitinuously toggle the SPI pins to read pin zero from the ADC

No, all this code is doing is reading the built in analogue input A0. You probably haven't got anything wired up to it so it shows just random pickup.

How have you wired up the mcp3204? Without a schematic it is difficult to say how you should write the software to drive it.

This is the code I am using, which is taken from the playground of the website. From the mcp3204, the CS pin is connected to SELPIN, DATAOUT to DATAOUT, DATAIN to DATAIN, CLOCK to SPICLOCK. Input is in pin 1 of the mcp3204. I hope this information can help you point me in the right direction. Thanks.

#define SELPIN 10 //Selection Pin
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//Clock
int readvalue;

void setup(){
//set pin modes
Serial.println("set pin modes");
pinMode(SELPIN, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
//disable device to start with
digitalWrite(SELPIN,HIGH);
digitalWrite(DATAOUT,LOW);
digitalWrite(SPICLOCK,LOW);

Serial.begin(9600);
}

int read_adc(int channel){
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)
//Serial.println("default commandbits");
//Serial.println(commandbits, BIN);

//allow channel selection
commandbits|=((channel-1)<<3);
Serial.println(commandbits, BIN);

digitalWrite(SELPIN,LOW); //Select adc
// setup bits to be written
for (int i=7; i>=3; i--){
digitalWrite(DATAOUT,commandbits&1<<i);
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);

}

digitalWrite(SPICLOCK,HIGH); //ignores 2 null bits
digitalWrite(SPICLOCK,LOW);
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);

//read bits from adc
for (int i=11; i>=0; i--){
adcvalue+=digitalRead(DATAIN)<<i;
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
digitalWrite(SELPIN, HIGH); //turn off device
return adcvalue;
}

void loop() {
readvalue = read_adc(0);
Serial.println(readvalue,DEC);
Serial.println(" ");
delay(250);
}

Assuming the commands are correct the line:-

digitalWrite(DATAOUT,commandbits&1<<i);

try
digitalWrite(DATAOUT,commandbits&(1<<i));

It might be doing the AND before the shift.

I've tried the bracket but it doesn't seem to be the problem.

I am starting to question the connection because it even if I ground the input, or if i do not power the the 3204, it is still giving me a 4095.

The connection I have right now is as followed:

Arduino pin 10 to MCP3204 pin 8
Arduino pin 11 to MCP3204 pin 10
Arduino pin 12 to MCP3204 pin 9
Arduino pin 13 to MCP3204 pin 11

Thanks again.

Have you connected pin 12 on the 3204 to pin 7 on the 3204? That is have a common ground for analogue and digital signals?

They're not shorted together but they're both AGND and DGND are connected to the ground rail. Does that make a difference?

Thanks.

No that's fine.

Originally you said:-

I'm currently getting 4095 for 2.5V input, and 1137 for 1.6V input

Then you said:-

even if I ground the input, or if i do not power the the 3204, it is still giving me a 4095.

Are those two still true or can you not get 1137 any more?

i'm not getting 1137 anymore.

You might have broke it. :cry:

:frowning: well, i suspected something was broken since i wasn't able to understand any results. Is it the ADC that's broken or did I break the Arduino? :S

My moneys on the ADC.