Skip to Content
Circuitera

Arduino map() extrapolates past its input range

See why Arduino map() can return values outside its output range and how to clamp sensor samples before scaling.

The input bounds define a line

map(value, fromLow, fromHigh, toLow, toHigh) performs a linear integer conversion. An input of 900 on the line from 200–800 to 0–100 gives 116. Bounds are calibration endpoints. Values beyond them continue along the same line. Reversed output bounds can be useful for an inverted control.

Limit values before mapping

Clip a raw sample separately if the receiving UI expects a bounded percentage. Check that your input endpoints differ before mapping. On AVR, map() uses signed long integer arithmetic. Fractions truncate, and very large ranges can overflow the intermediate multiplication. Choose an explicitly wider or floating-point calculation when required.

Open the exact Uno example

The sketch prints 116 and then 100. No sensor is required.

void setup() {
  Serial.begin(9600);
  const long sample = 900;
  Serial.println(map(sample, 200, 800, 0, 100));
  const long clipped = constrain(sample, 200, 800);
  Serial.println(map(clipped, 200, 800, 0, 100));
}
void loop() {}

Verified with [email protected] on Uno R3 (2026-12-05 UTC). This check did not use physical hardware.

Continue learning

Sources