ARM LPC2138/48 Mini Development Board – Interfacing Servo motor
Servos have simple electrical interface – they have 3 wires, one for power, one for ground and the other for the pulse train. Once the servo is powered, the signal wire is ready to receive signal in the form of pulse width modulation (PWM) from an external source.
The signal expects to be updated every 20 ms with a pulse between 0.5ms and 2.5ms. With a 1.5 ms pulse, the servo motor will be at the natural 90° position. With a 0.5 ms pulse, the servo will be at the 0° position, and with a 2.5 ms pulse, the servo will be at 180°. You can obtain the full range of motion by updating the servo with a value in between.
LPC2138/48 mini development board has a dedicated connector K13 to interface Servo motor. Regulated 5V is available on power supply pin and the signal pin is connected to the port pin P1.24.
SchematicNote: Use external source for powering since USB doesn’t provide required current .
Sample CodeSample code to check servo motor is given below. Upon receiving data servo rotates on predefined angle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
/************************************************************************************ * HEADER FILES * ************************************************************************************* * Requied Time = Value IN MR Register(x) / 30Mhz * *************************************************************************************/ #include<lpc21xx.h> #define SERVO 24 /* Servo connected to P1.24 */ /************************************************************************************ VARIABLE DECLARATION *************************************************************************************/ unsigned char servo_value; volatile unsigned char Timer_count=0; unsigned char servo_angle=0; unsigned char k=0; volatile unsigned char recdata; /************************************************************************************ * Function : PORT_Initial * * * * Description : Function for PORT Initialization * * * * Parameters : None * *************************************************************************************/ void PORT_Initial(void) { IO1DIR = 0x01000000; /* Servo pins as o/p */ IO0DIR = 0x00000001; /* Set direction for UART0 */ PINSEL0 = 0x00000005; /* Set pinsel for UART0 */ } /************************************************************************************ * Function : Convert_Angle * * * * Description : Function to calculate timer count for angle * * * * Parameters : k1 - angle * *************************************************************************************/ int Convert_Angle(unsigned char k1) { char timer_value; int temp; /* Converting angle to time count */ temp = k1*5; /* count for 0Degree = 25 */ timer_value = temp/9; /* Count/Degree = 5/9 */ timer_value = timer_value+25; /* eqn, count = (angle * (5/9)) + 25 */ return timer_value; } /************************************************************************************ * Function : Timer0_Init * * * * Description : Function for timer initialization * * * * Parameters : None * *************************************************************************************/ void Timer0_Init(void) { T0TCR = 0X02; /* Timer counter reset */ T0MR0 = 600; /* Value for 20us */ T0MCR = 0X0003; /* Interrupt & counter reset on match */ T0TCR = 0X01; /* Counter enabled */ } /*********************************************************************************** * Function : Uart_Init * * Description : UART_0 initialization function * * Parameter : None *************************************************************************************/ void Uart_Init(void) { U0LCR = 0X83; /* line control registor */ U0DLL = 0XC3; /* baud rate registor */ U0DLM = 0X00; /* baud rate registor */ U0LCR = 0X03; /* line control registor */ } /************************************************************************************ MAIN FUNCTION *************************************************************************************/ int main() { PORT_Initial(); /* Initialize port */ Uart_Init(); Timer0_Init(); /* Initialize timer */ while(1) { if(T0IR==1) /* Checking Timer */ { T0IR=0X01; /* Clear MR0 interrupt */ Timer_count++; /* Increment count */ if(Timer_count==125) T0MR0=525000; /* Load value to T0MR0 */ if(Timer_count==126) { T0MR0 = 600; /* Load value for 20us to TM0R0 */ Timer_count = 0; IO1SET |= 0X01000000; /* Make servo pin high */ } if( Timer_count == servo_value ) IO1CLR |=(1<< SERVO); /* Clear pin on value */ } if((U0LSR&0X01)==0X01) /* Checking reception */ { recdata=U0RBR; /* Store received data to a variable */ if(recdata=='a') /* When Received data 'a' */ servo_value = 120; /* Servo rotate for 180Degree angle */ else if(recdata=='b') /* When Received data 'b' */ servo_value = 30; /* Servo rotate for 0Degree angle */ } } } /************************************************************************************ END OF CODE *************************************************************************************/ |
Topics related to ARM LPC2138/48 Mini Development Board
- ARM LPC2138/48 Mini Development Board – Overview
- ARM LPC2138/48 Mini Development Board – LED Interfacing
- ARM LPC2138/48 Mini Development Board – LCD Interfacing
- ARM LPC2138/48 Mini Development Board – UART0 Interfacing
- ARM LPC2138/48 Mini Development Board – UART1 Interfacing
- ARM LPC2138/48 Mini Development Board – Switches Interfacing
- ARM LPC2138/48 Mini Development Board – BUZZER Interfacing
- ARM LPC2138/48 Mini Development Board – POT Interfacing (ADC)
- ARM LPC2138/48 Mini Development Board – Temperature Sensor Interfacing(ADC)
- ARM LPC2138/48 Mini Development Board – Interfacing Servo motor
- ARM LPC2138/48 Mini Development Board – Internal Real Time Clock (RTC)
- ARM LPC2148 Mini Development Board – USB Interfacing (Human Interface Device)
Resources
- Softwares
- Datasheets
How to buy?
- Click here to buy rhydoLABZ ARM LPC2129 Development board-Mini
- Click here to buy rhydoLABZ ARM LPC2138 Development Board-Mini
- Click here to buy rhydoLABZ ARM LPC2148 Development Board-Mini
Support
Please share your ideas with us, visit our forum for discussion
Leave a Reply
You must be logged in to post a comment.