Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Stepper motor limit switch


I’m building a ‘robot’ to take panorama pictures: basically a motorized camera gimbal. I’m using an Orangutan to generate signals for a stepper motor controller, which in turn drives a stepper motor.

I drive my stepper motors using a controller which has the following inputs:

  • Pulse (makes the stepper motor rotate one step)
  • Direction (0= anti-clockwise, +5V=clockwise)

To prevent my stepper motor going round too many times (and tear my cables) I would like to have a limit switch. If I had enough IO lines I would just connect a micro switch to the Orangutan and stop turning once I detect a signal. Unfortunately I barely have enough IO lines to drive my steppers and handle other stuff.

What I need is some electronics that prevents my stepper motor from turning in one direction when the micro switch is pressed.

What these hypothetical electronics need to do:

  • limit micro switch not pressed ===> Pulse signal always goes through (the limit is not reached, so the motor can rotate both ways)
  • limit micro switch pressed and Direction +5V ===> Pulse signal is blocked (the stepper reached it’s clockwise limit)
  • limit micro switch pressed and Direction +0V ===> Pulse signal is goes through (the stepper can still rotate anti-clockwise)

In a schematic:

It something like this easy to build with some simple components, or is there an off the self IC that can do this?

Thanks in advance for the help :sunglasses:


Yay panoramas!

You could do what you’re describing with logic chips, but it would get kind of complicated.

It’s not exactly what you described, but you could use a resistor (lets say 10K) in line with the direction signal, and have two limit switches that would use smaller resistors (lets say 1K) to pull the direction line high or low, depending on which limit switch was pressed. Pulses wouldn’t be blocked at all, but any pulse sent to the stepper motor controller when the motor was already pressing against a limit switch would cause it to rotate away from that limit switch.

Alternatively, if your steppers aren’t super-strong you could just build simple mechanical stops. After all, there are no gear teeth to strip (I’m assuming you’re using the stepper output shafts directly, and not any down-gearing).


P.S. By the way, have you seen this thread yet?


Thanks for the help Adam!

Yes, I’ve seen the helicopter panorama thread, excellent stuff. My version will be a bit more heavy duty, the goal is to make a weather proof autonomous panorama robot (so I can leave it outside).

A mechanical stop is not an option because I’m using a a really nice 72:1 worm gear and a 1Nm stepper motor. Taking into account the effectiviness of the gear I should have 36Nm of torque left. :open_mouth:

I also need the limit switch for bringing my panorama head to the default position (like a flatbed scanner recalibrates before scanning), so if the stepper switches direction when it has reached it’s limit I can’t default to a known rotation.

Having thought about it a bit more I can make this with 2 logical AND gates (transistors?) and one XOR gate:

If the limit is not reached the signal to AND gate 1 will always be high, so the pulse will always go through.
If the limit has been reached the signal to AND gate 1 will only be high when DIR is high, so the pulse will only go through in one direction.

Would this work and which electrical component can invert a signal?


Aah, yes, in that case mechanical stops are right out. I’m a little confused by your drawing though, your XOR gate only has one input, and it looks like AND gate 1 has three inputs.

It sounds like you’re probably using the EasyDriver v3, or something similar as your stepper motor driver, but in either case I’m assuming you’re using something that steps on the positive edge of the pulses. Also, are you talking about just one limit switch, like a “home position” switch? At first I thought you meant safety switches at both ends of the range of motion, which would make things a bit more complicated.

I think you can set up a home switch that works like you described with just a single transistor and a few passive components, if you use a SPDT switch (like this one for example):

When the switch is not pressed, the the pulse and dir lines are directly connected, but when the switch is pressed, the dir line has to trip the transistor to let pulses through. The resistor on the right pulls the pulse line low during the switch transition, and while the dir line is keeping pulses from going through. The capacitor prevents switch bounce in the unlikely event that the switch is thrown during a pulse while the direction is being switched to move back away from the switch. If you don’t have one lying around you can probably leave it out.

This circuit is a a little backwards from your original description (when the switch is pressed it lets pulses through when DIR is high and blocks them when DIR is low), but it’s about as simple as you can get. You can reverse the rotation direction of the stepper motor by swapping the wires of one of the windings to make it work. If it absolutely has to be the way you described, you can use a PNP transistor instead.


P.S. To answer your last question, you can use a transistor and some resistors to invert a logic signal, like this:

Note: That’s an NPN transistor, if you were to use a PNP transistor you would want to flip the output line and the 1K resistor to the ground side. This diagram is from the Transistor circuits page of the Electronics Club website, which has lots of nice little circuits like this and some other basic electronics background info.


A ‘home switch’ is indeed a better description of what I need. The schematic you’ve drawn looks like it does exactly what I need (as far as I can tell with my limited electronics knowledge :wink: ). I’ll probably try it out this weekend and I’ll let you know how it turns out. Thanks for the help! (again!)