Current meter without floats (fixed point)
Turn an ACS712's ADC reading into milliamps with one integer multiply and a shift, and time it against division and float.
Firmware · Intermediate
In a real product: Battery management system (Automotive): Current readings are converted in integer maths fast enough for protection.
An AVR has no floating-point hardware: every float operation is a library call, and the first one pulls about a kilobyte and a half of code into flash. Integer division is no rescue either: a 32-bit divide is also a library call, about as slow. The fixed-point way is to fold every constant into one scaled integer, then multiply and shift.
What you'll learn
- Folding a conversion's constants into a single factor, worked out once by hand
- Scaling that factor by 2^16 (Q16) so it is an integer, and shifting the product back down
- Checking the product cannot overflow its type
- Rounding by adding half before the shift
- Measuring the cost of each approach with
micros()
How it works
The ACS712 (5 A) puts out half the 5 V supply at zero current and 185 mV more for every amp, so
milliamps = (raw − 511.5) × 5000 / 1023 × 1000 / 185 = (2 × raw − 1023) × 13.2097…
13.2097 is not an integer, but 13.2097 × 65536 = 865 712 is. So the sketch multiplies 2 × raw − 1023 (at most ±1023) by 865 712 in int32_t, which stays under 2^31 (about 886 million at most), adds 2^15 to round, and shifts right by 16. That is one multiply and a shift: no division, no float, and the answer in whole milliamps, finer than one ADC step (about 26 mA) anyway.
The sketch does the same conversion three ways, 100 times each, and prints the time per conversion: the Q16 multiply, the textbook integer version with two 32-bit divisions, and float. On an Uno the Q16 version takes a few microseconds, the other two over 80: more than fifteen times as long.
Try this
- Click the ACS712 and change the current: all three columns agree to within a milliamp or two.
- Change the shift to 8 and the factor to 13.2097 × 256: coarser rounding, and a smaller product.
- Delete the float version and compare the sketch size in the build output.
sketch.ino
// An ACS712 (5 A) on A0, converted to milliamps three ways: a Q16 fixed-point multiply, integer division, and float,
// with the time each takes.
// (2 raw - 1023) x 5000 / 1023 x 1000 / 185 / 2 = (2 raw - 1023) x 13.2097..., and 13.2097 x 65536 = 865712.
const int32_t MA_PER_STEP_Q16 = 865712;
int32_t milliampsQ16(int raw) {
return ((int32_t)(2 * raw - 1023) * MA_PER_STEP_Q16 + (1L << 15)) >> 16;
}
int32_t milliampsDivide(int raw) {
int32_t mv = (int32_t)raw * 5000 / 1023;
return (mv - 2500) * 1000 / 185;
}
float milliampsFloat(int raw) {
return (raw * 5.0 / 1023 - 2.5) / 0.185 * 1000;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int raw = analogRead(A0);
volatile int input = raw; // volatile: the compiler cannot work the answer out once, outside the timing loops
volatile int32_t q16 = 0, divided = 0;
volatile float real = 0;
unsigned long t0 = micros();
for (int i = 0; i < 100; i++) q16 = milliampsQ16(input);
unsigned long t1 = micros();
for (int i = 0; i < 100; i++) divided = milliampsDivide(input);
unsigned long t2 = micros();
for (int i = 0; i < 100; i++) real = milliampsFloat(input);
unsigned long t3 = micros();
Serial.print("raw ");
Serial.print(raw);
Serial.print(" Q16 ");
Serial.print(q16);
Serial.print(" mA (");
Serial.print((t1 - t0) / 100.0, 1);
Serial.print(" us) divide ");
Serial.print(divided);
Serial.print(" mA (");
Serial.print((t2 - t1) / 100.0, 1);
Serial.print(" us) float ");
Serial.print(real, 0);
Serial.print(" mA (");
Serial.print((t3 - t2) / 100.0, 1);
Serial.println(" us)");
delay(500);
}