Speed control for stepper in sketch using coil matrix

240816_1_RLSTorsteuerung_auf_zu_button.ino (1,4 KB)
Hi there, I am using the attached code for a stepper control with each 2 x ULN2003A driver and 28BYJ-48 motor with a separate 6V power supply. This works fine, as I can control the two steppers to run in opposite direction to each other and I can change their directions simultaneously. What I do not seem to be able is to change the speed of the stepper motors. Does anyone have a clue how I can do this in the matrix control code attached ? Many thanks.

It is nicer when we can see the code:

const int IN1 = 6;
const int IN2 = 7;
const int IN3 = 8;
const int IN4 = 9;
const int upButton  = 4;
const int downButton = 5;
const int ledUp  = 2;
const int ledDown = 3;

int coil1[] = {0, 0, 0, 0, 0, 1, 1, 1, 0};
int coil2[] = {0, 0, 0, 1, 1, 1, 0, 0, 0};
int coil3[] = {0, 1, 1, 1, 0, 0, 0, 0, 0};
int coil4[] = {1, 1, 0, 0, 0, 0, 0, 1, 0};

int coilStep = 0;
int rotation = 3;

void setup()
{
  pinMode(IN1,OUTPUT);
  pinMode(IN2,OUTPUT);
  pinMode(IN3,OUTPUT);
  pinMode(IN4,OUTPUT);
  pinMode(ledUp,OUTPUT);
  pinMode(ledDown,OUTPUT);
  pinMode(upButton,INPUT_PULLUP);
  pinMode(downButton,INPUT_PULLUP);
}

void loop()
{
  if (digitalRead(upButton) == LOW)
  {
    rotation = 1;
  } else if (digitalRead(downButton) == LOW)
  {
    rotation  = 2;
  } else
  {
    rotation = 3;
  }
  if (rotation == 1) {              // Forward
    digitalWrite(ledUp, HIGH);
    coilStep++;
    motorDrive(coilStep);
  }   
  else if (rotation == 2) {       // Reverse
    digitalWrite(ledDown, HIGH);
    coilStep--;
    motorDrive(coilStep);
  }
  else {
    motorDrive(8);
    digitalWrite(ledUp, LOW);
    digitalWrite(ledDown, LOW);
  }
  if (coilStep > 7) {
    coilStep = 0;
  }
  if (coilStep < 0) {
    coilStep = 7;
  }
  delay(10);
}

void motorDrive(int d)
{
  digitalWrite(IN1, coil1[d]);
  digitalWrite(IN2, coil2[d]);
  digitalWrite(IN3, coil3[d]);
  digitalWrite(IN4, coil4[d]);
}

You would change the speed by changing the delay between steps:

  delay(10);
1 Like

Hi @christophrubel,

is there a reason to control the steppers on a "low level" instead of using a library, e.g as the MobaTools?

https://github.com/MicroBahner/MobaTools

The lib provides a lot of confortable functions and a great documentation in English and German. Unless you are interested in learning how to drive steppers directly, using the library might be a good choice!

Good luck!
ec2021

Got you. Will do next time. Changing the (just one) delay variable worked. This one line looked too insignificant for me. Thank you.

1 Like

Yes, as I am using Arduino's more as experiments, I wanted to understand how I can control the stepper motor. Next project will be another Loco garage, I will for certain use MobaTools with this. Thanks.

Ok that's a valid reason!

You might be (or not) interested to have a look at this thread dated 2021 about the stepper motor topic

https://arduino.stackexchange.com/questions/80818/how-to-control-speed-of-28byj-48-stepper-motor-without-using-library

I haven't checked the complete posts but it seems to be reliable information.

ec2021