Arduino desde cero en Español - Capítulo 6 - Servomotor (conexión, modelos, ajustes para uso óptimo)

Arduino desde cero en Español - Capítulo 6 - Servomotor (conexión, modelos, ajustes para uso óptimo)

Introduction to Servomotors

In this chapter, we will learn about servomotors and how they work. Servomotors are small DC motors connected to a series of control circuits. They provide precise movement with high torque. The motor is enclosed in a plastic casing with exposed control cables and can be connected to various mechanical accessories.

Working Principle of Servomotors

  • A servomotor consists of a small DC motor connected to control circuits.
  • The motor is connected to gears that reduce speed and increase force.
  • The system is enclosed in a plastic casing with exposed control cables.
  • Different sizes of servos are available, ranging from micro servos to larger ones with metal gears.
  • The servo has three cables: power supply (typically 4.5V - 6V), ground, and signal.

Cable Identification

  • The central cable is usually red and represents the positive power supply.
  • The darker cable (black or brown) is the ground or negative connection.
  • The signal cable can be yellow, orange, or white.

Signal Control for Servo Movement

  • Servos can rotate from 0 to 180 degrees based on the width of the pulse signal sent.
  • A pulse duration of 1ms indicates 0 degrees rotation, while 2ms indicates 180 degrees rotation.
  • Modulating the pulse width between 1ms and 2ms allows for controlling the servo's full range of motion.

Circuit Connection

  • Connect the power supply cable (positive) of the servo to the positive rail on Arduino.
  • Connect the ground cable (negative) of the servo to the ground rail on Arduino.
  • Connect the signal cable directly to digital pin 2 on Arduino.

Additional Components

  • Use a potentiometer (10k) connected as done in Chapter 4.
  • Connect the left end of the potentiometer to ground and the right end to positive.
  • Connect the central pin of the potentiometer to analog pin A0 on Arduino.

Power Supply Considerations

  • When working with motors, it is recommended to use an external power supply.
  • USB cable from a computer may not provide sufficient current for motor operation.
  • Use a DC power supply (7.5V - 12V) with a center-positive plug (5.5mm outer diameter, 2.1mm inner diameter).

Arduino Code and Servo Library

  • Include the servo library by adding #include <Servo.h> at the beginning of your code.
  • Create a servo object using Servo servoName; (e.g., Servo myServo;).
  • This library provides functions to control servos easily.

Initializing the Servo

This section explains how to initialize the servo by specifying the pin it is connected to, as well as the minimum and maximum pulse values in microseconds.

  • To initialize the servo, use the following command: servo.attach(pin, minPulse, maxPulse).
  • The minPulse and maxPulse values should be defined in microseconds.
  • The library makes it easy to control the servo with just one command.

Controlling the Servo Angle

This section discusses how to control the angle of the servo using a single command.

  • Use servo.write(angle) to set the angle of the servo.
  • In this example, we set it to 0 degrees using servo.write(0).
  • The library takes care of generating the appropriate signal with the correct pulse duration and frequency.

Using Library Functions for Servo Control

This section highlights how convenient it is to use library functions for controlling servos.

  • With just a few lines of code, we can make the servo move between 0 and 180 degrees at intervals of 5 seconds.
  • Example code:
  • Set servo position to 180 degrees using servo.write(180).
  • Wait for 5 seconds using delay(5000).
  • Repeat by setting servo position to 0 degrees and waiting for another 5 seconds.

Continuous Movement Between Two Positions

This section explains how to continuously move a servo between two positions at regular intervals.

  • By repeatedly executing a loop that sets the servo position to 0 and 180 degrees, we can make the servo move back and forth.
  • The servo will move between these two positions every 5 seconds.

Observing Servo Movement

This section discusses how to observe the movement of the servo.

  • The servo should move smoothly from 0 to 180 degrees every 5 seconds.
  • The direction of movement is similar to the clockwise rotation of clock hands.
  • If there is no movement, check the servo connection, correct pin assignment, and external power supply.

Fine-Tuning Servo Range

This section explains how to fine-tune the minimum and maximum pulse values for achieving a full range of motion.

  • By adjusting the pulse values, we can ensure that the servo moves from 0 to 180 degrees without any limitations.
  • It may require trial and error to find the exact minimum and maximum pulse values for our specific servo.
  • Example adjustment:
  • Set minPulse value to 900 microseconds instead of 1000.
  • Set maxPulse value to 2100 microseconds instead of 2000.

Practical Testing for Servo Range

This section emphasizes the importance of practical testing to determine accurate minimum and maximum pulse values for achieving a full range of motion.

  • We need to experiment with different pulse values through trial and error.
  • The goal is to find values that allow the servo to move smoothly from one extreme position (0 or 180 degrees) without any resistance or vibration.
  • Example process:
  • Start with adjusted values (e.g., minPulse = 900, maxPulse = 2100).
  • Gradually decrease/increase the values to find the optimal range.
  • Take note of the final minimum and maximum pulse values for future reference.

Verifying Servo Range Adjustment

This section suggests uploading the modified program to verify if the servo now moves within the desired range.

  • After adjusting the pulse values, upload the program to Arduino.
  • Observe if the servo now moves smoothly from 0 to 180 degrees.
  • If necessary, repeat the adjustment process until achieving a full range of motion.

Mechanical Limitations of Servo Movement

This section explains that servos have mechanical limitations that prevent them from moving beyond certain angles.

  • Internally, servos have physical stops that prevent them from rotating beyond a specific angle (usually 180 degrees).
  • The goal is to adjust the pulse values so that they align with these mechanical stops.
  • Example process:
  • If adjusting with 900 and 2100 worked well, try testing with 800 and 2200.
  • Gradually approach the limits without forcing or straining the motor.
  • Take note of these final adjusted values for future use.

Fine-Tuning Servo Range Continues

This section continues discussing how to fine-tune servo range by observing motor behavior at extreme positions.

  • While moving from one extreme position (e.g., 0 degrees), observe any slight release or vibration in your hand.
  • Adjust pulse values accordingly to find a value slightly higher than where vibrations occur (e.g., increase from 800 to 850).
  • Repeat this process for reaching maximum position (e.g., 180 degrees).

Noting Final Pulse Values

This section emphasizes the importance of noting the final minimum and maximum pulse values for future reference.

  • After finding the optimal pulse values, make sure to write them down.
  • These values will be used in your code to control the servo accurately.
  • Example: Minimum pulse value = 700, Maximum pulse value = 2350.

Using a Potentiometer for Servo Control

This section introduces using a potentiometer to control the angle of the servo.

  • After defining minimum and maximum pulse values, we can create a program that uses a potentiometer to generate the servo angle.
  • Define two variables: int val for reading potentiometer input and int angle for storing converted angle value.
  • Use analogRead() to read analog input from the potentiometer and assign it to val.
  • Use map() function to convert val from its range (0-1023) to an angle range (0-180).
  • Set servo position using servo.write(angle) and add a small delay between movements.

Understanding the Map Function

This section explains how the map function is used to convert one

New Section Servo Movement with Potentiometer

This section discusses the incrementation of the "s" variable and how it affects the movement of the servo when the potentiometer is adjusted.

Incrementing "s" Variable

  • The "s" variable should be incremented to verify and adjust the program.
  • By moving the potentiometer, we can observe that the servo also moves accordingly.