328 Motor and Servo Functions

Revision as of 21:12, 11 May 2012 by Tfriez (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
ARDUINOFunctions and Variables → 328 Motor and Servo

The 328P-Based Arduinos have 6 ports available for PWM generation. These ports can be used control motors and servos to extend the capabilities of your Arduino system. Users can use the "Motors and Sensor Setup" utility (found under the Robot menu) to configure their ports for the various types of motors:

Hardware Timing Servo Ports - Pin 9 and 10.

Hardware Timing PWM (H-Bridge Friendly) Ports - Pin 4, 5, 6, and 11.


Available Motor Types
Motor Type Comments
No Servo/Motor Generate no PWM from this pin.
Audio Speaker Use this Pin to generate Audio output (piezo-electric speaker compatible)
Servo - Standard Generate a 20ms-based Servo Pulse
Servo - Cont. Rotation Generate a 20ms-based Continuous Rotation Servo Pulse
Ext-HBridge - Single PWM Generates a standard PWM pulse and associates a second digital out pin to control the direction. Used for HBridges that have High/Low direction control.
Ext-HBridge - Dual PWM Generates a standard PWM pulse on two pins for speed and direction control. Used for HBridges that have PWM based direction control.
Variable Intensity LED Generates a standard PWM pulse on a single pin for brightness control of a LED.
Analog Write Generates a standard PWM pulse on a single pin for analog output.

Color Key


bool bMotorReflected
(bool) Boolean array with one entry for each motor. Indicates that the direction of a motor should be reflected 180 degrees. Useful when mechanical design results in a logical "reversed" condition of a motor.
bMotorReflected[motor_2] = true;   // motor 1 WILL be reflected 180 degrees
bMotorReflected[motor_2] = false;  // motor 1 will NOT be reflected 180 degrees


int motor[tMotor motor]
(int) An array variable with one element for each of the possible motors.

When used with a motor, this command is used to set the speed (-127 to +127) for a motor. Negative values are reverse; positive forward. Zero is stopped.

When used with a servo, this command is used to set the position (-127 to +127) of the servo. Zero is the "center" position of the servo, and -127 to +127 is the range of the servo. Note that your servo may not be able to travel the full -127 to +127 range, so please experiment with smaller values to avoid breaking your servo.

Parameter Explanation Data Type
motor A motor port or name tMotor
motor[motor_2] = 127;   // motor pin 2 is given a power level of 127 (forward)
motor[motor_2] = -127;  // motor pin 2 is given a power level of -127 (reverse)