The MotorHat Class is designed to work with the Adafruit DC & Stepper Motor Hat. This page documents the public API of the MotorHat Class.

Constructors
The MotorHat Class has a single constructor. The constructor takes an optional argument of an I2C address for the PCA9685 chip the Hat is based on. The I2C addresses for the Hat is 0x60.
If no argument is provided the constructor configures the class to use the default I2C address of 0x60.
The MotorHat class references the PCA9685 class as superclass. The I2C address in the constructor is passed to the superclass.
The constructor allocates 4 objects of type DCMotor Class, and two objects of type StepperMotor Class. These classes are used to control the motors attached to the motor ports on the Hat. Please see the API descriptions below for the DCMotor and Stepper classes.
public MotorHat(int i2cAddr = 0x60) : base(i2cAddr)
Types
Enumerations
the PinState enum is used for setting PWM pins to a logic state of full off (LOW) or full on (HIGH)
public void SetPin(int pin, PinState state)
Methods
InitAsync
The InitAsync method takes a single argument which is the the PWM frequency for the PCA9685 chip. The method initializaes the PCA9685, sets the PWM frequency, and sets the PWM for all pins to 0.
public async Task InitAsync(uint freq)
SetPWM
Sets the PWM value for a specified pin. PWM values should be in the range 0 to 4096 inclusive.
public void SetPWM(int pin, ushort value)
SetPin
Sets the logic state for a specified pin. Logic states are either 0 (off) or 4096 (on). Uses the PinState enum. Values are LOW or HIGH.
public enum PinState { LOW, HIGH };
GetMotor
Returns an instance of the DCMotor class for the specified motor port. Motor port numbers are in the range 1 to 4, inclusive.
public DCMotor GetMotor(int index)
GetStepper
Returns an instance of the Stepper class for the specified stepper port. Stepper port numbers are in the range 1 to 2, inclusive.
Also takes the number of steps per revolution of the stepper attached to the port. The steps per revolution argument is ignored if GetStepper has already been called for the specified stepper port.
public Stepper GetStepper(ushort steps, int index)
DCMotor Class
The DCMotor class controls a single DC motor on one of the Motor Hat's 4 DC motor ports.
Types
The class has a single public enumeration, Command, which specifies the motion to be used by the motor: FORWARD, BACKWARD, and RELEASE.
RELEASE stops the motor.
public enum Command { FORWARD, BACKWARD, BRAKE, RELEASE };
Methods
SetSpeed
SetSpeed specifies the speed for the motor. Values range from 0 to 255, with 255 being the fastest. The value maps into a PWM value for the PCA9685, and is not an RPM value.
public void SetSpeed(uint speed)
Run
Run specifies the rotation of the motor, using the enum Command. the motor can be set to run FORWARD or BACKWARD. RELEASE stops the motor. the enum value BRAKE is not used and will be ignored.
public void Run(Command cmd)
Stepper Class
The Stepper class controls a single stepper motor on one of the Motor Hat's 2 stepper motor ports.
Types
The class has a two public enumerations, Command and Style.
Command declares values which specifiy the direction of motion to be used by the motor: FORWARD and BACKWARD.
Style declares values which specify the type of steps to be used by the motor. SINGLE, DOUBLE, INTERLEAVE, and MICROSTEP.
public enum Command { FORWARD, BACKWARD }; public enum Style { SINGLE, DOUBLE, INTERLEAVE, MICROSTEP };
Methods
SetSpeed
SetSpeed specifies the speed for the motor. Values are in RPM. Maximum RPM depends on the motor and the number of steps per revolution.
public void SetSpeed(uint rpm)
public void Release()
step
The step method specifies the number of steps to be taken, the direction of the steps, and the style of stepping. See the Command and Style enumeration types.
public void step(ushort steps, Command direction, Style style)
OneStep
The OneStep method advances the motor by one step in the specified direction and style of stepping. See the Command and Style enumeration types.
public int OneStep(Command direction, Style style)
Page last edited December 04, 2016
Text editor powered by tinymce.