Motorised curtain that opens at sunrise
Build it step by step
0 of 7 doneBuild the belt loop on the rail
Motor one end, idler the other.
Fix the limit switch
At the closed end.
Set the A4988 current
Start low.
Test homing
It stops at the switch.
Test a move by hand
Open and close once.
Place the light sensor
At the window, away from lamps.
Let it run for a few days
Adjust the lux levels.
How do you build a motorised curtain that opens at sunrise?
A NEMA 17 stepper with a GT2 belt pulls the curtains open when it gets light and closed at dusk, with a limit switch to find home. You need Arduino Nano, NEMA 17 stepper motor, A4988 stepper driver, GT2 timing belt and pulley set, Limit switch (micro switch with lever), BH1750 light sensor, 12 V 2 A DC adapter and Electrolytic Capacitor 100 µF. It takes 7 steps and about 240 minutes.
- Level
- Intermediate
- Time
- About 240 min
- Parts
- 8
Wiring
| From | To | Why |
|---|---|---|
| A4988 STEP, DIR, ENABLE | Nano D3, D4, D5 | Step signals (ENABLE low turns the driver on) |
| A4988 MS1, MS2, MS3 | 5V (all three high) | 1/16 microstepping, quiet and smooth |
| A4988 RESET and SLEEP | Joined together | Keeps the driver awake |
| A4988 VDD and GND | Nano 5V and GND | Logic |
| A4988 VMOT and GND | 12 V adapter plus and minus, with a 100 uF capacitor across them | Motor power |
| NEMA 17 coils | A4988 2B, 2A, 1A, 1B | Motor (never connect or disconnect with power on) |
| Limit switch | Nano D2 and GND | Marks the closed end (internal pull-up) |
| BH1750 SDA and SCL | Nano A4 and A5 | I2C light sensor |
| BH1750 VCC and GND | Nano 5V and GND | Power |
Code
The curtain hangs on a rail, and a belt loop along the rail, driven by the motor at one end and an idler pulley at the other, pulls it. At start-up the code creeps towards the closed end until the limit switch is pressed and calls that position zero, so it always knows where it is. A BH1750 light sensor decides when to move: it must stay brighter than the open level for ten minutes before the curtain opens, and darker than the close level for ten minutes before it closes, and moves are at least six hours apart, so a passing cloud does not make it open and shut all day. There is no clock chip, so dusk, not a time, closes it; add a real-time clock if you want a time schedule. Before running, set the A4988's current limit with its small screw and a meter, starting low, since too high makes the motor and driver hot, and never connect or disconnect the motor while power is on. The enable pin turns the motor off when it is still, so it stays cool and quiet. Run it slowly, keep fingers and pets clear of the belt, and add an end stop at the open end if the rail is long. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).
Runs on a virtual Arduino Nano 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).
// Motorised curtain: homes on a limit switch, opens when it has been bright for a while, closes when it has been dark.
// Libraries: AccelStepper (Mike McCauley), BH1750 (Christopher Laws). Board: Arduino Nano.
#include <Wire.h>
#include <AccelStepper.h>
#include <BH1750.h>
const uint8_t STEP = 3, DIR = 4, EN = 5, LIMIT = 2;
const long STEPS_PER_MM = 80; // 200 steps x 16 microsteps / 40 mm per turn with a 20 tooth GT2 pulley
const long TRAVEL_MM = 800; // measure your curtain rail
const float OPEN_LUX = 120, CLOSE_LUX = 20;
const unsigned long CONFIRM_MS = 10UL * 60UL * 1000UL; // light must stay across the line for 10 minutes
const unsigned long MIN_GAP_MS = 6UL * 60UL * 60UL * 1000UL; // at most one move every 6 hours
AccelStepper stepper(AccelStepper::DRIVER, STEP, DIR);
BH1750 light;
bool isOpen = false;
unsigned long sinceBright = 0, sinceDark = 0, lastMove = 0, lastSample = 0;
void goTo(bool open) {
stepper.enableOutputs();
stepper.moveTo(open ? TRAVEL_MM * STEPS_PER_MM : 0);
isOpen = open;
lastMove = millis();
}
void setup() {
pinMode(LIMIT, INPUT_PULLUP);
Wire.begin(); light.begin();
stepper.setEnablePin(EN); stepper.setPinsInverted(false, false, true);
stepper.setMaxSpeed(2400); stepper.setAcceleration(800);
stepper.enableOutputs();
stepper.moveTo(-100000000L); // creep towards the closed end until the switch is pressed
stepper.setMaxSpeed(800);
while (digitalRead(LIMIT) == HIGH) stepper.run();
stepper.setCurrentPosition(0);
stepper.setMaxSpeed(2400);
stepper.disableOutputs();
lastMove = millis() - MIN_GAP_MS;
}
void loop() {
stepper.run();
if (stepper.distanceToGo() == 0) stepper.disableOutputs(); // let the motor cool between moves
if (millis() - lastSample > 10000) {
lastSample = millis();
float lux = light.readLightLevel();
if (lux > OPEN_LUX) { if (sinceBright == 0) sinceBright = millis(); } else sinceBright = 0;
if (lux < CLOSE_LUX) { if (sinceDark == 0) sinceDark = millis(); } else sinceDark = 0;
bool canMove = millis() - lastMove > MIN_GAP_MS && stepper.distanceToGo() == 0;
if (canMove && !isOpen && sinceBright && millis() - sinceBright > CONFIRM_MS) goTo(true);
if (canMove && isOpen && sinceDark && millis() - sinceDark > CONFIRM_MS) goTo(false);
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Power on | Curtain creeps closed, stops at the switch |
| Bright light for 10 minutes | Opens fully |
| Dark for 10 minutes | Closes fully |
| Standing still | Motor is off and cool |
If it does not work
Goes the wrong way
Swap one motor coil pair.
Never stops homing
Check the limit switch wiring to GND.
Opens and shuts too often
Raise CONFIRM_MS or MIN_GAP_MS.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the motorised curtain that opens at sunrise?
Arduino Nano, NEMA 17 stepper motor, A4988 stepper driver, GT2 timing belt and pulley set, Limit switch (micro switch with lever), BH1750 light sensor, 12 V 2 A DC adapter and Electrolytic Capacitor 100 µF.
How long does the motorised curtain that opens at sunrise take to build?
About 240 minutes for a intermediate maker, in 7 steps.
How do I know the motorised curtain that opens at sunrise works?
Power on: Curtain creeps closed, stops at the switch. Bright light for 10 minutes: Opens fully. Dark for 10 minutes: Closes fully.
Goes the wrong way: what should I check?
Swap one motor coil pair.
Never stops homing: what should I check?
Check the limit switch wiring to GND.
Opens and shuts too often: what should I check?
Raise CONFIRM_MS or MIN_GAP_MS.
