A knob, light and heat · 08 of 21

Lab: a night light

The TK20 light sensor on A1 switches the TK01 XL LED on D3 on when the room gets dark, and the TK07 knob on A0 sets how dark. The LED goes off only when the light is half as bright again as the line it came on at, and that gap is what stops it flickering at dusk.

Three modules: the TK20 light sensor, the TK01 XL LED and the TK07 knob. The sketch reads two of them with analogRead and switches the third with digitalWrite.

Where they plug in

ModuleSocketPin
TK07 disc potentiometerD9/A0A0
TK20 ambient light sensorA0/A1A1
TK01 XL LEDD2/D3D3

The D9/A0 and A0/A1 sockets both carry A0, but on different pins: a module in D9/A0 is on A0, one in A0/A1 is on A1. Point the LED away from the light sensor before you start.

Two lines, set by a knob

The TK20 reads higher the brighter it is. A night light with one line, on below it and off above it, works in the middle of the day and the middle of the night. At dusk it does not: the light falls slowly, with small wobbles on top (a shadow, a passing car, ripple a lamp leaves behind), and every wobble that crosses the line switches the lamp.

One line or two, at dusk
1 switch
Knob: the on line60 counts
On below
60 (about 125 lux)
Off above
90 (about 187 lux)
Switches
1
Once. The lamp comes on at minute 21 and the wobbles, smaller than the gap, cannot reach the off line 30 counts higher.

Leave it on One line and move the knob: most settings switch the lamp several times in one dusk. Pick Two lines. The off line now sits half as high again above the on line, the wobbles are smaller than that gap, and the lamp changes once. The gap is called hysteresis, and the TK20's own night light explains it at more length. The trace is drawn to show the behaviour, not recorded.

The gap grows with the line because the wobbles do: a shadow takes a share of the light, not a fixed number of counts. Near the bottom of the knob the gap is never less than 5 counts.

The sketch

The knob gives the on line, and the off line follows it:

int onLine = analogRead(KNOB_PIN) / 4;
int offLine = onLine + max(onLine / 2, GAP_MIN);

So the on line runs from 0 to 255 counts of light. On a typical TK20 one count is about 2 lux, which puts the top of the knob at roughly 500 lux, a bright room; any one sensor can be 30 % either way, which is why the line is on a knob and not in the code.

Then two comparisons, each of which can only move the lamp one way:

if (!lampOn && light < onLine) lampOn = true;
if (lampOn && light > offLine) lampOn = false;

light is not one reading but every reading taken in 50 ms, averaged. That is five whole cycles of a lamp that flickers at 100 Hz and six at 120 Hz, so the ripple cancels, as the TK20's flicker article shows.

Make it yours

  • Fade the lamp on over a second instead of switching it: D3 can PWM, so step analogWrite up from 0 while lampOn is new.
  • Turn it off after a while: note millis() when it comes on and switch it off after ten minutes, whatever the light says.
  • Make the lamp wait: only switch on once the light has stayed under the on line for a few seconds, so a hand passing over the sensor does nothing.

The code

Night_Light.ino

Average the light sensor over 50 ms, take the on line from the knob, put the off line half as high again above it, and switch the LED only when the light crosses the far line. The Serial Monitor prints the light, both lines and the lamp's state.

/*
  A night light                       TinkerBlock UNO kit, /manuals/tkbu

  The TK01 XL LED comes on when the TK20 ambient light sensor reads
  darker than a line, and goes off only when it reads well above it.
  The TK07 knob sets the line, so it can be set in the room it is in.
  The gap between the two lines is what stops a flicker at dusk.

  Wiring, on the TinkerBlock UNO shield:
    TK07 disc potentiometer      in the D9/A0 socket, so it is on A0
    TK20 ambient light sensor    in the A0/A1 socket, so it is on A1
    TK01 XL LED                  in the D2/D3 socket, so it is on D3
  A module is on the second pin printed beside its socket. Point the
  LED away from the light sensor.

  Arduino IDE
    Tools > Board: Arduino Uno
    Tools > Port:  the CH340 port
    No library needed. Serial Monitor at 115200.
*/

const int KNOB_PIN = A0;    // TK07 SIGNAL
const int LIGHT_PIN = A1;   // TK20 SIGNAL: higher is brighter
const int LED_PIN = 3;      // TK01 SIGNAL

const unsigned long AVERAGE_MS = 50;  // 5 mains cycles at 100 Hz
const int GAP_MIN = 5;                // counts, for a line near 0

bool lampOn = false;

// Everything read in 50 ms, averaged: a lamp's 100 or 120 Hz ripple
// cancels out over whole cycles.
int lightLevel() {
  unsigned long sum = 0;
  unsigned int count = 0;
  unsigned long start = millis();
  while (millis() - start < AVERAGE_MS) {
    sum += analogRead(LIGHT_PIN);
    count++;
  }
  return sum / count;
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  int light = lightLevel();

  // The knob sets the on line: 0 to 255 counts of light.
  int onLine = analogRead(KNOB_PIN) / 4;
  // Off only half as bright again, and at least 5 counts above.
  int offLine = onLine + max(onLine / 2, GAP_MIN);

  if (!lampOn && light < onLine) lampOn = true;
  if (lampOn && light > offLine) lampOn = false;
  digitalWrite(LED_PIN, lampOn ? HIGH : LOW);

  Serial.print("light ");
  Serial.print(light);
  Serial.print("  on below ");
  Serial.print(onLine);
  Serial.print("  off above ");
  Serial.print(offLine);
  Serial.println(lampOn ? "  ON" : "");
  delay(200);
}

Set the knob in the room it will live in: watch the light reading at the moment you would want the lamp on, and turn the knob until the on line is just above it. Keep the TK01 pointing away from the TK20, or the lamp's own light switches it off.

View on GitHub · kits/tkbu/night-light/Night_Light/Night_Light.ino @ v1.14

When it does not work

The LED flashes on and off and never settles.

The LED's own light is reaching the sensor. It comes on, the TK20 reads the room as brighter, and the reading climbs past the off line. Turn the TK01 away, or put something opaque between the two. No gap can fix light that the lamp adds to its own sensor.

The LED never comes on, even with the sensor covered.

Check the monitor. If the light reading stays near 0 in a lit room, the TK20 is not in the A0/A1 socket or its cable is out. If the on line reads 0, the knob is at its bottom stop: turn it up. Covered with a fingertip, the reading should fall to a few counts.

It comes on in daylight.

The on line is above what the room reads. Turn the knob down until the on line in the monitor is below the daytime light reading. The light reading is about 2 lux per count on a typical sensor, so 100 counts is roughly a dim indoor room.

Under room lights the reading jumps about.

Many mains lamps brighten and dim 100 or 120 times a second, and the TK20 follows them. The sketch averages everything it reads in 50 ms to cancel that. If you shortened AVERAGE_MS, put it back to 50.

Where this goes next

The thermistor in degrees, and a buzzer that beeps above a line without stuttering on it.

Lab: a temperature alarm →

Edit this page — content/books/tkbu/night-light.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

TinkerBlock UNO R3 Starter Kit, 12 Modules

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →