Ultrasonic and stepper motor work- Help with code

I would greatly appreciate some help in coding. I would like to have the stepper motor and ultrasonic work at the same time. My project is to have the stepper motor move the ultrasonic sensor. Once the the ultrasonic detects an object, it will start beeping using a buzzer. Thats what I have so far:#include <Stepper.h>
#define STEPS 255 // the number of steps in one revolution of your motor (28BYJ-48)
const int stepsPerRevolution = 2038;
const int trigPin = 7;
const int echoPin = 6;
const int buzzer = 12;
const int ledPin = 13;

long duration;
int distance;
int roundAngle;
int Angle;
Stepper stepper(STEPS, 8, 10, 9, 11);

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication

void loop() {

// Clears the trigPin
digitalWrite(trigPin, LOW);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
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;

safetyDistance = distance;
if (safetyDistance <= 20) // 20 cm
digitalWrite(buzzer, HIGH);
digitalWrite(ledPin, HIGH);
digitalWrite(buzzer, LOW);
digitalWrite(ledPin, LOW);


Re-Edit your posting by clicking on the pencil-button

mark all code and delete it.


You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Where in your loop() do you move the motor?
28BYJ-48 cannot do 60 RPM, about 12 ~ 14 is max without acceleration.

Hi Ray,

driving a stepper-motor requires to create a pulse-train.
measuring distance with an ultrasonic sensor requires to initiate a "ping"
and then waiting for the echo to arrive measuring the time until the echo arrives.

So these are two things that must be done in parallel.
A more specialised library like the MobaTools can drive a stepper-motor "in the backround". This enables to do the measurings with the ultrasonic sensor "in the foreground"

Programming is not as short as
"can somebody post the cheatkey to enter platinum-level?"

This means if you don't want to depend always on other users modifying your code there are some things to learn. Once you have a little experience with the "testing-method" it becomes fun to try and test.

You can combine two things to move up the learningcurve:

Posting your actual code that tries to achieve your goal asking a specific question. The probalitiy to get quick answers is much higher for specific questions about one detail than for generalised questions.

And posting your attempt clearly shows own effort and the will to learn

From your two postings (yes I saw the other post too) I estimate that you are a real beginner in writing code. This is completely OK!
I recommend take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial. Even if it is just two or three words.

Try to make a small modification to your complete sketch and post this new version with a specific question. Or If you don't like that post a specific question about that part you want to read a tutorial.

best regards Stefan

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