Saturday, March 19, 2016

Add light sensor to Wise Clock 4

This is long overdue.
As we know, the display brightness on Wise Clock 4 can be changed between 5 levels, by pressing the "Plus" button. To adjust the brightness automatically, based on the light conditions, we need to add a light-sensitive sensor of some sort, and the most common is the LDR (light-dependent resistor).
Any of the countless articles and tutorials on Arduino + LDR will teach how to connect the LDR to an analog pin, using a voltage divider. For Wise Clock 4, the LDR is connected between A0 (pin 40 of the processor) and ground, with a 10k resistor between A0 and Vcc, as shown below:

  GND|----[ LDR ]---A0---[10k resistor]----+Vcc

The automatic brightness adjustment is enabled in software with this macro (in file UserConf.h):

// use an LDR (connected to A0) to automatically adjust
// (every 5 seconds) screen brightness to ambient light;
#define AUTO_BRIGHTNESS

and implemented in this new function:

void WiseClock::checkBrightness()
{
#ifdef AUTO_BRIGHTNESS
  // adjust brightness every 5 seconds;
  if (millis() - lastCheck > 5000)
  {
    int ldrValue = analogRead(0); // A0
    byte brightLevel = map(ldrValue, 0, 1023, MAX_BRIGHTNESS, 0);
    if (nBrightness != brightLevel)
    {
      nBrightness = brightLevel;
      setBrightness(brightLevel);
    }
    lastCheck = millis();
  }
#endif
}

called from the main loop (TheClock.ino):

void loop()
{
unsigned long ms = millis();

checkSound(ms);
wiseClock.checkSerial();
wiseClock.checkScroll(ms);

if ((long)(ms - wiseClock.nextRun) >= 0)
{
alarmClock.getTimeFromRTC();
alarmClock.checkAlarm();
wiseClock.checkMenuActive();
wiseClock.runCrtApp();
}

wiseClock.checkBrightness();
}

It really doesn't get much simpler than this.

I also considered other hardware solutions (and actually bought the parts):
- I2C light sensor TSL2561
but I did not follow through after I saw how effective (and so much cheaper) the LDR is.

4 comments:

  1. Did you have any pictures of the final modification? I'm expecting pin 40 to be A0. I'm not fond of components leads to be dangling around, nor am I to keen on soldering leads directly to socket pins. was your mod on the bottom of the pcb?

    ReplyDelete
    Replies
    1. Photo here:
      https://timewitharduino.blogspot.com/2012/08/status-update.html

      Delete
  2. Thanks Florin. I looked at the schematic and found some good places to tap into it. I have the LDR positioned so it will be on top (opposite of control buttons) and receive ambient light. With the new display configuration, I decided to just rotate the whole clock. The display is now adjusting brightness automatically. Thank you!

    ReplyDelete
    Replies
    1. Please send me photos when you have a chance, so others can get inspired by your creativity as well.

      Delete