Level 3Unit B · Session 8
8

Unit B · What your device can sense

Light and dark, and a number you choose

What we are making: a page that does not just report a number — it reports a judgement. Is it dark in here, or not?

The first decision your device makes

A light sensor gives you a number from 0 to 4095. That is a fact. But “dark” is not a fact — it is a line somebody drew, and today you draw it.

This is the same idea as the threshold in Level 2, and it will come back in your project. The number you pick is not right or wrong in itself; what matters is whether you can say how you chose it.

New words
AnalogueA reading with a range, not just on or off.
analogRead()On an ESP32 this gives 0 to 4095, not 0 to 1023.
ThresholdThe line between one answer and the other. Yours to choose.
AO / DOAnalogue out gives the range. Digital out gives only two answers.
ESP32USB to the laptop3V3GNDGPIO 34Light sensorthe one with a clear beadVCCGNDAOa number, not on/offUse AO, the analogue output. DO gives you only bright-or-dark.
The light sensor. AO gives a whole range; DO gives only two answers.
Session 8 — a judgement, not just a number — 3 slots
SLOT 2
the pin, and the line you are about to choose
int lightPin = 34;
int darkBelow = 1500; // your number goes here
SLOT 3
turn the number into a judgement
int light = analogRead(lightPin); // 0 to 4095
if (light < darkBelow) return "DARK";
return "LIGHT";
SLOT 4
show the judgement, and the raw number underneath it
return "<h1>" + reading() + "</h1>"
"<p>reading " + String(analogRead(lightPin)) + "</p>";

Do it — measure before you decide

  1. Wire the sensor to GPIO 34, fill the three slots, and upload as they are.
  2. Write down four readings: normal room light; lights off; your hand cupped over it; a phone torch shining on it.
  3. Look at your four numbers and pick a threshold between ‘lights off’ and ‘normal room’.
  4. Change darkBelow in slot 2 to your number. Nothing else changes.
  5. Write one sentence in your notebook saying why that number.
💡 Tip
An ESP32 reads 0 to 4095, an Uno reads 0 to 1023. If you bring a threshold across from a Level 2 program it will be about four times too small. This catches people every year, and the symptom is a sensor that always says the same thing.

Change it

  1. Add a third answer — DIM — between the other two.
  2. Make the page show both the word and the raw number, so you can debug it.
  3. Cover the sensor slowly and find the exact reading where it changes its mind.
What you seeWhyWhat to do
Always says DARKThreshold set above every reading you get.Print the raw number and look at it before setting the line.
Reading is stuck near 4095The sensor is on a 5V pin, or it is DO not AO.Use AO, and 3.3V.
Reading jumps about by hundredsNormal for a cheap light sensor.Keep the two states far apart and the wobble stops mattering.
Nothing changes when I cover itWrong pin, or the wire is loose.GPIO 34 is input-only, which is fine — check the wire itself.

What you learned

  • A device turns a number into a judgement, and somebody chose the line.
  • Measure first, then decide. Never the other way round.
  • Different boards have different ranges. The number does not travel.
Challenge optional — only if you finish early
  • Add a third state between light and dark, and defend where you put both thresholds.
7. Temperature, on your page