Use servo motors with Arduino
Hi Everyone, this is a tutorial on how to use the servo motors with Arduino Uno you can check it in the video, here I’m gonna post the wiring and codes I used in the video hope you like it.
Servo motors are used in a lot of projects, so everyone learning how to use Arduino should know how to use one. These motors have a high torque, up to 13 kg-cm depending on the type of motor and the operating voltage, they are mostly known for positioning and robots articulations. Also as we can see in codes used, we make them rotate by mentioning the position, they have a known angle some are between 0-180° and others 0-360°, once the reach their limit they should return, not like the continuous servo-motors they can rotate freely but they cannot be positioned like this and I’ll do another tutorial on these types of motors.

Wiring:
Servo motors have three wires: GND/VCC/Signal, here in this case its powered using Arduino’s 5V, you can use 9V or 12V, but read your Servomotor datasheet first to know its supported voltage, just don’t forget Arduino and the Servo external power source should have common ground.
The Signal pin should be wired with one of Arduino’s PWM pins, they are marked with (~), the PWM signal will determine the Servo rotation angle.
Codes:
The servo library is an Arduino built-in, you don’t need to install a new one.
Code 1 : Simple write/rotation of the servo motor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//This code is to use a servo motor with Arduino board it rotate the servo from 0° to 180° //You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech// #include <Servo.h> //Servo libraries Servo myservo; //Declaring Servo you can change the name "myservo" void setup() { Serial.begin(9600); myservo.attach(10); //You declare on which pin your servo is wired (The signal pin) } void loop() { myservo.write(90); //"write" function permits you to rotate the servo to which position you want delay(3000); myservo.write(180); delay(3000); } |
Code2 : Sweep mode (taken from the Arduino Servo library)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* Sweep by BARRAGAN <http://barraganstudio.com> This example code is in the public domain. modified 8 Nov 2013 by Scott Fitzgerald */ #include <Servo.h> Servo myservo; // create servo object to control a servo // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position void setup() { myservo.attach(6); // attaches the servo on pin 9 to the servo object } void loop() { for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } } |
Code 3: Simple code on rotating the servo and keep it in that position, I used it on the video so I decided to write it here too
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//This code is to use a servo motor with Arduino board it rotate the servo from 0° to 90° //and stops there while writing "ON" on the serial monitor //You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech #include <Servo.h> Servo myservo; void setup() { Serial.begin(9600); myservo.attach(7); } void loop() { myservo.attach(7); Serial.println("ON"); myservo.write(50); } |
Code 4: How to use the function “detach”
The “detach” function is a bit forgotten, but it turned to be important in some projects I tested with.
When you send a command to a servo motor it stays on that position until you move it by another command, but sometimes you need the servo to have some “free rotation”, this is where the function is handy, it stops the command signal and the servo can be rotated by hand or external mechanism…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
//This code is to use a servo motor with Arduino board and the use of the function "detach" //You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech// #include <Servo.h> // Servo library Servo myservo; //Declaring the Servo void setup() { Serial.begin(9600); myservo.attach(6); //Declaring on which pin the servo is connected } void loop() { myservo.attach(6); //The first part until "delay" it attach the servo = connect it, then write "ON" Serial.println("ON"); //on the serial monitor and rotate the servo to 90° myservo.write(90); delay(5000); myservo.detach(); // In this part we detach the servo which means you can rotate it freely from 0 to 180° Serial.println("OFF");// While writing "OFF" on the monitor delay(5000); } |
Code 5: Code to read the postion of the Servo motor, I didn’t use it in the video but here is it, it will be useful in the future
The read function is important as well, sometimes you need to know the servo current position before proceeding to other actions, this function returns the servo current angle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
//This code is to use a servo motor with Arduino board and the use of the function "read" //You wire GND VCC SIGNAl to GND 5V D6 from Arduino //SurtrTech// #include <Servo.h> Servo myservo; void setup() { Serial.begin(9600); myservo.attach(6); } void loop() { float a=myservo.read(); // permits to read the position of the servo motor and show it on the serial monitor Serial.println(a); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
One thought on “Use servo motors with Arduino” Leave a comment ›