I want to stop a moving servo with input from a micro switch

Hi All,

I'm a noob so I want to thank all of you for the help I'm about to receive.

I'm trying to create a clamping device using a toggle clamp and a servo. My problem is that I'm clamping different thicknesses of material and therefore can't tell the servo what angle I want it to stop at. I have to sense the thickness and request an in-motion stop, using a micro switch, but I'm at a loss to figure out the code. More than likely because I don't understand how servos work yet. Or the code to control it.

Oh... by the way, I've spent close to 5 hours youtubing and surfing this and other forums for a solution.

Thanks so much for your help.

Force feedback might be called for.

You can mount your servo on a load cell and once some pressure is reached, stop movement.

there should be lots of tutorials of how servos work.
basically a motor and a position feedback of some sort.

you can add a pot and use that as your position sensor
add an encoder

add a spring to offer some range of force

Look at the servo examples that come with the IDE to see how to control a servo e.g. sweep.

Then move the servo a degree at a time, delay between each and check the microswitch.

Hi dave-in-nj!

Thanks so much for the fast response. I really want to keep this simple. I'm not worried about the force being applied. I just want to stop the servo at a point where enough force is applied to lock the material down. I'm not even sure that a servo is a good device for this application. I thought about a linear actuator but they are HUGE. Take up way too much space. And still I need some ideas on coding a hard stop on feed back from a micro switch.

Start with the servo in the fully open clamp position, say at angle 0. Command the servo to go to an angle of 180 using small steps by means of a for loop as in the Servo Sweep example. If at any time the stop button becomes pressed then exit the for loop and the servo will remain at that position

Have you thought about how you are going to return to the servo to the start position

What advantage does using a servo have over using a normal clamp of some kind ? I don't know what sort of servo you have in mind but you need to provide power to it from an external source and not the Arduino 5V pin

Hi wildbill,

Thanks so much for your response. I will check out the tutorials again but I only see one, so far, that talks about the servo and how the code works but it's limited. Not well explained either.

I'm a coding noob. I'll check out the tutorial again. Maybe I missed something.

Hi UKHeliBob,

Yup, I get the idea of a for loop but how to bring it to a stop is the question I have. How to return it to zero is the next challenge. I'm working on stopping it right now.

So if I have an input from a switch, how do I code an exit from the for loop?

Oh yeah... I do intend on getting a servo driver when I need to apply power. Right now it's just watching the motion of the servo until I have the action right.

Not tutorials, examples. There are two servo specific examples in the Arduino IDE.

AH! Examples.

Good I will check them out!

Thanks so much.

As I said previously

Do you know how to wire the switch and read the state of its pin ?

if you are in the planing stages on the mechanics, there are a lot of options.
a servo will not be delivering much force so it might be perfect, or it might be underpowered.

do you have a force in mind ? as much as a clothespin ?
as much as a 6 inch hand held spring clamp ?
holding down a 100 pound part ?

I highly expect that knowing distance and getting to it will not work.
the amount of pressure exerted with a movement of the thickness of a human hair might be hundreds of pounds.

higher motor amps might be a feedback.
a spring arm on the servo, etc.

Since you found no answers, I am guessing is because it is not the correct path. I was in machining for 20 years and would never consider a distance as a suitable clamping method.

Yes sir, Wiring and reading a switch is easy. How to exit the loop, that's another story.

Are you using the following kind of Servo? If you have written any codes, please post them.

Hi dave-in-nj,

So the toggle clamp has a rubber bumper on it that compresses over the material, I've found that I get equal clamping force when the rubber compresses about 3/64". That's all I need to keep it consistent. So if I set a micro switch to actuate and that amount of compression I have a secure material. I'm pretty sure I'll need to move to linear actuator to make this happen. Might make my own.


Hi GolamMostafa,

Yup, that's the exact servo I'm using for experimentation of the code. I'm just borrowing other people code so far so I have nothing original as yet. And not sure on how to exit a for loop.

Post those codes what you have borrowed.

Use the break; command


I saw that in the syntax page on Arduino but forgot to look at it.

IF that works, then add a pin that goes through the center of the rubber, so when the rubber is compressed, the pin is also pressed and it will press a microswitch and tell you to stop pressing. The pin will need a small spring to return it to it's original position.

That said, using a "for" loop to do this is poor coding practice.

Just saying ... :worried:

The concept of "state machine" coding is that in the main "loop()", on each pass through the loop you make decisions based on such things as a variable which defines the current position of the servo and whether the switch is actuated, and those decisions determine whether you change the current position of the servo or not, and how much you change it. Unless you change it, there is no need to alter the value of the servo command.

Doing this in the loop() allows you to add other functions if you need to which may or may not be even related to the servo operation. :sunglasses: