Last Updated: 13/08/2021
Servos
Components >> Servos
What is a Servo?
Inside they have a small DC motor and some circuitry that allows the servo to know it's position very accurately. By sending the appropriate signal we are able to tell the servo to move to a position with a 1 degree accuracy.
Although the servos that come with the average Arduino kit or robot car are 5v, there are servos that run on a number of voltages, so if you have salvaged a servo from elsewhere it's worth checking it's specification online and be aware that your Arduino will not have the power to control larger servos.
The servos that usually come in Arduino kits are based on the SG90 and although a Arduino can power a one or two servos, if you want to use multiple servos you will need to use an external 5v power supply, making sure that the ground to the servo and Arduino are connected. Using this technique an Arduino UNO can easily control 12 servos without any additional components.
If you need to control a lot of servos it is worth looking at a PCA9685 PWM driver as an Arduino can control 16 servos through one of these boards and they can be daisy chained together allowing you to control up to 992 servos from 2 pins on your Arduino, more than enough for most projects.
The basic SG90 is a plastic geared servo, there is a slightly more expensive version called the MG90 that has metal gears. For light weight work the SG90 is fine, however I have found when using them to control points on a model railway that the gears can wear so I tend to use the MG90 instead.
Circuit Diagram
The left hand circuit shows the direct connections to an Arduino Uno on pin 12. The right hand diagram is for those using a sensor shield, again it connects to pin 12.
You can change to other digital pins by just changing the pin number in the sketch and pugging the signal (Orange) into the new pin.
In general the servos that come with Arduino kits have the following wire colours:
Brown: Ground (Gnd)
Red: 5v DC (Vcc)
Oranges: Signal, connected to the pin that will control the servo
Arduino Servo Tutorial
How to install the servo library into the Arduino IDE
How to include the servo library in a sketch
Creating a servo object.
Attaching the servo to a pin
Controlling the servo
Example 1: Servov1.ino
Click to Download code:Servov1.ino
New items learnt in this lesson:
Using the servo library
servo.attach()
servo.write()
This code uses lessons learnt in:
Lesson2 "Hello World"
Lesson 6: Basic Numeric variables, boolean, byte, int, unsigned int, long, unsigned long
Lesson 7: delay();
Lesson 8: basic mathematics
/* 13/08/2021
servov1.ino
Simple sketch to show how to control a servo.
servo used is a SG90 or MG90 but other types can be used.
pin 12 Servo pin
*/
//include the servo library
//This allows us to use extra pre written servo functions
#include "Servo.h"
//Global variable
const int myServoPin = 12;//constant integer that will not change
//Create a variable that will store the pin position.
int servoPos;
//Create a servo object. In this example we will call the servo "myServo".
//It is similar to namind a variable.
Servo myServo; // create servo object to control a servo
void setup() {
Serial.begin(9600);//start the Serial monitor.
Serial.println("servov1.ino...");//file name for identification
Serial.println(" ");//send as blank line
//Servo object attached to a pin,
//this pin that the control signals will be sent to
//NOTE we do not need to set a pinMode() because the library will deal with that
myServo.attach(myServoPin);
}
void loop() {
servoPos = 0;//minimum value
Serial.println(servoPos);
myServo.write(servoPos);
delay(1000);
servoPos = 180;
Serial.println(servoPos);
myServo.write(servoPos);
delay(1000);//maximum value
//single degree increments
for (servoPos = 0; servoPos < 180; servoPos++) {
Serial.println(servoPos);
myServo.write(servoPos);
delay(20);
}
//10 degree increments
for (servoPos = 0; servoPos < 180; servoPos += 10) {
Serial.println(servoPos);
myServo.write(servoPos);
delay(40);
}
for (servoPos = 180; servoPos > 0; servoPos -= 10) {
Serial.println(servoPos);
myServo.write(servoPos);
delay(40);
}
}
Example 2: Servov2set90.ino
For those doing the robot car who just want to check their wiring just download the code and upload it to your Arduino,
The servo will move from one limit to the other a couple of times and then settle in a central position. Use this position when attaching the the Ultrasonic sensor bracket.
Click to Download code:Servov2set90.ino
/* 13/08/2021
* servov2set90.ino
*
* Simple sketch to show how to control a servo.
*
* servo used is a SG90 or MG90 but others can be used.
*
* Sketch will cause servo to move between limits and then settle at 90, the midpoint
*
* pin 12 Servo pin
*
*
*/
//include the servo library
//This allows us to use extra pre written servo functions
#include "Servo.h"
//Global variable
const int myServoPin = 12;//constant integer that will not change
//Create a variable that will store the pin position.
int servoPos;
//Create a servo object. In this example we will call the servo "myServo".
//It is similar to namind a variable.
Servo myServo; // create servo object to control a servo
void setup() {
Serial.begin(9600);//start the Serial monitor.
Serial.println("servov2set90.ino...");//file name for identification
Serial.println(" ");//send as blank line
//Servo object attached to a pin,
//this pin that the control signals will be sent to
//NOTE we do not need to set a pinMode() because the library will deal with that
myServo.attach(myServoPin);
servoPos = 0;//minimum value
Serial.println(servoPos);
myServo.write(servoPos);
delay(1000);
servoPos = 180;
Serial.println(servoPos);
myServo.write(servoPos);
delay(1000);
servoPos = 0;//minimum value
Serial.println(servoPos);
myServo.write(servoPos);
delay(1000);
servoPos = 180;
Serial.println(servoPos);
myServo.write(servoPos);
//Move to centre position
delay(1000);
servoPos = 90;
Serial.println(servoPos);
myServo.write(servoPos);
Serial.println("Servo in centre 90 position");
}
void loop() {
//no code required in loop
}
Additional Resource Links
All servo functions are listed on the Arduino servo library page
Comments
This site has been designed to be child friendly, this means that comments cannot be added to videos or directly to the site. To add a comment or ask a question please email the address in this image: and use Servos as a reference.