I'm trying to generate dim two LED strips simultaneously by generating two PWM signals based on potentionmeter value. The pot sets how bright each of the LED strips will be. To drive this I'm using Arduino Pro Micro equipped with Atmel 32u4 chip.
I'm using following code:
int sensorValue = 0;
int outputValue = 0;
void setup() {
}
void loop() {
sensorValue = analogRead(A0);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(5, outputValue);
analogWrite(6, 255 - outputValue);
delay(2);
}
The problem is, the analogRead
never returns 1023 when the pot is fully open. The value is always around 980. This itself would not be a big deal.
The main issue is that the analogRead
is incredibly noisy. The value returned for the maximum position is in range of [940-1020].
When I don't use the analogWrite
to generate PWM, the problem goes away an the input is crystal clear in range [0-1023] as expected.
I have tried to turn off the PWM before reading the value in, but this generates flicker, as the LEDs are temporarily off.
Is there any HW or SW solution to this? I don't mind writing some assembly if required.