I am using the Accelstepper library to control a stepper motor, according to commands input via Serial. It works as expected for the first time when I start Serial Monitor and send command.

When left idle for some seconds (> 10 seconds), the motor makes clicking sounds for the first few commands(without moving) and then it starts working again as expected. The longer I leave it, the more commands it misses.

It gets resolved if I restart Serial Monitor on UNO.

#include <Arduino.h>
#include "AccelStepper.h"


enum Direction { FORWARD, BACKWORD } direction;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete

void blockingRunSpeedToPosition();

AccelStepper stepper(AccelStepper::DRIVER, STEPPER_X_STEP_PIN, STEPPER_X_DIR_PIN);

void setup()

void loop()
  // print the string when a newline arrives:
  if (stringComplete) {
      // clear the string:
      if(inputString == "x") {
          direction = FORWARD;
      inputString = "";
      stringComplete = false;

void serialEvent() {
    while (Serial.available()) {
        char inChar = (char)Serial.read();
        inputString += inChar;
        if (inChar == '\n') {
          stringComplete = true;

void blockingRunSpeedToPosition()
    int steps = 400;
    while (stepper.distanceToGo() != 0) {

I think there is no issue with the code or my Arduino connection since the motor runs fine in the beginning.

I tried the above code on Arduino MEGA, UNO, different motors with different drivers. The problem persists.

I think it is something to do with motor excitation (energized coils). Please give me some hints and pointer to debug the issue, since I don't know where to look. I am an intermediate programmer and robotics hobbyist.

Here is the video demo of the issue.

I am using:

  • Arduino MEGA - ATmega2560
  • NEMA 23.
  • Rino stepper motor.
  • Arduino Pin 2 is Step and 3 is Direction.
  • Driver Vcc is connected to 5V pin.
  • 48 volt, 5 Amps SMPS.
  • Ubuntu 16.
  • Accelstepper library
  • 2
    Reopening the serial monitor probably resets your Arduino, clearing problematic state in the code. Your program is not very well architected - there's no real point to the expensive String operations you do, or to using the serial event to set a flag you read in the main loop. Consider handling everything in the main loop on a character-by-character basis. – Chris Stratton Jan 8 '17 at 20:03
  • Just checked, restarting serial monitor fixes the problem only on UNO. On mega restarting doesn't effect. – Praveen Singh Yadav Jan 8 '17 at 20:28
  • Will the code keep responding to commands as intended, as long as you don't leave it idle for >= 10 sec? – JRobert Jan 9 '17 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.