Table of Contents
Overview:
In this tutorial, learn how to connect a servo motor with Arduino microcontroller and control its movement through programming. Before getting started with Servo motors, It is also important to learn what is Servo motor and how servo motors work. So, lets learn everything.
What are servo motors? how Servo motors work?
Are you thinking to add movement to your project (example: robotics, remote control car turning) like rotating the device in particular direction at certain speed, to achieve this you might think about the motors but general DC motors rotate at high speeds in single direction with out the control of angle movement. So, servo motors are invented to serve this issue.
Servo motors is motors with rotatory actuator with control on its movement angle and precession which works with closed loop feedback control system. A servo motor has a simple DC motor whose shaft is connected to gears to control the speed of rotation and to add the precision.
It also has a sensor generally a potentiometer which feedbacks the position of the shaft to the control board, when a signal is given to the control board for a rotation of +45°, the control board turns on DC motor till the sensor feedbacks the correct +45° position.
There are several models of servo motors. Now lets talk about a specific servo motor which we are using in this tutorial Micro Servo 9g SG90 from Tower Pro. It allows us to sweep in 180 degrees which is between -90° to +90° angle.
Servo motors has a resolution near to 1 degree, which means it is possible to move the shaft in 1 degree angle. this is the maximum resolution which we are going to achieve with the PWM signal of Arduino.
Also read: Arduino Tachometer to Measure Accurate RPM
These motors work with a PWM signal(pulse width modulation), with a work pulse between 1ms and 2ms and with a period of 20ms (50 Hz). What does it mean? This data indicates the maximum speed at which we can move the servomotor with Arduino. We can only change position every 20ms. Which depend on the type and brand of our servo.
Pulse Width modulation in servo motors:
A series of pulses are send to the control pin, generally servo motors are capable of receiving pulses for every 20ms, so we can change the position of the motor every 20ms so the signal should be sent with 50Hz frequency.
The duration of the pulse determines the position of the servo motor. it ranges from 1ms to 2ms, but in some motors it is near 2.5ms. If the pulse is high of 1ms duration the servo angle will be 0°, if duration is 1.5ms the angle will be 90°, and if the duration is 2ms the angle positions is 180°.
Servo motor pinout:
As you can see from the above servo motor pinout image the brown wire is for Ground, Red wire is for Vcc 5v , and finally yellow or orange wire is for data or control signal.
Interfacing Servo motor with Arduino
Before connecting servo motor with Arduino, lets know what are the required components
Required components:
Product Name | Quantity | ||
---|---|---|---|
Arduino Uno Microcontroller | 1 | https://amzn.to/3H4cKxZ | https://amzn.to/3638aTS |
SG90 9G Micro Servo Motor | 1 | https://amzn.to/3hA5jVi | https://amzn.to/368WjUE |
Few Connecting Wires | https://amzn.to/3H2BV4e | https://amzn.to/3J0WVu2 |
Circuit diagram:
Connecting Servo motor with Arduino is very easy just connect the power wires red and brown of servo motor with 5V and GND pins of Arduino respectively. Connect the yellow wire which is control wire to pwm pin 9 of Arduino. That’s it for connection, now we can jump into the coding section.
Program code:
Now connect Arduino with PC where Arduino IDE is installed and install the Servo library through library manager or download the latest version through the below link and extract it into the Arduino libraries folder.
Arduino Servo Library Download: Link
Now to test it we are using 2 example codes one for moving from 0 to 90 and to 180 degrees and another one for sweeping from 0 to 180 and 180 to 0 degrees.
Program code to move servo from 0 to 90 and to 180 with 1 second interval.
Copy and paste the below code into Arduino IDE work space and choose the Board as Arduino UNO and select the correct port and then hit upload.
/* Arduino servo motor tutorial https://www.circuitschools.com */ // Including library to be able to control the servo #include <Servo.h> // declaring the variable to control the servo Servo Servo Motor; void setup() { // Initializing serial monitor to display the result Serial.begin(9600); // initializing servo so that it starts working with pin 9 servoMotor.attach(9); } void loop() { // moving to the 0º position servoMotor.write(0); // wait 1 second delay(1000); // moving to the 90º position servoMotor.write(90); // wait 1 second delay(1000); // moving to the 180º position servoMotor.write(180); // wait 1 second delay(1000); }
After uploading the code you can see the servo motor shaft moves from 0 to 90 and 90 to 180 with one second interval.
Program code to sweep servo from 0 to 180 and 180 to 0.
In this example we want the servo to sweep from 0 to 180º and then in the opposite direction degree by degree. So, here we are using the same methods but added two for loops , one for each direction.
/* Arduino Servo sweep example - https://www.circuitschools.com */ // Including library to be able to control the servo #include <Servo.h> // Declaring the variable to control the servo Servo Servo Motor; void setup() { // Initializing serial monitor to display the result Serial.begin(9600); // Initializing servo so that it starts working with pin 9 servoMotor.attach(9); // Initialize the servomotor at angle 0 servoMotor.write(0); } void loop() { // Adding two for loops, one to move in a positive direction and one in a negative direction // For the positive direction for (int i = 0; i <= 180; i++) { // Moving to the corresponding angle servoMotor.write(i); // We pause for 25ms delay(25); } // For the negative direction for (int i = 179; i > 0; i--) { // Moving to the corresponding angle servoMotor.write(i); // We pause for 25ms delay(25); } }
After uploading the code the servo motor sweeps.
Project Ideas:
- Can build a SunLight Tracker system for solar panels by rotating the panels towards sun to get more power efficiency.
- Can be using in remote cars for front wheels turning.
- Can build a robotic arm using servo motors and Arduino and control them with joystick.
And many more please share your creative projects if you had experienced with Servo motors in the below comment section. And if you like this tutorial please share it with your friends. Follow our Facebook channel “Circuit Schools” for more project updates.