Page 1 of 1

analogRead - really basic example failure

Posted: Thu Jun 28, 2018 11:19 pm
by davepl
Using PIN36, which is VP or A0 on my Devkit V1 (I think!).

The loop always prints the max value depending on what resolution I've set, such as 4095 by default. So in that sense the call is succeeding, but I get that value no matter whether the pin is connected to GND or 3.3 or a microphone!

So clearly I'm doing something pretty basic wrong, but what?

Code: Select all

void loop()
{
	adcAttachPin(AUDIO_PIN);
	adcStart(AUDIO_PIN);
	analogReadResolution(10); // Default of 12 is not very linear. Recommended to use 10 or 11 depending on needed resolution.
	analogSetAttenuation(ADC_6db); // Default is 11db which is very noisy. Recommended to use 2.5 or 6.

	pinMode(AUDIO_PIN, INPUT);
	for (;;)
	{
		Serial.println(analogRead(AUDIO_PIN));
	}
}

Re: analogRead - really basic example failure

Posted: Fri Jun 29, 2018 2:39 pm
by davepl
I fixed it, and it's a bit embarrassing, but since others noobs could hit it, I'll share:

You need a delay() of some kind in your loop or the actual ADC never seems to run. Simple as that.

Once I added a delay and adjusted the gain, I got results!