Stepper Motor and SPI devices

Hey Guys,

I'm having trouble to get my Nema17 to work while I read sensordata via SPI, im using a rotary encoder wich works fine with the as5048a Library by ZoetropeLabs, yet my Motor won't work.(I am using AccelStepper Lib for the Steppers)

Both works fine on its own, but together there is a problem like there is any delay in the code- but there isnt.
Thats the weird thing- the motor will just start vibrating and buzzing if i try to read the sensordata via SPI at the same time.

Has anyone a clue what might cause this weird issue and can help me?

Greetings RWTH_MASCHI

here is the code:

#include <AS5048A.h>
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 11, 10); // Step Dir

//define encoder
AS5048A angleSensor (9);

int currentStep;
int lastStep;
int angle;

boolean inDegrees = true; //set to Mode to "degrees" and not to "Step".. For "Step" set inDegrees=false
int winkelTeilung; //200Steps per revolution or 360 degrees
int fullRotationCounter = 0;


void setup()
{
  Serial.begin(9600);
  angleSensor.init(); //initialize the absolute rotary encoder
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(1000);
  stepper.moveTo(10000);
}
void loop()
{

  long data = (int)angleSensor.getRawRotation(); //get the 14bit position as int
  

  if (inDegrees == true) {
    
    currentStep = (int)data * 0.02197265625; //converts 14bit int/revolution to 360deg/rev
    winkelTeilung = 360;

  } else {
    
    currentStep = (int)data * 0.0122070703125; //converts 14bit int/revolution to 200steps/rev
    winkelTeilung = 200;
  }

  //if the currentstep value overflows add 1 rotation and substract one if it goes backwards
  if (currentStep >= 0 && currentStep <= 10 && lastStep <= winkelTeilung - 1 && lastStep >= winkelTeilung - 10 ) {
    fullRotationCounter++;
  }
  if (lastStep >= 0 && lastStep <= 10 && currentStep <= winkelTeilung - 1 && currentStep >= winkelTeilung - 10) {
    fullRotationCounter--;
  }

  //get the total value of rotation 
  angle = currentStep + (fullRotationCounter * winkelTeilung);

  Serial.print("Got rotation of: ");
  Serial.println(angle);

  lastStep = currentStep;

//just to Test if the steppermotor runs
  stepper.moveTo(10000);
  stepper.run();
}

RWTH_MASCHI:
here is the code:

I suspect it is not the correct code because line 30 has

long data = (int)angleSensor2.getRawRotation(); //get the 14bit position as int

but angleSensor2 has not been defined anywhere.

I wonder if the problem is that the call to getRawRotation() is slowing the program down so much that stepper.run() is not being called often enough.

...R

Oh you are right it must be just "angleSensor" in line 30. I changed it up to make more sense ^^ and forgot that line

And yeah you are right again. I don't really know if it's the .getRawRotation() or that I am using spi.
In the library i use, there's nothing what seems to slow the program down.
and why is it even slowing down?
Because its too memory intesive or because arduinos processor is too weak?

could i just use a teensy 3.5 to cure my problem?

My question is how to speed it up so that I can use it..

It seems to work in uStepper ( i cannot understand the code at all, -its crazy)
and it seems to work in Mechaduino ( im diggin into it, its easier to understand, but quite a lot for a guy with not too much coding experience

I had not noticed earlier, but you should not be using pin 11 for the stepper motor. Pins 11, 12 and 13 are part of the SPI system. For SPI to work pin 10 must also be set as OUTPUT. If you are not short of pins I would suggest not using pin 10 for the motor either.

...R

oh did not know that u cant use those pins. I was currently using the pins that are only usable for spi. now my stepper works as usual! thank you

But what can I do, so that if I wanna print out some data that my stepper keeps working. Serial.print(angle) slows the program down A LOT even so much that the stepper is only buzzing again. its annoying because i kinda have to see the data for debugging

RWTH_MASCHI:
But what can I do, so that if I wanna print out some data that my stepper keeps working. Serial.print(angle) slows the program down A LOT even so much that the stepper is only buzzing again.

You need to post the latest version of your program.

...R

ofc. but it hasnt changed much.

heres the new code:

#include <AS5048A.h>
#include <AccelStepper.h>

// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 3, 2); // Step Dir

//define encoder
AS5048A angleSensor (9);

int currentStep;
int lastStep;
int angle;

boolean inDegrees = true; //set to Mode to "degrees" and not to "Step".. For "Step" set inDegrees=false
int winkelTeilung; //200Steps per revolution or 360 degrees
int fullRotationCounter = 0;


void setup()
{
  Serial.begin(9600);
  angleSensor.init(); //initialize the absolute rotary encoder
  stepper.setMaxSpeed(1000);
  stepper.setAcceleration(1000);
  stepper.moveTo(10000);
}
void loop()
{

  long data = (int)angleSensor.getRawRotation(); //get the 14bit position as int


  if (inDegrees == true) {

    currentStep = (int)data * 0.02197265625; //converts 14bit int/revolution to 360deg/rev
    winkelTeilung = 360;

  } else {

    currentStep = (int)data * 0.0122070703125; //converts 14bit int/revolution to 200steps/rev
    winkelTeilung = 200;
  }

  //if the currentstep value overflows add 1 rotation and substract one if it goes backwards
  if (currentStep >= 0 && currentStep <= 10 && lastStep <= winkelTeilung - 1 && lastStep >= winkelTeilung - 10 ) {
    fullRotationCounter++;
  }
  if (lastStep >= 0 && lastStep <= 10 && currentStep <= winkelTeilung - 1 && currentStep >= winkelTeilung - 10) {
    fullRotationCounter--;
  }

  //get the total value of rotation
  angle = currentStep + (fullRotationCounter * winkelTeilung);


  //if you comment this in, the motor will only buzz- if u leave it out everything works fine 
  //but id like to see the angle data anyway 
  //*****************************************************************************
  //Serial.print("Got rotation of: ");
  //Serial.println(angle);
  //*****************************************************************************

  
  lastStep = currentStep;

  //just to Test if the steppermotor runs
  stepper.moveTo(10000);
  stepper.run();
}

I marked the code that still causes problems.

the printing takes too long so the stepper steps get slowed down and it does not work as intended
much appreciated if anyone has a solution :slight_smile:

Printing in every iteration of loop() will almost certainly fill the serial output buffer and cause Serial to block until it empties. You could try a much higher baud rate. I normally use 500,000 baud but I don't think the Serial Monitor can work that fast.

Better still, just print stuff at intervals of 500 millisecs or so.

...R