# Controlling Stepper Motor using Ultrasonic proximitySensor

**URL:** <https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936>\
**Category:** Programming\
**Created:** [November 1, 2017, 4:52pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936 "2017-11-01T16:52:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xertaw](https://avatars.discourse-cdn.com/v4/letter/x/e480ec/32.png) [@Xertaw](https://forum.arduino.cc/u/Xertaw)\
**Post date:** [November 1, 2017, 4:52pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/1 "2017-11-01T16:52:49Z")

</div>

Hello guys, i am very new to arduino, actually i have never done anything on arduino yet, i'm just trying to do my project. We have 28 BYJ-48 Stepper motor and ULN2003A driver. We are expected to design a garage door, and we are using proximity sensor that will control stepper motor and that motor needs to rotate the garage door 90 degrees. But we couldn't manage to do it. Our stepper rotates 90 degrees + and then -. It doesn't stop and also we don't know how to make sensor and motor work together. It is kinda urgent we couldn't make it by ourselves i need help. Thanks

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [November 1, 2017, 4:56pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/3 "2017-11-01T16:56:41Z")

</div>

HINT : There is nothing wrong with the code that you have not posted, nor with the circuit diagram that you have not posted.

---

<div class="post-metadata">

**Author:** ![Xertaw](https://avatars.discourse-cdn.com/v4/letter/x/e480ec/32.png) [@Xertaw](https://forum.arduino.cc/u/Xertaw)\
**Post date:** [November 1, 2017, 5:13pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/4 "2017-11-01T17:13:57Z")

</div>

#include \<Stepper.h\>

int motorPin1 = 8; // Blue - 28BYJ48 pin 1  
int motorPin2 = 9; // Pink - 28BYJ48 pin 2  
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3  
int motorPin4 = 11; // Orange - 28BYJ48 pin 4  
// Red - 28BYJ48 pin 5 (VCC)

int motorSpeed = 1000; //variable to set stepper speed  
int count = 0; // count of steps made  
int countsperrev = 128; // number of steps per 1/4 revolution  
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};

//////////////////////////////////////////////////////////////////////////////  
void setup() {

pinMode(motorPin1, OUTPUT);  
pinMode(motorPin2, OUTPUT);  
pinMode(motorPin3, OUTPUT);  
pinMode(motorPin4, OUTPUT);  
Serial.begin(9600);  
}

//////////////////////////////////////////////////////////////////////////////  
void loop(){  
if(count \< countsperrev )  
clockwise();  
else if (count == countsperrev \* 2)  
count = 0;  
else  
anticlockwise();  
count++;  
}

void anticlockwise()  
{  
for(int i = 0; i \< 8; i++)  
{  
setOutput(i);  
delayMicroseconds(motorSpeed);  
}  
}

void clockwise()  
{  
for(int i = 7; i \>= 0; i--)  
{  
setOutput(i);  
delayMicroseconds(motorSpeed);  
}  
}

void setOutput(int out)  
{  
digitalWrite(motorPin1, bitRead(lookup[out], 0));  
digitalWrite(motorPin2, bitRead(lookup[out], 1));  
digitalWrite(motorPin3, bitRead(lookup[out], 2));  
digitalWrite(motorPin4, bitRead(lookup[out], 3));  
}

---

<div class="post-metadata">

**Author:** ![Xertaw](https://avatars.discourse-cdn.com/v4/letter/x/e480ec/32.png) [@Xertaw](https://forum.arduino.cc/u/Xertaw)\
**Post date:** [November 1, 2017, 5:21pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/5 "2017-11-01T17:21:56Z")

</div>

#include \<Stepper.h\>

// the number of steps on your motor  
#define STEPS 4096

// create an instance of the stepper class, specifying  
// the number of steps of the motor and the pins it's  
// attached to  
Stepper stepper(STEPS, 8, 9, 10, 11);

// Variables for ultrasonic sensor  
// defines pins numbers  
const int trigPin = 5;  
const int echoPin = 6;

// defines variables  
long duration;  
float distance;

void setup() {  
// set the speed of the motor to 30 RPMs  
stepper.setSpeed(30);

//setup for the sensor  
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output  
pinMode(echoPin, INPUT); // Sets the echoPin as an Input

Serial.begin(9600); // Starts the serial communication  
}

void readSensor() {  
//reads the value of ultrasonic sensor and prints it

// Clears the trigPin  
digitalWrite(trigPin, LOW);  
delayMicroseconds(2);  
// Sets the trigPin on HIGH state for 10 micro seconds  
digitalWrite(trigPin, HIGH);  
delayMicroseconds(10);  
digitalWrite(trigPin, LOW);  
// Reads the echoPin, returns the sound wave travel time in microseconds  
duration = pulseIn(echoPin, HIGH);  
// Calculating the distance  
distance= duration\*0.034/2;  
// Prints the distance on the Serial Monitor  
Serial.print("Distance: ");  
Serial.println(distance);  
}

void loop() {

//clockwise rotation  
for(int i = 0; i \< 30; i++)  
{  
// move one step  
stepper.step(1);  
readSensor();  
}  
//anticlockwise rotation  
for(int i = 0; i \< 30; i++)  
{  
// move one step in reverse  
stepper.step(-1);  
readSensor();  
}

}

And we have this code, but when we put our hand in front of the sensor it stops and when we pull it back it rotates but it rotates 360. We need the opposite when we put the object it will rotate 90 degree when we pull it back it will rotate 90 degree opposite side.

---

<div class="post-metadata">

**Author:** ![Ballscrewbob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ballscrewbob/32/950458_2.png) [@Ballscrewbob](https://forum.arduino.cc/u/Ballscrewbob)\
**Post date:** [September 7, 2019, 8:29pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/7 "2019-09-07T20:29:25Z")

</div>

xmperez was banned as he requested.

Any more requests like that ?

🙂 🙂 🙂

Bob.

---

<div class="post-metadata">

**Author:** ![younzhy](https://avatars.discourse-cdn.com/v4/letter/y/c37758/32.png) [@younzhy](https://forum.arduino.cc/u/younzhy)\
**Post date:** [January 13, 2020, 7:44am UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/8 "2020-01-13T07:44:06Z")

</div>

do you still have the circuit diagram where the pins connections are.can you send it to me

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:48pm UTC](https://forum.arduino.cc/t/controlling-stepper-motor-using-ultrasonic-proximitysensor/488936/9 "2021-05-05T22:48:58Z")

</div>

Closed on May 5, 2021, 10:48pm UTC


