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”
salam bos boleh berikan saya mcm mana penyambungan antara vabriable resistor dengan servo motor tu ka
ferus, variable resistor sambung pada PORTA<0> dan servo motor sambung pada PORTC<0>.
apa kebaikan servo controller??
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?
You can try,
Give 1ms ON 19ms OFF, repeat 20 cycle.
Then give 1.5ms ON 18.5ms OFF, repeat 20 cycle.
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.
Hi, can I get the schematic diagram? How the pic is connected? Thanks in advance.
I have update the schematic.
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
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.
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
Goh, only Timer0 is use. Timer1 and Timer2 off. ADCON1 set in the read_a2d() function.
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!
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.
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.
Thanks for your reply.
However, from the OPTION register, I thought you are using internal clock which is 4MHz?
helo…bleh en. tolong terangkan tentang aturcara ini?
could you please show a sample code for PIC16F84A to control servo motor.
Aminath, I’m not using PIC16F84A. All the C code for PIC16 is same as above.
do i need to buy a 555 timer?
chin, no need 555 timer. All that can do with PIC programming.
can i know how to code so that the motor will rotate 90 then go back to 0?
when input is high motor rotate 90
when input low rotate back to 0..
how to code?
I don’t have time to write you a code. You just learn it.
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.
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
Servo motor memang hanya bergerak 180 darjah. Kalau berpusing 360 darjah, itu menggunakan DC motor.
cmne plak lau nk bt projek smart home
Smart Home mcm mana, byk konsep smart home. Tapi, kena study lah apa yg nak buat, smart home byk kena buat interfacing dan protocol.
salam, sy ingin bertanya kenapa apabila saya menggunakan complier hi-tech v9.82 , ada error & fail. tolong bantu sy…
ws, goldenpeace. Saya pun x tahu apa yg awak buat, x tahu kenapa error. Rujuk cara guna MPLab.
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.
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.
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….?
ws, x de pun rumus tu. Interrupt Timer0 berlaku apabila TMR0 overflow dari 255 ke 0.
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
ok…TQ saudara…
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 .
di mana saya boleh dapat “LibraryHardware.h”
“LibraryPeripheral.h”?saya carik dalam file hi-tech tetapi tidak ada.
faris, copy library kat page header.
maaf tapi saya tak faham,tidak ada library header file dalam file,HI-tech.boleh tunjuk saya,step by step
Saya dah byk tunjukkan cth, byk lagi kerja lain. X de masa nak tunjuk step by step.
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
w/salam, saya x tahu. Try-and-error je benda mcm tu.
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
slm, camna nak buat coading servo ramping? untuk slow dan lajukan RC servo.. terima kasih
w/salam, adam. Control duty cycle saja, tapi speed servo ada limit juga.
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
hi
can you tell me where can i download the code?