# AFMotor Library Reference

## AF_DCMotor Class

![](https://cdn-learn.adafruit.com/assets/assets/000/001/574/medium800/adafruit_products_DC_Motors.jpg?1396773984)

The AF\_DCMotor class provides speed and direction control for up to four DC motors when used with the Adafruit Motor Shield.&nbsp; To use this in a sketch you must first add the following line at the beginning of your sketch:

```auto
#include <AFMotor.h>

```

# **AF\_DCMotor _motorname_(_portnum_, _freq_)**

> This is the constructor for a DC motor.&nbsp; Call this constructor once for each motor in your sketch.&nbsp; Each motor instance must have a different name as in the example below.

**Parameters:**   

- **port num** - selects which channel (1-4) of the motor controller the motor will be connected to
- **freq** - selects the PWM frequency.&nbsp; If no frequency is specified, 1KHz is used by default.

> > Frequencies for channel 1 & 2 are:   
> > 
> > - MOTOR12\_64KHZ
> > - MOTOR12\_8KHZ 
> > - MOTOR12\_2KHZ 
> > - MOTOR12\_1KHZ 
> > 
> > Frequencies for channel 3 & 4 are:   
> > 
> > - MOTOR34\_64KHZ 
> > - MOTOR34\_8KHZ 
> > - MOTOR34\_1KHZ

**Example:**  
```auto
AF_DCMotor motor4(4); // define motor on channel 4 with 1KHz default PWM
AF_DCMotor left_motor(1, MOTOR12_64KHZ);  // define motor on channel 1 with 64KHz PWM
```

![](https://cdn-learn.adafruit.com/assets/assets/000/001/577/medium800/adafruit_products_DC_Motor_Ports.gif?1447864294)

_ **Note:** &nbsp; Higher frequencies will produce less audible hum in operation, but may result in lower torque with some motors._  
# **setSpeed(_speed_)**

> Sets the speed of the motor.

**Parameters:**  

- &nbsp; **speed** - Valid values for 'speed' are between 0 and 255 with 0 being off and 255 as full throttle.

**Example:**  
```auto
  motor1.setSpeed(255);  // Set motor 1 to maximum speed
  motor4.setSpeed(127);  // Set motor 4 to half speed 
```

_ **Note** _ **:** _DC Motor response is not typically linear, and so the actual RPM will not necessarily be proportional to the programmed speed._  
# **run(_cmd_)**

> Sets the run-mode of the motor.

**Parameters:**  

- **cmd** - the desired run mode for the motor  

> Valid values for cmd are:  
> 
> - **FORWARD** - run forward (actual direction of rotation will depend on motor wiring)
> - **BACKWARD** - run backwards (rotation will be in the opposite direction from FORWARD)
> - **RELEASE** - Stop the motor.&nbsp; This removes power from the motor and is equivalent to setSpeed(0).&nbsp; The motor shield does not implement dynamic breaking, so the motor may take some time to spin down

  
**Example:** ```auto
motor.run(FORWARD);
delay(1000);  // run forward for 1 second
motor.run(RELEASE);
delay(100);  // 'coast' for 1/10 second
motor.run(BACKWARDS);  // run in reverse
```

# AFMotor Library Reference

## AF_Stepper Class

![](https://cdn-learn.adafruit.com/assets/assets/000/001/575/medium800/adafruit_products_Stepper_Motors.jpg?1396773989)

The AF\_Stepper class provides single and multi-step control for up to 2 stepper motors when used with the Adafruit Motor Shield.&nbsp; To use this in a sketch you must first add the following line at the beginning of your sketch:

```auto
#include <AFMotor.h>
```

# AF\_Stepper _steppername_(_steps_, _portnumber_)
The AF\_Stepper constructor defines a stepper motor.&nbsp; Call this once for each stepper motor in your sketch.&nbsp; Each stepper motor instance must have a unique name as in the example below.  
  
**Parameters:**  

- **steps** - declare the number of steps per revolution for your motor.
- **num** - declare how the motor will be wired to the shield.&nbsp; 

> Valid values for 'num' are 1 (channels 1 & 2) and 2 (channels 3 & 4).

**Example:**  
![](https://cdn-learn.adafruit.com/assets/assets/000/001/570/medium800/adafruit_products_Stepper_Ports.gif?1447864293)

```auto
AF_Stepper Stepper1(48, 1);  // A 48-step-per-revolution motor on channels 1 & 2
AF_Stepper Stepper2(200, 2);   // A 200-step-per-revolution motor on channels 3 & 4
```

# step(_steps, direction, style_)
Step the motor.  
  
**Parameters:**  

- **steps** - the number of steps to turn
- **direction** - the direction of rotation ( **FORWARD** or **BACKWARD** )
- **style** - the style of stepping:

> Valid values for 'style' are:

> - **SINGLE** - One coil is energized at a time.
> - **DOUBLE** - Two coils are energized at a time for more torque.
> - **INTERLEAVE** - Alternate between single and double to create a half-step in between.&nbsp; This can result in smoother operation, but because of the extra half-step, the speed is reduced by half too.
> - **MICROSTEP** - Adjacent coils are ramped up and down to create a number of 'micro-steps' between each full step.&nbsp; This results in finer resolution and smoother rotation, but with a loss in torque.

_ **Note:** Step is a synchronous command and will not return until all steps have completed.&nbsp; For concurrent motion of two motors, you must handle the step timing for both motors and use the "onestep()" function below._  
  
**Example:**  
  
  
```auto
Stepper1.step(100, FORWARD, DOUBLE); // 100 steps forward using double coil stepping
Stepper2.step(100, BACKWARD, MICROSTEP);   // 100 steps backward using double microstepping
```

# **setSpeed(_RPMspeed_)**
set the speed of the motor  
  
**Parameters:**  

- Speed - the speed in RPM

  
_ **Note:** The resulting step speed is based on the 'steps' parameter in the constructor.&nbsp; If this does not match the number of steps for your motor, you actual speed will be off as well._  
  
**Example:**  
```auto
Stepper1.setSpeed(10);  // Set motor 1 speed to 10 rpm  
Stepper2.setSpeed(30);  // Set motor 2 speed to 30 rpm  
```

# onestep(_direction, stepstyle_)
Single step the motor.  
  
**Parameters:**  

- **direction** - the direction of rotation ( **FORWARD** or **BACKWARD** )
- **stepstyle** - the style of stepping:

> Valid values for 'style' are:

> - **SINGLE** - One coil is energized at a time.
> - **DOUBLE** - Two coils are energized at a time for more torque.
> - **INTERLEAVE** - Alternate between single and double to create a half-step in between.&nbsp; This can result in smoother operation, but because of the extra half-step, the speed is reduced by half too.
> - **MICROSTEP** - Adjacent coils are ramped up and down to create a number of 'micro-steps' between each full step.&nbsp; This results in finer resolution and smoother rotation, but with a loss in torque.

**Example:**  
```auto
Stepper1.onestep(FORWARD, DOUBLE);  // take one step forward using double coil stepping
```

# release()
Release the holding torque on the motor.&nbsp; This reduces heating and current demand, but the motor will not actively resist rotation.  
  
**Example:**  
```auto
Stepper1.release(); // stop rotation and turn off holding torque.
```


## Related Guides

- [Adafruit INA3221 Breakout](https://learn.adafruit.com/adafruit-ina3221-breakout.md)
- [Adafruit Hallowing M4](https://learn.adafruit.com/adafruit-hallowing-m4.md)
- [Adafruit 20W Stereo Audio Amplifier - MAX9744](https://learn.adafruit.com/adafruit-20w-stereo-audio-amplifier-class-d-max9744.md)
- [Adafruit A4988 Stepper Motor Driver Breakout Board](https://learn.adafruit.com/adafruit-a4988-stepper-motor-driver-breakout-board.md)
- [Adafruit ESP32-S3 Reverse TFT Feather](https://learn.adafruit.com/esp32-s3-reverse-tft-feather.md)
- [Dune Worm Thumper](https://learn.adafruit.com/dune-worm-thumper.md)
- [WiFi Music Alert Box ](https://learn.adafruit.com/wifi-music-alert-box.md)
- [Adafruit Audio BFF](https://learn.adafruit.com/adafruit-audio-bff.md)
- [Adafruit PiTFT 3.5" Touch Screen for Raspberry Pi](https://learn.adafruit.com/adafruit-pitft-3-dot-5-touch-screen-for-raspberry-pi.md)
- [CircuitPython BLE Crickit Rover](https://learn.adafruit.com/circuitpython-ble-crickit-rover.md)
- [Esenciales para CircuitPython](https://learn.adafruit.com/esenciales-para-circuitpython.md)
- [Adafruit PCF8591 Basic 4 x ADC + DAC Breakout](https://learn.adafruit.com/adafruit-pcf8591-adc-dac.md)
- [Trainable Robotic Arm](https://learn.adafruit.com/trainable-robotic-arm.md)
- [Fair Weather Friend: Internet-Connected Migraine or Allergies Detector](https://learn.adafruit.com/fair-weather-friend-internet-connected-health-and-leisure-forecaster.md)
- [Cup o' Sound](https://learn.adafruit.com/cup-o-sound.md)
