analogRead - really basic example failure

davepl
Posts: 29
Joined: Wed Jun 27, 2018 5:29 pm

analogRead - really basic example failure

Postby davepl » Thu Jun 28, 2018 11:19 pm

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));
	}
}

davepl
Posts: 29
Joined: Wed Jun 27, 2018 5:29 pm

Re: analogRead - really basic example failure

Postby davepl » Fri Jun 29, 2018 2:39 pm

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!

Who is online

Users browsing this forum: Baidu [Spider], ulaoulao and 81 guests