# Control a servo motor

I'm learning to control a servo (SG90). I read at this tutorial: http://www.seattlerobotics.org/guide/servos.html

The servo expects to see a pulse every 20 milliseconds (.02 seconds). The length of the pulse will determine how far the motor turns. A 1.5 millisecond pulse, for example, will make the motor turn to the 90 degree position (often called the neutral position). If the pulse is shorter than 1.5 ms, then the motor will turn the shaft to closer to 0 degrees. If the pulse is longer than 1.5ms, the shaft turns closer to 180 degrees.

Is this true for SG90 ? I can't find this info at Specification :

Operating Voltage : 4.8~6.0V
Operating Speed : 0.12sec/60 degree(4.8V)~0.1sec/60 degree(6.0V)
Torque : 1.6kg/cm(4.8V)
Temperature Range : -30~+60?
Cable Length : 25cm
Servo Type : Analog Servo
Brand Model : SG90

• Quite probably yes, but the specs of that specific servo should give the authoritative answer. Mar 15, 2014 at 14:40

While a datasheet for this servo does not seem to be readily available after a quick Google search, I would say you should be fine. All hobby/RC servos such as the SG90 that I have come across use that same protocol. Unless they specify they are a digital servo, which use a greater pulse frequency, the 1.5ms pulse width = center position standard is followed.

The best thing to do would just be to test it and see what happens. The pulses are just control signals. If you give the motor a pulse that is too short or too long, it will just turn to its minimum and maximum angles.

• ya, but I'm not sure about distance between 2 pulse, is it must be 20 ms? thank you for your answer
– nlnl
Mar 15, 2014 at 15:06
• Typically, the controllers are just looking for pulse width. However, a safe bet is a 50Hz signal with varying duty cycle to match the correct pulse widths. Mar 15, 2014 at 15:29

The SG90 appears to be a standard servo motor included in most hobbyist kits these days (2017). While the following information mentions the Raspberry Pi specifically, the explanation is good for a variety of micro-controllers.

Different software libraries expose the PWM functionality with slightly different interfaces. Some specify a frequency as a value with a single call. With others you make several calls to set a clock divisor and a range. With this second type of interface, the frequency is calculated by taking the original clock frequency divided by a clock divisor divided by a range.

This posting on the Raspberry Pi StackExchange, Control Hardware PWM frequency provides several answers on using Pulse Width Modulation (PWM) to control a servo motor with the second type of interface (clock divisor and a range).

This post was quoted in the adafruit tutorial Adafruit's Raspberry Pi Lesson 8. Using a Servo Motor which uses an SG90 servo.

Also see RASPBERRY PI LESSON 28: CONTROLLING A SERVO ON RASPBERRY PI WITH PYTHON using the first type of interface, using a frequency value, which says:

We can now start the pwm sequence by giving a command to specify the DutyCycle of the signal. Before we do this, we need to talk a little bit about how servos work. A typical servo wants to see a frequency of 50 Hz on the control line. The position it moves to depends on the pulse width of the signal. Most servos behave roughly as such, but you will need to tweak these numbers for your particular servo. Typically, the servo will go to the full left position when it sees a pulse width of 1 millisecond, it will go the middle position when it sees a pulse width of 1.5 millisecond, and it will go to the full right position when it sees a pulse width of 2 millisecond.

Also see the answer to Servo position with no pulse which has this to say about the pulse protocol for commanding a servo:

Servo control signals are not actually PWM but a variant, pulse duration modulation: Servo position is not defined by the PWM duty cycle (i.e., ON vs OFF time) but only by the duration of the pulse. As long as it is anywhere in a range of (typically) 40 Hz to 200 Hz, the exact value of the frame rate is irrelevant. The servo expects to see a pulse every so many ms, this can vary within a wide range that differs from servo to servo.