Using the STM32F4 Discovery to Control the Futaba S3003 Servo
Hi tecsploiters! This tutorial is going to guide you through controlling the popular Futaba S3003 servo using PWM (Pulse Width Modulation) on the STM32F4 Discovery board. For this project you will need the following kit:
- STM32F4 Discovery board and USB cable
- Futaba S3003 Servo (or similar)
- 3 male / female connector cables (or some other method of connecting the servo to the STM32F4 Discovery)
Servos in General A servo is a special kind of electric motor, however instead of rotating for ever like normal electric motors servos are made to move to certain positions, in the case of the futaba S3003 it can move clockwise and anti-clockwise within a range of around 180 degrees. (for more information on controlling standard electric motors see my tutorial on basic motor control). Servos are used when you need to be able to precisely control the position of a motor like if you were building a turret to hold a camera and are used a lot in remote control cars and plains. The Futaba S3003 has 3 wires:
- Red – this is the positive voltage (5v in this case)
- Black – Ground
- White – control wire
Note – if your using an external power source make sure you still connect the grounds together!!
Connecting the Servo All you need to do for this project is connect the red power wire to the 5v pin on the STM32F4 Discovery board, and the ground to the GND pin on the STM32F4 Discovery and the white control cable to pin PB6. Controlling the Servo Position If you connect up just the power terminals of the servo you will see that nothing interesting happens, this is because before a servo will move you need to tell it what position it should move to, this is where the white control wire comes in. A servo is controlled by sending a pulse (a square wave) to it, you can think of this as essentially switching pin PB6 on and off very quickly – the rate at which you switch the pin on and off determines the position that the servo will move to and stay in until a new signal is received. As we need to control the pulses to the servo very precisely in order to be able to accurately control its position we need to use the STM32F4 discoveries pulse width modulation capabilities (for more information on PWM see my previous tutorials on PWM). Essentially using PWM we are able to create a signal on the white wire that pulses at whatever rate we require, by changing this rate we can then tell the servo to move between positions, as you can see in the video below.
download the full source code from the downloads section