How To Control A Servo With Arduino
In this tutorial nosotros volition learn how servo motors work and how to control servo motors with Arduino. Servo motors are very popular and widely used in many Arduino projects because they are like shooting fish in a barrel to use and provide great position control.
Servos are great choice for robotics projects, automation, RC models and then on. I take already used them in many of my Arduino projects and you can check out some of them hither:
- DIY Arduino Robot Arm with Smartphone Control
- Arduino Emmet Hexapod Robot
- DIY Arduino based RC Hovercraft
- SCARA Robot | How To Build Your Ain Arduino Based Robot
- DIY Mars Perseverance Rover Replica – Arduino based Project
You can picket the post-obit video or read the written tutorial below. It includes several examples how to use a servo motor with Arduino, wiring diagram and codes. In boosted, information technology has a guide how to command multiple servo motors with Arduino using the PCA9685 PWM driver.
What is Servo Motor?
A servo motor is a closed-loop system that uses position feedback to control its motion and final position. In that location are many types of servo motors and their main feature is the ability to precisely control the position of their shaft.
In industrial blazon servo motors the position feedback sensor is usually a loftier precision encoder, while in the smaller RC or hobby servos the position sensor is usually a simple potentiometer. The actual position captured by these devices is fed back to the error detector where information technology is compared to the target position. Then according to the error the controller corrects the actual position of the motor to match with the target position.
In this tutorial we will accept a detailed look at the hobby servo motors. We volition explicate how these servos work and how to control them using Arduino.
Hobby servos are modest in size actuators used for controlling RC toys cars, boats, airplanes etc. They are likewise used by engineering students for prototyping in robotics, creating robotic arms, biologically inspired robots, humanoid robots and so on.
How Servo Motors Work?
There are four principal components inside of a hobby servo, a DC motor, a gearbox, a potentiometer and a control circuit. The DC motor is high speed and low torque merely the gearbox reduces the speed to effectually 60 RPM and at the same time increases the torque.
The potentiometer is attached on the final gear or the output shaft, so every bit the motor rotates the potentiometer rotates as well, thus producing a voltage that is related to the absolute angle of the output shaft. In the control circuit, this potentiometer voltage is compared to the voltage coming from the signal line. If needed, the controller activates an integrated H-Span which enables the motor to rotate in either direction until the two signals achieve a difference of zero.
A servo motor is controlled by sending a series of pulses through the indicate line. The frequency of the command signal should be 50Hz or a pulse should occur every 20ms. The width of pulse determines angular position of the servo and these blazon of servos tin can normally rotate 180 degrees (they have a physical limits of travel).
Generally pulses with 1ms duration stand for to 0 degrees position, 1.5ms duration to xc degrees and 2ms to 180 degrees. Though the minimum and maximum elapsing of the pulses can sometimes vary with unlike brands and they can exist 0.5ms for 0 degrees and two.5ms for 180 degrees position.
Read More: Stepper Motors and Arduino – The Ultimate Guide
Pop RC / Hobby Servos for Arduino Projects
There are many different models and manufacturers of RC or hobby. The main consideration when choosing a servo motor is its torque, operating voltage, current depict and size.
Hither are the two nigh popular servo models among makers, the SG90 Micro Servo and the MG996R.
SG90 Micro Servo technical specifications:
Stall Torque | one.2kg·cm @4.8V, 1.6kg·cm @6V, |
Operating Voltage | 3.5 – 6V |
No Load Current | 100mA |
Stall Electric current | 650mA |
Max Speed | 60 degrees in 0.12s |
Weight | 9g |
MG996R Servo technical specifications:
Stall Torque | 11kg.cm @4.8v, 13kg.cm @6V |
Operating Voltage | 4.eight – 7.2V |
No Load Current | 220mA @four.8V, 250mA @6V |
Stall Electric current | 650mA |
Max Speed | sixty degrees in 0.20s |
Weight | 55g |
Arduino Servo Motor Control
Let's put the above said to exam and make a practical example of controlling a hobby servo using Arduino. I volition use the MG996R which is a high-torque servo featuring metallic gearing with stall torque of 10 kg-cm. The loftier torque comes at a price and that's the stall current of the servo which is 2.5A. The running electric current is from 500mA to 900mA and the operating voltage is from 4.8 to 7.2V.
The current ratings betoken that nosotros cannot direct connect this servo to the Arduino, but we must use a separate ability supply for it.
Circuit Diagram
Here's the circuit diagram for this instance.
We merely need to connect the control pin of the servo to any digital pivot of the Arduino lath, connect the Ground and the positive wires to the external 5V power supply, and too connect the Arduino ground to the servo ground.
In instance we use a smaller hobby servo, the S90 Micro Servo, it's possible to power it directly from the 5V Arduino pin.
The S90 Micro Servo has lower electric current consumption, effectually 100-200mA no-load running electric current, but around 500-700mA stall current. On the other hand, the Arduino 5V pin tin output only around 500mA if powered via USB, or upwards to 1A in powered via the barrel connector.
Fifty-fifty though it's possible to run these 9g servo motors straight to Arduino, for more stable work I would suggest to ever utilize an external power supply for them.
You tin can become the components needed for this case from the links below:
- MG996R Servo Motor …………………………. Amazon / Banggood / AliExpress
- or S90 Micro Servo ………..…………………… Amazon / Banggood / AliExpress
- Arduino Board ……………………………………. Amazon / Banggood / AliExpress
- 5V 2A DC Power Supply …………………..….. Amazon / Banggood / AliExpress
Disclosure: These are affiliate links. As an Amazon Acquaintance I earn from qualifying purchases.
Servo Motor Control Arduino Code
Now let'due south accept a look at the Arduino code for decision-making the servo motor. The code is very unproblematic. Nosotros merely need to define the pin to which the servo is connect, define that pin as an output, and in the loop department generate pulses with the specific duration and frequency every bit we explained earlier.
/* Servo Motor Control - 50Hz Pulse Train Generator past Dejan, https://howtomechatronics.com */ #define servoPin 9 void setup () { pinMode(servoPin, OUTPUT); } void loop () { // A pulse each 20ms digitalWrite(servoPin, HIGH); delayMicroseconds(1450); // Duration of the pusle in microseconds digitalWrite(servoPin, LOW); delayMicroseconds(18550); // 20ms - elapsing of the pusle // Pulses duration: 600 - 0deg; 1450 - 90deg; 2300 - 180deg }
Code linguistic communication: Arduino ( arduino )
After some testing I came up with the post-obit values for the duration of the pulses that work with my servo. Pulses with 0.6ms duration corresponded to 0 degrees position, ane.45ms to ninety degrees and 2.3ms to 180 degrees.
I connected a multimeter in series with the servo to check the current describe. The maximum current draw that I noticed was up to 0.63A at stall. Well that'south because this isn't the original TowerPro MG996R servo, but a cheaper replica, which patently has worse performance.
All the same, let'south take a look at a more than convenient way of controlling servos using Arduino. That's using the Arduino servo library.
/* Servo Motor Control using the Arduino Servo Library by Dejan, https://howtomechatronics.com */ #include <Servo.h> Servo myservo; // create servo object to control a servo void setup () { myservo.attach(9,600,2300); // (pin, min, max) } void loop () { myservo.write(0); // tell servo to go to a particular angle delay(1000); myservo.write(90); delay(500); myservo.write(135); delay(500); myservo.write(180); filibuster(1500); }
Lawmaking language: Arduino ( arduino )
Hither we simply need to include the library, define the servo object, and using the attach() function define the pin to which the servo is continued equally well every bit define the minimum and maximum values of the pulses durations. And so using the write() function we simply set the position of the servo from 0 to 180 degrees.
Controlling Multiple Servo Motors with Arduino
The Arduino servo library supports controlling of upwardly to 12 servos at the aforementioned time with most the Arduino boards, and 48 servos using the Arduino Mega board. On top of that, decision-making multiple servo motors with Arduino is as piece of cake every bit controlling merely a unmarried one.
Hither's an case code for decision-making multiple servos:
/* Controlling multiple servo motors with Arduino by Dejan, https://howtomechatronics.com */ #include <Servo.h> Servo servo1; Servo servo2; Servo servo3; Servo servo4; Servo servo5; void setup () { servo1.attach(8); servo2.attach(9); servo3.attach(ten); servo4.attach(xi); servo5.attach(12); } void loop () { // motion all servos to position 0 servo1.write(0); servo2.write(0); servo3.write(0); servo4.write(0); servo5.write(0); delay(2000); // motion all servos to position 90 servo1.write(90); servo2.write(ninety); servo3.write(ninety); servo4.write(ninety); servo5.write(90); delay(2000); // move all servos to position 180 servo1.write(180); servo2.write(180); servo3.write(180); servo4.write(180); servo5.write(180); filibuster(2000); }
Lawmaking language: Arduino ( arduino )
And so, nosotros just take to create objects from the Servo class for each servo motor, and define to which Arduino pin is connected. Of course, we tin can set any servo to motility to any position, at any time.
Equally an example y'all can also check my Arduino Ant Hexapod Robot projection where I used an Arduino MEGA board to control 22 servo motors.
Arduino and PCA9685 PWM/ Servo Driver
There'southward also another style of controlling servos with Arduino, and that'south using the PCA9685 servo driver. This is a 16-Channel 12-bit PWM and servo driver which communicates with Arduino using the I2C double-decker. Information technology has a built in clock and so it can drive 16 servos free running, or independently of Arduino.
What'south even cooler we tin can daisy-concatenation up to 62 of these drivers on a single I2C double-decker. So theoretically we tin can control up to 992 servos using only the two I2C pins from the Arduino board. The 6 address select pins are used for setting different I2C addressed for each additional driver. Nosotros just need to connect the solder pads according to this table.
Hither's the circuit schematic and we can once once again notice that we demand a separate power supply for the servos.
You can get the components needed for this example from the links below:
- MG996R Servo Motor …………………………. Amazon / Banggood / AliExpress
- PCA9685 PWM Servo Driver ………………. Amazon / Banggood / AliExpress
- Arduino Lath ……………………………………. Amazon / Banggood / AliExpress
- 5V 6A DC Power Supply …………………..….. Amazon / Banggood / AliExpress
Disclosure: These are affiliate links. As an Amazon Acquaintance I earn from qualifying purchases.
Now let'southward take a await at the Arduino code. For decision-making this servo driver we will use the PCA9685 library which tin can be downloaded from GitHub.
Arduino and PCA9685 Code
/* Servo Motor Command using Arduino and PCA9685 Driver by Dejan, https://howtomechatronics.com Library: https://github.com/NachtRaveVL/PCA9685-Arduino */ #include <Wire.h> #include "PCA9685.h" PCA9685 driver; // PCA9685 outputs = 12-bit = 4096 steps // 2.5% of 20ms = 0.5ms ; 12.5% of 20ms = 2.5ms // two.v% of 4096 = 102 steps; 12.5% of 4096 = 512 steps PCA9685_ServoEvaluator pwmServo (102, 470) ; // (-90deg, +90deg) // Second Servo // PCA9685_ServoEvaluator pwmServo2(102, 310, 505); // (0deg, 90deg, 180deg) void setup () { Wire.brainstorm(); // Wire must be started first Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz driver.resetDevices(); // Software resets all PCA9685 devices on Wire line commuter.init(B000000); // Accost pins A5-A0 set to B000000 driver.setPWMFrequency(50); // Set frequency to 50Hz } void loop () { driver.setChannelPWM(0, pwmServo.pwmForAngle(-90)); delay(chiliad); driver.setChannelPWM(0, pwmServo.pwmForAngle(0)); delay(g); commuter.setChannelPWM(0, pwmServo.pwmForAngle(90)); delay(k); }
Code language: Arduino ( arduino )
So first we need to include the libraries and define the PCA9685 object. Then using the Servo_Evaluator instance define the pulses elapsing or the PWM output of the driver. Notation that the outputs are 12-bit, or that's a resolution of 4096 steps. So the minimum pulse duration of 0.5ms or 0 degrees position would correspond to 102 steps, and the maximum pulse duration of two.5ms or 180 degrees position to 512 steps. Just as explained earlier these values should exist adjusted co-ordinate your servo motor. I had value from 102 to 470 which corresponded to 0 to 180 degrees position.
In the setup section we demand to define the I2C clock rate, gear up the driver address and set the frequency to 50Hz.
In the loop section, using the setChannelPWM() and pwmForAngle() functions we simply set up the servo to the desired angle.
I continued a second servo to the driver, and as I expected, information technology wasn't positioning the same every bit the outset 1, and that's because the servos that I'm using are cheap copies and they are not and so reliable. Nevertheless, this isn't a big problem because using the Servo_Evaluator instance we can prepare unlike output settings for each servo. We can likewise adjust the 90 degrees position in case it's non precisely in the middle. In that way all servos will work the same and position at the exact angle.
Controlling a lot of servos with Arduino and the PCA9685 drivers
We will take a wait at i more example and that's controlling a lot of servos with multiple chained PCA9685 drivers.
For that purpose nosotros need to connect the drivers to each other and connect the appropriate address select solder pads. Here's the circuit schematic:
Permit'due south take a wait at the Arduino code now.
/* Servo Motor Control using Arduino and PCA9685 Commuter by Dejan, https://howtomechatronics.com Library: https://github.com/NachtRaveVL/PCA9685-Arduino */ #include <Wire.h> #include "PCA9685.h" PCA9685 driver; // PCA9685 outputs = 12-flake = 4096 steps // 2.v% of 20ms = 0.5ms ; 12.5% of 20ms = two.5ms // 2.v% of 4096 = 102 steps; 12.5% of 4096 = 512 steps PCA9685_ServoEvaluator pwmServo (102, 470) ; // (-90deg, +90deg) // Second Servo PCA9685_ServoEvaluator pwmServo2 (102, 310, 505) ; // (0deg, 90deg, 180deg) void setup () { Wire.begin(); // Wire must be started showtime Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz driver.resetDevices(); // Software resets all PCA9685 devices on Wire line driver.init(B000000); // Address pins A5-A0 set to B000000 driver.setPWMFrequency(50); // Set frequency to 50Hz } void loop () { driver.setChannelPWM(0, pwmServo.pwmForAngle(-xc)); delay(yard); driver.setChannelPWM(0, pwmServo.pwmForAngle(0)); delay(thou); commuter.setChannelPWM(0, pwmServo.pwmForAngle(90)); filibuster(1000); }
Code language: Arduino ( arduino )
And then we should create split up PCA9685 object for each driver, ascertain the addresses for each driver also every bit ready the frequency to 50Hz. Now merely using the setChannelPWM() and pwmForAngle() functions we can gear up whatsoever servo at any driver to position any bending we want.
Troubleshooting
Servo motor jitters and resets my Arduino board
This is a common problem with these hobby servo motors, the SG90 Micro Servo and the MG996R. The reason for this is that, equally mentioned earlier, they can draw quite pregnant amount of current when they are at load. This tin can cause the Arduino board to reset, peculiarly if y'all are powering the servo directly from the Arduino 5V pin.
In order to solve this issue yous tin apply a capacitor across the GND and the 5V pivot. It will human activity equally a decouple capacitor which will provide additional current to the system at start upward when the DC motor starts.
Servo motor won't move entire range from 0 to 180 degrees
This is another common trouble with these hobby servos. As nosotros explained earlier, a pulse width of 1ms (0.5ms) corresponds to 0 degrees position, and 2ms (two.5ms) to 180 degrees. Withal, these values can vary from servo to servo and betwixt unlike manufacturers.
In society to solve this problem, we demand to adjust the pulse width nosotros are sending to the servo motor with the Arduino. Luckily, using the Arduino Servo library we can easily arrange the pulse widths values in the attach() function.
The attach() function can have two additional parameters, and that's the minimum and maximum pulse width in microseconds. The default values are 544 microseconds (0.544milliseconds) for minimum (0 degrees) bending, and 2400 microseconds (2.4ms). So past adjusting these values nosotros tin can fine tune the moment range of the servo.
myservo.adhere(nine,600,2300); // (pin, min, max)
Code language: Arduino ( arduino )
Dimensions and 3D Model
I made 3D models of the two near popular servo motors, the SG90 Micro Servo and the MG996R servo motor. You can download load them from the links below.
SG90 Micro Servo
3D Model: Download from Thangs.
Dimensions:
MG996R Servo Motor
MG996R Servo Motor 3D Model: Download from Thangs.
Dimensions:
Conclusion
And then, nosotros have covered almost everything we need to know about using servo motors with Arduino. Of course, in that location are some many manufacturers and models of these type of hobby or RC servo motors, and each of them has its ain unique features that might differ from what we explained above.
The possibilities for creating awesome robotics, automation and RC projects using motors are countless, nonetheless choosing the right model for your application is very of import.
I hope you enjoyed this tutorial and learned something new. Experience complimentary to inquire any question in the comments department beneath, as well as make sure you lot can my Arduino Projects Collection.
Frequently Asked Questions (FAQs)
How do I use a servo motor with Arduino?
Using a servo motor with Arduino is quite easy. The servo motor has just three wires, two of which are GND and 5V for powering, and the third wire is the control line which goes to the Arduino board.
Can Arduino run servo motors?
Nosotros can run servo motors straight from Arduino, but we might take ability problems. If the servo motor draws more than 500mA of current, the Arduino board might lose it'south ability and reset. Information technology'southward better to always apply a separate power source for the servo motors.
How many servo motors can an Arduino control?
Using the Arduino Servo library we can control up to 12 servo motors with most Arduino boards, and upward to 48 servo motors with the Arduino Mega board. Of course, nosotros need to use a dedicated power source for the servo motors.
How To Control A Servo With Arduino,
Source: https://howtomechatronics.com/how-it-works/how-servo-motors-work-how-to-control-servos-using-arduino/
Posted by: buckleypase1998.blogspot.com
0 Response to "How To Control A Servo With Arduino"
Post a Comment