Drive a motor with PWM
A simple Arduino example of variable motor speed. Check your driver and power wiring before use.
// Example pins for a motor driver
const int enablePin = 9;
const int directionPin = 8;
void setup() {
pinMode(enablePin, OUTPUT);
pinMode(directionPin, OUTPUT);
}
void loop() {
digitalWrite(directionPin, HIGH);
analogWrite(enablePin, 160); // speed: 0–255
delay(1000);
analogWrite(enablePin, 0);
delay(1000);
}