Moving motors in sequence with Sensors

Starting with 3 sensors and 3 motors.
Each motor is connected to a gate within a maze. Gates 1 and 2 are open at the start of the experiment. Gate three is closed.
A proximity sensor is placed after each gate. Sensor 1, when triggered, while cause two actions:

  • motor 1 to rotate 90 degrees counterclockwise (to close gate 1),
  • and motor 3 to rotate 90 degrees clockwise (to open gate 3).

The remaining two sensors will open and close the other two gates in a similar fashion.

Currently, the problem that I am having is that when the code is uploaded motor 1 rotates 90 degrees without sensor 1 being triggered. Nothing happens when the other sensors are triggered.
I have checked all my wiring, multiple arduino boards, different motors and sensors.

Also, can anyone who has used these motors confirm if I have specified the steps, pins, and speeds correctly? Trying to use only two pins (not all 4) from the step motor controller.

Using 28BYJ-48 5V Step Motors with the drivers that came with them.

#include <Stepper.h>
const int stepsPerRevolution = 512;

//declaring pins for motors 1, 2, and 3 (motors will be configured to rotate clockwise 90 degrees (open position) or rotate counter clockwise 90 degrees (closed position))
Stepper motor1(stepsPerRevolution, 1, 2, 1, 2);
Stepper motor2(stepsPerRevolution, 3, 4, 3, 4);
Stepper motor3(stepsPerRevolution, 5, 6, 5, 6);

// declaring pins for proximity sensors 1, 2, and 3
int sensor1 = 8;
int sensor2 = 9;
int sensor3 = 10;

//seting up variables to read sensor values (low when triggered, high when not triggered)
int sensor1value;
int sensor2value;
int sensor3value;

// setting up counters which i am using to prevent motors from changing states (open or closed) until subsequent conditions are met
int sensor1counter;
int sensor2counter;
int sensor3counter;

//defining motor states
int counter1 = 0; //open
int counter2 = 0; //open
int counter3 = 1; //closed

void setup() {
pinMode(sensor1, INPUT);
pinMode(sensor2, INPUT);
pinMode(sensor3, INPUT);


void loop()
//read sensor values
sensor1value = digitalRead(sensor1);
sensor2value = digitalRead(sensor2);
sensor3value = digitalRead(sensor3);

if (sensor1value == LOW); //if sensor 1 is triggered
if (counter1 == 0) //and if gate 1 is open
motor1.step(-544); //rotate motor 1 counterclockwise 90 degrees (close gate 1)
counter1 = 1; //note that gate 1 is now closed
motor3.step(544); //rotate motor 3 clockwise 90 degrees (open gate 3)
counter3 = 0; //note that gate 3 is now open

if (sensor2value == LOW);
if (counter2 == 0)
counter2 = 1;
counter1 = 0;


if (sensor3value == LOW);
if (counter3 == 0);
counter3 = 1;
counter2 = 0;

So what is your issue? Does the program do anything? What does it do that is different from what you want?

You have no comments with useful information like if a sensor counts as triggered when it's high or low or when a gate is open or closed. I'm not going to try to guess what the program is doing.


First thing you should notice 28BYJ-48 5V Step Motor’s pins. Because of i had it a long time ago for building my first cnc plotter. When i compile it motors just vibrated not turned. I was figure it out with check out of the motors wire and there was wrong of wiring.
So be carefull with 28BYJ-48 stepper motors. :wink:

Thank you, just updated new code, comments, background, and trouble shooting.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you post a table showing
All sensors OFF, which gates open/closed
Sensor 1 triggered, which gates open/closed
Sensor 2 triggered, which gates open/closed
Sensor 3 triggered, which gates open/closed

Can there be a situation where more than one sensor is triggered.
By drawing a table you can easily see how your logic needs to work.

This could use the switch … case type function

Tom… :slight_smile:

    if (counter1 == 0) //and if gate 1 is open

What would lead anyone to believe that the code does what the comment suggests that it does?

  if (sensor3value == LOW);

It is extremely rare for an if statement to end with a ;.

If you are using sensors to open the gates, are you going to delay when the gate closes after the sensor deactivates, so people can clear the gateway?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

  • How are you controlling your steppers.
  • How are you powering the project.
  • Have you written code just to test the stepper drivers and stepper motors?

Tom.... :slight_smile: