You are on page 1of 5

5/21/13

How to build a PIC16F84 Servo Controller

Building a Serial Servo Motor Controller Several people contacted me about our other project for controlling hobby servo motors with the PC serial port. Servo Commander was designed after quite a few people asked for a version of the serial servo controller that would let the user select a different serial port on the PC, and had the ability to control 8 servos. The first servo controller project at http://www.rentron.com/servo.htm had the com port fixed at com1. This new version will seek out the available com ports on your PC and then let you select the desired port and baud rates. It has 1 additional servo port as well to let you control a total of 8 hobby servo motors from the PC.

Servo Commander Screen Capture Click on image for larger picture Applications: Many robotics projects require several servo motors. The process of figuring out the exact positions for each motor to achieve different gaits for walking can be extremely time consuming if we have to download the code several hundred times to the Basic Stamp to record these position settings. Using Servo Commander we can cut this time by a considerable amount. Just hook-up your robot, or up to 8 servo motors to the simple circuit shown below and watch the actual position data below each motors control knob as you move the motor. Servo Commander makes recording each position for different walking gaits a very simple operation. The Software: The Servo Commander software provides you with the PC interface to the outside world to control hobby servo motors. The Firmware: The firmware for this project lets you program the Microchip PIC16F84 or the Basic Stamp microcontroller to be used as a dedicated servo motor controller. The PIC16F84 receives motor control data via a single asynchronous serial connection from the PC or any other source capable of transmitting asynchronous serial data, such as the Basic Stamp. Asynchronous Vs Synchronous: Asynchronous (also referred to as non-synchronous) means that the serial transmission line doesn't require a (clock signal). For synchronous transmission lines, each device uses a common clock line. The clock signal is normally generated by one device on the transmission bus, or an external clock source. Asynchronous is preferable when using microcontrollers with limited I/O capabilities by not requiring the extra I/O-pin for the clock signal. Operation of Servo Commander: To use Servo Commander just click your mouse on a position by each control knob. Holding and dragging the control knobs will work also. Once you release the mouse button, the motor will move into position. You can also click on the (tick marks) around each control knob, or the knob itself. Each motors position data will remain visible under the motors control knob until you change the position of the control knob. You can move up to 8 motors into the position you want and then record the position data. The value under each control knob is the actual value that will be serially transmitted to the PIC16F84 or the Basic Stamp. Although it was designed to use the PIC16F84 microcontroller, it will work just as well with the Basic Stamp I and Basic Stamp II. The PIC16F84 code for this project was compiled using the MeLabs PicBasic Pro Compiler, Version 2.20. The only limitation using Servo Commander with the Basic Stamp I is that you will only be able to
www.rentron.com/SerialServo.htm 1/5

5/21/13

Version 2.20. The only limitation using Servo Commander with the Basic Stamp I is that you will only be able to control (7) servo motors because at least one of the (8) available I/O pins will be needed to receive serial data from the PC. This project has quite a few more applications than just being used with the Servo Commander software however. One of the biggest handicaps we run into when controlling motors with a microcontroller is the processor overhead that's required to maintain control of our motors. Many companies manufacture motor control IC's, but most of them still require that we feed a continuous signal to the control IC to maintain the motor position and speed. By using another microcontroller specifically designed to handle these tasks we can relieve the overhead required by the (main) controller to handle other tasks. This project is an example of how to use a (cheaper) microcontroller to expand the I/O capabilities of a more expensive controller such as the Basic Stamp. Just like the Basic Stamp: The programming structure of PicBasic is much the same as the Basic Stamp and is an easy transition for most users of the Basic Stamp. The price of the Basic Stamp makes it an expensive choice to dedicate to such a simple task as controlling motors. By adding a relatively inexpensive version of the PIC family such as the PIC16C84 or PIC16F84 to the Basic Stamp project, we can dedicate the Stamp to other higher level functions like monitoring additional sensors or performing calculations. Using the Basic Stamp II, you have plenty of spare I/O available and can control all 8 servo motors using Servo Commander. Here is the simple code used in conjunction with the PIC16F84 or the Basic Stamp, to accept serial commands from the PC serial port and position the hobby servo motors. For the PIC16F84: Include "modedefs.bas" ' Include serial modes si var PORTA.0 pos var byte servo var byte cont var byte portb = 0 trisb = 0 ' Set portA.0 as our serial input pin ' Define our storage location for the servo position information. ' Define our storage location for the servo number to move. ' Define a variable to hold the count. ' Make all of port B outputs to send position information to each servo.

How to build a PIC16F84 Servo Controller

start: serin si,N2400,servo,pos ' Get the servo# and position for that servo. for cont = 0 to 50 pulsout servo,pos ' Send servo# ? to position ?. pause 15 ' Wait 15 ms. next cont ' Next pulse. goto start ' Return to get more serial input. For the Basic Stamp I: symbol si = 0 symbol pos = B0 symbol servo = B1 symbol cont = B2 ' Serial input on pin 0 ' B0 holds the position ' B1 holds the servo # ' B2 holds the count

