ESP32-S3 stair night light
A motion sensor and a light sensor at the stairs: when someone walks by in the dark, a 12 V LED strip lights up and stays on for two minutes after the last movement. By day nothing happens. The strip runs on its own 12 V supply through a MOSFET switch, never from the board.
The program
Chip BASIC: every line is one instruction you can read and change. The compiler checks it at every release of the site.
REM Luce delle scale
REM @part scale = motion
REM @part luce = bh1750
REM @part interruttore = mosfet
REM @part striscia = ledstrip on=interruttore
10 LET buio = 10 : REM @cfg buio 1..100 Buio sotto (lux)
20 LET minuti = 2 : REM @cfg minuti 1..10 Resta accesa (minuti)
30 IF scale AND (luce.LUX < buio OR acceso = 1) THEN striscia ON : LET acceso = 1 : LET t = TIME()
40 IF acceso = 1 AND TIME() - t > minuti * 60000 THEN striscia OFF : LET acceso = 0
50 WAIT 200
60 GOTO 30What you should see: in daylight the strip stays off even with movement; in the dark a movement turns it on, and it goes off two minutes after the last one. In the simulator set the light slider low and click the motion sensor.
▶ Open it in the IDEIn the simulator you see the logic run right away, with no hardware. The simulator does not check the wires: you check those on the board, and the IDE's wiring check walks you through it.
The parts
The compiler finds the parts in the program. Each has its own page with a test program to run before you build the whole thing.
- Motion sensorA PIR sensor: 1 when it sees someone move.
- BH1750 light sensorMeasures how much light there is, in lux: tells dusk from day.
- MOSFET driverA switch a board pin turns on and off, for a 12 V pump, fan or valve.
- 12 V LED stripA strip of white LEDs, all on or off: for a cabinet, a shelf, a desk lamp.
How to wire it to the ESP32-S3
- SIG → ESP32-S3: GPIO2The pin tells the switch when to close: the load's current goes through the switch, never through the board.
- GND → ESP32-S3: GNDCommon ground: this link is mandatory.
- OUT → ESP32-S3: GPIO1
- VCC → ESP32-S3: 5V
- GND → ESP32-S3: GNDCommon ground: this link is mandatory.
- SDA → ESP32-S3: GPIO38The sensor bus: every I2C module shares these two pins
- SCL → ESP32-S3: GPIO39
- VCC → ESP32-S3: 3V3
- GND → ESP32-S3: GNDCommon ground: this link is mandatory.
- striscia: + → interruttore: V+
- striscia: − → interruttore: OUT−
- 12 V supply: − → interruttore: GNDCommon ground: this link is mandatory.
- 12 V supply: + → interruttore: V+Connect the supply last, after checking every wire.
If it does not work
What you can change
How dark is `buio` (line 10, in lux) and how long it stays on is `minuti` (line 20), both sliders in the IDE. Point the light sensor away from the strip. Ask the assistant for a second motion sensor at the top of the stairs.
Which boards it works on
- ESP32-S3 Freenove: yes
- Nordic nRF54LM20 DK: no: one of its parts does not run on this board yet
- Arduino UNO R4 WiFi: yes