TinySkiff ESP32-S3 Lab · Day 17 of 30

Make a lamp
read the room

Every knob and button so far has been something you operated. Today a photoresistor and one fixed resistor turn the room's light into a voltage, the ADC turns that voltage into a number, and the number sets an LED's brightness — cover the sensor with your hand and the lamp answers.

About 25 minutesArduino firstMicroPython optionalNo electronics assumed
Agent assist code TSK-DAY17-NIGHTLAMP

Hand this to an agent so it can pull the lesson packet and coach you step by step.

01 First, know the pieces

What you need

Seven things, one of them new. Tap Define on any part you haven't met — the answer opens as a field note you can read and dismiss without losing your place.

Official manual photo of the ESP32-S3 development board.
Manual photo

ESP32-S3 board

The brain that runs your uploaded sketch.

Official manual image of the ESP32-S3 GPIO extension board.
Manual photo

GPIO extension board

Spreads the pins into rows you can reach and label.

Official manual photo of a photoresistor with its two legs labelled 1 and 2.
Manual photo

Photoresistor

Today's sensor — its resistance follows the light falling on it.

Official manual photo of a red LED with its longer positive leg and shorter negative leg labelled.
Manual photo

LED

The lamp itself — it only works one way round.

Official manual photo of a resistor with coloured value bands.
Manual photo

220 Ω resistor

Sits in series with the LED to keep the current gentle.

Official manual photo of a resistor with coloured value bands.
Manual photo

10 kΩ resistor

Pairs with the photoresistor to turn light into a readable voltage.

Official manual image of a jumper wire.
Manual photo

4 jumper wires

Temporary, solder-free connections.

02 Make the physical circuit

Chart the circuit

The official Freenove diagram is your chart — schematic on top, the same circuit built on a breadboard below. Click it to enlarge. Two small circuits share the board today: the divider that reads the light, and the LED that answers it.

Official Freenove circuit — C Tutorial, Chapter 11 (Photoresistor & LED), page 118.
10 kΩ resistor 3.3V and the divider midpoint The fixed top half of the voltage divider.
Photoresistor divider midpoint and GND Its light-driven resistance sets the midpoint voltage.
Divider midpoint GPIO 1 The ADC reads the light level here.
LED long leg (+) GPIO 14 via 220 Ω This pin sets the lamp's brightness with PWM.
LED short leg (−) GND Completes the LED's path back to zero volts.

Mind the LED's legs. The LED only lights one way round — long leg toward GPIO 14 through the 220 Ω resistor, short leg to ground. The photoresistor has no polarity, so either leg can face either way. Unplug USB before you move any wire.

03 One action at a time

Build it

This is the main path — you can finish the day without opening a single field note. Tap each step as you go to keep your place.

0 / 9 done
  1. Seat the ESP32-S3 on the GPIO extension board and keep USB unplugged while you wire.

  2. Place the photoresistor on the breadboard — its two legs are interchangeable.

  3. Run the 10 kΩ resistor from the 3.3V rail to one leg of the photoresistor, and take the photoresistor's other leg to GND.

  4. Wire the point where the resistor and the photoresistor meet — the divider's midpoint — to GPIO 1.

  5. Place the LED so its long leg (+) heads toward GPIO 14 and its short leg (−) toward ground.

  6. Put the 220 Ω resistor in series between GPIO 14 and the LED's long leg.

  7. Compare every wire to the chart before you plug in USB.

  8. Open Sketch_11.1_NightLamp.ino in Arduino IDE and upload it.

  9. Cup a hand over the photoresistor, then light it with a phone torch — watch the LED answer each move.

The board is sensing.

The lamp is following the room's light all by itself. Head to Test & debug to confirm the behaviour.

04 Read just enough code

Read the code

The whole sketch is five working lines. One line in setup attaches the LED pin to a PWM channel; the loop reads the light and maps it straight onto the lamp's brightness. Switch to MicroPython if you'd rather see the same idea in Python — the wiring never changes.