start: serin si,N2400,servo,pos for cont = 0 to 50 pulsout servo,pos pause 15 next cont goto start This simple piece of code handles all of the communications with the PC by waiting for each servo number and position to be sent via the PC serial port to the PIC16F84 or the Basic Stamp. As soon as these two bytes of information are received from the PC, the program sends this information to the the port B I/O pins (of the PIC16F84) or the Basic Stamp I/O pins to position the servo motors. Application of the PIC16F84 Servo Control Chip: Once you have programmed the PIC16F84 with this firmware, you have many options for using it. The PIC16F84 can be used by the Basic Stamp as a peripheral motor control chip to free the Basic Stamp from handling the servo motors control functions. This can help to expand the I/O capabilities of the Basic Stamp I by using a single I/O pin to communicate with the PIC16F84. Below is the schematic for using the PIC16F84 with the Servo
www.rentron.com/SerialServo.htm 2/5

5/21/13

single I/O pin to communicate with the PIC16F84. Below is the schematic for using the PIC16F84 with the Servo Commander PC software. Robotics Applications: This project has quite a few robotics applications as well. If you're building the Hexapod robot from http://www.lynxmotion.com it comes in pretty handy for figuring out the walking gaits. You could build a robot with 8 servo motors and control him directly from the Servo Commander software interface, or use the PIC16F84 as the dedicated servo control chip in your Basic Stamp projects. Once you have experimented with the Servo Commander software and the PIC servo controller you will probably come up with hundreds of other applications. A few ideas: A PC control system for controlling your venetian blinds on your windows. Controlling the volume of your stereo from the PC by adjusting the knobs with the servos. Controlling mechanical switches. Table-top hobby robots. Testing new servo motors. See what you can come up with, the possibilities are virtually endless.

How to build a PIC16F84 Servo Controller

PIC16F84 Schematic for Servo Commander Click on image for larger picture You can connect the Basic Stamp to the PIC16F84 serial input line (RA0) and send servo control information to the PIC. You don't need the 22K resistor as it's only for limiting the current through the direct interface to the PC serial port. Tie both circuits GND together and use any I/O line from the Basic Stamp that you have available. The Visual Basic Software: The Visual Basic 5 software for Servo Commander has built-in schematics to help you build the above circuit. The screen capture above was taken directly from the screen while Servo Commander was running. The code for the PIC can easily be modified (as shown above) for use with either the Basic Stamp I, or the Basic Stamp II. Below is a sample of the Visual Basic software and how it operates. Private Sub Knob1_Change() Text1.Text = Knob1.Value MSComm1.Output = Chr$(0) MSComm1.Output = Chr$(Text1.Text) End Sub By placing the control knob on the main form and double-clicking on it we can then enter the code shown above. The above code sample is the code for knob #1 that will control servo #1. If you refer to the first graphic at the top of the page you will notice that directly below the control knobs there is also a text box. Each change you make to the knob is reflected in the text box. The line above that states: Text1.Text = Knob1.Value assigns the actual value of the knob position to the text box. By setting the MIN MAX properties for each knob control, we can control the actual values that will be sent out to the PIC. The Servo Commander software has the following values set as the MIN MAX for each control knob. MIN = 200, MAX = 100. Sounds like it's backwards doesn't it...? This is because the maker of the knob control KNOB.OCX designed each knob to be at the MIN value when the knob is to the far right side. Since a value of 100 will send the servo to its far right position, this was necessary to make the motors move in the same direction as the control knobs. If you move the knob to the right, the servo moves to the right, etc... The PicBasic Pro PULSOUT command has a resolution or period of 10uS when the PIC is operating at 4MHz, and 2uS when operating at 20MHz. The table below shows oscillator speeds with corresponding pulse timing for the PBP PULSOUT command. Use this chart to set your MIN/MAX values based on the oscillator you use. If you use the Stamp VS PBP look this value up in your Stamp manual, and simply adjust your MIN/MAX values as needed. Some servo motors may work better with adjusted values as well since none seem to be 100% the same. Tweak this until you get the results you need with a given servo motor.
PBP PULSOUT C ommand Timing & C onstant Values

www.rentron.com/SerialServo.htm

3/5

5/21/13
Pulse Time C ON stant Value 1000 500 200 100

PBP PULSOUT C ommand Timing & C onstant Values x Pulse Period Pulse Width = 2uS 2mS 2uS 1mS 10uS 2mS 10uS 1mS

How to build a PIC16F84 Servo Controller

Oscillator Speed 20MHz 20MHz 4MHz 4MHz

