LED light dimming (beginner)

Hello, I'm very new to arduino. And recently I'm trying to create an LED light that dims over time and turns off after 1.5-2hours. Should I start out with the fade example first..or? And what are the things I need?

edit: i'm trying to make a 'reverse' sunrise clock

you need an LED, the proper resistors , if it is other than a simple small LED, you should look at the data sheet to see how much power it needs and what voltage drop is has. you can use an on-line led calculator to figure out the resistors needed.

fade is the right place to start. however it does nothing for your long time

there are two ways to allow for the passage of time.

delay() can be used, however it is like a complete halt for the time it takes to count, we say is blocks the program.
it blocks anything else from being done.

the alternate is use blink without delay. this uses the system clock to determine the elapsed time and is much more accurate than delay()
it is MUCH harder to understand than delay().

it all starts with your switch or button. something that can load the start time, but does not get reset on every loop of the program.

If the LED runs at 100mA or over then you need a constant current driver not a resistor to limit the current.

dave-in-nj:
you need an LED, the proper resistors , if it is other than a simple small LED, you should look at the data sheet to see how much power it needs and what voltage drop is has. you can use an on-line led calculator to figure out the resistors needed.

fade is the right place to start. however it does nothing for your long time

there are two ways to allow for the passage of time.

delay() can be used, however it is like a complete halt for the time it takes to count, we say is blocks the program.
it blocks anything else from being done.

the alternate is use blink without delay. this uses the system clock to determine the elapsed time and is much more accurate than delay()
it is MUCH harder to understand than delay().

it all starts with your switch or button. something that can load the start time, but does not get reset on every loop of the program.

is there an online tutorial of some sort? (blink without delay)

dave-in-nj:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

is it better if i use a timeswitch?

M0nsterr:
is it better if i use a timeswitch?

No.

use the examples, dim your LED over a minute or two.

if you want to dim your LED over an hour or two, anyone watching will have a hard time knowing it is dimming.

also, LEDs do not dim in linear fashion, the brightness is not linear to the pwm of analog out.
there are dimming routines that will appear much more linear, once you get to that point.

There is a [u]Fade Example[/u]. You can modify it to start-out at full brightness and then fade-down and hold in the off-state when it's done.

During development, I suggest you use a shorter time so you can test & debug without waiting one or two hours between test runs.

also, LEDs do not dim in linear fashion, the brightness is not linear to the pwm of analog out.

That might not be a problem since the change is very slow. It could be critical if you want it to appear half-bright at half-time...

Years ago, I made a "regular" sunrise dimmer (which I'm still using) for a "regular" incandescent lamp and it's not linear either. I thought that might be an issue when I made it, but the 10-minuite period I use is long enough so that you can barely see it's changing and you can't tell if the rate of change constant or not.

is it better if i use a timeswitch?

My setup uses an external AC timer switch to turn on the power to start the sequence, then it turns power off about an hour later. The microcontroller (which is not an Arduino) just handles the dimming. If you need a time-of-day clock to start your sequence at the same time everyday, that's the easiest way to handle it. But if you want to do something more advanced, you can add a real-time clock module and an LCD, and build a clock into your device.

If you do build a clock into your design, you'll need to avoid the delay() function because although the clock module will run in the background during the delay time, your program gets paused during delay() and it cannot respond to button pushes, and it can't update the LCD time display, etc., during the delay() time.