A drop-in replacement for the Adafruit Motor Shield V1 library that works with Arduino R4 WiFi and other non-AVR boards.
This library provides full compatibility with the original AFMotor library API while supporting modern Arduino boards including the Arduino R4 WiFi. It maintains the same function calls and behavior as the original library, making it a true drop-in replacement.
- Open the Arduino IDE
- Go to Tools → Manage Libraries
- Search for "AFMotor R4 Compatible"
- Click Install
- Download the library
- Extract to your Arduino libraries folder
- Restart the Arduino IDE
#include "AFMotor_R4.h"
AF_DCMotor motor(1); // Create motor on M1
void setup() {
motor.setSpeed(200); // Set speed (0-255)
motor.run(FORWARD); // Start motor
}
void loop() {
// Motor control code
}#include "AFMotor_R4.h"
AF_Stepper stepper(200, 1); // 200 steps/rev, use M1&M2
void setup() {
stepper.setSpeed(10); // 10 RPM
}
void loop() {
stepper.step(100, FORWARD, SINGLE); // 100 steps forward
}The library includes several examples:
- BasicMotorTest - Simple DC motor control demonstration
- StepperTest - Basic stepper motor functionality
- CompleteExample - Comprehensive demo of all features
AF_DCMotor(uint8_t motor_num)- Constructor (motor 1-4)void setSpeed(uint8_t speed)- Set motor speed (0-255)void run(uint8_t direction)- Control motor (FORWARD, BACKWARD, BRAKE, RELEASE)
AF_Stepper(uint16_t steps_per_rev, uint8_t motor_num)- Constructorvoid setSpeed(uint16_t rpm)- Set stepper speed in RPMvoid step(uint16_t steps, uint8_t direction, uint8_t style)- Move stepperuint8_t onestep(uint8_t direction, uint8_t style)- Single stepvoid release()- Release stepper motor
This library uses direct port manipulation optimized for the Arduino R4 (Wifi) while maintaining compatibility with other Arduino boards. The original AFMotor library relied on AVR-specific code that doesn't work on ARM-based boards like the R4.
Contributions are welcome! Please submit issues and pull requests on GitHub.
Based on the original Adafruit Motor Shield library. Adapted for Arduino R4 compatibility by Mia Müßig and Julian Hein.
Original AFMotor library: https://github.com/adafruit/Adafruit-Motor-Shield-library