PIC Lesson: Servo Motor

April 12th, 2010  Posted at   PIC Lesson
, arrow   |   57 Commentsarrow

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.

Berikut adalah videonya.

Anda mungkin belum baca post ini

57 komen untuk PIC Lesson: Servo Motor

  1. 1. Komen dari ferus pada 6:00 PM, 9th August 2010

    salam bos boleh berikan saya mcm mana penyambungan antara vabriable resistor dengan servo motor tu ka

  2. 2. Komen dari Shahrul pada 6:04 PM, 9th August 2010

    ferus, variable resistor sambung pada PORTA<0> dan servo motor sambung pada PORTC<0>.

  3. 3. Komen dari kasim pada 12:17 AM, 28th October 2010

    apa kebaikan servo controller??

  4. 4. Komen dari LEONG pada 12:51 PM, 24th November 2010

    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?

  5. 5. Komen dari Shahrul pada 1:07 PM, 24th November 2010

    You can try,
    Give 1ms ON 19ms OFF, repeat 20 cycle.
    Then give 1.5ms ON 18.5ms OFF, repeat 20 cycle.

  6. 6. Komen dari LEONG pada 4:56 PM, 17th January 2011

    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?

  7. 7. Komen dari Shahrul pada 5:21 PM, 17th January 2011

    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.

  8. 8. Komen dari Goh pada 11:46 AM, 13th February 2011

    Hi, can I get the schematic diagram? How the pic is connected? Thanks in advance.

  9. 9. Komen dari Shahrul pada 10:12 AM, 16th February 2011

    I have update the schematic.

  10. 10. Komen dari Goh pada 1:07 PM, 17th February 2011

    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

  11. 11. Komen dari Goh pada 3:51 PM, 17th March 2011

    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

  12. 12. Komen dari Shahrul pada 4:09 PM, 17th March 2011

    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.

  13. 13. Komen dari Goh pada 8:14 PM, 18th March 2011

    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

  14. 14. Komen dari Shahrul pada 10:00 PM, 19th March 2011

    Goh, only Timer0 is use. Timer1 and Timer2 off. ADCON1 set in the read_a2d() function.

  15. 15. Komen dari Goh pada 12:50 AM, 21st March 2011

    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!

  16. 16. Komen dari LEONG pada 12:31 PM, 21st March 2011

    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

  17. 17. Komen dari Shahrul pada 12:54 PM, 21st March 2011

    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.

  18. 18. Komen dari Goh pada 12:13 AM, 30th March 2011

    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!

  19. 19. Komen dari Shahrul pada 2:14 AM, 30th March 2011

    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.

  20. 20. Komen dari Goh pada 12:37 AM, 31st March 2011

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

  21. 21. Komen dari patricia pada 9:10 PM, 4th April 2011

    helo…bleh en. tolong terangkan tentang aturcara ini?

  22. 22. Komen dari Aminath pada 5:50 PM, 14th April 2011

    could you please show a sample code for PIC16F84A to control servo motor.

  23. 23. Komen dari Shahrul pada 11:44 PM, 14th April 2011

    Aminath, I’m not using PIC16F84A. All the C code for PIC16 is same as above.

  24. 24. Komen dari chin pada 10:18 AM, 31st May 2011

    do i need to buy a 555 timer?

  25. 25. Komen dari Shahrul pada 11:21 AM, 31st May 2011

    chin, no need 555 timer. All that can do with PIC programming.

  26. 26. Komen dari chin pada 8:57 PM, 4th July 2011

    can i know how to code so that the motor will rotate 90 then go back to 0?

  27. 27. Komen dari chin pada 9:01 PM, 4th July 2011

    when input is high motor rotate 90
    when input low rotate back to 0..
    how to code?

  28. 28. Komen dari Shahrul pada 9:40 PM, 4th July 2011

    I don’t have time to write you a code. You just learn it.

  29. 29. Komen dari Bizbot Technology pada 2:22 AM, 12th July 2011

    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.

  30. 30. Komen dari z pada 3:39 AM, 1st August 2011

    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

  31. 31. Komen dari Shahrul pada 5:23 PM, 1st August 2011

    Servo motor memang hanya bergerak 180 darjah. Kalau berpusing 360 darjah, itu menggunakan DC motor.

  32. 32. Komen dari Pijoe pada 5:29 PM, 17th October 2011

    cmne plak lau nk bt projek smart home

  33. 33. Komen dari Shahrul pada 5:44 PM, 17th October 2011

    Smart Home mcm mana, byk konsep smart home. Tapi, kena study lah apa yg nak buat, smart home byk kena buat interfacing dan protocol.

  34. 34. Komen dari goldenpeace pada 4:00 PM, 26th October 2011

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

  35. 35. Komen dari Shahrul pada 7:20 PM, 26th October 2011

    ws, goldenpeace. Saya pun x tahu apa yg awak buat, x tahu kenapa error. Rujuk cara guna MPLab.

  36. 36. Komen dari Sodrohu pada 10:30 PM, 10th February 2012

    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?

  37. 37. Komen dari Sodrohu pada 10:37 PM, 10th February 2012

    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.

  38. 38. Komen dari Shahrul pada 11:37 PM, 11th February 2012

    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.

  39. 39. Komen dari mtaufiq23 pada 10:09 PM, 6th March 2012

    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….?

  40. 40. Komen dari Shahrul pada 10:43 AM, 7th March 2012

    ws, x de pun rumus tu. Interrupt Timer0 berlaku apabila TMR0 overflow dari 255 ke 0.

  41. 41. Komen dari mtaufiq23 pada 10:50 AM, 7th March 2012

    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…

  42. 42. Komen dari Shahrul pada 5:44 PM, 7th March 2012

    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

  43. 43. Komen dari mtaufiq23 pada 7:09 PM, 7th March 2012

    ok…TQ saudara…

  44. 44. Komen dari Lin pada 7:44 PM, 23rd March 2012

    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.

  45. 45. Komen dari Shahrul pada 8:02 PM, 23rd March 2012

    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.

  46. 46. Komen dari Lin pada 6:05 AM, 24th March 2012

    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 .

  47. 47. Komen dari faris pada 5:20 PM, 18th June 2012

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

  48. 48. Komen dari Shahrul pada 6:19 PM, 18th June 2012

    faris, copy library kat page header.

  49. 49. Komen dari faris pada 3:42 PM, 19th June 2012

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

  50. 50. Komen dari Shahrul pada 8:21 AM, 23rd June 2012

    Saya dah byk tunjukkan cth, byk lagi kerja lain. X de masa nak tunjuk step by step.

  51. 51. Komen dari hafiz pada 3:13 AM, 6th September 2012

    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

  52. 52. Komen dari MooQe pada 11:14 AM, 6th September 2012

    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

  53. 53. Komen dari Shahrul pada 6:32 PM, 7th September 2012

    w/salam, saya x tahu. Try-and-error je benda mcm tu.

  54. 54. Komen dari adam pada 2:11 PM, 3rd November 2012

    slm, camna nak buat coading servo ramping? untuk slow dan lajukan RC servo.. terima kasih

  55. 55. Komen dari Shahrul pada 2:32 PM, 3rd November 2012

    w/salam, adam. Control duty cycle saja, tapi speed servo ada limit juga.

  56. 56. Komen dari Samuel pada 11:33 PM, 23rd November 2012

    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

  57. 57. Komen dari mohd pada 12:12 AM, 2nd May 2013

    hi

    can you tell me where can i download the code?

Dah baca, tinggalkan komen anda disini

What is 15 + 12 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)