Controlling Stepper Motor Using LDR-Automatic Blinds

I would like to automate my blinds so that they open during the day and close during the night. The blinds are simply designed like a projector screen, so that the rod rotates clockwise to allow the fabric to go down and rotates counterclockwise to open the blinds. However, I am having trouble with the code, as the stepper motor just continuously rotates in the counter clockwise direction regardless of the value of the LDR. I would appreciate any help. Thank you.

Here is the code:

#include <Stepper.h>
int stepsPerRevolution=2048;
int ldrPin=A0;
int analogValue=0;
int prevAnalogValue=0;
Stepper myStepper(stepsPerRevolution, 8,10,9,11);
void setup() {

void loop() {
  if(analogValue < 50 && prevAnalogValue > 50){
  else if (analogValue >= 50 && prevAnalogValue < 50){

Welcome to the forum

Please follow the advice on posting code given in Read this before posting a programming question in order to make your sketch easy to follow, copy and test

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here as it prevents some combinations of characters in code being interpreted as HTML commands such as italics, bold or a smiley character, all of which render the code useless

If the code exceeds the 9000 character inline limit then attach it to a post

What do you see if you print analogValue before testing it ?
Does the value change as you cover/expose the LDR and is it in the expected range ?
How is the LDR wired ?

Does the stepper move in the required direction if you use the commands to move it in a standalone sketch ?

I would write something simpler and prove that the stepper can be run both ways without the complication of the analog readings.

Also, put some serial prints in there so you can see what the analog reading actually is.

Edit: Or exactly what UKHeliBob said :slight_smile:

Great minds !

Thank you for the suggestions, I will try them!

How did you establish the trigger level of 50 in the first place ?

Once you have it working you may want to implement a dead band (Google hysteresis) between the open and close light levels to avoid the blinds opening and closing with a small change of light level up and down such as when a cloud passes or a car with headlight passes

An alternative would be to require the reading to stay in the trigger band for a period of time before reacting to a change

Thank you, UKHeliBob, I like that idea.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.