PIC Lesson: Servo Motor


Servo motor adalah alat electromechanical. Ia adalah sebuah motor yang mempunyai feedback loop. Oleh itu ia sebuah motor yang boleh dikawal posisi dan kelajuannya tanpa feedback kepada microcontroller. Servo motor hanya berputar dalam lingkungan sudutnya sahaja, contohnya hanya boleh berputar pada 1800.

Servo motor mempunyai tiga wayar. Kebiasaannya wayar merah adalah supply positif, wayar hitam adalah ground manakala wayar ketiga adalah signal daripada microcontroller. Wayar signal mempunyai beberapa standard warna iaitu warna putih, kuning atau oren.

Servo motor bergerak mengikut pulse signal. Signal ke servo adalah berbentuk PWM dengan frequency lebih kurang 50Hz iaitu masa bagi satu cycle adalah 20mS. Posisi armature servo motor bergantung pada duty cycle PWM tersebut. Dengan mengubah-ubah duty cycle, servo motor akan bergerak atau berputar. Dengan ini juga, kelajuannya boleh di kawal. Contoh signal untuk menggerakkan servo motor berputar mengikut arah jam atau mengikut arah lawan jam seperti diagram di bawah.

Walaubagaimanapun, untuk memprogram servo, tidak boleh menggunakan PWM Module dalam PIC kerana PWM yang diperlukan adalah frekuensi rendah. Oleh itu, PWM boleh dihasilkan menggunakan counter di dalam timer interrupt. Kemudian, hanya perlu menetapkan nilai posisi servo tersebut.

Berikut adalah skematik untuk menggerakkan servo mnggunakan input analog.

Bagi interface menggunakan Arduino boleh rujuk Arduino Lesson: Servo Motor, menggunakan ESP32 boleh rujuk ESP32 Lesson: Servo Motor dan menggunakan Raspberry Pi boleh rujuk Raspberry Pi Lesson: Servo Motor.

Berikut adalah video demo mengawal Servo Motor melalui Variable Resistor.

PIC Source Code


