web analytics
Arduino

Servo Motor with Arduino

In this lesson we will connect the servo motor with Arduino controller, we will make two projects, the first control of the angle of the servo motor automatically by reference angle called by Sweep and the second project control of the servo motor using potentiometer but please before reading this lesson read SERVO MOTOR lesson. in order in these projects, we will need any Arduino board and any servo model support 5 volts DC and the latest version of Arduino Software (IDE). if you use older versions you need to download a Servo library

Servo Motor Sweep

We will make a simple code to control the server angles so that it moves from angle 0 to 180 every step equal to 2 degrees Then returns from 180 to 0, The principle of the servo work on the angels so you must adjust the angels. Servo motors have three pins: power 5V, ground GND, and signal. The power wire is color usually red and connected to the 5V pin on the Arduino. The ground wire is color usually black or brown connected to a ground pin on the board. The signal pin is color usually yellow, orange some times whit we will connect signal to pin 5 or any PWM on the Arduino. to build sweep servo motor we need The following component’s:

Component Number
Arduino UNO R3 1
Servo motor any type support 5 V DC 1
Mini Breadboard (optional) 1
wires 3

Schematic Diagram

In this project, we will connect the servo motor to the digital output that support PWM D5, Connections of servo motors with Arduino UNO R3 it will be as follows :


Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

#include <Servo.h> // select servo library

Servo sweeepservo;  // create servo motor parameter 

int first_postion = 0;    // variable store the for servo motor position

void setup() {
  sweeepservo.attach(5); 
}

void loop() {
  for (first_postion = 0; first_postion <= 180; first_postion += 2) { 

    sweeepservo.write(first_postion); 
 
    delay(25);    // waits 25ms for the servo to reach next position
  }
  for (first_postion = 180; first_postion >= 0; first_postion -= 2) { 
    sweeepservo.write(first_postion);  
    delay(25); 
  }
}

Control Servo Motor using Potentiometer

In the second project we will control the servo motor using Arduino and potentiometer, we need to add the potentiometer to the components in the table above. But what is potentiometer?

Potentiometer it is variable resistance but its value can be changed manually using knob since the increase of resistance will reduce the current, this piece is used for example in the control of the volume in radio, in our project we use to control the degree of servo motor.

 

There are many types of them, some of which moves in a linear and some moves in a rotating, the following picture shows some types:

In our project, you can choose any type but it should be worth 10k ohm, However, we recommend that you use the circular type because it is more flexible and gives greater control accuracy, such as this type:

Circular Potentiometer
Circular Potentiometer

Schematic Diagram

In this project, we will connect the servo motor to the digital output that supports PWM (D10), And connect the potentiometer to the analog inputs (A1). Connections of servo motor and potentiometer with Arduino UNO R3 it will be as follows :


Code

Make sure your connections are correct then connect Arduino with PC using USB then upload this code to Arduino Uno board or any board you own:

#include <Servo.h> // add servo library

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); 
delay(15); 
}

Now change the values of potentiometers (clockwise or counter clockwise) and watch the servo motors move, and you can modify the code to add more servo motors.

Note: If you are using a 5V servo motor, you do not need an external power unit, because the USB computer is capable of providing you with sufficient power, so don’t worry about power.

Also you can build (Basic Robotic Arm) using servo motor.

Leave a Reply

Your email address will not be published. Required fields are marked *

Adblock Detected

Please consider supporting us by disabling your ad blocker