0

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.

circuit schematic

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.

4
  • 1
    It sounds like fluctuations in the power caused by the switching of the LEDs. Add some large capacitors across the power near the LEDs.
    – Majenko
    Nov 28, 2018 at 13:46
  • 1
    Have you checked your power source? If it is a switching source you might want to add some capacitors to cancel out the ripple. I suggest you use a scope. If you don't have a scope just connect your voltage source to A1 and analogread(A1). Nov 28, 2018 at 14:28
  • Try adding some delay, say sample the pot just 10 or 20 times a second, vs the ~500 times a second that your code would do now. That gives the PWM outputs a chance to perform vs being constantly given new values to use.
    – CrossRoads
    Nov 28, 2018 at 15:12
  • @Majenko it was an issue with the power source. It's an USB led strip, so I was powering it through Arduino, which was a bad idea as it turns out.
    – jnovacho
    Nov 28, 2018 at 15:57

1 Answer 1

1

It was an issue with the power source. It's an USB led strip, so I was powering it through Arduino, which was a bad idea as it turns out.

When I have added external power source to the circuit everything started working correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.