What is map()?
map() re-scales a number from one range to another. It's one of the most useful Arduino functions.
long map(long value, long fromLow, long fromHigh, long toLow, long toHigh)
How It Works
map(value, fromLow, fromHigh, toLow, toHigh)
Translates value from the range [fromLow, fromHigh] to [toLow, toHigh].
Common Examples
Potentiometer → Servo angle
int potValue = analogRead(A0); // 0-1023
int angle = map(potValue, 0, 1023, 0, 180); // 0-180
Potentiometer → LED brightness
int potValue = analogRead(A0); // 0-1023
int brightness = map(potValue, 0, 1023, 0, 255); // 0-255
Sensor → Display percentage
int raw = analogRead(A0); // 0-1023
int percent = map(raw, 0, 1023, 0, 100); // 0-100
Inverted mapping
// Reverse: more light = lower value
int brightness = map(lightLevel, 0, 1023, 255, 0);
Important Notes
map()uses integer math, so no decimal results- Values outside the input range get extrapolated (not clamped)
- Use
constrain()if you need to clamp the result:
int value = constrain(map(raw, 200, 800, 0, 255), 0, 255);