The Arduino map() function is a versatile and widely used tool for scaling numbers from one range to another. Whether you're a beginner or an experienced Arduino enthusiast, understanding how this function works and how to use it effectively is crucial for optimizing your projects. In this blog, we’ll cover everything from what the map() function does to practical examples and advanced tips for its use.

If this sounds interesting to you and you'd like to request a demo or learn more, please contact sales.

Contact Sales

The Arduino map() function is a versatile and widely used tool for scaling numbers from one range to another. Whether you're a beginner or an experienced Arduino enthusiast, understanding how this function works and how to use it effectively is crucial for optimizing your projects. In this blog, we’ll cover everything from what the map() function does to practical examples and advanced tips for its use.

What is the Arduino map() Function?

At its core, the map() function takes an input number within a specific range and maps it to an output number within a different range. This is especially useful when working with sensors, where raw data needs to be converted into meaningful values like temperature, distance, or percentage.

The syntax for the map() function is straightforward:

long map(long x, long in_min, long in_max, long out_min, long out_max);
  • x: The input value to be mapped.
  • in_min: The lower bound of the input range.
  • in_max: The upper bound of the input range.
  • out_min: The lower bound of the output range.
  • out_max: The upper bound of the output range.

The function calculates the mapped value using this formula:

(x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

Why Use the map() Function?

In Arduino projects, raw sensor data often needs to be scaled for meaningful interaction. For example:

  • Analog Sensors: Converting a sensor's 0-1023 output to a 0-5V range.
  • Servo Motors: Mapping input from a joystick to servo angles.
  • LED Brightness: Scaling values for pulse-width modulation (PWM).

Using the map() function simplifies your code and reduces errors that can arise from manually calculating scaled values.

Basic Example: Reading a Potentiometer

A common use case for the map() function is reading the input from a potentiometer and converting it to a different range. Here’s an example:

const int potPin = A0; // Pin connected to the potentiometer
int potValue;          // Variable to store raw potentiometer value
int mappedValue;       // Variable to store the mapped value

void setup() {
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  potValue = analogRead(potPin); // Read the potentiometer value (0-1023)
  mappedValue = map(potValue, 0, 1023, 0, 100); // Map it to a percentage (0-100)

  Serial.print("Potentiometer Value: ");
  Serial.print(potValue);
  Serial.print(" | Mapped Value: ");
  Serial.println(mappedValue);

  delay(500); // Small delay for readability
}

This code reads the raw analog value from a potentiometer and maps it to a percentage (0-100%). This is ideal for applications where you want to control brightness, volume, or other scaled parameters.

Practical Applications of the map() Function

1. Controlling Servo Motors

Servo motors usually operate within a range of 0 to 180 degrees. If you're using a joystick with an analog output, you can map its range (0-1023) to match the servo's range:

#include <servo.h>

Servo myServo;
const int joystickPin = A0;

void setup() {
  myServo.attach(9); // Attach servo to pin 9
}

void loop() {
  int joystickValue = analogRead(joystickPin); // Read joystick value
  int servoAngle = map(joystickValue, 0, 1023, 0, 180); // Map to servo range

  myServo.write(servoAngle); // Move the servo
  delay(15); // Allow the servo to reach the position
}

2. LED Brightness Control

The map() function can be used to adjust the brightness of an LED using PWM. Here’s an example:

const int potPin = A0;
const int ledPin = 9;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int potValue = analogRead(potPin); // Read potentiometer
  int brightness = map(potValue, 0, 1023, 0, 255); // Map to PWM range

  analogWrite(ledPin, brightness); // Set LED brightness
}

Advanced Tips for Using the map() Function

Handle Out-of-Range Values: The map() function does not automatically constrain input values to the defined range. For safety, you can use the constrain() function:

int constrainedValue = constrain(x, in_min, in_max);
int mappedValue = map(constrainedValue, in_min, in_max, out_min, out_max);

Floating-Point Mapping: The map() function only works with integers. For floating-point precision, you can implement a custom version:

float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Inverse Mapping: You can reverse the input and output ranges to invert the mapping. For example, map 0-1023 to 255-0 for inverting brightness:

int invertedValue = map(inputValue, 0, 1023, 255, 0);

Common Mistakes When Using map()

  1. Ignoring Range Mismatches: Ensure the input value falls within the specified input range, or you might get unexpected results.
  2. Using map() for Nonlinear Scaling: The map() function only provides linear scaling. For exponential or logarithmic scaling, you need custom formulas.
  3. Forgetting Units: Always confirm that the input and output ranges use consistent units (e.g., voltage, degrees, percentage).

Conclusion

The Arduino map() function is a simple yet powerful tool that enhances the flexibility and functionality of your projects. From controlling servos to scaling sensor data, its applications are vast and varied. By mastering its use and understanding its limitations, you’ll be well-equipped to handle a wide range of Arduino projects.

Profile avatar of the blog author

Jharwin Barrozo

Jharwin is an electronics engineer mainly focused on satellites. He built his own ground station using Flux to monitor RF activities on the International Space Station. Find him on Flux @jharwinbarrozo

Go 10x faster from idea to PCB
Flux is an all-in-one EDA. Use re-usable blocks, scripting, and a library you don’t have to manage to dramatically reduce the time it takes to go from idea to prototype.
Illustration of sub-layout. Several groups of parts and traces hover above a layout.
Illustration of sub-layout. Several groups of parts and traces hover above a layout.
Flux is a better way to build PCBs
Go 10x faster from idea to PCB by reducing busy work, never starting from scratch, and keeping your team in sync. All from the browser.
Screenshot of the Flux app showing a PCB in 3D mode with collaborative cursors, a comment thread pinned on the canvas, and live pricing and availability for a part on the board.
Flux is a better way to build PCBs
Go 10x faster from idea to PCB by reducing busy work, never starting from scratch, and keeping your team in sync. All from the browser.
Screenshot of the Flux app showing a PCB in 3D mode with collaborative cursors, a comment thread pinned on the canvas, and live pricing and availability for a part on the board.
Flux is a better way to build PCBs
Go 10x faster from idea to PCB by reducing busy work, never starting from scratch, and keeping your team in sync. All from the browser.
Screenshot of the Flux app showing a PCB in 3D mode with collaborative cursors, a comment thread pinned on the canvas, and live pricing and availability for a part on the board.
Flux for Enterprise
Learn how Fortune 500s are revolutionizing hardware design at scale with AI.
Flux for Enterprise
Join leading Fortune 500s and over 300k hardware engineers revolutionizing the way they build PCBs with AI
Flux for Enterprise
Join leading Fortune 500s and over 300k hardware engineers revolutionizing the way they build PCBs with AI