TK20ANALOGbeginner

Ambient Light Sensor

A phototransistor and a 4.7 kΩ resistor on a TinkerBlock board: light makes a current, the resistor turns it into a voltage, and SIGNAL rises with the light. A lit room reads low on the scale, bright light flattens just under VCC, and it sees near infrared best.

Comes in this kit — not sold separately

Specifications

TypeAnalog light sensor: an NPN phototransistor with a 4.7 kΩ load. More light, higher voltage on SIGNAL
SensorNEWOPTO XYC-PT21C-L1 phototransistor, a clear 1206 package (3.2 × 1.5 mm) near the top of the board. Not an LDR
OutputAbout 0.5 µA per lux through 4.7 kΩ: about 0.24 V at 100 lux for a typical part, about 0 V in the dark. Any one part within 30 % of typical
CeilingAbout VCC − 0.4 V: about 4.6 V from 5 V (near 1960 lux, typical) and about 2.9 V from 3.3 V (near 1230 lux). Brighter light reads the same
Response15 µs switching and a 0.47 ms filter (4.7 kΩ with 100 nF): fast enough to follow the 100 or 120 Hz ripple of many mains lamps
SpectrumPeaks at 850 nm, in the near infrared; responds from 400 to 1100 nm. Not calibrated in lux
Supply3.3 V or 5 V on VCC. VCC sets the ceiling, not the reading, so use 3V3 beside an ESP32, ESP32-S3 or Pico and 5V beside an Uno
CurrentUnder 1 mA at most, at the ceiling from 5 V; tens to a couple of hundred µA in a lit room
Pins to wire3 of the 4: GND, VCC and SIGNAL. NC is connected to nothing on the board
Header4-pin right-angle male, 2.54 mm pitch: GND, VCC, NC, SIGNAL, with GND on the square pad
Board22.4 × 30.4 mm, two 4.8 mm mounting holes 16 mm apart. No LED on the board
In the box1 × TK20 block. It also ships inside the TinkerBlock kits

What it is

A light sensor, a resistor and a capacitor. The sensor is a phototransistor: light lets a current flow through it, about half a microamp per lux for a typical part, and the 4.7 kΩ resistor turns that current into a voltage. That voltage is SIGNAL. Dark, about 0 V; a lit room, a few tenths of a volt to about a volt; bright light, up to just under VCC.

The TK20 at an angle: a black board with a wide gold border, a small clear rectangular sensor near the top with a C and an E printed either side, a brown capacitor and a black resistor below it, a large gold sun at the top right, two big mounting holes, lonely binary along the left edge, a boxed ANALOG along the right, and a right-angle header whose four pins point out past the bottom edge.
The TK20. The sensor is the small clear part near the top, between the printed C and E.

Three things about it decide how a sketch should treat it. It is fast, switching in about 15 µs, so it sees the ripple in many mains lamps and a single reading jitters; averaging over 50 ms cures that. It sees near infrared best, peaking at 850 nm, so the same brightness to your eye reads very differently under different lamps. And the number is not a unit: any one sensor can be 30 % more or less sensitive than typical. Calibrate at runtime: read the two conditions you care about and put the threshold between them.

The page this replaces called the sensor an LDR, a resistor that changes with light, and said it was slow and saw green best. None of that is this board.

VCC sets the ceiling, not the reading

The light sets the current and the resistor sets the voltage, so in a room VCC changes nothing. What VCC does set is how high SIGNAL can go: about 0.4 V under it, where bright light flattens out.

Your boardVCC toA lit roomBright lightReads up to
Arduino Uno5Vroughly 50 to 240about 4.6 Vabout 940 of 1023
ESP32, ESP32-S33V3roughly 180 to 1470, illustrativeabout 2.9 Vabout 3800 of 4095, illustrative
Raspberry Pi Pico3V3roughly 70 to 360about 2.9 Vabout 900 of 1023

Never 5V beside a 3.3 V board: bright light would take SIGNAL towards 4.6 V. The lux and room figures are a typical part under the datasheet's light, which stops at 100 lux; above that they are the same line extended. On an ESP32, a room under about 43 lux reads 0.

Which pin is which

Parts up, header at the bottom, reading left to right:

GNDto your board's GNDthe square pad: count from here
VCCto 3V3 or 5Vyour board's logic voltage
NCnothingnot connected on the board
SIGNALto an analog inputhigher with more light

