I am experimenting with using different PWM frequencies for controlling some DC motors. I've discovered that changing the PWM frequency also affects analogRead(), and need to understand what's going on.
For example, I have this code that I'm using to set the PWM frequency for output 10, to 31kHz (this is on a Yun):
TCCR1B = _BV(CS10); TCCR1A = _BV(WGM10); TCCR1A = TCCR1A | _BV(COM1A1) | _BV(COM1B1) | _BV(COM1C1);
This works fine, it sets the PWM frequency as desired, as I verified with an oscilloscope.
Then, later on in my sketch, I am reading from pin A5, using:
int val = analogRead(A5);
This also works fine.
Once having adjusted the PWM frequency as per above, I write to pin 10 using:
OCR1B = x;
where "x" is a value from 1 to 255. This works fine if and only if x is 0. If I write a non-zero value to OCR1B, my reads from A5 become random.
Can anyone explain this? Why can't I do an analogRead() after adjusting the PWM frequency, and is there any way to get this to work?