arduinoesp32stepperpinsmotordriver

Driving stepper motor with only one pin


I'm trying to move a stepper motor (a 12-volt 28BYJ-48) with a A4988 driver using an Arduino. The motor is only going to move in one direction at one speed. I'm trying to drive this motor with a single pin.

Since I don't care which direction it moves, I had assumed that I wouldn't need to give a pin to the "DIR" input, but I can only seem to get it moving if I include that. I've tried using the BasicStepperDriver/Stepper libraries, but they seem to only work with two pins.

Is it possible to drive a stepper motor with only one pin? If so, how can I do it?


Solution

  • You can hardwire the DIR pin of your driver to be either HIGH or LOW to set a fixed direction. Do not keep it floating, as it could lead to unexpected direction changes. Also use a current limiting resistor. You could of course also set the DIR pins state in code.

    A stepper driver has a simple STEP/DIR interface with just two connections (3 if you include GND) to the controller. Give a short voltage pulse on the STEP pin to make it move one single step, set the direction by driving the DIR pin logical HIGH or LOW.

    When you know that, driving the stepper in one direction is then as simple as sending a short pulse to the STEP pin of your driver. You could of course also do that with a simple button instead of a microcontroller, pulse is pulse.

    Every stepper motor has a steps/revolution rating, e.g. 200/360° or a step angle rating, e.g. 1,8°. So if you pulse 200 times on the STEP pin of the driver, the motor will move one complete revolution, or 360°. Of course there is also mircostepping, but this goes to far offtopic.

    In your simple usecase there is no need to use a library, you could simply do:

    int stepPin = 12;
    int dirPin = 11;
    
    void setup()
    {
        pinMode(stepPin, OUTPUT);
        pinMode(dirPin, OUTPUT);
        digitalWrite(dirPin, HIGH); //set a fixed direction
    }
    
    void loop()
    {
        digitalWrite(stepPin, HIGH);
        delay(50); //set HIGH pulse length/width
        digitalWrite(stepPin, LOW);
        delay(100); //set time between single steps
    }
    

    The first delay can be reduced to be as short as possible. If you make it too short, your driver could miss the STEP signal. The second delay is the interval between to steps. With that you can adjust the rotational speed of your motor. The lower you set that, the faster your motor will run. In this example the motor would step once every 150ms, when using a 200steps/revolution motor the resulting speed would be 150ms x 200 / rev = 30000ms / rev = 30s / rev = 2RPM.

    This of course is only a very simple example with blocking code. To get rid of blocking delays, in the arduino IDE have a look at File > Examples > 02. Digital > BlinkWithoutDelay.

    If you are interested how a stepper driver switches the 2 coils of a (bipolar) stepper motor, have a look at this simplified interactive animation.