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.
Thanks for your reply.
However, from the OPTION register, I thought you are using internal clock which is 4MHz?
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 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!
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.
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
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, only Timer0 is use. Timer1 and Timer2 off. ADCON1 set in the read_a2d() function.
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, 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 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