Stepper motor rotates in + direction when photosensor is activated and stops and then moves - direction when sensor is not activated then stops


Hi all,
I am new to Arduino and am working on an architectural project where a sun shading device opens up when it is sunny out and then closes when it is shady or night. Also, am trying to incorporate a proximity sensor to try and have it slightly interact with people who are walking underneath by closing the canopy slightly. The design is basically a upside-down umbrella controlled by strings and the stepper motor pulls those strings (to close it) or releases the strings (to open it). I have been able to get the stepper motor and photoresistor to work however the motors will continue to try and rotate since it is in a loop. I have tried putting everything in Void Setup and leaving Void Loop blank but I keep getting errors that Void loop is blank. I have attached my code and images if that helps!

Thank you so much,
-Sam Sawyer-Standley

// Arduino stepper motor control code

#include <Stepper.h>

// Include the header file

// change this to the number of steps on your motor

#define STEPS 32

// create an instance of the stepper class using the steps and pins

Stepper stepper(STEPS, 8, 10, 9, 11);

int val = 0;

int LED = 7;

int LDR = A0;

void setup()

{

Serial.begin(9600);

stepper.setSpeed(800);

pinMode(LED, OUTPUT);

pinMode(LDR, INPUT);



}

void loop()

{

int LDRValue = analogRead(LDR);

Serial.print("sensor = ");

if (LDRValue <=100)


{

digitalWrite(LED, HIGH);

val = Serial.parseInt();

stepper.step(800);

Serial.println(val);



//for debugging

}

if (LDRValue >=100)

{

digitalWrite(LED, LOW);

val = Serial.parseInt();

stepper.step(-800);

Serial.println(val);


//for debugging


}

}
![IMG_6282|666x500](upload://zcp5PzMHnGRwFHqvcys9OlEfHJ0.jpeg)

No. You never got that error. Proof below.

... and raining.

What value is "val" collecting?

  • You need to add a flag when the motor has done its job in each direction, and only make the motor run (in either direction) if the flag is in a good state (for that direction).
  • at LDR = 100, your motor is being told to go forward and backward.

Here is your code, edited. It stops after one "800" rotation... your motor runs the same way in both loops... and it is easy to see that the program was copied from an HTML document.

// Arduino stepper motor control code

// Include the header file
#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 32

// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);
int val = 0;
int LED = 7;
int LDR = A0;

int flag = 0;

void setup() {
  Serial.begin(9600);
  stepper.setSpeed(800);
  pinMode(LED, OUTPUT);
  pinMode(LDR, INPUT);

  while (1) {
    int LDRValue = map(analogRead(LDR), 0, 1023, 90, 110);
    // Serial.print("sensor = ");

    if (LDRValue <= 100  && flag == 0)  {
      flag = 1; // set the flag to stop this direction and let the other direction run
      digitalWrite(LED, HIGH);
      // val = Serial.parseInt();
      stepper.step(800);
      // Serial.println(val); //for debugging
    }

    if (LDRValue > 100 && flag == 1) {
      flag = 0; // clear the flag to stop this direction and let the other direction run
      digitalWrite(LED, LOW);
      // val = Serial.parseInt();
      stepper.step(-800);
      // Serial.println(val); //for debugging
    }
  }
}

void loop() { /* I AM BLANK AS */ }
Here is the diagram.json for WOKWI.COM
{
  "version": 1,
  "author": "Anonymous maker",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -4.8, "left": -0.5, "attrs": {} },
    {
      "type": "wokwi-stepper-motor",
      "id": "stepper1",
      "top": -115.21,
      "left": 3.42,
      "attrs": { "size": "8" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -20.5, "left": 191.8, "attrs": {} },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": -70.8,
      "left": 109.4,
      "attrs": { "color": "red" }
    }
  ],
  "connections": [
    [ "nano:11", "stepper1:A-", "green", [ "v0" ] ],
    [ "nano:10", "stepper1:A+", "green", [ "v0" ] ],
    [ "nano:9", "stepper1:B+", "green", [ "v0" ] ],
    [ "nano:8", "stepper1:B-", "green", [ "v0" ] ],
    [ "pot1:GND", "nano:GND.1", "black", [ "v19.2", "h-76.8" ] ],
    [ "pot1:SIG", "nano:A0", "green", [ "v28.8", "h-173.2" ] ],
    [ "pot1:VCC", "nano:5V", "red", [ "v38.4", "h-144.8" ] ],
    [ "nano:GND.2", "led1:C", "black", [ "v0" ] ],
    [ "led1:A", "nano:7", "green", [ "v9.6", "h-48" ] ]
  ],
  "dependencies": {}
}

Maybe, maybe not, depending on the wiring and which stepper is being used. The 28BYJ-48 steppers have an awkward ABAB wiring compared to what I think of as normal AABB wired steppers. For straight-connected ABAB steppers, the Stepper.h configuration line is:

...with the two center coils swapped. If you hook an AABB type stepper to this, it will only turn one direction since the coils are crossed. One can fix it by physically swapping the two inner wires. (or by editing the Stepper line)

The steps per rev for a 28BYJ-48 is 2048 and max reliable speed about 10 ~ 12 RPM.
800 steps will only turn it about 140 degrees.

This makes logical sense, but how does your guess overrule my guess?

Well, since the OP didn't show a schematic, parts or a pic, it's hard to say what is actually going on. But, per their title, the OP seems to get +/- bi-directional operation out of their stepper with their wiring and code. I copied your code and json into Wokwi and saw the:

...symptom. I then switched the center wires and it worked perfectly. I'm not certain your wiring matches the OP's unknown wiring.

Yes... I see now how I overlooked this. I should learn to enjoy my falls-short... keeps me on the "learning" track. Eventually, I do appreciate your knowledge and corrections, after my tantrums (tantra?) subside.

1 Like

This is making more sense! I am going to try it all again tonight and will report back, I will also make a schematic, since I just haven't gotten to that just yet. Thank you all for helping me out, I know it can probably be difficult to work with someone who learning the basics!

I've run into the one-way stepper problem a couple times and it is tricky -- it depends on library, config, stepper-type, and wiring. Recently, I found that the Mobatools 4-wire example needs the center-swap for AABB steppers, and not for the ABAB steppers, while the Stepper.h example and AccelStepper Bounce needs the opposite.

Stepper drivers are the way to go.

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