Build it step by step
0 of 7 doneBuild the rail drive
Belt and pulley along the curtain track.
Test the motor
Move a few centimetres each way.
Add the open switch
Home on start-up.
Measure the travel
Enter the total in steps.
Add the light sensor
Print the lux at dawn and dusk.
Add the thresholds
Tune open and close levels.
Test the clock backup
Cover the sensor and unplug its wire.
How do you build a light-following curtains?
Curtains open at sunrise and close at dusk using a light sensor and an RTC backup. You need ESP32 DevKit V1, BH1750 light sensor, DS3231 RTC module, NEMA 17 stepper motor, TMC2208 stepper driver, GT2 timing belt and pulley set, 12 V 2 A DC adapter, Limit switch (micro switch with lever) and Electrolytic Capacitor 100 µF. It takes 7 steps and about 300 minutes.
- Level
- Intermediate
- Time
- About 300 min
- Parts
- 9
Wiring
| From | To | Why |
|---|---|---|
| BH1750 VCC and GND | ESP32 3V3 and GND | Power |
| BH1750 SDA and SCL | ESP32 GPIO21 and GPIO22 | I2C |
| DS3231 VCC and GND | ESP32 3V3 and GND | Power |
| DS3231 SDA and SCL | ESP32 GPIO21 and GPIO22 | Shares the I2C bus |
| TMC2208 VM and GND | 12 V adapter plus and minus | Motor supply |
| 100 µF capacitor | Across VM and GND at the driver | Absorbs spikes |
| TMC2208 VIO and GND | ESP32 3V3 and GND | Logic supply |
| TMC2208 STEP | ESP32 GPIO25 | Step pulses |
| TMC2208 DIR | ESP32 GPIO26 | Direction |
| TMC2208 EN | ESP32 GPIO27 | Enable (LOW = on) |
| TMC2208 A1, A2, B1, B2 | Motor coils | Check pairs with a multimeter |
| Open-end switch | ESP32 GPIO32 and GND | Curtain fully open |
| ESP32 USB or buck module | 5 V | Power |
Code
The curtain follows daylight: it opens when the room's light passes the open threshold and closes when it drops below the close threshold, with a gap between them so it does not hunt back and forth at dawn and dusk. The clock is the backup; if the light sensor stops answering the curtain follows fixed times. The open-end switch lets the board find the fully open position at start-up, and the travel distance is a number of steps that you measure on your own rail. Fit a second switch at the closed end if you want extra safety. Limit the motor current with the driver's own settings, start low, and never connect or disconnect the motor when powered. Keep the cords and belt out of reach of small children and pets, as moving curtain cords are a strangling hazard, and fit a pinch-safe gap. Run the motor slowly; the curtain is light and does not need force. Arduino IDE with the ESP32 boards package, board "ESP32 Dev Module".
Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Light-following curtains. Libraries: AccelStepper, BH1750 (by Christopher Laws), RTClib. Board: ESP32 Dev Module.
#include <Wire.h>
#include <AccelStepper.h>
#include <BH1750.h>
#include <RTClib.h>
const int STEP_PIN = 25, DIR_PIN = 26, EN_PIN = 27, OPEN_SW = 32;
const float STEPS_PER_MM = 200.0 * 16 / 40.0; // TMC2208 default 1/16 steps, 40 mm of belt per turn (20 teeth, 2 mm pitch)
const long TRAVEL_STEPS = (long)(1200 * STEPS_PER_MM); // 1.2 m of curtain travel, measure yours
const float OPEN_LUX = 150, CLOSE_LUX = 40; // thresholds, tune for your room
const int FALLBACK_OPEN = 6 * 60 + 30, FALLBACK_CLOSE = 18 * 60 + 30; // minutes after midnight, used if the sensor fails
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
BH1750 lightMeter; RTC_DS3231 rtc;
bool isOpen = true; unsigned long lastCheck = 0; bool sensorOk = true;
void home() {
stepper.setMaxSpeed(1500); stepper.setSpeed(-600);
unsigned long t0 = millis();
while (digitalRead(OPEN_SW) == HIGH && millis() - t0 < 60000) stepper.runSpeed(); // give up after a minute
stepper.setCurrentPosition(0); isOpen = true;
}
void goTo(long target) { stepper.moveTo(target); }
void setup() {
pinMode(OPEN_SW, INPUT_PULLUP); pinMode(EN_PIN, OUTPUT); digitalWrite(EN_PIN, LOW);
Wire.begin(21, 22);
sensorOk = lightMeter.begin();
rtc.begin();
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
stepper.setAcceleration(500); stepper.setMaxSpeed(1500);
home();
}
void loop() {
if (millis() - lastCheck > 30000) { // decide every 30 seconds
lastCheck = millis();
DateTime now = rtc.now(); int minutes = now.hour() * 60 + now.minute();
float lux = sensorOk ? lightMeter.readLightLevel() : -1;
bool wantOpen = isOpen;
if (lux >= 0 && lux < 100000) { // sensor looks sane: follow the light, only after 05:00
if (lux > OPEN_LUX && minutes > 5 * 60) wantOpen = true;
if (lux < CLOSE_LUX) wantOpen = false;
} else { // sensor failed: follow the clock
wantOpen = (minutes >= FALLBACK_OPEN && minutes < FALLBACK_CLOSE);
}
if (wantOpen != isOpen && !stepper.distanceToGo()) { goTo(wantOpen ? 0 : TRAVEL_STEPS); isOpen = wantOpen; }
}
stepper.run();
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Sunrise | Curtain opens when light passes the open level |
| Dusk | Curtain closes below the close level |
| Cover the sensor | Falls back to the clock times |
| Power cut and return | Homes, then resumes |
If it does not work
Motor only buzzes
Swap one coil pair and check the power supply.
Curtain hunts back and forth
Widen the gap between the two lux levels.
Opens in the night
Raise OPEN_LUX or keep the after-05:00 rule.
Loses position
Reduce the speed and re-home each morning.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the light-following curtains?
ESP32 DevKit V1, BH1750 light sensor, DS3231 RTC module, NEMA 17 stepper motor, TMC2208 stepper driver, GT2 timing belt and pulley set, 12 V 2 A DC adapter, Limit switch (micro switch with lever) and Electrolytic Capacitor 100 µF.
How long does the light-following curtains take to build?
About 300 minutes for a intermediate maker, in 7 steps.
How do I know the light-following curtains works?
Sunrise: Curtain opens when light passes the open level. Dusk: Curtain closes below the close level. Cover the sensor: Falls back to the clock times.
Motor only buzzes: what should I check?
Swap one coil pair and check the power supply.
Curtain hunts back and forth: what should I check?
Widen the gap between the two lux levels.
Opens in the night: what should I check?
Raise OPEN_LUX or keep the after-05:00 rule.
Loses position: what should I check?
Reduce the speed and re-home each morning.
