Novice questions about Arduino control of servos

Thread Starter

Terrypin

Joined Feb 5, 2016
113
I'm using the sketch below to experiment with the 'SG90 micro servo' that came with my starter kit.

Q1: With the values shown I would expect to get a 180 degree sweep in both directions, but I estimate it as about 150. Trying other values, it always falls short by20-30 degrees. Is that normal?

Q2: Is there a sketch available anywhere that uses a function, with parameters (such as start and finish angle, and duration)? That might be neater and easier to use for testing.

Q3: For the project I have in mind I will probably need a servo with greater torque but still compact in size. Any recommendations before I start googling please?

Q4: On a general point, why are comments inside /* always in a faint, hard-to-read font? Can that be changed?


Any other practical advice would be appreciated please.

Code:
//www.elegoo.com
//2016.12.08
#include <Servo.h>

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() {
  Serial.begin(9600);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop() {
  for (pos = 10; pos <= 180; pos += 1)
  { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }

 delay(500);
 
  for (pos <= 180; pos = 10; pos -= 1)
  { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(30);                       // waits 15ms for the servo to reach the position
  }
  delay(1000);
}
Terry, UK
 

AlbertHall

Joined Jun 4, 2014
12,347
Your program starts and ends the rotation at 10 degrees - though the example code I found does the same thing. You could try changing the two 'pos = 10' lines to 'pos = 0'
 

Thread Starter

Terrypin

Joined Feb 5, 2016
113
Thanks both.
I've also posted in the Arduino forum. The first reply there advises that many servos cannot cover the full 180, so that answers Q1.
 

Thread Starter

Terrypin

Joined Feb 5, 2016
113
Thanks Dana, that PDF on hobby servos is going to be valuable reading.

I'm tempted to divert from my Arduino coding experiments onto one or more of those GUI interfaces, but I'll probably pass. With help here and in the ardduino.cc forum I'm making slow but encouraging progress. Currently also using Simon Monk's 'Programming Arduino' and Michael Margolis's 'Arduino Cookbook'. And of course the sometimes daunting web resources,
--------------------

Anyone re my Q4? Is that how the IDE is designed, or is it a setting I've missed?

Terry
 

Attachments

djsfantasi

Joined Apr 11, 2010
9,163
Q1: While RC servos are supposed to position themselves between 0-180 degrees, amongst different servos and manufacturers, the pulse widths widths needed vary. One servo might go to 180 degrees with a pulse width of 2250 ms while another needs a width of 2500 ms.

The built-in servo library doesn’t have a way to map between pulse width and degrees.
There is a SoftwareServo library that may work. I don’t know for sure. I use external servo drivers that operate with pulse widths and map degrees to pulse widths in software.

Q2: I have no idea

Q3: See my answer to Q2. I would go to a servo supplier and compare specifications.

Q4: The grey comments is how the IDE works. I just loaded the IDE and I don't see a setting for this. The IDE supports something called "themes". But I don't see any documentation of this feature.
 

Thread Starter

Terrypin

Joined Feb 5, 2016
113
Thanks a lot, that’s very helpful.

Could you name or link to your preferred external servo driver please? That route sounds easier.
 

djsfantasi

Joined Apr 11, 2010
9,163
Thanks a lot, that’s very helpful.

Could you name or link to your preferred external servo driver please? That route sounds easier.
It’s not necessarily easier. I used an SSC-32 from Roboshop partly because I was replacing an external laptop in a working system with an Arduino.
 
Top