Background and Theory
Servos are frequently used in embedded projects for different reasons. They are used for robotic arms, opening/closing lids, pan/tilt cameras, lights, RC planes, and RC cars. Some variation of servos are even used as the main motors for small mobile robots.
Servos require a 50Hz square wave with a positive width length ranging from 1mS to 2mS at all times, violating this timing specification will create erratic movement which can lead to unwanted behavior from damaging property to hurting someone. The position or speed (for continuous servos) changes depending on the duty cycle.
Due to the timing requirements, it's usually not recommended to have the main processor of the project to control the servos since it requires either one or more dedicated hardware timers or uses many CPU cycles to keep sending a correct waveform to the servo. It's better to use dedicated hardware for this purpose.
In this project, I use an MSP430G2553 to build a circuit that produces servo signals using C. The reason I chose this microcontroller is due to their ultra low power consumption when put to sleep, and since the controller doesn't do much most of the time, we can shut it off and have the timer interrupt wake the MCU only when signals are needed to be toggled. After toggling the signal, the microcontroller will check if it has received any commands through the UART port. If a UART command was received, it will be analyzed to determine if the position of the servos should be updated.Getting Ready
This project can be built on a breadboard or on a prefboard. In anyway, we need a programmer to program the MCU, I use the MSP-FET to do this. But all Texas Instruments LaunchPads based on the MSP430 include a programmer/emulator and can be used to program the MCU externally as well.
1 / 2 • Figure 1: Prefboard is perfect for quick Prototyping
I have written the code in C using Code Composer Studio IDE from TI. It will be necessary to compile the code and flash the MCU through the MSP-FET.
I have also included the drill and gerber files to fabricate the PCBs of two different versions of this project. One version doesn't have optocouplers and the other version does. The optocouplers are used to allow the MCU to send signals to the servo without having them galvanically coupled (there is no current flow between both components). Since the "ground" connections of the servos and the MCU are not connected together, the noise produced by the servos will never reach the power supply of the MCU, creating a project that is more immune to noise. For demo purposes I will be working on the PCB without optocouplers since it only requires one power supply instead of two.
1 / 2 • Figure 2-1: Servo Controller Board with optocouplers
I have uploaded the code for this project in GitHub. Click here to download it in order to compile it and program the microcontroller.
I have also attached the schematics with the bill of materials, please follow those instructions to implement the circuit on a PCB, prefboard, or breadboard.
Compiling the Servo Controller Project
1 / 2 • Figure 3-1: C2 and C3 are not needed. Power headers and SPI headers are not needed either.
To compile the code for the MCU, follow the next steps:
- Download the repository as mentioned above
- Extract the ZIP file somewhere where the path name doesn't contain many characters, e.g. Root of the C: drive
- Open Code Composer Studio (install it if you have to, make sure to install support for MSP430 Family and Debugger/Programmers)
- Accept the default workspace location
- Go to Projects->Import CCS Projects...
- Under "Select search-directory" find the directory that was just extracted and make sure that "Copy projects into workspace" is selected
- The project should have opened up successfully. On the "Project Explorer" double click "msp430_servo.h" to open the header file
- If no optocouplers will be used, make sure to comment out the 12th line and uncomment the 11th line
- On the menu bar, go to Project->Clean... to clean the directory that was downloaded
- On the "Clean" window, make sure that "Clean all projects" is not check-marked, that "msp430_servo" is check-marked, and that "Start a build immediately" is also check-marked. Also select "Build only the selected projects"
The project should have cleaned up any old files and compiled the code into an executable file with the ".out" file extension.Programming the Microcontroller
To program the microcontroller, it must be powered. There is one screw terminal that powers both the servos and the MCU (applicable for the board without optocouplers). The MCU takes 3.3V and no more so this project contains an onboard 3.3V low-dropout regulator to make sure the MCU will work properly as long as the input volatge is 0.3 volts higher. The servos usually can work with a minimum of 5V to a maximum of 6V. The MSP-FET (programmer) must also be connected to both the computer and the Servo Controller Board.
- Go to Run->Debug Configurations... to open the "Debug Configuration" Window
- Under "Main" Tab, remove the check mark on "Use default target configuration", this will enable to modify the directory path of "Target Configuration"
- Click on "Workspace..." to the right of the path bar
- Then, on the "Select Target Configuration" window, drop down our project menu, the " msp430_servo" one
- Several folders should be listed now, select and open "targetConfigs"
- Select the file: "MSP430G2553.ccxml" and press "OK" to close the window
- Press "Apply" on the "Debug Configurations" window, then "Close" to go back to CCS
- Go to Run->Load->Select Program to Load
- Press on "Browse project.." on the window that just opened
- Drop down our project, then open debug and then select the file "msp430_servo.out" file
- Press "okay" on the previous window
- Wait for the programming process to finish
These are the steps to program the Servo controller board. All the steps and explanations are included on the YouTube video above. Please watch it as it also explains how to use it. If different commands are required, modify the switch statement in the super-loop (while(1)) in the main() function.