optical sensor/Reading it/06. The first reading
Reading it · 06 of 9

The first reading

Three wires, no library, and a sketch that prints the reading ten times a second with a bar beside it. VCC to your board's logic voltage, SIGNAL to an analog pin: GPIO 4 on an ESP32-S3, GPIO 34 on an ESP32, A0 on an Uno, GP26 on a Pico. The bar is long over black tape or open air and short over white paper.

Three wires

Three wires
Your board
VCC to
3V3
SIGNAL to
GPIO 4
Read with
analogRead
GND to GND, VCC to 3V3, SIGNAL to GPIO 4. It is on ADC1 (GPIO 1 to 10), which keeps working with Wi-Fi on, and it is not a strapping pin. Not the 5V pin: a dark surface puts VCC on GPIO 4.

GND to GND. VCC to the voltage your board's pins run at: 3V3 on an ESP32-S3, an ESP32 or a Pico, 5V on an Uno. SIGNAL to a pin with an ADC: GPIO 4 on an ESP32-S3, GPIO 34 on an ESP32, A0 on an Uno, GP26 on a Pico. NC stays unconnected.

The ESP32 pins are on ADC1, which keeps reading while Wi-Fi is on; the ADC2 pins stop. They are the same pins the Ambient Light book uses, so one wiring serves both blocks.

The sketch

analogRead(SENSOR_PIN) and nothing else: there is no library and no setup for an analog input. The sketch prints the number, then a bar of # as long as the number is large, scaled so a full-scale reading is 40 characters. On the ESP32 family full scale is 4095; on an Uno and a Pico, 1023.

What you should see

At 115200 the serial monitor prints ten lines a second. With nothing in front of the sensor the reading is at or near the top, the bar full. Slide a sheet of white paper under the domes, a few millimetres away, and the number falls to well under half of full scale and the bar shrinks with it. A strip of black electrical tape brings it back up. Move the paper slowly away and watch it climb: the next article is about exactly that.

Here is the block with the owner's own sketches, which light an XL LED block while the reading is high:

The TK57 on a LEGO baseplate, face up, wired to an Uno-format board beside an XL LED block. With nothing over the sensor the XL LED is lit; a finger held over the two domes turns it off.
The same two blocks with a Pico 2. A finger over the sensor sends infrared back and turns the XL LED off; taken away, the LED lights again.

A finger works as well as paper: skin reflects infrared well.

The same, in MicroPython

The MicroPython version on this page does the same on an ESP32, an ESP32-S3 or a Pico. read_u16() returns 0 to 65535 on every board, so the numbers are larger; they still fall as white paper comes near.

The code

No library. analogRead returns 0 to 4095 on the ESP32 family and 0 to 1023 on an Uno or a Pico; the sketch prints the number and a bar as long as it, ten times a second. Change SENSOR_PIN to the pin you wired SIGNAL to.

optical_first_reading.ino
/*
  Reflective Optical Sensor - first reading             TK57 / /p/tk57

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

    GND    -> GND
    VCC    -> 3V3 on an ESP32, ESP32-S3 or Pico; 5V on an Uno.
              SIGNAL rises to VCC over a dark surface, so VCC
              is the voltage your board's pins run at.
    NC     -> nothing   (unconnected on the board)
    SIGNAL -> GPIO 4 on an ESP32-S3, GPIO 34 on an ESP32,
              A0 on an Uno, GP26 on a Raspberry Pi Pico

  Arduino IDE
    Tools > Board                 your board, e.g. ESP32S3 Dev Module
    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 SENSOR_PIN = 4;

#if defined(ARDUINO_ARCH_ESP32)
const int FULL = 4095;   // 12-bit on the ESP32 family
#else
const int FULL = 1023;   // 10-bit on an Uno and a Pico
#endif

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

void loop() {
  // More light back, LOWER: the sensor pulls SIGNAL down.
  int level = analogRead(SENSOR_PIN);

  // A bar as long as the reading: long over black or air,
  // short over white paper.
  int bars = map(level, 0, FULL, 0, 40);
  Serial.print(level);
  Serial.print('\t');
  for (int i = 0; i < bars; i++) Serial.print('#');
  Serial.println();

  delay(100);
}

More light back reads lower: the bar shrinks as white paper comes near. The sketch compiles for an ESP32-S3 and an Uno.

View on GitHub · blocks/tk57-reflective-optical-sensor/arduino/optical_first_reading/optical_first_reading.ino @ v1.5

When it does not work

It prints 4095 whatever I hold in front of it.

Nothing is pulling SIGNAL down. Check GND, which the emitter needs to light; check the front of the block, with the domes, faces the paper; and hold the paper 2 to 5 mm away, not flat against the domes. Past a couple of centimetres white paper reads like nothing at all.

The numbers jump about with nothing moving.

A small wobble of a few counts is normal for any ADC. Large jumps, or numbers that follow your hand, mean SIGNAL is floating: it is on NC or on another pin than SENSOR_PIN, or VCC is not connected. Count from the square pad.

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.

On my Uno it never goes above about 675.

VCC is on the Uno's 3.3V pin: the Uno reads against 5 V, and SIGNAL tops out at 3.3 V, about 675 of 1023. That still tells white from black. For the whole range, move VCC to the Uno's 5V pin; the block is fine on either.

Where this goes next

Why the reading tells you how much light came back, and not how far.

Near is not a number →

Edit this page — content/books/reflective-optical-sensor/the-first-reading.mdx

Community

Questions about this product

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

Ask a question ↗

Reflective Optical 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 →