Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to use PIC18F4550 to control a servo motor?

Not open for further replies.


Newbie level 3
Jun 15, 2009
Reaction score
Trophy points
Activity points
servo pic18f4550

Hi... I am new to microcontroller. Recently, I was given a task to use a PIC18F4550 to control a servo motor. How can this be done? I've damaged quite a few of them in my school lab.

I heard for a servo motor, the PWM on time for its centre position is 1.5ms. I am required to activate a laser pointer (at RC0) when it is at its centre position.

This servo motor will have to move back and forth for 5 times. I.e. it will act like as if it's performing a scanning.

I am not sure if a threshold is required; if it's needed then it'll be 3.5V.

Can somebody please help me out? I need a sample program to try things out. I’ve tried searching online but I couldn’t find any one of them for this particular PIC. I am in a desperate position because I've been trying doing this for a few weeks but I still couldn’t get it done and it's due soon. Really appreciate if you can help.

FYI u may want to use the following I/O ports:
Start switch: RB0 (active low)
Pause switch: RB1
Photodiode sensor: RA0
Feedback from LDR to indicate if there is a hit: RA1

Servo motor: RC2
Laser Pointer RC0

pic18f4550 servo motor program

Most programs are compatible between PICs with just a few changes (header file names, port names, configuration, etc), so those servo programs you found may work for the 18F4550 also.

There are a few ways to go about doing this:
- Use TMR1 running on a high prescaler (so it ticks slow) and have it trip at the minimum pulse, the end of the servo pulse, and again at the beginning of the next pulse.
- If you don't need to do anything while the servo is running, use delays and create the servo pulse using a variable delay between the minimum pulse and where you want the pulse to end (i.e. turn on RC2, wait the minimum time (lets say 1ms), then wait some more time until it reaches your desired length (0.5ms in this particular example for center), turn off RC2, then wait the 18ms or whatever until the next pulse is to be issued)

Now, rotating the arm is really easy: just have the variable that will keep track of your desired pulse length incremented or decremented. When it reaches a center value turn on the laser pointer.

Sorry, no program. I don't feel like writing one and then saying that it will be bug free since I won't be testing it.

EDIT: I used this site for the servo pulse reference the first time I did a servo controller:

Not open for further replies.

Part and Inventory Search

Welcome to