Servo Motor
Servo motor is a type of motor that rotate to specific degree assigned by a controller. Some motor rotates within 180 degree, while others rotate up to 360 degree.
A sample of servo motor from Ali Express |
Supply voltage is typically a +5V DC supply from microcontroller board. Its control signal is a TTL type logic level with a frequency of about 50Hz. Duty time of control signal is between 1.5ms to 2ms that rotate its angle between 0 and 180 degrees.
Microcontroller Interfacing and Programming
Controller needs to generate Pulse Width Modulation (PWM) signal with a frequency around 50Hz, with a variation of high time between 1.5ms to 2ms to rotate this motor. Without using PWM module of controller, programmer can create a software PWM routine instead. However this method has a timing latency.
Simulation of this program |
A POT connects to AN0 of PIC16F887 that's channel 0 of ADC module. Controller reads and converts this ADC value into timing value of output PWM signal generates a pin RD0. This pin controls the angle of rotation of servo motor.
MikroC Program:
#define Servo PORTD.RD0 #define Max_Degree 2000 unsigned ADC_Value; unsigned Servo_Value; void main() { int i,j; PORTD=0x00; PORTA=0x00; TRISA=0x01; ANSEL=0x01; TRISD=0x00; OSCCON=0x70; ADC_Init(); while(1) { ADC_Value=ADC_Get_Sample(0); Servo_Value=ADC_Value/4; Servo=1; for(i=0;i<Servo_Value;i++) { delay_us(1); } Servo=0; for(j=0;j<20000-Servo_Value;j++) { delay_us(1); } delay_ms(5); } }
Click here to download this example from GitHub.
No comments:
Post a Comment