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.

Comments

Popular posts from this blog

How to Remove �Powered by Blogger� attribution From Blogspot Blog?

Fritzing: an Electronic Design Automation software