Monitoring a Stepper Motor with serial print without slowing it down

Stepper Motor: Modified 28BYJ-48
Ext. Power Supply: 6V
UC: Uno
Hbridge: SN754410

The motor works fine with the current code with significantly high torque.
The issue I'm having is that I cant calibrate a revolution without monitoring the steps over serial.

The thing is once I add the code "Serial.begin(9600);" the motor loses torque and dramatically slows down no matter how much I decrease the delay between each step.

I'm not sure what to do and how to set up the thing now...

Code:

int A = 5;//1
int B = 7;//3
int C = 4;//0
int D = 6;//2
long del = 1500;
int stepCount = 0;
bool fs[]={1,0,0,1,                        
           1,1,0,0,                      
           0,1,1,0,                        
           0,0,1,1};

bool hs[]={1,0,0,1,                        
           1,0,0,0,                      
           1,1,0,0,                      
           0,1,0,0,                      
           0,1,1,0,                      
           0,0,1,0,                      
           0,0,1,1,                        
           0,0,0,1};


#define L LOW
#define H HIGH
void setup() {                
  pinMode(A, OUTPUT);     
  pinMode(B, OUTPUT);     
  pinMode(C, OUTPUT);     
  pinMode(D, OUTPUT); 
  Serial.begin(9600);  <<<<<<<<<  
}


void fullStep(int step){
  int stepNum = 4;
  int trueStep = step-stepNum*round((step/stepNum)-0.4);
    switch(trueStep){
      case 0:
        digitalWrite(A, fs[0]);   
        digitalWrite(B, fs[1]);   
        digitalWrite(C, fs[2]);   
        digitalWrite(D, fs[3]);   
        break;

      case 1:
        digitalWrite(A, fs[4]);   
        digitalWrite(B, fs[5]);   
        digitalWrite(C, fs[6]);   
        digitalWrite(D, fs[7]);   
        break;

      case 2:
        digitalWrite(A, fs[8]);   
        digitalWrite(B, fs[9]);   
        digitalWrite(C, fs[10]);   
        digitalWrite(D, fs[11]);   
        break;

      case 3:
        digitalWrite(A, fs[12]);   
        digitalWrite(B, fs[13]);   
        digitalWrite(C, fs[14]);   
        digitalWrite(D, fs[15]);   
        break;

      default:
        break;
      }
  }

void halfStep(int step){
  int stepNum = 8;
  int trueStep = step-stepNum*round((step/stepNum)-0.4);
    switch(trueStep){
      case 0:
        digitalWrite(A, hs[0]);   
        digitalWrite(B, hs[1]);   
        digitalWrite(C, hs[2]);   
        digitalWrite(D, hs[3]);   
        break;

      case 1:        
        digitalWrite(A, hs[4]);   
        digitalWrite(B, hs[5]);   
        digitalWrite(C, hs[6]);   
        digitalWrite(D, hs[7]);   
        break;

      case 2:        
        digitalWrite(A, hs[8]);   
        digitalWrite(B, hs[9]);   
        digitalWrite(C, hs[10]);   
        digitalWrite(D, hs[11]);   
        break;

      case 3:        
        digitalWrite(A, hs[12]);   
        digitalWrite(B, hs[13]);   
        digitalWrite(C, hs[14]);   
        digitalWrite(D, hs[15]);   
        break;

      case 4:
        digitalWrite(A, hs[16]);   
        digitalWrite(B, hs[17]);   
        digitalWrite(C, hs[18]);   
        digitalWrite(D, hs[19]);   
        break;

      case 5:        
        digitalWrite(A, hs[20]);   
        digitalWrite(B, hs[21]);   
        digitalWrite(C, hs[22]);   
        digitalWrite(D, hs[23]);   
        break;

      case 6:        
        digitalWrite(A, hs[24]);   
        digitalWrite(B, hs[25]);   
        digitalWrite(C, hs[26]);   
        digitalWrite(D, hs[27]);   
        break;

      case 7:        
        digitalWrite(A, hs[28]);   
        digitalWrite(B, hs[29]);   
        digitalWrite(C, hs[30]);   
        digitalWrite(D, hs[31]);   
        break;

      default:
        break;
      }
  }    


void loop(){
  Serial.println(stepCount);
  // fullStep(a);
  halfStep(stepCount);
  stepCount++;
  
  delayMicroseconds(del);
//  delay(del);
}

Well, if you're going to print every step, your motor won't run any faster than Serial.println() can print, which isn't very fast.

1 Like

Try increasing the baud rate to 115200 or higher to decrease the time spent sending characters.

1 Like

FredScuttle:
Well, if you're going to print every step, your motor won't run any faster than Serial.println() can print, which isn't very fast.

It could be a lot faster, if OP wasn't stuck in the stone ages.

115200 is quite easily accomplished, when communicating with a PC.

The delayMicroseconds() call is used to wait between steps (or half-steps). Since you are already wasting time serial printing useless information, the additional delay is not needed.

1 Like

If you use millis() or micros() for non-blocking timing the time taken to print can overlap with the time between steps.

Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

And, as others have said, use a higher baud rate.

...R