Sub topic 14
Sensing Current and Detecting Stalls
This chapter assumes you can wire a driver to an Arduino and run the setMotor function from the earlier chapters. You should also know Ohm's law, and that analogRead returns 0 to 1023 on a 5 V Uno. Here you will measure motor current, turn the reading into amps, and use it to detect a stalled motor.
Why current tells you what the motor is doing
A brushed DC motor draws current in proportion to its torque. A motor spinning freely produces back-EMF that opposes the supply, so it draws little current. A motor held still produces no back-EMF, so the current is limited only by winding resistance. That is the stall current, often 5 to 10 times the free-running current.
This gives you a signal without adding a sensor to the shaft:
- Low current at a given duty cycle means the motor is unloaded or disconnected.
- Moderate current means normal work.
- Current near the stall value means the shaft is blocked or the load is too heavy.
A sustained stall overheats the windings and the driver, and it can damage gearboxes. Detecting it lets you cut power before that happens. It is also how simple robots notice they have hit a wall.
Ways to measure current
| Method | How it works | Strengths | Weaknesses |
|---|---|---|---|
| Low-side shunt | A small resistor between the driver's ground or sense pin and ground. You measure the voltage across it. | Cheap, and the Arduino can read it directly. | Lifts the driver's ground slightly. Sees current only during part of the PWM cycle. |
| High-side shunt | A resistor in the motor supply line, read by a current-sense amplifier. | Does not disturb ground. Can see the whole cycle. | Needs an amplifier IC or a module such as an INA219. |
| Driver current-sense output | The driver has a pin that outputs a voltage or current proportional to motor current. | No extra parts. | Scaling differs per driver. Check the datasheet. |
Many driver boards already provide a sense output. Pololu's VNH5019 carrier, for example, has a CS pin with a documented scale factor. The common L298N module ties its SENSE pins to ground through a jumper. If you want to measure current, remove the jumper and place a shunt there. The rest of this chapter uses a low-side shunt because it needs only one resistor.
Choosing and reading a shunt
The voltage across the shunt is V = I × R. You want a voltage large enough to read but a resistance small enough to waste little power.
Take a motor with a 1 A stall current and a 0.5 Ω shunt:
- Voltage at stall: 1 A × 0.5 Ω = 0.5 V.
- Power in the shunt at stall: I² × R = 1 × 0.5 = 0.5 W. Use a resistor rated for at least double that.
- Arduino resolution: 5 V / 1023 ≈ 4.9 mV per step, which is about 9.8 mA per step.
Voltage across the shunt also appears as a rise in the driver's ground. At 0.5 V that costs you 0.5 V of motor voltage and shifts logic thresholds a little. Larger shunts read better but waste more voltage and power. Smaller shunts need an amplifier to be readable. Choose the smallest value that gives a usable reading at the currents you care about.
Reading the signal correctly with PWM
The motor current is not steady when you drive it with PWM. A low-side shunt may see current only while the bridge is on, and the inductance of the motor smooths it into a ripple. A single analogRead can land on the high or low part of the cycle and give an unstable number.
Two fixes are common:
- Average many samples. At the default 490 Hz, one PWM period is about 2 ms. An
analogReadtakes about 112 µs on an ATmega328P, so 64 samples take about 7 ms and span several periods. - Add an RC low-pass filter between the shunt and the analog pin, for example 10 kΩ and 1 µF, which gives a time constant of 10 ms.
Because the shunt may see current only during the on-time, the averaged reading falls as duty cycle falls. A stall at low duty gives a smaller average than a stall at full duty. Two ways to handle this:
- Only check for stalls above a minimum duty, such as 150 out of 255.
- Scale the threshold with duty.
The example below uses the first approach, which is simpler. Treat your thresholds as values to calibrate on your own motor, not as universal numbers.
Ignoring the start-up surge
When the motor starts, or when you change speed, current briefly spikes. The shaft is still, so there is no back-EMF until it accelerates. If you compare current to the stall threshold during this period, every start looks like a stall.
The standard fix is blanking: ignore the current for a short time after any speed change, typically 100 to 500 ms depending on the motor's inertia. You then require the current to stay above the limit for a hold time before declaring a stall. A brief bump from a rough spot on the track will not trip it.
A complete example
This sketch drives a motor forward, measures current through a 0.5 Ω shunt on A0, and stops the motor if it stalls. After 5 seconds it clears the fault and tries again.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9; // PWM-capable pin
const uint8_t SENSE_PIN = A0; // voltage across the shunt
const float SHUNT_OHMS = 0.5;
const float VREF = 5.0;
const int STALL_LIMIT_MA = 800;
const unsigned long BLANKING_MS = 300;
const unsigned long STALL_HOLD_MS = 150;
const unsigned long RETRY_MS = 5000;
const int RUN_SPEED = 200;
unsigned long lastChangeMs = 0;
unsigned long overSinceMs = 0;
unsigned long trippedAtMs = 0;
bool overLimit = false;
bool tripped = false;
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 {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
analogWrite(EN, abs(speed));
lastChangeMs = millis();
}
float readCurrentMa() {
long sum = 0;
for (int i = 0; i < 64; i++) {
sum += analogRead(SENSE_PIN);
}
float volts = (sum / 64.0) * VREF / 1023.0;
return volts / SHUNT_OHMS * 1000.0;
}
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
Serial.begin(115200);
setMotor(RUN_SPEED);
}
void loop() {
unsigned long now = millis();
if (tripped) {
if (now - trippedAtMs >= RETRY_MS) {
tripped = false;
overLimit = false;
setMotor(RUN_SPEED);
Serial.println("Retrying");
}
return;
}
float ma = readCurrentMa();
if (now - lastChangeMs < BLANKING_MS) {
return; // ignore the start-up surge
}
if (ma > STALL_LIMIT_MA) {
if (!overLimit) {
overLimit = true;
overSinceMs = now;
} else if (now - overSinceMs >= STALL_HOLD_MS) {
setMotor(0);
tripped = true;
trippedAtMs = now;
Serial.print("Stall detected at ");
Serial.print(ma);
Serial.println(" mA");
}
} else {
overLimit = false;
}
}To calibrate, print ma while the motor runs freely, then while you hold the shaft by hand. Set STALL_LIMIT_MA between the two, closer to the stall reading. Repeat at the duty cycles you plan to use.
Edge cases and limits
- Heavy loads look like stalls. A motor lifting a load near its limit can draw close to stall current. The detector cannot tell the difference, so set the limit above your worst legitimate load.
- Supply voltage matters. Stall current scales with supply voltage. A threshold set on a fresh battery is too high when the battery sags, and a stall can go undetected.
- Electrical noise. Brushes create sparks and voltage spikes that appear on the sense line. Averaging and an RC filter help. Keep sense wiring short and away from motor leads.
- Driver current limits are separate. Some driver ICs limit current in hardware. Your software detector reacts more slowly than that protection, so it complements the hardware limit and does not replace it.
- Reverse and braking. In brake mode, current flows through the motor and the bridge in ways the shunt may not see. Test your sensing in every mode you use.
Exercise
You use a 0.25 Ω shunt and the motor's stall current is 2 A. What is the voltage across the shunt at stall, and how much power does the shunt dissipate? Would you use a 0.25 W resistor?
Show answer
The voltage is 2 A × 0.25 Ω = 0.5 V. The power is I² × R = 4 × 0.25 = 1 W. A 0.25 W resistor would be overloaded at stall, so use one rated for at least 2 W to leave margin.
Exercise
Your stall detector trips every time the motor starts, even though the shaft turns freely. What is the cause, and which two parameters in the sketch would you adjust?
Show answer
The start-up current surge exceeds the limit before back-EMF builds up. Increase BLANKINGMS so the surge is ignored, and if needed increase STALLHOLD_MS so a short spike cannot trigger the fault.
Exercise
A stall is detected correctly at full speed but missed at a duty of 100 out of 255 when using a low-side shunt. Give a likely reason and one fix.
Show answer
The shunt sees current mainly during the PWM on-time, so the averaged reading falls with duty and stays under the fixed threshold. Either only run the check above a minimum duty or scale the threshold with the duty cycle.