Timed Dimming Help

Hi,

Im very new to Arduino and Im trying to find some code that will allow me to dim an LED on and off at various times,

for example:

On: 10:00
Off: 19:00

I want the times to be hard coded.

Ive got an Arduino uno and a DS3231 RTC

Can anyone point me in the direction of some sample code?

Thanks!

start here

If you want to control the brightness of a led, look up pwm (pulse width modulation).

Thanks for your replies, Ive taken a look and started to write some code. Ive wired up my 24v LED via a IRF840 MOSFET

it sort of works but with strange results... it dimms to about 70-80% then back up to 100% using the following code:

int ledPin = 11;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

not sure why it wont fade to off!

any ideas?

What does a simple " analogWrite(ledPin, 0);" do? (no loops, just this statement in setup())

Do you have a schematic?

int ledPin = 11; // LED connected to digital pin 9

It helps when code and comment agree

wreckitsteve:
Thanks for your replies, Ive taken a look and started to write some code. Ive wired up my 24v LED via a IRF840 MOSFET

it sort of works but with strange results... it dimms to about 70-80% then back up to 100% using the following code:

int ledPin = 11;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

// fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}




not sure why it wont fade to off!

any ideas?

Sorry about the comment confusion, i forget to edit it. When i put the code you said in setup it just stays on about about 50% bright.

Do you have a schematic?

im totally new to electronics so dont know how to draw one yet

well i think ive solved it! I swapped out the MOSFET for a new one that I had (same type) and it now works like a dream!!

Maybe faulty MOSFET?

wreckitsteve:
Maybe faulty MOSFET?

An IRF840 is not a logic level mosfet.
The 5volt logic of an Arduino might or might not turn it on sufficiently.
You might have been lucky that the second fet had a lower gate threshold voltage.
Leo..

Wawa:
An IRF840 is not a logic level mosfet.
The 5volt logic of an Arduino might or might not turn it on sufficiently.
You might have been lucky that the second fet had a lower gate threshold voltage.
Leo..

Ahh i see, what MOSFET should I be looking for?

One described as "logic level"

CtrlAltElite:
One described as "logic level"

thank you