I am trying to reverse the direction of a stepper motor. The stepper motor drives a scanner head and when it gets to the end, I want it to reverse direction. I have some photo interrupters that trigger when the signal is interrupted but how do I make it switch direction when they are triggered? Do I use a flip-flop and if so what type. I am driving the stepper motor with a H-bridge that is controlled from an Arduino but I would prefer to switch the direction with hardware instead of programming it to reverse when it gets to the end. So what type of flip-flop do I use had how do I wire it? I have a bipolar 4 wire stepper motor.

Code to make the stepper motor move forward:

#include <Stepper.h>
#define STEPS 96
#define D0 3
#define D1 4
#define D2 5
#define D3 6
const int enab1 = 2;
const int enab2 = 7;

Stepper stepper(STEPS, D0, D1, D2, D3);

void setup() {
  digitalWrite(enab1, HIGH);
  digitalWrite(enab2, HIGH);
  digitalWrite(enab1, LOW);
  digitalWrite(enab2, LOW);
void loop() {



  • \$\begingroup\$ You would XOR all the logic driving the bridge. But this seems primitive with no absolute position accuracy \$\endgroup\$ Jan 22, 2019 at 4:11
  • 2
    \$\begingroup\$ Doing this in hardware when you already have an MCU-based Arduino is absurd. Do it in software. \$\endgroup\$ Jan 22, 2019 at 4:35
  • \$\begingroup\$ Then how do you suggest doing it in Arduino? What happens if it has to move 10 steps but in 5 is the end? \$\endgroup\$
    – user138346
    Jan 22, 2019 at 4:43
  • \$\begingroup\$ the switch will stop you from getting lost. \$\endgroup\$ Jan 22, 2019 at 4:48
  • \$\begingroup\$ How would I program that though. \$\endgroup\$
    – user138346
    Jan 22, 2019 at 4:49

2 Answers 2


I have a switch that triggers when it gets pushed

A physical switch WILL NOT give you accurate end stops. They are used for emergency stops on CNCs (everything has turned to mud at this point), but they are not typically used to provide an accurate home position. Using a switch (at both ends of your travel I assume) will therefore be inaccurate for your scanning.

You should use an opto interrupter to produce your home signal (at one end of your travel, and a typical methodology is:

  1. If not Home, seek to home at slow speed
  2. When home signal occurs, stop (this may involve slowing down)
  3. Seek forward at really slow speed till Home disappears and stop
  4. Seek forward for Home offset and set Zero position

You now have an Home position that is your Zero count and can seek to your maximum count and back to the Zero position. You then only need the Home flag for initialization.

Home sensors such as this work well and provide reasonable accuracy (0.1-0.2mm or better) when you use the narrow aperture version.

If you are dealing with cheap Arduino type sensors such as this, which are analog then you can have significant drift in your home sensor position with temperature. I don't recommend them.

how do I reverse the direction when it gets to the end?

You don't specify what type of H-Bridge you are using to drive the stepper. Assuming that you are using an A4988 or DRV8825 then you already produce your Step/DIR signal in your MCU. All you need to do to scan is:

  1. Start scan from Zero, set DIR and step count (you may be doing acceleration and deceleration control of course)
  2. Stop at step count
  3. Scan reverse to Zero, set DIR and step count
  4. Stop at Zero

If you are using something more primitive to drive the stepper, you really should consider moving to Step/Dir control, it will reduce your software development considerably.
There would seem to be no reason to try and implement a hardware direction change, since this would mean the accuracy of the trip points would depend absolutely on your switch, which will not be accurate. Your stepper motor count is way more accurate and repeatable.

  • \$\begingroup\$ I also have photo interrupters which I was planning to use at a later stage or if they were better. Also I have a L298N H-Bridge. \$\endgroup\$
    – user138346
    Jan 22, 2019 at 7:04
  • \$\begingroup\$ @TheTrialBot I really suggest you change from using Stepper.h. This is a really primitive way to drive a stepper motor. Make the change to a driver such as the A4988, you will not regret it. \$\endgroup\$ Jan 22, 2019 at 7:06
  • \$\begingroup\$ Is the L298N not good for what I am doing? \$\endgroup\$
    – user138346
    Jan 22, 2019 at 7:08
  • \$\begingroup\$ The L298 is really a bad joke, it has no smarts at all. About the only thing it's useful for is the little Ebay stepper motors like the 28BYJ-48. You have not really described what you are doing, what type of stepper motor, accuracy, resolution or the like. It's hard to help without that information. \$\endgroup\$ Jan 22, 2019 at 7:16

you could build a reversable quadrature counter like this:


simulate this circuit – Schematic created using CircuitLab


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.