I am attempting to control brightness of vehicle LED lights using PWM. This is on a custom pcb with an atmega328p, which analogWrites a 122Hz PWM signal to an AND gate, which then passes along to a high-side switch (TPS22810) that powers the lights. Fully on, one of these lights operates at 14V, 0.5A. The issue is that the lights unexpectedly flicker off at repeatable threshold points in the program, and I'm hoping someone can shed some light.
Am I missing a simple hardware or software adjustment I can make on my end? Or is it very likely that there's some capacitance or inductance internal to the lights' electronics (that I'm not privy to) that is causing this behavior?
The simplest code I've developed to reproduce my issues is this, where pin 1 is a potentiometer I control. You'll see I'm trying to have the light either at full brightness or at a range of low brightnesses, depending on the potentiometer.
void loop() {
newMin = analogRead(1);
if (newMin >= 1004) {
minBrightness = 255;
}
else {
minBrightness = map(newMin, 0, 1004, 0, 25);
}
analogWrite(3, minBrightness);
}
Anecdotes:
-
This code results in a flicker off every time I increase the potentiometer from less than 1004 to greater than 1004. So for example, brightness starts at 0 then increases steadily, then drops out for a split second, then resumes at full brightness. The light decreases in brightness as expected, however, without flickering.
-
Changing the "25" to 40 produces the opposite behavior. It increases to full brightness correctly, but then does the flicker on the decrease.
-
Changing the "25" to 46 seems to get rid of the problem when the engine is off. However the problem comes back when the engine is on.
-
Everything works as expected in bench testing with "normal" (non-vehicle) LEDs.





