Can't program stepper motors and they just vibrate

Been trying to make a simple car with 2 28BYJ-48 5VDC motors and their drivers, using the CustomStepper library. If I use the demo script from the lib it does work well, but with my custom manager for handling movement with 2 motors they just vibrate in place and do nothing. It worked fine earlier and the only change I made was restructuring the code. I power them from a separate 9V battery (I know that can be problematic but the same problem happens when I connect the motors straight to the Arduino). Here’s all my code:

MotorManager.h:

#ifndef MOTORMANAGER_H
#define MOTORMANAGER_H

#include <CustomStepper.h>

class MotorManager {
  public:
    CustomStepper &lMotor;
    CustomStepper &rMotor;

    MotorManager(CustomStepper &_lMotor, CustomStepper &_rMotor)
      : lMotor(_lMotor), rMotor(_rMotor) {}

    void setup(float RPM, float SPR) {
      lMotor.setRPM(RPM);
      rMotor.setRPM(RPM);
      lMotor.setSPR(SPR);
      rMotor.setSPR(SPR);
    }

    bool areDone() {
      return lMotor.isDone() && rMotor.isDone();
    }

    void stopMoving() {
      lMotor.setDirection(STOP);
      rMotor.setDirection(STOP);
    }

    void startMoving() {
      lMotor.rotate();
      rMotor.rotate();
    }

    void dirForward() {
      lMotor.setDirection(CCW);
      rMotor.setDirection(CW);
    }

    void dirBackward() {
      lMotor.setDirection(CW);
      rMotor.setDirection(CCW);
    }

    void rotateDegrees(float degrees) {
      lMotor.rotateDegrees(degrees);
      rMotor.rotateDegrees(degrees);
    }

    void dirLeft() {
      lMotor.setDirection(CW);
      rMotor.setDirection(CW);
    }

    void dirRight() {
      lMotor.setDirection(CCW);
      rMotor.setDirection(CCW);
    }

    void run() {
      lMotor.run();
      rMotor.run();
    }
};

#endif

car.ino:

#include <CustomStepper.h>
#include "MotorManager.h"

CustomStepper lMotor(8, 9, 10, 11);
CustomStepper rMotor(2, 3, 4, 5);

MotorManager motors(lMotor, rMotor);

const int joyX = A0;
const int joyY = A1;
const int button = A5;

const float RPM = 18.0;
const float SPR = 4075.7728395;

void setup() {
  Serial.begin(9600);
  
  motors.setup(RPM, SPR);
  
  // joystick is unused for now
  pinMode(joyX, INPUT);
  pinMode(joyY, INPUT);
  pinMode(button, INPUT_PULLUP);
}

void loop() {
  if (motors.areDone()) {
    // this is a dummy program used just as a test
    motors.dirLeft();
    motors.rotateDegrees(180.0);
    Serial.println("Rotated 180 deg left");
   
  }
  
  motors.run();
}

Looked online and couldn’t find any solutions to similar code-related problems. I highly doubt the hardware is connected wrong since the demo stepper.ino script from the lib works flawlessly with both steppers. Please help

not familiar with the library you're using. might help if you post the "earlier" code that you said worked

You need a better power source.

1 Like

I know that in he future I’ll have to use a battery pack, but even with the smoke alarm battery the example program is able to rotate the motors just fine

Then the code is at fault. Try using MobaTools, it is highly thought of here on the forum.

1 Like

managed to figure out the problem, was, in fact, the power source (or lack thereof). The demo program worked because it only operated on one motor and the power in the Arduino or the battery wasn’t enough to start up both of the steppers. So to fix that you either need a much better power source or (as a temporary solution) turn the motors on one by one, via hardware or software. Guess I’ll have to buy a battery pack after all :melting_face:

Post #3 was the solution, I believe.

1 Like