Sub topic 2
Brake, coast, and decay modes
This chapter assumes you have wired an H-bridge driver to an Arduino, know how analogWrite sets PWM duty cycle, and understand that a driver's inputs select which motor terminal is tied to supply and which to ground. Check the datasheet for your module, because some drivers invert these rules.
Brake and coast
A driver has two ways to stop a motor:
- Coast disconnects both motor terminals from the supply. The motor spins down under friction and any load.
- Brake connects both motor terminals to the same rail. The motor's back-EMF (the voltage a spinning motor generates) drives current through the windings, which stops the shaft faster.
On many L298N and L293D style drivers, both direction inputs LOW gives coast, and both HIGH with the enable pin active gives brake.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // PWM-capable pin
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
}
void forward(uint8_t speed) {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EN, speed);
}
void brake() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, HIGH);
analogWrite(EN, 255);
}
void coast() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
analogWrite(EN, 0);
}
void loop() {
forward(180);
delay(2000);
brake();
delay(1000);
coast();
delay(1000);
}Controlling speed with PWM
PWM (pulse-width modulation) switches the supply on and off quickly. The duty cycle, the fraction of each cycle that is ON, sets the average voltage. A 50 percent duty cycle gives about half the speed.
During the OFF part of each cycle, the motor current must still flow. The driver handles this with a decay mode:
- Slow decay keeps the motor terminals shorted through the low-side switches during OFF time. Current falls slowly, so the motor runs smoothly.
- Fast decay reconnects the motor to the supply in reverse during OFF time. Current falls quickly, which gives more control at low speed but adds ripple and noise.
Choosing a driver
| Driver type | Switching element | Voltage drop | Typical use |
|---|---|---|---|
| Bipolar integrated (L298N, L293D) | Bipolar transistors | About 1.5 to 2.5 V | Low-cost, tolerant, but wastes power |
| MOSFET integrated (DRV8833) | MOSFETs | Low | Efficient at low voltage |
| Discrete H-bridge | Your own MOSFETs and gate driver | Depends on parts | High current, custom designs |
Check the motor's voltage, continuous current, and stall current against the driver's ratings. Leave margin, because stall current can be several times the running current.
Powering the motor and logic
Power the motor from its own supply and the Arduino from its own supply. Connect all grounds together at one point, so the motor and logic share a reference voltage.
Place a large electrolytic capacitor (100 µF or more, rated above the motor voltage) across the motor supply near the driver. Place a 0.1 µF ceramic capacitor across the logic supply near the driver's logic pins. These capacitors absorb the current spikes from switching and brush noise.
Exercise
A driver is set to brake while the motor runs at 200 PWM. Which input pattern should you use, and what enable value gives the strongest brake?
Show answer
Set both direction inputs HIGH and set the enable to 255 with analogWrite, which gives full enable.