The back prints TK20 AMBIENT LIGHT instead of pin names. Turned over, the square pad is on the right, and it is still GND. There is no LED: test it by covering the sensor and shining a torch on it.

Wiring, in three lines

  1. GND to your board's GND.
  2. VCC to 5V on an Uno, 3V3 on an ESP32, ESP32-S3 or Pico.
  3. SIGNAL to an analog pin: A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an ESP32-S3, GP26 on a Pico. On an ESP32, use an ADC1 pin so Wi-Fi does not stop the reading.

Leave NC unconnected.

Example

// The analog pin SIGNAL is wired to.
// Uno: A0. ESP32: 34. ESP32-S3: 4. Pico: 26.
const int LIGHT_PIN = A0;

void setup() {
  Serial.begin(115200);
}

void loop() {
  // The mean of everything read in 50 ms: whole cycles of a
  // mains lamp's 100 or 120 Hz ripple, so the ripple cancels.
  long sum = 0;
  long n = 0;
  unsigned long start = millis();
  while (millis() - start < 50) {
    sum += analogRead(LIGHT_PIN);   // more light, higher
    n++;
  }
  Serial.println(sum / n);
  delay(200);
}

Where to start

The handbook below is ten short articles, each with a working figure. The first reading is the whole build in three wires and a few lines, and not an LDR is what the part really is.

For a light that comes on at dusk, a night light is the project. If the numbers wobble, read the flicker under room lights; if an ESP32 reads 0, reading it on an ESP32.

And before plugging it into an ESP32, VCC sets the ceiling is the one page that protects the board.

When it doesn’t work

What kind of sensor is it?
A phototransistor, the NEWOPTO XYC-PT21C-L1. Light lets a current through it in proportion to the light, and a 4.7 kΩ resistor turns that current into the voltage on SIGNAL. It is not an LDR, though this page used to say so, and the difference shows in its speed, the colour it sees best and what VCC does.
What should a lit room read?
Less than you might expect. A lit room is roughly 100 to 500 lux, which on an Uno reads roughly 50 to 240 of 1023. Covered, it reads near 0; a phone torch held close takes it to about 940, the ceiling from 5 V. Your numbers depend on the light and on the part.
Should VCC go to 3V3 or 5V?
To your board's logic voltage: 3V3 on an ESP32, ESP32-S3 or Pico, 5V on an Uno. In a room VCC makes no difference to the reading, but bright light takes SIGNAL up to about 0.4 V under VCC, so 5V on VCC can put about 4.6 V on a 3.3 V pin the first time the sun reaches it.
Why does my ESP32 read 0 in a dim room?
Its ADC reads nothing below about 0.1 V at the Arduino core's default setting, and this block gives about 0.1 V at about 43 lux for a typical part. Below that an ESP32 reads 0 while an Uno still reads a little. Shine a torch on it: if the number jumps, the block is fine.
Why does it read so differently under different lamps?
It is most sensitive at 850 nm, in the near infrared you cannot see. A filament or halogen bulb, or daylight, gives out plenty of infrared and reads several times higher than a white LED that looks just as bright. A TV remote makes it jump. Calibrate under the light it will live with.
Why does the reading jitter under my ceiling light?
Many LED and fluorescent lamps ripple at 100 or 120 times a second, and this sensor is fast enough to follow them. Average everything read over 50 ms, which is whole cycles at both rates, and the jitter goes away.

The ambient light sensor handbook

10 articles · about 50 minutes

This page is the reference: what the part is, what it is made of, and the questions people arrive already asking. The handbook is the walk — the same part in the order somebody actually meets it.

What is on the board

2 articles

Four pins of which three are wired, and three parts: a clear phototransistor near the top, and the resistor and capacitor that turn its current into a voltage. It is not an LDR, whatever the old page said.

From light to a voltage

2 articles

Light makes a current, the 4.7 kΩ turns it into SIGNAL, and VCC decides only where bright light stops counting.

Reading it

2 articles

Three wires and a sketch that prints a count, then why an ESP32 can read 0 in a dim room and what to read instead.

What it sees

2 articles

It sees near infrared best, and it is fast enough to see the ripple in the lights over your desk. Both change the number.

Using it

2 articles

A night light that calibrates itself and does not flicker at dusk, and the short list of reasons a reading looks wrong.

Lessons using TK20

Each one is a working build, not a snippet.

Edit this page — content/modules/ambient-light-sensor.mdx

Community

Questions about this product

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

Ask a question ↗

Ambient Light Sensor

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