Using it · 09 of 11

A light switch

A TK20 light sensor in IN, the switch at L, and a TK01 lamp that copies DIG: dark means HIGH means on. The sketch is four lines. The surprise is the knob: at 3V3 a dusk threshold of about 50 lux is about 0.12 V, only a few per cent of the way round from the GND end.

Three blocks

A TK20 Ambient Light Sensor pushes into the TK29's IN socket. Its SIGNAL rises with the light, about 0.24 V at 100 lux for a typical part, so it is the input. The TK29's OUT goes to your board as in the first read, and a TK01 XL LED goes on another pin as the lamp.

Put the slide switch at L. Then DIG is HIGH while the light is lower than the knob: HIGH means dark, which is when the lamp should be on. The sketch needs no test at all; it copies DIG to the lamp.

Where dusk is on the knob

A light switch
Light on the TK20200 lux
Knob, % of its travel3.5 %, 0.12 V
TK20 SIGNAL
0.47 V
Threshold
0.12 V
Lamp
off
At 200 lux the TK20 gives 0.47 V, above the knob's 0.12 V, so DIG is LOW and the lamp stays off. The TK29's own LED is lit, because it shows LOW. Bright light can take SIGNAL above the comparator's range; with the knob this low that does no harm.

Dusk in a room is a few tens of lux. The TK20 turns that into about 2.35 mV per lux, typical, so a lamp that comes on around 50 lux needs a threshold of about 0.12 V. From 3V3, that is about 3.5 % of the knob's travel from the GND end. A whole lit room, 500 lux, is still only about 1.2 V.

So everything that matters for a light switch happens in the first sliver of the knob, and a small turn moves the switching point a long way. Do not set it by numbers. Wait for the light you want, or dim the room to it, and turn the knob slowly until the TK29's red LED goes out. At L, the LED is lit while it is light, and it goes out at the moment DIG goes HIGH.

The bright end is safe

Bright daylight can take the TK20's SIGNAL up towards 2.9 V from 3V3, above the comparator's input range of about 1.8 V. That does no harm here: the knob is far below the range's ceiling, and one input inside the range is enough for a right answer. See the input range.

What it does at dusk

Run it and dim the room slowly. The serial monitor prints light and ANA falling, then dark as ANA passes the threshold, and the lamp comes on. Then, very often, it goes off again, and on, and off, for a few seconds while the light hovers at the line. That is not the sketch; it is the comparator doing exactly what it is told. Chatter and hysteresis is the fix.

The code

Reads DIG and writes it straight to a TK01 XL LED on another pin. The switch at L makes DIG HIGH when it is darker than the knob, so no test is needed. ANA is printed alongside so you can see the TK20's reading when it switches.

adc_light_switch.ino
/*
  Analog to Digital Signal - a light switch              TK29 / /p/tk29

  A TK20 Ambient Light Sensor pushed into IN, parts facing the same
  way. Slide switch at L: DIG is HIGH while it is darker than the knob.

  Wiring, the TK29's OUT. Count from the square pad, parts up, OUT at
  the bottom:

    GND -> GND
    VCC -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
           (DIG is pulled up to VCC)
    DIG -> D2 on an Uno, GPIO 25 on an ESP32, GPIO 7 on an
           ESP32-S3, GP15 on a Raspberry Pi Pico
    ANA -> A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an
           ESP32-S3, GP26 on a Raspberry Pi Pico

  The TK01 XL LED, counted the same way. Point it away from the TK20:

    GND    -> GND
    NC     -> nothing   (both of its NC pins)
    SIGNAL -> D9 on an Uno, GPIO 4 on an ESP32, GPIO 5 on an
              ESP32-S3, GP14 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. Arduino Uno
    Tools > Port                  the one that appears when you plug in
    Tools > USB CDC On Boot       Enabled   (ESP32-S3 only)
    No library needed.
*/

// DIG_PIN, then ANA_PIN.
// Uno: 2 and A0. ESP32: 25 and 34. ESP32-S3: 7 and 4. Pico: 15 and 26.
const int DIG_PIN = 2;
const int ANA_PIN = A0;
// The TK01's SIGNAL. Uno: 9. ESP32: 4. ESP32-S3: 5. Pico: 14.
const int LAMP_PIN = 9;

void setup() {
  Serial.begin(115200);
  pinMode(DIG_PIN, INPUT);          // the block has its own pull-up
  pinMode(LAMP_PIN, OUTPUT);
}

void loop() {
  int dark = digitalRead(DIG_PIN);  // at L: HIGH when darker
  digitalWrite(LAMP_PIN, dark);     // the lamp copies DIG

  Serial.print(dark == HIGH ? "dark   ANA " : "light  ANA ");
  Serial.println(analogRead(ANA_PIN));
  delay(100);
}

Set the knob in the light you want the lamp to come on at: turn it until the TK29's red LED goes out, and stop. Point the TK01 away from the TK20, or its light reaches the sensor and turns it off again.

When it does not work

The lamp is on in daylight and off in the dark.

The switch is at H. At H, DIG is HIGH while the light is brighter than the knob. Slide it to L, where HIGH means darker, and the lamp follows the dark.

The lamp never comes on, however dark it gets.

The knob is at the GND end, below anything the TK20 gives even covered. Cover the sensor with a finger and turn the knob slowly away from that end until the TK29's LED goes out. That is the threshold at the darkness of your finger; back off a little for dusk.

Turning the knob a hair changes the switching light a lot.

Yes. A dusk threshold is a tenth of a volt or so, and from 3V3 the whole knob spans 3.3 V, so dusk is a few per cent of its travel. Turn it in very small steps, and set it in the light you want rather than by the numbers.

The lamp flickers as the evening fades.

At the threshold the light wobbles across it, and the TK29 has no hysteresis, so DIG follows every wobble and the lamp with it. The next article fixes it in the sketch. Keep the lamp's own light off the sensor too, or it switches itself off.

Where this goes next

Why the lamp flickers at dusk, and the two lines in the sketch that stop it.

Chatter and hysteresis

Edit this page — content/books/analog-to-digital-signal/a-light-switch.mdx

Community

Questions about this product

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

Ask a question ↗

Analog to Digital Signal

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