Sub topic 12
Reversing Direction and Ramping Speed Safely
Assume you know basic Arduino functions such as pinMode(), digitalWrite(), and analogWrite(). This chapter also assumes that your motor is connected through an H-bridge driver with two direction inputs and one PWM enable input.
Why an instant reversal is risky
A spinning motor has mechanical momentum. Its rotor and the attached mechanism keep moving after the control signal changes. If the H-bridge immediately applies reverse voltage, the driver tries to stop the motor and accelerate it in the opposite direction at the same time.
This can cause:
- A large current spike
- Mechanical shock to gears, wheels, or couplings
- Driver overheating
- Power-supply voltage drops
- Arduino resets
- Motor brush arcing or electrical noise
The current can be especially high when the motor is stopped. This condition is called stall current. During an abrupt reversal, the motor can briefly behave much like a stalled motor.
A safer sequence is:
- Reduce the commanded speed toward zero.
- Wait briefly if the mechanism needs time to settle.
- Increase speed in the opposite direction.
This process is called ramping. A speed ramp changes the command gradually instead of making a sudden jump.
Signed speed and the zero crossing
A convenient control model uses one signed number:
255means full forward.0means stopped.-255means full reverse.
The sign selects the direction. The absolute value selects the PWM duty cycle, which controls the average motor voltage.
When changing from positive to negative, the ramp must pass through zero. The following diagram shows the command changing from forward to reverse without an instantaneous sign change.
A ramping Arduino sketch
This complete example uses pins 7 and 8 for direction and pin 9 for PWM. The rampTo() function changes the current command by one step at a time.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // PWM-capable pin on an Arduino Uno
int currentSpeed = 0;
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
}
void setMotor(int speed) {
speed = constrain(speed, -255, 255);
if (speed > 0) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
} else if (speed < 0) {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
} else {
// Both inputs low usually selects coast on common drivers.
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
analogWrite(EN, abs(speed));
}
void rampTo(int targetSpeed, int stepDelayMs) {
targetSpeed = constrain(targetSpeed, -255, 255);
while (currentSpeed != targetSpeed) {
if (currentSpeed < targetSpeed) {
currentSpeed++;
} else {
currentSpeed--;
}
setMotor(currentSpeed);
delay(stepDelayMs);
}
}
void loop() {
rampTo(200, 5);
delay(1000);
rampTo(-200, 5);
delay(1000);
rampTo(0, 5);
delay(2000);
}The stepDelayMs value controls how quickly the command changes. A larger value creates a slower, gentler ramp. A smaller value makes the change faster. The best value depends on the motor, load, gearbox, and driver.
This example uses delay(), so the Arduino cannot perform other tasks during a ramp. A robot that must read sensors while moving should use a non-blocking ramp based on millis() instead.
Exercise
Exercise
Starting at currentSpeed = 200, what sequence does rampTo(-100, 5) command before reaching reverse speed?
Show answer
It decreases through positive values to zero, continues through negative values, and stops at -100. It never commands an instant change from +200 to -100.