Wednesday, May 17, 2017

Teensy 3.6 Basics - Using Servo Motors

A servo motor is a physical actuator that usually rotates for half a circle or less, or moves forwards and backwards by a set amount. Arduino add-on includes a servo library, which can be used to easily control a servo motor.

A simple, lightweight servo motor can be connected and powered by the Teensy 3.6. Note that anything more heavy duty than a very light motor will require an external power supply.

A servo motor has a control input, which expects a pulse wave with a duty cycle of around 1 - 2 ms and a period length of 20 ms. The duty cycle determines the angle or position of the servo. A duty cycle of 1 ms sets the minimum rotation or position. A duty cycle of 2 ms sets the maximum rotation or position.

Hardware Setup
The servo motor has three connections. Black or brown is usually ground, and should be connected to Teensy ground. Red is usually the positive power terminal, and should be connected to Teensy 3.3V. Orange or yellow is usually the control input, and should be connected to a Teensy digital pin, in this case pin 0.

Note that if a more heavy-duty servo motor is used, an external power supply is required. In this case, the ground of the motor, teensy and power supply should be connected. The positive terminal of the power supply should be connected to the positive terminal of the servo. The control input should be connected to a Teensy digital pin.

Software Setup 
The servo library must be included at the start of the code. A servo object with a unique name can then be created by using the Servo structure.

The servo object can then be attached to a digital pin of the Teensy. This can be any digital pin. Two optional arguments as part of attaching the pin set the minimum and maximum duty cycle time periods in microseconds. A suggestion is to start with 1000 microseconds and 2000 microseconds, and adjust from there.

To control the servo motor, the write command is used with the Servo object. The value is in degrees, from 0 - 180.

Example 1 - Rotating the Servo
In this example, a servo is connected to the Teensy. The servo is then simply rotated and delayed on loop via the servo library.

Download here:

Example 2 - Controlling the Servo via MIDI
In this example, the servo is controlled via a MIDI control message on channel 1, controller 1. The range of 0 - 127 is mapped to 0 - 180 degrees.

Download here:

The servo motor is an example of physical control via the Teensy. MIDI data can be used to control the movement of the motor.