Night light

Hi all,

I'm currently making a project for my child's bedroom and would really appreciated some guidance.

The project will essentially be an LED which when you press a switch, it fades from full brightness to being turned off over a 15 minute period. Once the light has turned off I need it to stay off. The latter part is problematic because technically an Arduino can't 'stop'.

What's the best way to prevent the loop from starting again once the light has fully faded? I have thought about forcing it in to a loop but I want it to be armed and ready to do future 15 minute fades once the first is finished.

I have thought about combining a 555 monostable that would power up the arduino for 15 minutes but that seems a bit of a bodge and I'd rather learn how to code the solution.

I'd appreciate being pointed in the right direction rather than just been given a solution as I'm trying to learn as much as I can from the project.

This is the slightly modified code I have so far (without the switch):

int led = 9;
int brightness = 255;
int fadeAmount = 1;

void setup() {
  pinMode(led, OUTPUT);
}

void loop() {
  analogWrite(led, brightness);

  brightness = brightness - fadeAmount;

  delay(3529);
}

Just put in “while (brightness <=0){}” to freeze up the processor when the LED has gone out. When you reset the processor (Ground the Reset pin) the sketch will restart at full brightness.

Hi John,

Thank you for the reply. I tried the ‘while’ method you mentioned and it worked a treat but for some reason the LED was left turned on (very dim, I presume brightness=1).

I made it work by modifying the code as below… not sure this is the best solution or indeed why it didn’t work in the first place but it is working which is the main thing.

I wasn’t aware that shorting reset to gnd was a feature so thanks for that, this is a great but simple solution.

  while (brightness <=0){
      analogWrite(led, 0);
}

JacksonGee: Hi all,

I'm currently making a project for my child's bedroom and would really appreciated some guidance.

The project will essentially be an LED which when you press a switch, it fades from full brightness to being turned off over a 15 minute period. Once the light has turned off I need it to stay off. The latter part is problematic because technically an Arduino can't 'stop'.

But it can be put to sleep.

This is a slightly modified solution, untested. Suggest; use the Arduino reset button to activate.

int led = 9;
int brightness = 255;
int fadeAmount = 1;

void setup()  // put things here that only run once
{
  pinMode(led, OUTPUT);

  while ( brightness >= 0 ) // or some other value for OFF
  {
    analogWrite(led, brightness);
    brightness = brightness - fadeAmount;
    delay(3529);
  }

  // put Arduino sleep code here
}

void loop() {
}

How is the LED switched on in the first place? I assume at least that the light is on already, and only when it’s time to go to sleep it’s told to slowly dim.

(you could easily add code to have the Arduino do the opposite 8 hours later: slowly go to full brightness).

Use blue LEDs and leave it on all the time. The eyelids are full of blood which is red, and completely filters out any (pure) blue light. Once you close your eyes, it is as if complete darkness as far as the human body is concerned.

wvmarle: How is the LED switched on in the first place? I assume at least that the light is on already, and only when it's time to go to sleep it's told to slowly dim.

(you could easily add code to have the Arduino do the opposite 8 hours later: slowly go to full brightness).

You leave a night light on all day? Why?

GoForSmoke: You leave a night light on all day? Why?

Oh, right. Mine does automatically switch off in the day time. Good point. It's debatable which would use more power - leaving it on, or running a PIR or clap sensor to turn it on in the middle of the night if needed.

GoForSmoke: You leave a night light on all day? Why?

Never said I would leave it on all day. Just switch it on when needed, like when going to bed, and before starting the countdown sequence. A physical switch for the whole thing (Arduino + light) would work great for that. When switched on, full brightness, then upon button press starting the dim sequence. Just wondering how bright your night light really is, as the ones we have are so dim it's only just enough to find your way around. Dimming those even more just doesn't make sense.

With the sketch I left and AVR sleep code added the Arduino reset is the ON button and when the light gets to zero, it sleeps.

A bigger ON/reset button can be wired between the reset pin and ground. Activation will be on button release after press.

It is a shame that you can only run one led at a time with an Arduino. You'd think they'd have thought of that by now?

GoForSmoke:
It is a shame that you can only run one led at a time with an Arduino. You’d think they’d have thought of that by now?

People are routinely controlling whole LED strips and all kinds of high-powered equipment by Arduinos and other microcontrollers. Just add a driver transistor or MOSFET and proper power source.

Arduinos are controllers, not power supplies. It is trivial to add a driver circuit. It is not trivial to allow a microcontroller to supply a few amps of current on each of its I/O pins.

Night light needs how much light? Arduino can safely source more power than a night light needs. It is even more trivial to use less unnecessary circuitry.

White led typical forward voltage is 3.3V.

220R is what usually gets used with leds here,
With 220 ohm resistor and 5V input current would be (5-3.3)/220 = 7.7mA through the led.
With 68 ohm resistor and 5V input, it would be 25mA which is not going to hurt the pin and be much brighter.
Next higher standard value is 82 ohms to get 21mA through the led, closer to 50% maximum pin current.

Arduino is what it is which in at least one case is more than “it’s supposed to be” in a purist view.

It’s not like we don’t teach people to run limited numbers of leds directly off the board here. WHY NOW A DIFFERENCE?

Actually, my night light (which is part of a clock) is a blue LED directly driven from an Arduino Mega2560 output with a 220 ohm limiting resistor. There is also a red and green LED for daytime effects, so the most current it draws is about 25mA. It was a quick and dirty build on perf board that plugs right into a few of the Mega pin sockets. I wouldn't do it any other way unless I needed a lot more current.

