I am relatively new to arduino and electronics overall. I am trying to create car headlights that take input from footwell lights that light up when you unlock the car. That way you get an fade in effect that as soon as you unlock the car.
First I tried to do this with big capacitors and resistors. That worked out fine for one led. But I did not manage to make it slow enough for my led strips. I have four strips and they have 132 led's each.
Goal is to slowly fade in with atleast 10 or maybe up to 20 seconds time interval. Ofcourse when car is turned off, It should fade out the same way.
After I failed with the capacitor approach I learned about something called PWM. I also managed to make it work, kind of. I found a circuit from internet and bought all the components needed. Hooked it up and it actually works. With slight problems thought. This is the circuit:
NOTE: This circuit is not mine, all the credit goes to this blog post. starlight: Dimming a 12V LED strip with a mosfet and PWM (damad.be)
If I use this circuit and make create a loop in arduino that goes from 0 to 255 brightness in few seconds. The fade in is smooth and has no issues. But if I go for a longer transition time. Lets say 10 seconds, it is no longer smooth. It goes in steps now, and I can see these steps when watching these leds. It goes from 0% to 10% brightness instantly. Then waits for a second and then jumps to 20%. This goes on and on until max brightness. It is no longer competely "smooth".
Any ideas where to find the fault? Code is as follows:
Hi, Huge thanks for answer! This actually made an visible improvement.
Still the start of the fade in was a bit fast for my taste, so I slowed it down by increasing the period in your code to 20000ms.
With this adjustment the problem still exists.. Is it possible that there is just not enough "resolution" with this 0-255 adjusting? If this is the case, is there anyway to increase this resolution?
The code you are using? Not the OP post or the other code where you do not show your code that produces the issue. Post the code that produces the issue.
I get the problem with every piece of code I have tried so far. All of the codes work fine when the transition speed is fast. When I start slowing it down, then the stutter starts. But just for example. Code posted below is slow enough. But it generates the problem.
//Setup the output PWM pin
const int outputPin = 9;
// The number of Steps between the output being on and off
const int pwmIntervals = 500;
// The R value in the graph equation
float R;
void setup() {
// set the pin connected to the LED as an output
pinMode(outputPin, OUTPUT);
// Calculate the R variable (only needs to be done once at setup)
R = (pwmIntervals * log10(2))/(log10(255));
}
void loop() {
int brightness = 0;
for (int interval = 0; interval <= pwmIntervals; interval++) {
// Calculate the required PWM value for this interval step
brightness = pow (2, (interval / R)) - 1;
// Set the LED output to the calculated brightness
analogWrite(outputPin, brightness);
delay(100);
}
}
This code was found on a blog post too, It calculates more linear brightness increase.
If this is indeed the case, that there is nothing wrong with the circuit and nothing wrong with the code. Is there any way to get around this problem? Surely someone has somewhere done longer fade in transitions than few seconds..
As pointed out, using analogWrite() you can only get 256 different values, at least if you are using an AVR processor. Using other processors, such as an ESP32 would allow you to use more steps