Stepper motors running in background using the Accelstepper library.

Can the Arduino Uno receive signals from an ultrasonic sensor and turn stepper motors are the same time? Without causing slowdown in the motors.

I’ve got two stepper motors (28BYJ-48), a plastic tub and the Accelstepper library and I’ve been having a great time having it zip across the floor.

Here’s is a very simple piece of code to make it travel forward for a short while:

#include <AccelStepper.h>
#define HALFSTEP 8

#define motorPin1  9
#define motorPin2  10
#define motorPin3  11
#define motorPin4  12

AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4); 

#define motorPin11  3
#define motorPin22  4
#define motorPin33  5
#define motorPin44  6

AccelStepper stepper2(HALFSTEP, motorPin11, motorPin33, motorPin22, motorPin44); 

void setup()


void loop()

I’ve also bought these ultrasonic sensors and been using them to detect the size of small distances.

Here is the code for the sensors:

const int trigPin = 7;
const int echoPin = 8;

long duration;
int distance;

void setup()
  //Sets up pins
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

void loop()
  // Clears the trigPin
  digitalWrite(trigPin, LOW);

  // Sends a signal for 10 microsecs
  digitalWrite(trigPin, HIGH);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculatings the distance
  distance = duration * 0.034 / 2;

  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");

Both the sensor and the motors work fine independently, but when I try to run them together the motors pause for a moment. If the sensor runs often (several times a second) it will cause the motors to become very jittery. I think its pulseIn line that causes the slowdown enough to disrupt the motors.

So my question is can you run the motors in a background or in some other kind of way? Is it possible with or without the Accelstepper library? I’ve searched quite a bit for answer but I only found a forum post from five years ago saying it might be possible. It might be a better question to ask if the Arduino is capable of doing these two tasks at the same time.

There is no "background". You can run motors and read sensors and do hundreds of other things at once IF you write non-blocking code to do it all. That means no delay calls and no long for or while loops waiting for something to finish. Take small steps at a time.

PS. No puleIn either since it blocks waiting for the signal. You'll have to send your signal and note the time from millis and then pick up millis again when you get the echo and figure the time from that.

I've also bought these ultrasonic sensors and been using them to detect the size of small distances.

The size of small distances? Are the sizes of small distances bigger than the sizes of large distances?

There is a NewPing library that uses interrupts to determine when the return pulse has been received, so you don't need to block waiting for the echo. THAT will allow the steppers and the ultrasonic sensors to work with only minimal interference with each other.

Since there is only one processor, it should be obvious that if you are telling one of the steppers that it is time to think about stepping, you can't also be sending a trigger pulse or reading the echo AT THE SAME TIME.

You can do those things in sequence, where the time between instructions is on the order of nanoseconds, so they can appear to us really slow humans as happening at the same time.