Sunday, September 6, 2015

ARTICLE #4 - Using Servo Motors with Arduino

Introduction :


The aim of this project is to learn about servo motors and how to control them using Arduino microcontroller. Servo motors are a type of actuators that are very used in electromechanics, robotics and mechatronic applications because they are responsible for the moving part of these applications such as rotating a wheel, moving a mechanical arm etc. In our case we will see first how to configure and program a servo motor to rotate forwards and backwards, then we will add to this system a potentiometer that will allow us to control the servo motor. But now I will answer some basic questions that will help us to  better understand this device.  

  • How a servo motor works ?
A servo motor uses pulse width modulation (pwm) to convert the signal coming to its output pin into a rotating movement. We will use the Arduino Servo library’s basic functions to apply this concept

  • What is the difference between servo and DC motor ?
Unlike a DC motor that rotates continuously, a servo motor reacts to preset angle value between 0 and 180 degrees to move to it. That’s what makes it useful for robotics and mechatronic applications.

  •  What are the main applications of a servo motor ?
Because of its special concept of feedback loop that allows it to measure the position of its output shaft, a servo motor becomes widely used in different applications requiring precise position control such as using it to control a robotic arm to allow it to move to a precise angle, it used to control conveyor belts in production factories, we can also find servo motors in solar panels for solar tracking systems, printers, camera’s auto-focus option, antennas positioning and so many application varieties.


Circuit wiring :


    Project parts :


The parts needed for this project are :

  • 1 Arduino Uno
  • 1 breadboard
  • 1 Servo motor
  • Multiple wire connectors to connect all the parts together







    Servo pin configuration:

Servo motors have three wires: power, ground and signal. You can generally identify every one of them by their colors.

The power wire should be connected to the 5V pin of your Arduino, its color is red.
The ground wire should be connected to the GND pin on your Arduino, its color may be black or brown.
The signal wire is typically white, yellow or orange and should be connected to a PWM pin on your Arduino. You can identify a PWM pin by finding a ‘~’ sign next to it.



    Wiring 1 :


The circuit wiring of the project is very simple. You connect the servo pins to the breadboard and Arduino as you see in the image below. Notice that you have to connect the signal wire to a pwm pin in Arduino. PWM pins are 3, 5, 6, 9, 10, 11.  





    Code :

After compiling and loading the sketch below into the Arduino board. The servo motor shaft starts to rotate clockwise from 0° to 180° then counter clockwise from 180° to 0° in a half circle form. 

//includes all the functions used for the servo motor
#include <Servo.h>

Servo servoMotor;
int positionServo = 0;
int potPin = 3;
  

void setup() {

     servoMotor.attach(10);
     servoMotor.write(0);

}

void loop() {

    //this loop turns the servo shaft clockwise
    for(positionServo = 0; positionServo < 180; positionServo += 10)     {

         servoMotor.write(positionServo);
         delay(100);
      
    }

   //this loop turns the servo shaft counter-clockwise
   for(positionServo = 180; positionServo > 0; positionServo -= 10)    {

         servoMotor.write(positionServo);
         delay(100);

    }

}

  
     Wiring 2 :


This new circuit is similar to the one above, you have just to add a potentiometer. The servo is still attached as shown in the previous example. The potentiometer has also 3 pins, connect the top pin to 5V, the bottom pin to GND, the middle pin has to be attached to one of the analog inputs of the microcontroller as shown below.





     Code :

The code below controls the position of the servo motor with the potentiometer. as seen in the previous image, the user can determine the position of the servo by turning the potentiometer that will send the corresponding variable resistance to the to the analog input pin A3, wich the Arduino code interprets and converts it to a PWM pulse that drives the servo into the determined position angle.


#include <Servo.h>

Servo servoMotor;
int positionServo = 0;
int potPin = 3
  

void setup() {

     servoMotor.attach(10);
     servoMotor.write(0);

}

void loop() {
  
    //reads the potentiometer value
    float pot_val = analogRead(potPin);

         //the map function re-maps a value number from one range to another
    //the pot_val variable gives a number from 0 to 1023
    //this number range converts to the 0 to 180 rotation degree range of the servo
         positionServo = map(pot_val, 0, 1023, 0, 180);

    //limits the potentiometer value to be between 0 and 180 
    constrain(positionServo, 0, 180);
  
    //sends the positionServo value to the servo motor
    servoMoteur.write(positionServo);

    delay(200);

}

   


No comments:

Post a Comment