Wednesday, May 17, 2017

Teensy 3.6 Basics - Using DC Motors

Overview
DC motors rotate when a voltage is applied across the terminals. The speed of the motor is related to the voltage.

When using a microcontroller to control a DC motor a PWM (or pulse width modulated) signal is used. The Teensy forms a pulse wave, whereby the duty cycle is proportional to the average, equivalent over time. The duty cycle refers to the percentage of the waveform that the pulse wave is high as opposed to low.

A PWM output with a duty cycle of 50% will result in an average of half the voltage of the digital pin, a duty cycle of 25% in one quarter and so on. More information can be found about PWM signals here.

For instance, given a 3V digital pin with a PWM wave of 50%, this would provide the equivalent voltage of 1.5V over time. Thus, the digital PWM outputs on a Teensy can change the speed of a DC motor, not just turn it off and on.

The default frequency for PWM signals on the Teensy 3.6 is 488.28 Hz. Digital outputs 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 16, 17, 20, 21, 22, 23, 29, 30, 35, 36, 37, 38 are all PWM-capable pins. More information about the PWM functionality of the Teensy can be found here.

When using a PWM signal to control a motor, a certain amount of current is needed to power the motor. The outputs of the Teensy cannot provide sufficient current for the DC motor, and attempting to do so may damage the Teensy.

Instead, a Darlington transistor array in a convenient chip form can manage power source, current and control input and act as mediator between the teensy, the USB power and the DC motor. Note that anything more heavy duty than a very light motor will require an external power supply.

The Darlington transistor array comes packaged in an 8-channel version (ULN2803AN) and 7-channel version (ULN2003AN). In both cases, multiple channels can be combined to control larger motors. Each channel can provide up to 500mA of current, if a sufficient power supply is used.

In the example shown below, a single channel of a ULN2803AN is used to drive a single DC motor using 3V from the Teensy USB power supply.






Hardware Setup




Ground from the Teensy is connected to pin 9 of the ULN2803AN. 3V from the Teensy is connected to pin 10 of the ULN2803AN. Digital pin 2 of the Teensy is connected to pin 1 of the ULN2803AN. Pin 18 of the ULN2803AN is connected to one terminal of the motor. 3V from the Teensy is connected to the other terminal of the motor.





Software Setup
To control the motor, make sure that the motor is connected to a PWM-capable pin. Digital outputs 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 16, 17, 20, 21, 22, 23, 29, 30, 35, 36, 37, 38 are all PWM-capable pins.

Use analogWrite(pin, value) to set the speed of the motor. The pin is the pin number. The value is a range of 0 - 255.

There is no need to set the pin as an output pin in setup().




Example 1 - Turning the DC Motor On and Off
The motor is turned on and off.







Example 2 - Gradually Turning the DC Motor On Over Time

Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Using_DC_Motors_Example_2/





Summary
DC motors can be easily controlled via analogWrite(), however they require additional hardware to work. Combined with MIDI control, there can be used in projects of various types.

0 comments: