Arduino map() Function Calculator
Compute map(value, fromLow, fromHigh, toLow, toHigh), truncation included.
map() result (integer, as Arduino computes it)
—
Exact (unrounded) value
—
Generated code
—
For reference and prototyping. Verify any safety- or design-critical value against the component datasheet or a second method before relying on it. Full disclaimer.
About this tool
Evaluate Arduino's map(value, fromLow, fromHigh, toLow, toHigh) for any five values, and see both the integer result your sketch will actually get and the exact unrounded value — so you can check a call before flashing rather than after.
What map() does. It's a linear re-scale: work out where value sits as a fraction of the input range fromLow…fromHigh, then place it at the same fraction of the output range toLow…toHigh. The formula is:
(value − fromLow) × (toHigh − toLow) ÷ (fromHigh − fromLow) + toLow
The classic use is turning a 0–1023 analogRead() into a 0–255 PWM value, a 0–180 servo angle, or a 0–100 percentage.
It truncates, it doesn't round. Every operation is integer (long) arithmetic, and the division truncates toward zero. map(20, 0, 100, 0, 3) is (20×3)÷100 = 60÷100 = 0, not 1. A mathematically exact 127.8 becomes 127. Across a sweep this means the output steps unevenly and only reaches the top of its range at the very last input value: mapping 0–1023 to 0–100 gives 100 only at exactly 1023.
It doesn't clamp. If value is outside fromLow…fromHigh, the result goes outside toLow…toHigh too — which can drive a servo past its limit or overflow a byte. Wrap it: constrain(map(x, ...), toLow, toHigh).
Reversed ranges are allowed. map(x, 0, 1023, 255, 0) inverts the reading — useful for a sensor wired backwards. A zero-width input range (fromLow == fromHigh) divides by zero and is undefined.
Overflow. The intermediate product (value - fromLow) * (toHigh - toLow) is a 32-bit long. With large ranges (mapping a microsecond count in the millions, say) it can overflow — scale down first or use 64-bit maths.
When to use floats instead. If you need rounding, a smooth fractional result, or a non-linear sensor curve, map() is the wrong tool — do the maths in float, or use a lookup table for non-linear inputs.
The Arduino map() guide covers each of these traps with examples. To convert an ADC count to an actual voltage, use the analog voltage calculator; for servo pulse timing, the servo calculator.
Frequently asked questions
- Why is the integer result different from the exact value?
- Arduino's
map()uses integer arithmetic and truncates toward zero rather than rounding — an exact 127.8 becomes 127. This tool shows both so you know what your sketch will get. - Does map() constrain the output to the target range?
- No. If
valueis outside the input range, the result can fall outside the output range. Wrap the call inconstrain()to clamp it. - Can fromLow be greater than fromHigh?
- Yes — reversing a range this way inverts the value (e.g. 0–1023 to 255–0). The calculator handles it.
- Why doesn't my mapped output ever reach the maximum?
- Truncation. Mapping 0–1023 to 0–100 only returns 100 at exactly 1023; 1013 already gives 99. Add a little to the input range, or round in floating point.
- My result is a huge negative number — what happened?
- Likely a 32-bit overflow in the intermediate multiplication because the ranges are very large. Scale the numbers down or compute in
int64_t. - Is map() suitable for a thermistor or log-taper pot?
- No. Those aren't linear, so a straight-line map is wrong across the range. Use the component's equation or a lookup table.