top of page

Servo Motor SG90 Control angle using a Potentiometer

By Bernice Zuiya

Introduction

                A servo motor is basically a DC Motor which works with PWM (pulse width Modulation) to set its angle, these motor are used when an accurate angle of rotation is needed, in this project we'll show how to control a servo motor (SG90) using a potentiometer connected to one analog input of micro-controller, and the signal wire of the servo motor is taken also from one digital pin of the micro controller as an algorithm written in embedded C (mikroC compiler) used to generate a wave compared to a PWM. read more about servos here

SG90 Servo Motor

PWM Wave form

Working

              

              What I going to show from here is that in PWM (pulse width modulation) is apply to the line signal of the servo motor to get the corresponding angle, and actually there lots ICs which are use to generate PWM wave where the width of pulse varies, for example the 555 IC can also be used to generate PWM waves. or the micro-controller of some families are doted of the CCP Module (capture compare and PWM), they can easily generate the PWM using some line of codes this maybe achieved easily. therefore another approximate way of generating a PWM wave is by first knowing the duty cycle of the servo motor which you are going to use then try to generate an embedded C code to produce a pulse width modulation wave form, and the question maybe how to control the width of pulses ! then the answer for that question is : the analog input to PORTA (RA0) will be sample and give DC values corresponding to the analog input

 

so finally when everything is connected you just need to try to rotate the axis of  the potentiometer the image of a potentiometer is shown bellow it maybe of any value between 5k to 10k. 

 

This project is basically a great one because if you understand how this works then you can implement it to your robotic hobbyist projects at home or anywhere, and this is the most common way used by engineers and designers of robotics arm. 

Potentiometer

Circuit Diagram

         The circuit diagram shown below is the very basic diagram required driving a servo motor using the 16F877A micro-controller where the angle of rotation is controlled by a,potentiometer  you need to respect the connections to get best result.

  • a resistor of value 4k7 ohm is connected in series with +5 volt and the reset pin (1st pin)

  • Q1 is the crystal oscillator in this case its frequency oscillation is 20 MHz.

  • supply: the circuit is connected with a voltage maximum equal 5 volt

  • C1 & C2 are of 22pF.

Code and Comment

Download the full project here

          int adc;
          unsigned int i;
          void main(void) 
               {
                    TRISD = 0X00;
                    PORTD = 0X00;
                            for(;;){
                                        adc = ADC_Read(0);
                                        delay_ms(30);
                                        PORTD.B1=1;     
                                delay_us(750);   
                 for(i= 0;i < adc ; i++){    
                 delay_us(2);     
                 }
             PORTD.B1 = 0;        
        Delay_us(20000);  
     }
}

bottom of page