This picture is from arduino.cc . Could someone please help me understand these parameters min and max? I was using TinkerCad taking input for the angle using a potentiometer (having mapped correctly the input from (0 to 1023) to (0 to 180) ) but i don't see any change when i vary either min or max in my code. The servo still goes to 180-deg position when input is 1023.
2 Answers
In your question you're referencing the servo library. You mention mapping ranges of 0-1023 (the sampling range of your potentiometer) and 0-180 (the degrees of rotary travel of the average hobby servo).
There is an additional important concept to get familiar with as you learn about servos. That is the Pulse Width Modulated (PWM) signal that the Arduino (or any other controller) uses to communicate with the servo to set what angle of rotation it should assume. PWM is used a few different ways in electronics, but in this scenario it is being used as a servo control signal, and the pulse width (measured as a time duration) communicates the required angle to the servo. The library you're referencing helps users by translating between angles and PWM pulse lengths. But under the covers servos always (close enough for our conversation anyway) receive a signal based on PWM.
Another complicating factor is that servos vary greatly. There is a loose convention that says that a pulse width of 1500 micro-seconds is centre position, 1000 micro-seconds is the minimum -90 degress (or 0 degrees depending on how you look at it), and 2000 micro-seconds is the maximum +90 (or 180) degrees. These match the defaults that @Bra1n notes in their answer.
(Diagram by Wikimedia user Hforesti - https://commons.wikimedia.org/wiki/File:Sinais_controle_servomotor.JPG, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=101951622)
But that convention is broken all the time. Some servos are capable of less than or greater than 180 degrees. Others use different pulsewidths to specify the maximum and minimum rotations. It's for this reason that the library allows the minimum and maximum pulsewidth (expressed in micro-seconds) to be specified for the particular physical servo being attached. This allows the library to correctly map to its range of travel.
Everytime you use a new type of servo (or consider buying one for a new project) you'll need to look at its datasheet to get the important information about how that servo works.
One last thought about traps for new players, I've also seen servo libraries which express the maximum and minimum servo PWM values in the range of 0-4096. Some people also express this as a % duty cycle (mixing the language of those other purposes I mentioned PWM is also used for). Knowing that these all map back to a PWM pulse width is useful for making sense as you learn more about servos. Good luck!
It's hard coded in the servo library (well it is on the ESp8266 version I used).
// The following values are in us (microseconds).
// Since the defaults can be overwritten in the new attach() member function,
// they were modified from the Arduino AVR defaults to be in the safe range
// of publicly available specifications. While this implies that many 180°
// servos do not operate the full 0° to 180° sweep using these, it also prevents
// unsuspecting damage. For Arduino AVR, the same change is being discussed.
#define DEFAULT_MIN_PULSE_WIDTH 1000 // uncalibrated default, the shortest duty cycle sent to a servo
#define DEFAULT_MAX_PULSE_WIDTH 2000 // uncalibrated default, the longest duty cycle sent to a servo
#define DEFAULT_NEUTRAL_PULSE_WIDTH 1500 // default duty cycle when servo is attached
I discovered this when a servo attached to an ESP8266 was only moving half the expected angle so I modified the library (saving it as MyServo) and used that instead. You need to be careful when doing this as you can strip the servo gears if you try to move the servo outside its capable range.
-
Well I am a very beginner! Could you please guide me how I can learn this? What you said is a bit too advanced for me to understand. ( please bear with me). But I want to surely know what I should do to not strip my servo gears ( so what should min and max ) be set to? Maybe you could give some detailed references.– HarryJan 22, 2022 at 11:39