Sub topic 14
Sensing Current and Detecting Stalls
Assume you know basic Arduino programming, Ohm’s law, and how to connect an H-bridge driver. This chapter uses an analog current sensor between the motor supply and driver. The sensor produces a voltage that changes with motor current.
Why measure motor current?
A brushed DC motor draws more current as its load increases. When the shaft cannot turn, the motor reaches stall. At stall, back-EMF disappears, so the winding can draw a large current limited mainly by its resistance and the driver.
High stall current can:
- Overheat the motor and driver.
- Cause the Arduino supply voltage to fall.
- Reset the Arduino.
- Damage gears or mechanical parts.
- Indicate that the motor is blocked.
A current reading can therefore detect a likely stall. Current alone cannot prove that the shaft stopped. A heavy load, rapid acceleration, or a short mechanical movement can also produce a current spike. An encoder gives stronger stall detection because it measures whether the shaft is moving.
Choosing a current-sensing method
| Method | What it measures | Main trade-off |
|---|---|---|
| Hall-effect sensor | Current through a conductor using magnetic coupling | Easy isolation, but offset and noise need calibration |
| Low-side shunt | Voltage across a small resistor near ground | Simple and accurate, but changes the ground path |
| High-side shunt | Voltage across a resistor in the motor supply | Better system grounding, but needs a special amplifier |
A Hall-effect module often has an output near half the supply voltage when current is zero. For example, an ACS712-5A commonly changes by about 185 millivolts per ampere. Check the exact sensor data sheet before using that value.
Build a stall detector
The detector should not react to one noisy sample. Instead, it should:
- Ignore current when the motor command is zero.
- Sample current repeatedly.
- Compare the reading with a threshold.
- Require the threshold to remain exceeded for a short time.
- Stop the motor if the condition continues.
The threshold must be below the driver’s safe continuous current and above the normal current needed by the mechanism. Measure the motor under its heaviest expected normal load before selecting it.
Arduino example
This sketch assumes a 5 V sensor with 2.5 V output at zero current and sensitivity of 185 millivolts per ampere. It averages eight readings and stops the motor after high current lasts 150 milliseconds.
The Arduino ground, sensor ground, and driver logic ground must be connected together. The motor supply current must pass through the sensor, but the motor must not draw current through an Arduino pin.
const uint8_t IN1 = 7;
const uint8_t IN2 = 8;
const uint8_t EN = 9;
const uint8_t CURRENT_PIN = A0;
const float ADC_REFERENCE = 5.0;
const float ZERO_CURRENT_VOLTAGE = 2.50;
const float SENSOR_SENSITIVITY = 0.185; // volts per ampere
const float STALL_CURRENT = 1.8; // adjust for your motor and driver
const unsigned long STALL_TIME_MS = 150;
unsigned long highCurrentSince = 0;
bool stallDetected = false;
float readCurrent() {
long total = 0;
for (int i = 0; i < 8; i++) {
total += analogRead(CURRENT_PIN);
}
float averageAdc = total / 8.0;
float voltage = averageAdc * ADC_REFERENCE / 1023.0;
return abs(voltage - ZERO_CURRENT_VOLTAGE) / SENSOR_SENSITIVITY;
}
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));
}
void setup() {
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(EN, OUTPUT);
Serial.begin(115200);
}
void loop() {
int command = 180;
setMotor(stallDetected ? 0 : command);
float current = readCurrent();
Serial.println(current);
if (command != 0 && current >= STALL_CURRENT) {
if (highCurrentSince == 0) {
highCurrentSince = millis();
} else if (millis() - highCurrentSince >= STALL_TIME_MS) {
stallDetected = true;
setMotor(0);
}
} else {
highCurrentSince = 0;
}
delay(10);
}Exercise
The motor normally draws 0.6 A but draws 2.4 A for 40 milliseconds whenever it starts. Why might a 2.0 A threshold with a 150 millisecond delay avoid a false stall report?
Show answer
The detector requires high current to continue for 150 milliseconds, so the short startup spike ends before the stall condition is declared.