Sketch_11.1_NightLamp.ino
#define PIN_ANALOG_IN   1
#define PIN_LED         14
#define CHAN            0
#define LIGHT_MIN       372
#define LIGHT_MAX       2048
void setup() {
  ledcAttachChannel(PIN_LED, 1000, 12, CHAN);
}

void loop() {
  int adcVal = analogRead(PIN_ANALOG_IN); //read adc
  int pwmVal = map(constrain(adcVal, LIGHT_MIN, LIGHT_MAX), LIGHT_MIN, LIGHT_MAX, 0, 4095);  // adcVal re-map to pwmVal
  ledcWrite(PIN_LED, pwmVal);    // set the pulse width.
  delay(10);
}
analogRead(PIN_ANALOG_IN)Reads the divider's midpoint voltage on GPIO 1 as a number from 0 to 4095.
constrain(adcVal, LIGHT_MIN, LIGHT_MAX)Fences the reading inside the 372–2048 window before map stretches that window across the full brightness scale — this is where constrain earns its keep.
ledcWrite(PIN_LED, pwmVal)Sets the lamp's brightness on GPIO 14 — 0 is dark and 4095 is full.

05 Understand, don't memorise

Four small conversions

The whole day is one chain of four conversions, each small enough to hold in your head, running many times a second.

Light

Resistance shifts

The photoresistor's resistance follows the light falling on it.

Divide

Volts at the midpoint

The 10 kΩ divider turns that resistance into a voltage on GPIO 1.

Read

ADC to a number

analogRead measures the voltage as a value from 0 to 4095.

Drive

Map to brightness

constrain and map squeeze the reading into a PWM level for the LED.

The chain light → resistance → voltage → number → brightness

Why the window is 372 to 2048

A room rarely swings the ADC across its whole range. constrain fences the reading inside a realistic window, and map stretches that window over the full 0–4095 brightness scale, so the lamp spends all its range on light levels your room actually produces.

What the 10 kΩ resistor is doing

On its own the photoresistor only changes resistance, which a pin can't read directly. Paired with a fixed resistor it forms a divider, and the midpoint voltage rises and falls with the light — exactly the kind of signal the ADC measures.

06 Know it worked

Test & debug

Nothing prints to the screen today — the proof is the lamp answering your hand.

What you should see
LED
  • After upload the LED settles at a brightness set by the room's light.
  • Cup a hand over the photoresistor — the brightness slides one way.
  • Shine a phone torch on the sensor — the brightness slides back the other way.

Which direction is which follows how the divider sits on your board — the proof is the slide itself, steady and repeatable each time you cover and light the sensor.

If it doesn't
  • LED never changes? Check the photoresistor's legs and that the divider's midpoint wires to GPIO 1.
  • LED always at full? The divider is likely on the wrong rail — confirm the 10 kΩ goes to 3.3V and the photoresistor's far leg to GND.
  • Nothing lights at all? Check the LED's direction and the 220 Ω resistor on GPIO 14.
  • Upload fails? Swap in a data-capable USB cable.

07 Make the idea yours

Try this: make it your room's lamp

Same working circuit, two edits to the numbers. It fits inside today's 25 minutes.

Find your room's numbers

Add Serial.begin(115200); to setup and Serial.println(adcVal); to the loop, re-upload, and open Serial Monitor. Note the values at your room's darkest and brightest, then write those into LIGHT_MIN and LIGHT_MAX.

Flip the lamp's direction

Swap the last two arguments of map so 0 and 4095 trade places, then re-upload. Whatever your lamp did before, it now does in reverse — set it so darkness makes it glow, and you have a true night lamp.

Logbook

08 Learn it with a hand on the tiller

Coach me through it

Every lesson ships with a code and a machine-readable packet, so an agent can guide you with full context.

Lesson code

TSK-DAY17-NIGHTLAMP

How the agent should behave: guide one physical connection at a time, wait for you to confirm, explain terms on request, and always check wiring, board, port, and USB before changing code.

Field note

Shortcut

Prompt copied