Controllig car led's with pwm

Hey guys,

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:

 1 void setup()
 2 {
 3   pinMode( 9, OUTPUT);
 5 }
 6 
 7 int b = 0;
 8 
 9 void loop()
10 {
11   analogWrite(9, b);
13   delay(500);
14   ++b;
15 }

NOTE: this is not my code, this is also from the blog referenced above.

If someone is able to give some insights, I am forever grateful! Thanks :slight_smile:

edit: made corrections as comment suggested.

what's the code for this longer transition ?

Hi, @joonasm
Welcome to the forum.
To add code please click this link;

What model Arduino are you using?

Tom... :smiley: :+1: :coffee: :australia:

I Have played around with b value and delay. For example

float b = 0;

b += 0.1;

with delay of 100

Hi, Thanks for answer!

I am using arduino uno R3.

analogWrite():

value : the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int .

Fixed code. Thanks for tip!

int b = 0;

I have to use delay to slow down the effect, right? is there a better way to do it?

you could try something like this (untested)

const byte ledPin = 9;
const unsigned long period = 10000ul; // 10s in ms
void setup() {
  pinMode( ledPin, OUTPUT);
}

void loop() {
  analogWrite(ledPin, map(millis() % period, 0, period - 1, 0, 255));
}

% is the modulo operator, so millis() % period will always be between 0 and period-1 which you map to a brightness value of 0 to 255

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.

You only get indeed 256 steps - as you increase the period the same value stays on for a longer time and thus you eye might notice some transitions

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.. :smiley:

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

Which Arduino board are you using ?

Will have to check those out. Thanks for advice.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.