Light and Proximity Sensing Workbench

Drive an LDR voltage divider from ambient lux and an ultrasonic proximity detector from object distance, with every voltage, ADC count, and echo time following from the stated formula.

animation
light
proximity
ldr
photoresistor
ultrasonic
adc
sensing
beginner
A beginner-first light and proximity sensing workbench: photoresistor voltage divider with lux-driven resistance, live Vout and 12-bit ADC count, night-light hysteresis, plus ultrasonic time-of-flight distance with temperature-corrected speed of sound and threshold detection with hysteresis.
Sensing LDR + Ultrasonic Formula-driven

Light and Proximity Sensing Workbench

Drive a photoresistor voltage divider from ambient light, and an ultrasonic detector from object distance. Every voltage, ADC count, and echo time on this page follows from the formula shown beside it, so you can predict a reading before you trust it.

1 luxAmbient light
0.033 VADC voltage
41ADC count / 4095
Night light ONOutput state

Goal

Turn a physical quantity (lux or distance) into a number a microcontroller can act on.

Try First

Pick a preset, then drag the ambient light or distance slider and watch the readouts move.

Watch

Near a threshold, toggle hysteresis on and off to see chatter appear and disappear.

Why It Matters

Smart lights, night lights, and parking sensors all depend on these exact conversions.

Sensor Mode

Light mode reads a photoresistor through a voltage divider.

Preset Scene

Dark room: almost no ambient light, so the LDR resistance is very high.

Light Inputs

Proximity Inputs

Reset

LDR resistance

1.00 MOhm

Output voltage

0.033 V

12-bit ADC count

41 / 4095

LDR Voltage Divider

The photoresistor sits on the high side. As light rises its resistance falls, so more of the 3.3 V supply appears across the fixed resistor at the ADC pin.

Light Classification

Where this reading sits on the everyday light scale.

Near darkness

Divider Fit

Whether the chosen resistor gives a useful voltage swing here.

Check swing

Trigger Logic

The output decision and whether it is at risk of chattering.

ON
Beginner Ramp

A photoresistor (LDR) is just a resistor whose resistance drops when light hits it. Put it in a voltage divider and the middle voltage tells you how bright it is. An ultrasonic sensor instead measures time: it chirps, listens for the echo, and turns the round-trip time into a distance. Both give a microcontroller a number it can compare against a threshold.

Light Formula Reference
  • Teaching LDR model: R_LDR = 1 MOhm x lux^(-0.75) (1 lux -> 1 MOhm, 10,000 lux -> 1 kOhm).
  • Divider (LDR on top): Vout = 3.3 x Rf / (R_LDR + Rf).
  • 12-bit ADC: count = round(Vout / 3.3 x 4095), clamped 0 to 4095.
  • More light -> lower R_LDR -> higher Vout -> higher count.
Proximity Formula Reference
  • Speed of sound: v = 331.3 + 0.606 x T m/s (T in °C).
  • Echo round-trip time: t = 2 x d / (v / 10000) microseconds (d in cm).
  • Sensor's own direction: d = t x (v / 10000) / 2.
  • Cold air is slower, so the same distance returns a longer echo time.
Why Hysteresis

If a single threshold sits right where the reading is hovering, tiny noise flips the output on and off many times a second. Hysteresis uses two thresholds with a gap between them: the output only turns on past one edge and only turns off past the other, so small wobble in the middle is ignored.

Choosing The Divider Resistor

The voltage swing is largest when the fixed resistor is near the geometric mean of the LDR's bright and dark resistance. Pick it too small and dim scenes barely move the voltage; too large and bright scenes saturate near the rail. Switch resistors here and read the Divider Fit card.

Real Sensor Notes

Real LDRs vary part to part and respond slowly; a calibrated I2C sensor such as the BH1750 reports lux directly. The HC-SR04 has a ~2 cm blind zone and a ~400 cm ceiling, and its ECHO pin is 5 V, so level-shift it before an ESP32 GPIO.

Practice 1: Divider Calculator

In Light mode, keep the 10 kOhm resistor and step the presets dark room to office to daylight. Record R_LDR, Vout, and the ADC count each time. This is the lab's LDR Voltage Divider Calculator, now driven by lux.

Practice 2: Night Light Hysteresis

Set ON below 50 lux and OFF above 80 lux, matching the lab's Automatic Night Light build. Drag the light slowly through that band with hysteresis off, then on, and watch the output stop flickering.

Practice 3: Temperature and Echo

Switch to Proximity, fix the object at 30 cm, and sweep temperature from 0 to 35 °C. Confirm the echo time shrinks as the air warms, exactly as the lab's ultrasonic temperature-compensation warning predicts.