Note: If you use the Basic Stamp to interface to the PC and control the servos directly, you will have only (7) I/O lines to control the servo motors with. If you refer to the above code you will notice that the knob1 control will output the value (0) first. The first value is for the servo number. Since 0 corresponds to the Basic Stamp I I/O line 0 you will not be able to use knob1 if you use the Basic Stamp I/O line 0 as the serial input line. Use knobs 2-7 for the Basic Stamp I to control the servos, and I/O line 0 on the Stamp as your serial input. This project was primarily designed to expand the I/O capabilities when using the Basic Stamp I, but will work using the Basic Stamp I as the receiving controller to directly control the servos. Another option would be to setup the Basic Stamp as the receiver with the PIC16F84 tied to another I/O line of the Basic Stamp. The Basic Stamp can receive the information directly from the PC and then (relay) the incoming data using the SEROUT command to the PIC16F84 to move the servo motors. This method lets you use the Basic Stamp in much the same manner as you would during normal operations, and will give you (8) servo control outputs while requiring only (2) of the Basic Stamp I I/O lines. The PIC only requires (1) I/O line from the Basic Stamp to receive the asynchronous serial data sent from the Stamp. You can purchase the KNOB.OCX visual basic control from http://www.mabry.com. They have several different controls that are really handy when you start developing visual basic control applications. I think the knob control is still about $20.00 and it's worth every penny. The first serial servo control software we posted here at: http://www.rentron.com/servo.htm was very limited. This new version has a considerably nicer looking interface combined with more functionality. Below is a picture of the port and baud rate selection screen.

Port & Baud Rate Selection If you have the book by Jan Axelson you should recognize this part. I used part of Jan's port selection routines to build this part of the project. The software included with Jan's book Serial Port Complete has a nice template that is (extremely handy). Using Jan Axelsons template to build your own visual basic serial routines around can save you tons of time and I highly recommend this book if you plan to write your own visual basic serial applications. If you don't have Jans outstanding book Serial Port Complete, you can just click on the blue link above to grab your own copy from Amazon.com. Several functions are built into the template that will scan your PC to find the available serial ports. Once you have made your selection, it will be remembered each time you start Servo Commander. This saves you the aggravation of having to select your preferred port & baud rate each time you start the software. The box below Port Status will give you an error message if your previous sessions port selection is no longer available when you start the program again. Another nifty little code segment from Jan's book. The file Servo2.hex (in zip file below) included with this project is ready to burn directly into the PIC16F84. If you own a copy of PicBasic Pro, you can modify the program to do considerably more than this project was intended. The included file however, is all you will need to have a dedicated servo control chip ready to interface to your Basic Stamp application. Controlling the PIC16F84 with the Basic Stamp: Just connect your Basic Stamp to the PIC instead of connecting it to the PC. You won't need the 22K resistor shown in the above schematic. This resistor is to help limit the current to acceptable levels from the direct serial interface to the PC. To move a servo connected to the PIC just send the following command to the PIC. serout 0,N2400,(1,255) ' Where 1 is the servo number to address and 255 is the position to send the servo to. The above command would move servo #1 to position 255 (left). Using this method you can address each servo while tying up only "1" I/O-Pin on the Basic Stamp to control the PIC16F84 servo control outputs. You can use

www.rentron.com/SerialServo.htm

4/5

5/21/13

How to build a PIC16F84 Servo Controller

while tying up only "1" I/O-Pin on the Basic Stamp to control the PIC16F84 servo control outputs. You can use this project to expand the I/O capabilities of the BSI and the BSII or use the PIC16F84 as a stand-alone servo motor controller directly interfaced to your PC. The software for this project has been developed and is being distributed FREE of charge as a service to all of our visitors. This software is not for sale and can be distributed freely as long as you do not charge anyone for it. The software comes with a complete installation program and is very simple to use. After you download the file, unzip it into a temporary directory and the run the file setup.exe. A readme.txt file comes with it to help you install or uninstall the program. Click the blue link here to download the complete Servo Commander. The full version is about 1.8meg in size and includes both of the PicBasic HEX & BAS files and the complete installation program for the Servo Commander software. The full version comes complete with the schematic integrated into the program. Just click on the setup menu and select the schematic from the menu options. If you have any questions or suggestions, you can contact me at: webmaster@rentron.com. Further Study Recommendation: If you're new to serial communications between the PC and the outside world, you can get a really (solid) introduction to interfacing the PC to your microcontroller or other projects from Jan Axelsons' excellent book titled Serial Port Complete. I most highly recommend Jans' book if you plan to do any serious applications development, or you would just like to gain a better understanding of what goes on under the hood. I have just about every book ever published on serial communications and I have yet to find a book that even comes close to Jans' on this topic. Applications "just like this one" are simple with the fantastic information Jan provides in her book. There have been quite a few people asking for the RAW basic code for this project, so here it is. Click HERE for the raw Visual basic code for the servo commander project. You will need to purchase the KNOB.OCX visual basic control from http://www.mabry.com to use the raw visual basic code for this project. The Next Project: For the next project I will show you how to control relays from the PC. This project will have Visual Basic software with a schematic similar to the one shown above for the PIC16F84. It will include command buttons directly placed on the schematic that you can click on to activate the relays. If you have a good idea for another similar project you would like to see, just drop me a line via email with your ideas or suggestions. Until then - Have fun....;o) Bruce Click HERE to purchase PicBasic Compilers - and hardware. Click HERE to learn more about the PicBasic compiler. Click HERE to return to the PicBasic projects page. Note: The new project has been posted. Controlling relays from your PC with the Basic Stamp I is here. Click HERE to see it.

www.rentron.com/SerialServo.htm

5/5

You might also like