Need to make servo motor pivot 180 back and forth with button push

Hello all, i'm looking to do a hobby project, and I need to sweep a servo motor back and forth 180 degrees rather quick, by pushing a button to trigger the movement. I'm still on the hunt for the proper servo motor with the torque and speed needed (as fast as possible), but I am lost when it comes to writing the code for this. My guess is someone has probably already done something like this, maybe even exact, and I can use their code to modify to fit my needs, but it seems pretty basic in nature. Does anyone have any sample codes I can study and play around with? Thanks!

Start by having a look at the servo sweep example that comes with the Arduino IDE. There are also examples about buttons and switches.

...R

There are several examples in the IDE which read a button and do something. Then the Sweep example shows how to set up a servo but for the quickest movement you don't need the for loops technique it uses. Just replace with write(180) then write(0).

Steve

Thank you guys! i have found a servo I will test out and this seems like it should be easy enough!

slipstick:
There are several examples in the IDE

Just be careful, the IDE tutorials show that the servo can be powered (as distinct from controlled) from the Arduino. You must NOT do this:

Rather get a set of batteries or a wall wart capable of supplying better than say 1A and use that; connect all the grounds.

Thank you! I just read that somewhere else actually!

I just came across this also, which seems pretty much what i need. The only difference I'd like is rather than 2 buttons, Id rather have 1 button and upon pressing the full 180, and then pressed again, go the full 180 the other way.

http://www.arduinotutorialonline.com/2018/01/control-servo-motor-with-push-button.html

I have ordered the materials I need, but If anyone has a suggestion on how I would change the code to do as said above I'd appreciate it!

Can't wait to play around with this!

mhunkler:
I just came across this also, which seems pretty much what i need.

Came across what.... you leave off a link?

This diagram illustrates how a separate servo power supply should be connected

ServoPower.png

...R

ServoPower.png

anthonyHope:
Came across what.... you leave off a link?

oops sorry, its late i've been up all night researching lol. i edited, but here is is again

http://www.arduinotutorialonline.com/2018/01/control-servo-motor-with-push-button.html

Have a look at this switch example.

Then use the state variable there instead of turning an led on or off, to send the servo left or right.

Robin2:
This diagram illustrates how a separate servo power supply should be connected

ServoPower.png

...R

thank you for clarifying! I don't plan on using the USB connection when in use, so the arduino will need to be powered as well. I bought a kit with my arduino that looks like it has a 9volt battey connector that could be to plug into the DC power port? or maybe its just something that came with it. Either way, that should be ok to power the arduino board since its only using 5v from the USB correct?

anthonyHope:
Have a look at this switch example.

Then use the state variable there instead of turning an led on or off, to send the servo left or right.

this is perfect thanks!!

mhunkler:
I bought a kit with my arduino that looks like it has a 9volt battey connector that could be to plug into the DC power port?

Without more details all I can say is DO NOT use one of the small PP3 style 9v batteries - they cannot provide enough current. If you need 9v then use a pack of 6 x AA alkaline cells.

Another simple option is to use a pack of 3 x AA Alkaline cells (4.5v) or 4 x AA NiMh cells (4.8v) and use them to power the servo and the Arduino. Connect the batteries to the Arduino 5v pin.

The important thing is not to draw power for the servo (or any other motor) through the Arduino board.

...R

Robin2:
Without more details all I can say is DO NOT use one of the small PP3 style 9v batteries - they cannot provide enough current. If you need 9v then use a pack of 6 x AA alkaline cells.

Another simple option is to use a pack of 3 x AA Alkaline cells (4.5v) or 4 x AA NiMh cells (4.8v) and use them to power the servo and the Arduino. Connect the batteries to the Arduino 5v pin.

The important thing is not to draw power for the servo (or any other motor) through the Arduino board.

...R

What I was talking about was power supply for the arduino, not power for the servo motor. Using AA batteries to power the servo sounds good to avoid using the 5v from the arduino, but I was just saying that the power for the arduino itself won't be coming from the USB connection and i need to power that as well, and was asking for powering the arduino, would using a 9v battery to the dc connector be correct... however since my last post I have found that it can operate with 6-20v supply and the + of a 9v can just be connected to the vin port.

mhunkler:
the + of a 9v can just be connected to the vin port.

But the capacity of a PP3 style battery is so crap that even an Arduino will drain it very quickly. Or do they still make PM9s?

anthonyHope:
Just be careful, the IDE tutorials show that the servo can be powered (as distinct from controlled) from the Arduino.

I'd be grateful if you didn't selectively misquote me. I said there were examples in the IDE of reading a button. Nothing about powering servos.

Steve

slipstick:
I'd be grateful if you didn't selectively misquote me. I said there were examples in the IDE of reading a button. Nothing about powering servos.

Steve

Let me fix that then, by using your next sentence:

slipstick:
Then the Sweep example shows how to set up a servo

anthonyHope:
Just be careful, the IDE tutorials show that the servo can be powered (as distinct from controlled) from the Arduino.

(Important point though is that hopefully one day the owners of the site will fix the crap that's been wrong since, like, forever.)

mhunkler:
What I was talking about was power supply for the arduino, not power for the servo motor. Using AA batteries to power the servo sounds good to avoid using the 5v from the arduino, but I was just saying that the power for the arduino itself won't be coming from the USB connection and i need to power that as well,

You have the option of powering the Arduino from the same battery that is powering the servos PROVIDED the battery voltage does not exceed 5v.

...R