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;

and implemented in this new function:

void WiseClock::checkBrightness()
  // 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;
    lastCheck = millis();

called from the main loop (TheClock.ino):

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


if ((long)(ms - wiseClock.nextRun) >= 0)


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.

No comments:

Post a Comment