First Arduino where do I get started to control servos?

I watched a lot of youtube videos but most of them just shows things working but none of the basics.

I just got me a starter kit, a nano and a servo. What I'd like to do is be able to control the servo turning either direction. I've gotten as far as putting the nano on the breadboard and installing the softwares. Does anyone have recommendation on good starting points to read.

Have a look at the official Arduino servo library (Servo.h).

1 Like

You could work through the Servo examples provided with the library and the IDE.

Awesome, followed the link and got my motor spinning. Now onto more complex stuff.

The purpose of this project is to automate my blinds to open and close. Servo is going to have a thread bobbin mounted and pull strings wrapped around it to open and close it.

Ultimate goal is to get it to work with a Zwave switch/relay of some kind.

  1. I'd like to add an input or two so if I press button 1, go in one direction, button 2, go in the other position.

  2. How can I code it so that regardless of the servo position (modified HS311 servo for continuous) spin X amount of rotation whenever I push a button.

My servo now i randomly spinning back and forth but never stop at the same location

dannieboiz:
2. How can I code it so that regardless of the servo position (modified HS311 servo for continuous) spin X amount of rotation whenever I push a button.

Something like:
if (digitalRead (YOUROPENBUTTON_PIN)) {
yourServo.write (120); // Start the servo spinning to open.
delay (1000); // Wait one second and let the servo run.
yourServo.write (90); // Set back to neutral. (STOP)
}

if (digitalRead (YOURCLOSEBUTTON_PIN)) {
yourServo.write (60); // Start the servo spinning to close.
delay (1000); // Wait one second and let the servo run.
yourServo.write (90); // Set back to neutral. (STOP)
}

Or similar should work for you. It does not have to be too precise for this application.

Using your code the way it is, the servo doesn't stop, it keeps on going in one direction, When I press the button it seems to speed up the motor couple press later it reverse a little bit then go back to the opposite direction. This is what my code looks like at the moment.

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {

  • myservo.attach(9); // attaches the servo on pin 9 to the servo object*
    }
    void loop() {
  • if (digitalRead (3)) {*
  • myservo.write (120); // Start the servo spinning to open.*
  • delay (1000); // Wait one second and let the servo run.*
  • myservo.write (90); // Set back to neutral. (STOP)*
  • }*
    if (digitalRead (3)) {
  • myservo.write (60); // Start the servo spinning to close.*
  • delay (1000); // Wait one second and let the servo run.*
  • myservo.write (90); // Set back to neutral. (STOP)*
    }
    }

You will need to adjust for whatever your servo's neutral position is. 90 is the normal one, but it can be off especially on modded servos. You may also want to add some sort of debounce to your switch reads in the code. Also, I thought you were going to have a button for each direction. If you are just going to have one, then delete one of the if sets.

I did realize that your original code calls for 1 switch. my code looks like this at the moment but regardless of which button I push it yield the same result. I'll order me a real continuous motor and see how it goes.

Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
if (digitalRead (3)) {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(1); // sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
delay(1000);
}
if (digitalRead (3)){
myservo.attach(9); // attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(179);// sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
delay(1000);
}
if (digitalRead (4)) {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(-50); // sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
delay(1000);
}
if (digitalRead (4)){
myservo.attach(9); // attaches the servo on pin 9 to the servo object
delay(15);
myservo.write(-100);// sets the servo position according to the scaled value
delay(1000); // waits for it to get to the position
myservo.detach();
delay(1000);

dannieboiz:
spin X amount of rotation
....
never stop at the same location

You have no control over the position of a continuous servo. You relinquished that control when you modded it: as you now know, what is seen as position by a normal servo, is now seen as speed and direction.

JimboZA:
You have no control over the position of a continuous servo. You relinquished that control when you modded it: as you now know, what is seen as position by a normal servo, is now seen as speed and direction.

Yeah, that's why I gave him the delay code. A known or constant speed at a set time will be close to the same distance each time.

His code needs to just have one reaction to the button, and a debounce added (see Simple multi-mode 4wd Rover JR-001 - Hackster.io for a debounce example)

He also needs to figure out what the neutral point of the servo is. I assume he 2.2kx2 the pot for the mod.
To find it, just write a script to set the servo to 90 and change the val up or down (change in the direction where it gets slower) until the value is found. You could also use the Serial.Read and use the + and - keys to increment or decrement the value, printing the current val. Once found, base your forward and reverse off of that.

I will admit, it is easier this pre-modded servos, as most have a hole to get to the pot, but it's not hard to find it on a self modded one, just takes some time.

jscottb:
A known or constant speed at a set time will be close to the same distance each time.

A known speed and a set time will give exactly the same distance each time, pretty much by definition of the words "known" and "set" ;). Trouble is you can't guarantee the speed.

It's a leap of faith to try to position the blinds on dead reckoning like that. It needs for example encoders to count the turns and / or limit switches to detect the position.

JimboZA:
A known speed and a set time will give exactly the same distance each time, pretty much by definition of the words "known" and "set" ;). Trouble is you can't guarantee the speed.

It's a leap of faith to try to position the blinds on dead reckoning like that. It needs for example encoders to count the turns and / or limit switches to detect the position.

It should be close enough for as they say "for government work" :slight_smile: I do 45 and 90 angle turns like this with servo bots. Once you know the neutral pos for the servo, the speed should be close to the same the value + and - from that number.

i.e. NEUTRAL=95; 125 and 65 should be very close to the same speed in opposite directions. I'm not sure if the OP was looking for true accuracy. If they are, then I would use a DC gear motor and an encoder, like you say.

The purpose of this project is to automate my blinds to open and close. Servo is going to have a thread bobbin mounted and pull strings wrapped around it to open and close it.

An old blind project that used a standard servo. If you blind has a wand that is twisted to open the slats, then a sail winch servo might be used to operate the wand.

http://www.oocities.org/zoomkat/blindtilt.htm

I took your advice and bought a Parralax continuous motor.

zoomkat:
An old blind project that used a standard servo. If you blind has a wand that is twisted to open the slats, then a sail winch servo might be used to operate the wand.

http://www.oocities.org/zoomkat/blindtilt.htm

My approach is much simpler. Just going to wrap the draw string around a thread bobbin and pull the strings. Hence I need to learn how to set how many rotation the servo spin.

Just wanted to give kudos to everyone's help. I think I got the basics to start fine tuning.

Using the codes below, I'm able to control the servo to with 2 buttons to go either direction.

I guess my next time is to play with the delay stop time and figure out how long each rotation is?

#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
if (digitalRead (3)){
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(180); // sets the servo position according to the scaled value
delay(2000); // waits for it to get to the position
myservo.detach();
delay(1000);
}
if (digitalRead (4)){
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(45); // sets the servo position according to the scaled value
delay(2000); // waits for it to get to the position
myservo.detach();
delay(1000);
}}

Just to let you know, using code tags will help make your code easier to read. It is good that you are posting your code, though.

goodinventor:
Just to let you know, using code tags will help make your code easier to read. It is good that you are posting your code, though.

thanks, how do i do a code tag?

dannieboiz:
thanks, how do i do a code tag?

Select the code when you typed it (or while modifying a post) and hit the </> icon top left. Then it

will
{
look like
}
this