Sunday, 3 March 2024

Driving a nema-17 stepper motor

 Ok, we're not messing about now.
Yes, it was always a gamble that using a cheap 28BYJ-48 stepper motor with a laser cut home-made gear and a super-glued length of timing belt to make a continuous loop might not work. And it turns out it didn't.

Sure, we could add some kind of spring rollers to push the belt against the drive gear to help reduce slippage. But right at the very start of this project, we suspected this might happen...


So we're not going to waste spend any more time cobbling together something that may or may not work -it's time to focus on the end result here, not tinker with possibles and maybes (however interesting it might be to try out lots of different ideas). It's time to do it properly (as we probably should have done in the first place!) and use a "proper" stepper motor, and a "proper" closed loop timing belt and remove as many points of weakness/failure from the system as possible.

There are a few common-fixed-length closed loop timing belts on the market - the largest I could find (at a reasonable cost and able to deliver quickly) was 610mm. They are available in multi-packs of different sized belts:




These come with T2 pulleys for use with any common 3d printer kit.
Which means we're going to be driving our nema-17 stepper motor using an A4988 driver board.

(whether we go for the full 12V or stick with our preferred 9V, we'll have to wait and see, but the principle is pretty much the same not matter which supply voltage we eventually go with)

The nice thing about the A4988 board is that we don't need to worry about investigating coils and working out step sequences and making sure we drive the coils in the correct sequence and so on. You simply provide a direction signal and a step pulse and each time the step pin rises from low-to-high, the board sends the appropriate signals to advance the stepper motor by one step (in the appropriate direction).

There's also an Arduino library that allows you to send single, individual step commands - so we can run a function on a timer-based interrupt which checks to see if the motor should be running, and sends the appropriate pulse-step if necessary - this will allow us to run our code without having to worry about "blocking functions" or the microcontroller becoming unresponsive while the motor is turning.

As before, the first step is to just get our motor spinning in response to a single input condition - we can then expand this for use with a closed-loop belt and to make our "racetrack" for a remake of the classic GW game Full Tilt.


const int stepPin = 8;
const int dirPin = 9;
int delay_ms = 1;
int potValue = 0;

void setup() {
    pinMode(stepPin,OUTPUT);
    pinMode(dirPin,OUTPUT);
}

void loop() {

    digitalWrite(dirPin,HIGH);
    for(int x = 0; x < 200; x++) {
        digitalWrite(stepPin,HIGH);
        delay(delay_ms);
        digitalWrite(stepPin,LOW);
        delay(delay_ms);
        getSpeed();
    }
    delay(100);

    // change rotation direction
    digitalWrite(dirPin,LOW);
    for(int x = 0; x < 200; x++) {
        digitalWrite(stepPin,HIGH);
        delay(delay_ms);
        digitalWrite(stepPin,LOW);
        delay(delay_ms);
        getSpeed();
    }
    delay(100);
}

void getSpeed(){
    // read the speed input pot and set the speed value as appropriate
    potValue = analogRead(A0);
    delay_ms = map(potValue, 0, 1023, 1, 25);
}

The end result is a variable speed motor, which we can use a simple potentiometer to control to speed of rotation.



At slow speeds, the stepping becomes almost visible, and the noise from the motor becomes very noticeable. At higher speeds, the motor is less "noisy" but at its fastest, the motor is clearly moving too quickly for our purposes - probably great if you're driving a CNC or a 3d printer, to be able to move the head around so quickly, but for us, we'd much rather a slower "top speed" and a quieter operation.

There are driver boards out there that specialise in ultra-quiet operation. If it comes to it, we might give this some consideration. But before we do that, there is still one option available to us..... microstepping.

No comments:

Post a Comment