+1 (315) 557-6473 

Servo Motor Control using PIC18F4321 Microcontroller

This C code demonstrates the control of a servo motor using a PIC18F4321 microcontroller. The program continuously monitors the states of three switches (SW2, SW3, and SW4) and adjusts the servo motor's position accordingly. The servo motor is connected to pin RA1, and the switches are connected to pins RC0, RC1, and RC2. The main logic inside an infinite loop interprets the switch states, generating appropriate signals to move the servo motor to the leftmost, rightmost, or middle position. The program includes timing delays to ensure precise servo motor control. Additionally, a test LED on pin RC4 (connected to LED D3) provides visual feedback of the program's execution.

Exploring Servo Motor Control with PIC18F4321

The provided C code showcases servo motor control with a PIC18F4321 microcontroller, offering a practical demonstration of embedded systems programming. The program continuously monitors switches SW2, SW3, and SW4 to dynamically adjust the servo motor's position. The initialization phase sets up crucial configurations and pin assignments, emphasizing the digital port setup and switch connections. The code's clarity and structure make it an instructive example for those seeking to understand microcontroller-based servo motor control. Whether you are exploring embedded systems or seeking help with your C assignment, this code provides valuable insights into programming for hardware interaction and real-time applications. Additionally, the inclusion of a visual indicator through an LED on pin RC4 enhances the code's educational value, showcasing a tangible feedback mechanism in action.

Block 1: Header Files and Configuration Bits

#include < stdio.h > #include < stdlib.h > #include < p18f4321.h > #include < delays.h > #define SERVO PORTAbits.RA1 #define SW2 PORTCbits.RC0 #define SW3 PORTCbits.RC1 #define SW4 PORTCbits.RC2 # pragma config WDT = OFF # pragma config LVP = OFF # pragma config BOR = OFF # pragma config OSC = INTIO2


  • The #include statements are used to include necessary header files.
  • #pragma config statements are configuration bits for the microcontroller, setting options like watchdog timer (WDT), low-voltage programming (LVP), brown-out reset (BOR), and oscillator source (OSC).
  • Definitions for SERVO, SW2, SW3, and SW4 are provided for easier understanding of the code.

Block 2: Main Function

void main(void) { ADCON1=0X0F; // configure port A as digital TRISAbits.TRISA1=0; // set as an output pin. This is the servo control signal pin TRISCbits.TRISC0=1; // enable SW2 as input TRISCbits.TRISC1=1; // enable SW3 as input TRISCbits.TRISC2=1; // enable SW4 as input TRISCbits.TRISC4=0; // Make RC4 as output - this is connected to portc 4th pin, this is to test the program LATCbits.LATC4 =1; // Turn LED D3 ON


  • main() is the starting point of the program.
  • ADCON1=0X0F; configures port A as digital.
  • TRISAbits.TRISA1=0; sets RA1 as an output pin for controlling the servo.
  • Configuration of inputs (SW2, SW3, SW4) and an additional output (RC4) for testing purposes.
  • Turning on LED D3 (connected to RC4).

Block 3: Infinite Loop for Servo Control

while(1) { if (SW2==0) { //Move the servo to the leftmost position SERVO = 1; Delay10TCYx(12); // on for 1 ms SERVO = 0; Delay100TCYx(48); // off for 19 ms } if (SW3==0) { //Move the servo to the rightmost position SERVO = 1; Delay10TCYx(58); // on for 2 ms SERVO = 0; Delay100TCYx(45); //off for 18ms } if (SW4==0) { //Move the servo to the mid position SERVO = 1; Delay10TCYx(35); // on for 1.5 ms Delay1TCY(); // add 0.5ms delay Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); SERVO = 0; Delay100TCYx(44); //off for 18.5ms } } }


  • The program enters an infinite loop, continuously checking the state of three switches.
  • If SW2 is pressed, it moves the servo to the leftmost position with specified delays for on and off states.
  • If SW3 is pressed, it moves the servo to the rightmost position with corresponding delays.
  • If SW4 is pressed, it moves the servo to the mid position with specific delays for on and off states. Additional delays are used to fine-tune the position.


In conclusion, this exemplary C code not only showcases the efficient control of a servo motor through a PIC18F4321 microcontroller but also serves as a valuable educational tool. With meticulous attention to digital port setup, switch connections, and a real-time control loop, it provides a practical illustration for enthusiasts diving into embedded systems programming. Whether you're a student grappling with a C programming assignment or an individual eager to explore microcontroller applications, this code offers clear insights and structured guidance. The inclusion of a visual indicator, an LED on pin RC4, not only reinforces the concepts but also adds a hands-on dimension to the learning experience, making this resource an accessible and informative tool for hardware interaction.