A high efficiency LED can easily dimly light up a room at 7mA. Your eyes become night adapted after about one minute, and it's amazing what you can see by.

Thanks for all of the replies. The light is for my 6month old daughter so starting the 15 minute countdown will be done by my better half or myself.

When we turn the light off at the moment she doesn't like it being so dark so the idea is to slowly create the dark room like you may cook a frog by slowly rising the temperature :D

I have gone for a red light as I believe blue light is more likely to keep you awake. This could easily be changed if I'm wrong so happy to play about with this.

I have 3D printed a transparent enclosure which acts like a light diffuser. The lights are 2 x 12v LEDs in parallel so are fairly bright, I think they're designed for light clusters in a car or perhaps interior lights, either way they're pretty powerful.

I am now considering having just the one switch which will be a reset switch so when the arduino is powered up it will run the code once and then wait to be activated again the next night.

Ideally I'd have a way to cut power to the arduino after 15 minutes or properly put it to sleep but I'm not sure how to go about that yet.

This is what the enclosure looks like with the LEDs poking through a hole in the side.

JacksonGee: I have gone for a red light as I believe blue light is more likely to keep you awake. This could easily be changed if I'm wrong so happy to play about with this.

You're not wrong concerning the research, but it's taken out of context. People who are exposed to high levels of blue light do not fall asleep as easily, prior to sleep. An example is sitting in front of a computer screen or TV. The studies that were done didn't analyze the effects of pure blue light, just bright light that has a very strong blue component. It also didn't analyze the effects with the eyes closed. So that research really doesn't apply to a blue night light.

It really doesn't matter what colour it is if it is slowly turned down. That would get into more psychological or perceptual aspects. Whatever colour makes you feel relaxed, I guess. My point about the blue, is that unlike any other light, once you close your eyes, it is effectively 100% off. I proved it by flashing the light right in front of my face, I could not see the flashing at all. But it won't work unless the colour is pure chromatic blue. A filtered blue still has enough red and green to make it through the eyelids.

It has a measure of psychological comfort, in that you can open your eyes and see the room at any time, without impacting the darkness with your eyes closed that you need for a good sleep.

Besides, 7 of 9 regenerated with blue light. :)

Hi Jackson,
I think you should go down to basics…
You can add a ldr(you can adjust the value you need in the code) which is sort of a trigger to switch on and off the light. In addition you can keep the switch parameter like a toggle switch. Connect the switch to Vcc.
Add it so that it is connected as shown in the diagram attached.
Here is the improved code which I think might help you.

int fadeAmount = 1;
int tog=8;
int ldrvalue;
int switchval;

void setup() {
  pinMode(led, OUTPUT);
 pinMode(tog, INPUT);
}

void loop() {
ldrvalue = analogRead(ldrPin);
switchval = digitalRead(tog);
if (ldrvalue <= Trigval){
 if (switchval == LOW){
  while(brightness = 0){
  analogWrite(led, brightness);
  brightness= brightness-fadeAmount;
 delay(3529);
  }
 
}
else{
digitalWrite(led, HIGH);
}
}
else{
 digitalWrite(led, LOW);
}

}

ldr.png

The room is almost always pitch black due to really effective blackout blinds so an LDR wouldn't work in this setup, thanks for the idea though Nandan.

I don’t know if i understood correctly or if i can help you but this is what i thought…

You simply write a code what will fade the led in 15 min.

You attach an interrupt to a pin that the button is connect to. So when you push the button the arduino will wake up and do the loop.

You end your code(loop) with a deep sleep function

edit:

//including the libraris for sleep modes
#include <avr/power.h>
#include <avr/sleep.h>

#define ledPin 9 // must be a pwm pin
#define buttonPin 2 // must be and interrupt pin

int dimming_time_seconds = 900; // HERE you can adjust the time period of fading in seconds (15min*60s=900)

void go_sleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set arduino to deep sleep
  sleep_enable();
  attachInterrupt(digitalPinToInterrupt(buttonPin), wakeUp, CHANGE); // add interrupt on the button PIN
  //CHANGE means - this will wake up the arduino if the voltage is changed on this pin
  sleep_mode();            // here the device is actually put to sleep!!
  // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

  led_status = 1; //set the flag for the led status as ON
  led_dim = 255; //another flag
  analogWrite(ledPin, led_dim); // turn on the led

  sleep_disable();         // first thing after waking from sleep:
  detachInterrupt(digitalPinToInterrupt(wakeUpPin_RF)); //and remove the interrupt until the next sleep
}

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); //or INPUT
}

void loop() {
  //time in seconds * 1000ms(each secound) = delay time between each light step down
  delay(dimming_time_seconds * 1000); // used for time period
  
  if (led_status == 1) { //if the led is currently ON
    if (led_dim != 0) { //if the led is not on the minimum opacity(0/OFF)
      led_dim = led_dim - 1; //reduce the led opacity step by step
      analogWrite(ledPin, led_dim);
    }
    else { //if the led_dim is 0 (the led is off)
      ledi_status = 0; // set the led flag to OFF
      go_sleep(); //go to sleep
    }
  }
  
}

void wakeUp()
{
  //nothing here
}

I follow what you mean in terms of a the idea but the code is beyond what I can fully understand at the moment.

Once I’ve got my head around how to design/construct code I’ll come back to this as I’d quite like to remake the project with a battery instead.

Had a bit of a nightmare once it was all assembled, nothing I could do could get it to work even though I’d already proven the code before…

An hour later I realised that the switch I had used was a PTB not PTM :blush: I was convinced I had ordered a PTM so I didn’t even think so check, the PTB switch was forcing the arduino to constantly restart, doh!

snip_20170803003724.png