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
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:
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.
/*
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.5The same reading in MicroPython, for an ESP32, an ESP32-S3 or a Pico. read_u16 returns SIGNAL as a count from 0 to 65535 on all three, and the bar is scaled to it.
"""
Reflective Optical Sensor - first reading, MicroPython TK57 / /p/tk57
Wiring. Count from the square pad on the TinkerBlock board, parts
up, header at the bottom:
GND -> GND
VCC -> 3V3 (never 5V: SIGNAL rises to VCC over a dark
surface, and these pins take 3.3 V)
NC -> nothing (unconnected on the board)
SIGNAL -> GPIO 4 on an ESP32-S3, GPIO 34 on an ESP32,
GP26 on a Raspberry Pi Pico
Thonny
Run > Configure interpreter MicroPython (ESP32) or
MicroPython (Raspberry Pi Pico)
Save it to the board as main.py to run it on every power-up.
Nothing to install: machine, sys and time are built in.
"""
import sys
import time
from machine import ADC, Pin
# The GPIO number SIGNAL is wired to. ESP32: 34. ESP32-S3: 4. Pico: 26.
SENSOR_PIN = 4
adc = ADC(Pin(SENSOR_PIN))
if sys.platform == "esp32": # ESP32 and ESP32-S3
adc.atten(ADC.ATTN_11DB) # the full range, to about 3.1 V
while True:
level = adc.read_u16() # 0 to 65535; more light back, lower
print(level, "#" * (level * 40 // 65535))
time.sleep_ms(100)There is no Uno here: an Uno cannot run MicroPython. On an ESP32 the atten line widens the ADC's range to about 3.1 V; without it the range stops near 1 V and every surface but white reads full scale. Stop it with Ctrl-C.
View on GitHub · blocks/tk57-reflective-optical-sensor/micropython/optical_first_reading.py @ v1.5When it does not work
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.
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.
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.
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.
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
Questions about this product
See what other owners have asked, and read their solutions.
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.