map()

Description

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc.

Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The

constrain()
function may be used either before or after this function, if limits to the ranges are desired.

Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds", so the

map()
function may be used to reverse a range of numbers, for example

y = map(x, 1, 50, 50, 1);

The function also handles negative numbers well, so that the following example is also valid and works well.

y = map(x, 1, 50, 50, -100);

The

map()
function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.

Syntax

Use the following function to re-map a variable range to another:

map(value, fromLow, fromHigh, toLow, toHigh)

Parameters

The function admits the following parameters:

  • value
    : the input variable with the number to map.
  • fromLow
    : the lower bound of the value’s current range.
  • fromHigh
    : the upper bound of the value’s current range.
  • toLow
    : the lower bound of the value’s target range.
  • toHigh
    : the upper bound of the value’s target range.

Returns

The mapped value. Data type:

long
.

Example Code

Maps a 10-bit (0-1023) analog input value to 8-bit (0-255) value and controls pin 9 PWM output. The input and output values are printed in the Serial Monitor.

1/* Map an analog value to 8 bits (0 to 255) */
2void setup() {
3 Serial.begin(9600);
4}
5
6void loop() {
7 int val = analogRead(0);
8
9 Serial.print("Input value: ");
10 Serial.print(val);
11
12 val = map(val, 0, 1023, 0, 255); // mapping function
13
14 Serial.print(", Output value: ");
15 Serial.println(val);
16
17 analogWrite(9, val); // PWM output
18 delay(200);
19}

If you connect a potentiometer to the analog pin (A0) and an LED to pin 9, you can control its brightness by turning the potentiometer.

Appendix

For the mathematically inclined, here’s the whole function.

1long map(long x, long in_min, long in_max, long out_min, long out_max) {
2 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
3}

Notes & Warnings

As previously mentioned, the map() function uses integer math. So fractions might get suppressed due to this. For example, fractions like 3/2, 4/3, 5/4 will all be returned as 1 from the map() function, despite their different actual values. So if your project requires precise calculations (e.g. voltage accurate to 3 decimal places), please consider avoiding map() and implementing the calculations manually in your code yourself.

See also

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.