Automatic Nightlight

This is a simple project to solve a very specific problem: I can't easily turn off my bedroom light from in bed. The switch on the lamp is out of reach, and there's also not really anywhere to stick a remote control. To solve this, I decided to just build a simple contraption that would turn on a light for 30 seconds if it detected that the room had suddenly become dark — basically, enough time and illumination to climb into bed and get settled while still being able to see. This could probably be done in some elegant analog way with a comparator and a 555 timer (or without DIYing anything at all, but that's no fun), but I decided to do it a lazier way and just use a microcontroller and whatever else I already had on hand. Since I already have a collection of different cheap USB lights, I decided to have it just toggle power to a USB connector, so I could swap in different lights. I prototyped it with an Arduino so I could spit the light readings into the serial terminal while tweaking values, then moved everything over to an ATtiny85 once I was satisfied with how it works. Here's how everything is connected, there's not much to it:



The MOSFET is just one I had sitting in my parts drawer. I chose a 20k resistor to form the voltage divider with the LDR because there was one within reach on my desk. The code just reads in the analog value from the voltage divider to determine a rough light level, and if it's below the "dark" threshold it turns on the light for 30 seconds, then back off again. It then stops checking for darkness until a "light" threshold is reached, either when morning arrives the next day or the room lights are turned back on. There was a bit of tweaking required to find the right values and physical arrangement, because at too low of a "light" threshold, some USB lights actually shone enough light onto the sensor to make it think the room was fully lit again, turning the whole thing into an incredibly slow, awkward oscillator.


bool roomLit = true;
int thresholdDark = 200;
int thresholdLight = 500;

#define LIGHTPIN A2
#define OUTPIN 3

void setup() {
  pinMode(LIGHTPIN, INPUT);
  pinMode(OUTPIN, OUTPUT);
}

void loop() {
  int light = analogRead(LIGHTPIN);
  if (light >= thresholdLight + 100)
    roomLit = true;
  else if (light < thresholdDark && roomLit) {
    roomLit = false;
    digitalWrite(OUTPIN, HIGH);
    delay(30000);
    digitalWrite(OUTPIN, LOW);
  }
}

To finish the project, I haphazardly soldered all the parts to a scrap of prototype board, coming up with a layout that didn't require any wire jumpers completely by accident. Then I stripped the end off of an old proprietary USB cable for power, added some hot glue to the cable and USB connector for stability, plugged it into a phone charger by my bed, and duct taped it into place on the bed frame. Now whenever I turn out the light at night, it powers up a USB LED strip for 30 seconds, which is enough to dimly light the entire room.




The prototype, using an Arduino Mini clone to turn on a USB LED strip


Final version, top


Final version, bottom