57 responses to “PIC Lesson: Servo Motor”

  1. I writing my program in assembly code. I have make 1mS on and 19mS off pulse. I test is with oscilloscope and get the result. But when i connect to the servo motor, it suppose to rotate to 0 degree, but it rotated to 180 degree. So may i know what is the problem? What can i do?

  2. I discover the problem y the servo motor is moving no according to the program. It is because of the current that i supplied from DC power supply. I have one problem now. It i use DC power supply i can control the current i wan. But when i use battery i cannot control the current i wan. Especially now i need to you 4 servo motor. So how i connect the servo motor to the battery? Any additional circuit to improve the current?

    • Leong, if use 6V from voltage regulator, it’s problem when the servo consumes high current that make the voltage regulator down. Further more, parallel 4 servo it’s need high current. So, must use direct 6V battery or use high current 5V/6V power supply.

  3. After I programmed the pic, just connect the pic to the sevo and variable resistor as shown in the schematic diagram? So the whole system just consists of a servo, pic, variable resistor and battery?

    Thanks

  4. Hi, I tried to compile your code. It showed errors as below:
    undefined identifier “OPTION”
    undefined identifier “ADGO”
    Hence, I changed the OPTION to OPTION_REG. But I don’t know what I can modify for ADG0. Please help. Thx

    • Goh, you click Start >> Search >> All Files.
      First search the ‘pic.h’ files. Then open it. Find the name include file of your pic example ‘pic1687x.h’
      Second search the ‘pic1687x.h’. Then open it. Find the declaration of the ADCON bit. You can see what the name it’s use.

  5. Hi,
    I tried the code, it is not working. The led is blinking and I can load the Hex file into pic. But the servo is not rotating. It is possible that there is some error with the code? As I see all the timers are turn off and prr is set to 225. As I know prr is related to timer2.
    Besides, why ADCON1=0b00000110. All AN is set to digital input as I thought AN0 is analog? Please help. Thx

  6. Shahrul, I understand that the code should be working. But my problem now is the servo rotate clockwise to maximum then stop and fix there whenever I connect it to the circuit regardless of the potential meter.
    May I know what errors that can cause to this problem? It is because I changed option and adgo?
    Thanks!

  7. GOH,
    I dunno u whether you are facing the same problem like me last time.What i facing last time, not matter what signal i give, it will turn to maximum and start to shaking. After several testing, i notice is because of the current i supply from dc power supply to the servo motor is no enough. If you are using dc power supply to test your servo motor,you can turn the current to maximum. Because when the servo motor turn, it need current to turn, if the current is no enough, it will not turn to the position you set and turn to the maximun.I think you no need worry that the current will burn your motor because when it turn, it only use the current it need to turn only. Hope it help you

    • Thanks Leong for help comment. When using servo motor, the power supply need high continuous current. The 9V battery not suitable, better use rechargeable LiPo battery or DC power supply.

  8. Thanks Shahrul and Leong! It’s working fine now.
    However, two more questions. May I know why the max and min value are set to 10 and 50? I tried other values, they are causing some problem? How to derive the values?
    Besides, why
    if(counter==390){
    counter=0;
    SERVO=1;}
    Why 390? How to derive?
    Please help! Thanks a lot!

    • Goh, value 390 for develop 50Hz servo signal. (20MHz/(4*256*390)=50Hz). Value 10 to 50 for develop duty cycle 0.5ms to 2.5ms out of 20ms. This is still can be try-and-error for increase or decrease from 50Hz and get other min and max position.

  9. Thanks for your reply.
    However, from the OPTION register, I thought you are using internal clock which is 4MHz?

  10. Hi,
    Please do the tutorial first rather than asking “how to” before you try it.The tutorial given is complete to control the servo motor using timer interrupt.. this code can be use for 8 bit PIC microcontroller which have interrupt capability.

  11. hai shahrulnizam, nak tanya sket. saya menggunakan PIC16F877A dan juga menggunakan servo motor controller untuk menggerakkan servo motor. persoalannya, bagaimana cara-cara untuk menggerakkan servo motor dalam keadaan tiada had ( unlimited rotation ) kerana servo motor hanya boleh rotate dalam maksimum 180 darjah sahaja jikalau kefahaman saya. minta respon dari saudara. TQ

  12. salam, sy ingin bertanya kenapa apabila saya menggunakan complier hi-tech v9.82 , ada error & fail. tolong bantu sy…

  13. Salam. Beberape soklan..

    1. Yang (20MHz/(4*256*390)=50Hz) tu, 256 mane datang? Nk kira internal clock cycle kene Fosc/4, jadi ko set PS0PS2 tu 111 eh? Tapi dlm code knape ko set OPTION_REG 00001000, PS tu sume 0, ke ko set dlm library adc?

    • ws, Sodrohu.
      1. 256 sebab Timer0 8bit. Fosc/4 sebab set Tocs=0 (OPTION_REG).
      2. Sebenarnya 0.9775*(max-min)*read_a2d(0)/1000 sama juga dengan 1*(max-min)*read_a2d(0)/1023. Nanti saya akan edit code untuk elak kekeliruan.

  14. 2. yang bhagian ni…
    position=(int)(0.9775*(max-min)*read_a2d(0)/1000+min);

    adc 10-bit bkan max 1023 ke? Kalo read_a2d(0)= 1022, min = 10, max =50,
    position = 0.9775* 40.47525 = 39.56=39…
    Knape pkai 1000 bukan 1023, dan 0.9775 tu utk ape?

    Sori double post.

  15. salam..bukan ke rumus utk kire frequency, f=fosc/(4x256x(256-TMR0)xcount)…
    knp hilang bhgn 256-TMR0 dr rumus saudara utk tentukan f=50hz….?

  16. ini comment saudara yg sy angkat dr page dc motor lesson saudara:

    frequency = Crystal_freq/(4x256xPreescalexCounter) = 20000000/(4x256x2x4883)=2Hz (T=0.5s)

    compare with this page:

    (20MHz/(4*256*390)=50Hz)

    there is another 256 here…klau sy tgk kat website http://www.microcontrollerboard.com/pic-timer0-tutorial.html ni rumus utk internal clock adalah seperti diatas dimana jika kita set tmr0=0…how? sy confuse sbb nampak mcm rumus untuk external clock cam tulis dlm website tu…

    • mtaufiq23, contoh tersebut guna preescale 256. Tgk setting kat OPTION_REG.
      Contoh kat Servo Motor, preescale=1. 20MHz/(4x1x(256-0)x390) = 50Hz
      Contoh kat DC Motor, preescale=2. 20MHz/(4x2x(256-0)x4883) = 2Hz

  17. Is this code able to control more then one servo ?
    I modify it into this.
    static void interrupt isr(void){
    if(TMR0IF = 1){
    TMR0IF = 0;
    counter++;
    if (counter == position1) servo1=0;
    if (counter == position2) servo2=0;
    if (counter == 391){
    counter=0;
    servo1=1;
    servo2=1;
    }
    }
    }

    But somehow servo1 will rotate clockwise nonstop till it hit the border.

    • Lin, I don’t know why. Make sure your power supply to the servo can supply enough current. Sometimes servo drain power supply current and program not proper working.

      • Im using a power supply that can supply 15A it should be more than enought ?

        I modify it into this and it worked.
        if (counter1 == position1) servo1=0;
        if (counter2 == position2) servo2=0;
        if (counter3 == position3) servo3=0;
        if (counter4 == position4) servo4=0;
        if (counter5 == position5) servo5=0;
        if (counter6 == position6) servo6=0;

        if (counter1 == 391){
        counter1=0;
        servo1=1;
        }
        if (counter2 == 391){
        counter2=0;
        servo2=1;
        }
        if (counter3 == 391){
        counter3=0;
        servo3=1;
        }
        if (counter4 == 391){
        counter4=0;
        servo4=1;
        }
        if (counter5 == 391){
        counter5=0;
        servo5=1;
        }
        if (counter6 == 391){
        counter6=0;
        servo6=1;
        }

        And i still dun understand what is the difference .

  18. di mana saya boleh dapat “LibraryHardware.h”
    “LibraryPeripheral.h”?saya carik dalam file hi-tech tetapi tidak ada.

  19. maaf tapi saya tak faham,tidak ada library header file dalam file,HI-tech.boleh tunjuk saya,step by step

  20. salam, sy nie bru jew bergiat dalam bidang elektronik…boleh x bg tau sec/60deg ngn kg-m nie mcm ne…
    contoh nye Speed (sec/60deg): 0.16/4.8V, 0.14/6.0V ngn Torque (Kg-m): 3.5/4.8V, 4.5/6.0V (Maximum 6.0V)…

    terima kasih

  21. salam sy nie baru kenal motor…untuk projek sy kene gune servo motor…bleh trangkn x pasal unit speed(sec/deg) ngn torque (kg.m)….contohnye Speed (sec/60deg): 0.16/4.8V, 0.14/6.0V
    ngn Torque (Kg-cm): 3.5/4.8V, 4.5/6.0V (Maximum 6.0V)…minta maaf sy mmg xfaham..minta tlg sgt2

  22. Hello,

    please could you show me where I can download the source code and schematic for this project. I can not find the download link.

    Thank you in advance

    Sam

Leave a Reply to mtaufiq23 Cancel reply

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