ambient light sensor/Reading it/05. The first reading
Reading it · 05 of 10

The first reading

Three wires, no library and a sketch that prints the raw count twice a second. A lit room reads low on the scale, roughly 50 to 240 on an Uno. Cover the sensor and it falls to near 0; hold a phone torch over it and it jumps to about 940.

Three wires

Three wires
Your board
VCC to
5V
SIGNAL to
A0
A lit room
roughly 48 to 240
GND to GND, VCC to 5V, SIGNAL to A0. analogRead returns 0 to 1023 against 5 V, and a lit room gives roughly 48 to 240: well under half the scale. Covered, it reads close to 0.

GND to GND. VCC to your board's logic supply: 5V on an Uno, 3V3 on the three 3.3 V boards. SIGNAL to an analog pin. NC stays unconnected.

The analog pin is the same in every sketch in this book, and the same as in the NTC thermistor's: A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an ESP32-S3, GP26 on a Pico. The two ESP32 pins are on ADC1, the converter that keeps working when Wi-Fi is on.

The sketch

One call does the work. analogRead(LIGHT_PIN) measures the voltage on the pin and returns it as a count. There is no pinMode: an analog pin does not need one to be read, and the block drives SIGNAL itself.

On an Uno the count is the voltage as a share of 5 V, out of 1023. So a count is about 4.9 mV, and for a typical sensor about 2 lux. On a Pico it is out of 1023 against 3.3 V. On an ESP32 it is out of 4095, and not quite a straight line, which is the next article.

What you should see

On an Uno in a lit room, the serial monitor at 115200 prints a count well under half the scale: roughly 50 to 240, for a room somewhere between 100 and 500 lux. Cover the sensor with a finger, then hold a phone torch close over it:

count 142
count 139
count 3
count 2
count 941
count 941
count 138

Covered, it falls to a few counts. Under the torch it jumps to about 941 and stops there, because that is the ceiling from 5V, about 4.6 V. Your room's numbers will differ, by the light and by the part: two sensors can differ by 30 % either way. The direction will not. More light, higher.

The code

No library. analogRead returns SIGNAL as a count: 0 to 1023 on an Uno and a Pico, 0 to 4095 on an ESP32 or ESP32-S3. Change LIGHT_PIN to the pin you wired SIGNAL to.

light_first_reading.ino
/*
  Ambient Light Sensor - first reading                   TK20 / /p/tk20

  Wiring. Count from the square pad on the TinkerBlock board, parts
  up, header at the bottom:

    GND    -> GND
    VCC    -> 5V on an Uno; 3V3 on an ESP32, ESP32-S3 or Pico
              (bright light takes SIGNAL up towards VCC)
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> A0 on an Uno, GPIO 34 on an ESP32, GPIO 4 on an
              ESP32-S3, GP26 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.
*/

// 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() {
  int count = analogRead(LIGHT_PIN);   // more light, higher

  Serial.print("count ");
  Serial.println(count);
  delay(500);                          // twice a second
}

The count is not lux, and on an ESP32 it is not even an exact voltage. It is here to show the direction and the size: covered, near 0; a room, low; a torch, high. The next article reads millivolts instead.

When it does not work

It prints 0 all the time, even under a torch.

SIGNAL has no current to show. VCC is not connected, or the cable is one pin over: count from the square pad, GND, VCC, NC, SIGNAL. A LIGHT_PIN that names a different analog pin from the one SIGNAL is on also reads near 0.

It prints about 30 in my room. Is it broken?

Probably not; the room is dimmer than it looks. 30 on an Uno is about 60 lux for a typical part, which is a dim room or the far side of one. Shine a phone torch on the sensor: if the number climbs to several hundred, it is working.

It never gets past about 940 on my Uno.

That is the ceiling. The transistor needs about 0.4 V across itself, so from 5V SIGNAL stops at about 4.6 V, which is about 940 of 1023. Brighter light reads the same. It is not a fault.

The serial monitor stays empty on my ESP32-S3.

Set Tools > USB CDC On Boot to Enabled and upload again. Without it the S3's USB port does not bring up a serial port at boot, so the sketch runs with nowhere to print.

Where this goes next

Why a dim room can read 0 on an ESP32, and reading millivolts instead of counts.

Reading it on an ESP32

Edit this page — content/books/ambient-light-sensor/the-first-reading.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