Take it on /learn as Lighting and lighting controls.
Low voltage only
Every circuit here runs on 5 V or 12 V. Lighting in a building runs on line voltage (120 V or 277 V in the US), and wiring it is licensed work with listed devices, a permit and an inspection. Build the logic here, and leave the line side to an electrician. Energy code references in the lessons (the IECC and ASHRAE 90.1) are informational.
The lessons
- Lamps and LEDs: a 12 V filament lamp and a 3 W power LED on a 12 ohm resistor. Current, forward voltage, a sagging supply, and what too little resistance does.
- Drivers: an LM317 with a 1.8 ohm resistor holds 0.69 A from 12 V or 15 V, runs out of headroom at 6 V, becomes a 350 mA driver at 3.6 ohm, and turns about 5 W into heat.
- Dimming methods: the gallery's lamp dimmer: PWM on a MOSFET module, and how constant-current reduction, 0-10 V and phase dimmers compare.
- Photocells: an LDR divider switches a lamp through a MOSFET, with a narrow gray zone near 33 lux and a trim resistor that moves it.
- Occupancy sensors: the gallery's PIR motion light: daylight hold-off, a relay, and a 20 s vacancy delay.
- Schedules: a Raspberry Pi runs a porch light's day in Python, with sunrise and sunset offsets, a summer bug and its fix, and the kWh it costs.
- Boss: a hallway controller: wire the LM317 driver, then a photocell with hysteresis, a PIR, a wall switch and an SSR run a 3 W LED. You set the vacancy delay and halve the power.
The hallway controller
The boss sketch is hallway. The photocell is 5 V, an LDR, A0, 10k, GND, so
bright light reads high. The PIR is on pin 2, the wall switch on pin 3 (to
GND, with the internal pull-up), and an SSR-25DD's input on pin 7. The SSR
switches 12 V into an LM317 constant-current driver and a 3 W white LED.
- Day: the reading is above 450 (about 13 lux) and the light stays off.
- Night: below 300 (about 5 lux), motion or the wall switch turns it on, and it goes off 10 s after the last movement.
- The wall switch works day or night and toggles the light.
Serial settings, one per line: delay 10 (seconds), dark 300,
light 450 (readings 0 to 1023, at least 50 apart) and status.
Stand-ins and limits
- The 12 V lamp and the power LED are the lights; the filament's efficiency and an LED's lumens are not modeled, only current and brightness.
- An overloaded LED is flagged with a warning the first time instead of burning out.
- The SSR-25DD switches as soon as its input does. A real one takes a few milliseconds, so it is used for on and off here, not for fast PWM.
- The sunrise and sunset table is Denver, mid-month, rounded to 5 minutes.