analogRead() minimum value of 1?

Status
Not open for further replies.

DogP

Well-known member
Hey,

When I do an analogRead() on my Teensy 3.1 and 3.2, I never read a value of 0... for example at 12-bit resolution, grounding the pin gives me a value of 1, connecting it to 3.3V gives me a value of 4095. If I set analogReadResolution() to a value that's not native (i.e. 11-bits, 9-bits, etc), I get 0, but that's just because the library right shifts to get it to my requested resolution (1>>1=0).

Anyone else have this problem? Am I doing something wrong? Is there some limitation that I'm unaware of?

Thanks,
Pat
 
Check table 24 of the Kinetis K20 datasheet, this contains the quantization and other error margins of the ADC.
 
Yeah, this sort of thing can happen. There might be a fraction of 0.8 mV between your ground and the VREFL pin at Teensy. Using AGND for your circuit might help. But if you're drawing a few mA of current through pots or other analog circuits, it's not hard to get half a mV of more drop through the wires and those ferrite bead inductors on the PCB.

If you *really* want to track this down, the voltage Teensy 3.2 considers 0 is whatever's on the VREFL pin of the MK20 chip.

http://www.pjrc.com/teensy/schematic.html

If you have a higher quality multimeter with a good mV scale, and you can successfully probe that tiny pin on the MK20 chip (touch between pins 15 & 16 on the chip, since they're connected together anyway), you could try measuring there's actually some voltage between your analog signal and VREFL. My guess is you'll see about 0.4 mV or more....
 
Thanks for the replies guys. While not really a big deal, it's just one of those things that kinda bothered me.

I used my Fluke DMM on mV and probed the pins and read 0.0mV between the wire to AGND I was using, and pin 15 on the chip (I had good contact as it was jumping around several tenths of a mV until I touched the pins).

I checked the datasheet, and came across the offset correction register (ADCx_OFS). I decided to try setting that at the end of the calibration section in analog.c, and it seems to have resolved my issue.

I put in the line "ADC0_OFS = ADC0_OFS+51;"... which was middle value from the tests I tried on one of my boards. For 12-bit resolution, at +2, I was right on the edge of 0 and 1 at minimum... and at +100, I was right at the edge of 4094 and 4095 at maximum. So, I split the difference to +51.

Does nobody else see this same issue, or is it just an easy enough thing for most to ignore? I see it on both my 3.1 and 3.2 board (though I haven't checked whether the OFS range differs on my 3.1 board yet). Both of my boards are installed in controllers with a bunch of wires connected, so I can't check a bone stock board at the moment (I'll probably grab another board at the next opportunity though).

Thanks,
Pat
 
I ended up grabbing another Teensy 3.2 and an LC as well today. Before any soldering, etc... I quickly tested them to see if they were consistent with my other two. The 3.2 again bottomed out at 1, and ADC0_OFS+51 seemed to work for full range (I didn't try finding the limits again to see if 51 was the center though).

The Teensy LC was kinda strange though. It actually bottomed out at 0, but would only go up to 4088 (at 12-bit resolution). By giving it a negative offset of 20 (ADC0_OFS-20) it bottomed out at 3 and maxed out at 4091. So simply changing the offset wouldn't give me full scale at all... I guess maybe the gain setting (ADC0_PG) would need to be tweaked as well.

Anyway, I don't know how much this varies across boards, so I'm not sure whether an official "fixed value" is possible... but I just wanted to post this info in case anyone else ever comes across this, and cares enough to get the last bit (get it?) of range out of their ADC. Below is the code for the test I'm running to check the values.

Code:
void setup()
{
  //serial port for debugging
  Serial.begin(9600);

  //read analog inputs with 12-bit resolution
  analogReadResolution(12);

  //do 32 read averaging
  analogReadAveraging(32);

  Serial.println("Analog test started");
}

void loop()
{  
  Serial.print("\r\n0:");
  Serial.print(analogRead(A0), DEC);
  Serial.print("\r\n1:");
  Serial.print(analogRead(A1), DEC);
  Serial.print("\r\n2:");
  Serial.print(analogRead(A2), DEC);
  Serial.print("\r\n3:");
  Serial.print(analogRead(A3), DEC);
  delay(200);
}

Pat
 
Status
Not open for further replies.
Back
Top