Stepper motor and A4988 - Problem

Good day

I am currently working on a project involving several different sensors and three motors, two DC and one Stepper.

I made a topic last week (Stepper motor -Problem) where the problem was I did not use the appropriate stepper motor controller.

I got myself Pololu A4988 - Black Edition and tested the following code, which writes to the LCD and turns the motor:

#include <AccelStepper.h>
#include <LiquidCrystal.h> 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define STEP_PIN 46
#define DIR_PIN 48

AccelStepper stepper(1, STEP_PIN, DIR_PIN);

void setup()
{
 
  stepper.setMaxSpeed(2000);
  stepper.setSpeed(-1000); 

  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("MOTOR TESTING");

}


void loop()
{
    stepper.runSpeed();
}

The motor worked perfectly as can be seen in this VIDEO

Now I want to be able to update the LCD screen and display sensor readings and therefore I put the LCD commands in the void loop just to test the screen. See following code:

#include <AccelStepper.h>
#include <LiquidCrystal.h> 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define STEP_PIN 46
#define DIR_PIN 48

AccelStepper stepper(1, STEP_PIN, DIR_PIN);

void setup()
{
 
  stepper.setMaxSpeed(2000);
  stepper.setSpeed(-1000); 

  lcd.begin(16,2);
  

}


void loop()
{
    stepper.runSpeed();
    lcd.setCursor(0,0);
    lcd.print("MOTOR TESTING");
}

After I ran the program the motor was acting strange. See this VIDEO

So bottom line is: Whenever there are some other commands with stepper.runSpeed() in the void loop (like sensor readings, LCD displaying, DC motors turning) the motor does not fully function. Its like there is some disturbance in the signal from the arduino to the motor.

Hopefully someone can help!

My guess is that the LCD code is preventing loop() from repeating fast enough.

You are trying to move at 2000 steps per second so loop() needs to be able to repeat more frequently than that.

Is your motor capable of working at that speed if you don't make the calls to the LCD ?

...R

Robin2:
My guess is that the LCD code is preventing loop() from repeating fast enough.

You are trying to move at 2000 steps per second so loop() needs to be able to repeat more frequently than that.

Is your motor capable of working at that speed if you don't make the calls to the LCD ?

...R

Robin, thank you as always for answering.

Just ignor the stepper.setMaxSpeed(2000) it is actually supposed to be 1600, that is the maximum value for my motor(I've got 0.1125° half step, so full 360 are 1600)

In the first video the motor is working at set speed 1000 and also in the other video.

The main difference between the codes is that I add the LCD commands to the void loop instead of the setup.

I will give you another example:

If I remove the LCD commands from the void loop and switch to Serial commands

Serial.print("MOTOR TESTING") the same thing happens with the motor. It behaves in a strange way.

I mean that any other command like the following make the motor strange:

lcd.print

Serial.print

Running sensors

Running DC motors via adafruit motorshield

For example if I want to run the two DC motors I have at the same time I would use the following code:

#include <AccelStepper.h>
#include <LiquidCrystal.h> 
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

#define STEP_PIN 46
#define DIR_PIN 48

AccelStepper stepper(1, STEP_PIN, DIR_PIN);


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

// Select which port motor uses
Adafruit_DCMotor *DC_1 = AFMS.getMotor(1);
Adafruit_DCMotor *DC_2 = AFMS.getMotor(2);



void setup()
{
  stepper.setMaxSpeed(1600);
  stepper.setSpeed(1000); 

  AFMS.begin(); 
  
  
  DC_1->setSpeed(55); //have values from 0 to 255 for DC
  DC_2->setSpeed(55);
  
  DC_1->run(FORWARD);
  DC_2->run(FORWARD);
  
  DC_1->run(RELEASE);
  DC_2->run(RELEASE);


}


void loop()
{
    DC_1->setSpeed(250);
    DC_2->setSpeed(250);
    DC_1->run(FORWARD);
    DC_2->run(FORWARD);
    stepper.runSpeed();
}

The same thing happens, no matter what I put with the stepper function in the void loop

Have you tried running the stepper very much slower - perhaps 200 steps per second or even 100?

Do you still have the problem at those slow speeds?
If not, it suggests that the Serial/LCD stuff is slowing things down so that the high speeds are not possible.

And note that you cannot draw conclusions from the behaviour of a DC motor because it does not require a sequence of timed steps.

I don't know the insides of the Accelstepper library and whether it might be incompatible with Serial or LCD stuff. But my simple stepper code does not use any library and would be good for testing - especially the 2nd example which does not use delay().

...R

You may need interrupt driven stepper code if the LCD library is sluggish.

Robin2:
Have you tried running the stepper very much slower - perhaps 200 steps per second or even 100?

Do you still have the problem at those slow speeds?

Thank you for your suggestions.

I decreased the speed to 200 steps per second and ran the motor, and also lower.
Then I implemented the Serial.print function and ran the motor, it made some difference but not as much as running the motor at high speed.

It seems like the arduino is unable to send sufficient pulses to the stepper motor at high speed when running another commands.

MarkT:
You may need interrupt driven stepper code if the LCD library is sluggish.

I am a beginner at these things, do you recommend any arduino tutorials on interrupts? I've been watching Jeremy Blum's tutorial on youtube, very useful but it would be nice to have more detailed versions on interrupts.

UPDATE:
I connected the NEMA 17 motor from adafruit with the controller and tried to run with other commands and it worked perfectly.
Why is the other motor unable to run at high speed with other commands but the NEMA 17 can?

LiljarM:
UPDATE:
I connected the NEMA 17 motor from adafruit with the controller and tried to run with other commands and it worked perfectly.
Why is the other motor unable to run at high speed with other commands but the NEMA 17 can?

Post links to the datasheets for the two motors so that we can see how their characteristics vary.

...R

Robin2:
Post links to the datasheets for the two motors

Here is the stepper motor which I am having trouble with (BA version): Minebea Motor

This is the motor with no problem: NEMA 17 - Adafruit

I am driving both motors with A4988 black version, I made sure each time that the current limit was set right.

Is there a possibility to use two arduinos(MEGA and UNO)?

One arduino(MEGA) is used for all types of sensors and running the DC motors and the UNO is only for driving the stepper motor.

I need to change a flow rate by increasing or decreasing the motor speed. So that the MEGA sends flow rate data to the UNO which changes the speed if needed to.

(By the way I presume they are both NEMA17 motors - that is just the size of the faceplate.)

The low-current motor works and the high-current motor causes problems.

My guess is there is a power supply issue.

I can't see where you have told us what power supply you are using for the stepper motors - volts and amps ?
Is that power supply also powering other stuff ? if so, what ? A photo of a pencil drawing of all the wiring connections would be useful.

I believe it ought to be possible to drive either motor with a single Arduino. It should only be concerned with the link to the A4988 - which is the same in both cases.

...R