RC Servo

A servo motor is a generic term for a motor that has an integrated driver and controller. Servos are used in all kinds of motion control. An RC servo is a small motor used in remote control cars, planes and boats, and is easy to control using a microcontroller. The Teensy can command the RC servo to move to any angle from 0 to 180 degrees, and the servo will hold that angle as best as it can. Servos come in many sizes and qualities, and are often highly geared to have lots of torque. The downside of the gears is audible noise when moving. Most servos use a potentiometer as a position sensor, and this limits their motion to 180 degrees. The output shaft of the servo attaches to a part called the servo horn, which can be tricky to attach to. Try not to glue directly to it (the plastic is too inert for anything to stick well), instead use a stiff wire as a pushrod.

Image of servo, horn, pushrod The RC servo

Wire up the RC servo, run the sweep example

The RC servo has 3 wires: brown for ground, red for 5V, and orange for signal. You can find 5V from the Teensy on pin VIN. Some larger servos can use more current than the USB cable can provide, and would need an external power source from a wall adapter or a battery pack. The kit contains a set of 3 extra long header pins so you can plug the servo into the breadboard.

Image of headers and how to plug in How to plug the RC servo into the breadboard

The servo is commanded using the PWM, like how analogWrite() pins work, so choose a PWM capable pin for the orange signal wire.

The code that the Teensy uses to command the servo is not automatically included, so you need to include the library at the top of your code with #include <Servo.h>. In the definitions area, a servo variable type is declared, and in setup(), set the pin using myservo.attach(). Then you can command an angle using myservo.write(), which takes a number from 0 to 180 degrees.

Open the example code for the servo in File->Examples->Servo->Sweep. Change the pin and run the code and the servo should oscillate back and forth.

Make the servo motion mirror the potentiometer

Make a new file and copy in the contents of sweep. Edit the code so that at 10Hz the potentiometer angle is read and sent to the servo, so that the servo angle matches the potentiometer angle.

Solution