Contents
Servo motor control using Arduino
Introduction
What is Servo Motor
Servo motor is a type of motor that can move or rotate its shaft in a specific angle with high precision control of angular or linear position, velocity, and acceleration. This is actually a DC Motor whose speed is slowly lowered by the gears. Servo motor is Also called Servos. The shaft of servos commonly does not rotate freely around similar to a DC motor, Servos are controlled by sending an electrical pulse of variable width or pulse width modulation (PWM), through the control wire. Their rotation is limited between the fixed angles. The motor is attached with gears for controlling the wheel. As the motor rotates, the potentiometer’s resistance changes, and then the control circuit regulates how much movement there is and in which direction also with precisely. servo motors do not rotate constantly. Their rotation is limited in between the fixed angles.
A normal high speed tiny electric motor does not have much torque, but it can spin very fast. But Servos provide more torque than normal high rpm DC motor.
A servo motor consists of three wires– a black wire connected to the ground, a white/yellow wire connected to the control unit and a red wire connected to the power supply.
Applications of servo motor
Servomotor used in camera zooms, lift doors, or tools we may have at home. radio-controlled airplanes to position control. Servo motors are also used in industrial applications, robotics, in-line manufacturing, pharmaceutics, and food services. Hobby servos come in a variety of shapes and sizes for different applications. You may want a large, powerful one for moving the arm of a big robot, or a tiny one to make a robot’s eyebrows go up and down.
Here I am discussing some circuit connections of servo motor with Arduino UNO.
Auto sweep (without Potentiometer)
1st connection is for rotating in a fixed targeted angle. the shaft of servo goes to that angle and then returns. If the required angle set in the program is 180° and the start angle is 0º then start rotation from 0º to 180º and then return automatically to oº and repeat this process. If we set the rotation angle is 90º to 160º then rotation starts from the 90º and goes up to 160º and returns to 90º and repeats this process continuously. The speed of the servo can be controlled by changing the value of the time delay function. If you use one micro servo motor then you don’t need to connect an external power source, you can use only USB for power.
Code- Programm code of servo rotation without potentiometer is available in Arduino IDE in File – Examples – Servo – Sweep
Servo motor control using a potentiometer (Knob) –
We can use a potentiometer of any value between 10k to 100k. One side pin of the potentiometer is connected with the ground, another side pin is connected with 5v supply and the middle pin is connected with the analog input pin of Arduino (A0). analog input gets from the pot and according to this input, the movement of servo changes. When moving the pot shaft or knob then the servo shaft is also rotating oº to 180º according to pot movement. Code- Programm code of servo rotation using potentiometer is available in Arduino IDE in File – Examples – Servo – Knob
Demo Video
In this circuit, the Arduino saves the position of the servo in his memory for the next rotation. Connect the one terminal of push-button with the ground directly and the other terminal is with +5v through a 1k or 10k resistor and Connect a wire between the digital pin of Arduino to the resistor as given image. When pushing the button the servo starts to move up to 180 and returns from 180 to 0. this process will continue till pressed the button of the switch, After the release of the switch, the servo stops moving and stay in that position has released the switch. The previous position is saved in memory and when pushed next time then it starts to rotate from that previous position. Speed can be controlled by changing the value of time delay in code.
// SERVO_MEMORY
// Controlling a servo position using a switch, will
// turn moveDegrees every time the switch is pushed
// and change the direction once it reaches the end.
// The position is recorded in the non-volatile memory so the
// position of the servo is kept if switched off an on.
#include <Servo.h>
#include <EEPROM.h>
Servo myservo; // create servo object to control a servo
int switchPin = 4; // pin used to connect a pulled up button
int val; // variable to store the servo position
int moveDegrees=1; // degrees to turn for every button push
int memoryPos=5; // memory slot to store the position
void setup()
{
val=EEPROM.read(memoryPos); //read the stored position
if (val < 1 || val > 180){ // if it is not a valid position
val=1; // assign a default value
}
//Serial.begin(9600);
pinMode(switchPin, INPUT);
myservo.attach(6); // pin 6 controls the servo
myservo.write(val); // moves to the stored position
}
void loop()
{
//Serial.println(digitalRead(switchPin));
while(digitalRead(switchPin)){ // wait for button pushed
delay(1);
}
val=val+moveDegrees; // increase the position
//Serial.println(val);
myservo.write(val); // sets the servo position
EEPROM.write(memoryPos, val); //stores the current position
delay(5); // waits for servo to get there
if (val == 1 || val == 180) { // if the servo is at the end
moveDegrees = -moveDegrees ; // changes the movement direction
}
}
Video
Servo control using Single push button
Posted by Electronics Circuit Diagram on Tuesday, 7 April 2020
servo control using 2 push Button-
In this circuit 2 push buttons are used to control the direction of the movement of the servo. One button is used for moving in clockwise and the nd push button is used for movement in the anticlockwise direction. When pushed the 1st push button the servo starts to move in clockwise and when pushed the 2nd pushbutton then the servo starts in a reverse direction.
1 1k resistor is connected to one pin of both pushbuttons directly with the ground, these resistors work as a pull-up resistor to remain in OFF state by default. wires connected to the same pin with the digital pins of Arduino. The value of resistors can be between 1k to 10k.
#include<Servo.h>
int pos = 0;
Servo servo;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
servo.attach(9);
}
void loop() {
while (digitalRead(2) == HIGH && pos < 180) {
pos++;
servo.write(pos);
delay(15);
}
while (digitalRead(3) == HIGH && pos > 0) {
pos--;
servo.write(pos);
delay(15);
}
}
Video
servo motor control using 2 Push button switch
Posted by Electronics Circuit Diagram on Wednesday, 8 April 2020
Save and play Servo motors
This saves and play circuit requires an external 5v or 6v DC power supply (1A or 2A). A total of 3 servos are connected in the given circuit. 3 potentiometers are connected, one for each servo to adjust the required position for saving. first, adjust the position of one or all servos and press once the switch 1. The first position is now saved, accordingly, 10 positions can be saved using this coding, after one position press again switch 1 for save. When all 10 positions are saved, then once press switch 2 which is the play button. now servos start to move according to the saved position. analog signals from potentiometers go to analog input pins of Arduino.
During saving the positions open the serial monitor for show and confirm to position save.
//save and play 3 servo motor arduino uno
#include <Servo.h> //add servo library. this library is standard library
//define the servos
Servo servo1;
Servo servo2;
Servo servo3;
//define the buttons
const int button1 = 12;
const int button2 = 13;
//define variable for values of the button
int button1Pressed = 0;
boolean button2Pressed = false;
//define potentiometers
const int pot1 = A0;
const int pot2 = A1;
const int pot3 = A2;
//define variable for values of the potentiometers
int pot1Val;
int pot2Val;
int pot3Val;
//define variable for angles of the potentiometer
int pot1Angle;
int pot2Angle;
int pot3Angle;
//define variable for saved position of the servos
int servo1PosSave[]={1,1,1,1,1,1,1,1,1,1};
int servo2PosSave[]={1,1,1,1,1,1,1,1,1,1};
int servo3PosSave[]={1,1,1,1,1,1,1,1,1,1};
void setup() {
//define attached pins of the servos
servo1.attach(3);
servo2.attach(5);
servo3.attach(6);
//define buttons as input units
pinMode(button1, INPUT);
pinMode(button2, INPUT);
//initialize serial communication for saved position
Serial.begin(9600);
}
void loop() {
//read the potentiometer values and define the servo angle to
//the potentiometer value with the map function
pot1Val = analogRead(pot1);
pot1Angle = map (pot1Val, 5, 1023, 0, 170);
pot2Val = analogRead(pot2);
pot2Angle = map (pot2Val, 5, 1023, 0, 170);
pot3Val = analogRead(pot3);
pot3Angle = map (pot3Val, 5, 1023, 0, 170);
//servos move to mapped angles
servo1.write(pot1Angle);
servo2.write(pot2Angle);
servo3.write(pot3Angle);
//if button1 is pressed (HIGH), save the potentiometers position
//as long as button1 is pressed
if(digitalRead(button1) == HIGH){
button1Pressed++;
switch(button1Pressed){
case 1:
servo1PosSave[0] = pot1Angle;
servo2PosSave[0] = pot2Angle;
servo3PosSave[0] = pot3Angle;
Serial.println("Position #1 Saved");
break;
case 2:
servo1PosSave[1] = pot1Angle;
servo2PosSave[1] = pot2Angle;
servo3PosSave[1] = pot3Angle;
Serial.println("Position #2 Saved");
break;
case 3:
servo1PosSave[2] = pot1Angle;
servo2PosSave[2] = pot2Angle;
servo3PosSave[2] = pot3Angle;
Serial.println("Position #3 Saved");
break;
case 4:
servo1PosSave[3] = pot1Angle;
servo2PosSave[3] = pot2Angle;
servo3PosSave[3] = pot3Angle;
Serial.println("Position #4 Saved");
break;
case 5:
servo1PosSave[4] = pot1Angle;
servo2PosSave[4] = pot2Angle;
servo3PosSave[4] = pot3Angle;
Serial.println("Position #5 Saved");
break;
case 6:
servo1PosSave[5] = pot1Angle;
servo2PosSave[5] = pot2Angle;
servo3PosSave[5] = pot3Angle;
Serial.println("Position #6 Saved");
break;
case 7:
servo1PosSave[6] = pot1Angle;
servo2PosSave[6] = pot2Angle;
servo3PosSave[6] = pot3Angle;
Serial.println("Position #7 Saved");
break;
case 8:
servo1PosSave[7] = pot1Angle;
servo2PosSave[7] = pot2Angle;
servo3PosSave[7] = pot3Angle;
Serial.println("Position #8 Saved");
break;
case 9:
servo1PosSave[8] = pot1Angle;
servo2PosSave[8] = pot2Angle;
servo3PosSave[8] = pot3Angle;
Serial.println("Position #9 Saved");
break;
case 10:
servo1PosSave[9] = pot1Angle;
servo2PosSave[9] = pot2Angle;
servo3PosSave[9] = pot3Angle;
Serial.println("Position #10 Saved");
}
}
//if button2 pressed (HIGH), the servos move saved position
if(digitalRead(button2) == HIGH){
button2Pressed = true;
}
if(button2Pressed){
for(int i=0; i<10; i++){
servo1.write(servo1PosSave[i]);
servo2.write(servo2PosSave[i]);
servo3.write(servo3PosSave[i]);
delay(600);
}
}
delay(300);
}
Video
save and play servo motor control
save and play servo motor using Arduino Uno
Posted by Electronics Circuit Diagram on Tuesday, 7 April 2020
You should never power a servo from the Arduino’s 5V pin. Even those tiny servos can draw too much current . Always use a different power supply, and connect all the grounds together.
Great tutorial! I recently purchased an Arduino board and was looking for a straightforward guide to controlling servo motors with a potentiometer and push button. This blog post exceeded my expectations, providing clear instructions and useful tips throughout. I’m excited to try it out for myself and see the results. Thanks for sharing your knowledge!
Just built this project and it’s working beautifully! The explanation was so clear and the code was easy to follow. It’s nice to see a practical example of how to use servos and potentiometers together. Thanks for sharing!