July 11, 2024

How to Use the Arduino map() Function: A Complete Guide

arduino board and text suggesting this is all about the arduino map () function

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.

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
Work with Flux like an engineering intern—automating the grunt work, learning your standards, explaining its decisions, and checking in for feedback at key moments.
Illustration of sub-layout. Several groups of parts and traces hover above a layout.
Design PCBs with AI
Introducing a new way to work: Give Flux a job and it plans, explains, and executes workflows inside a full browser-based eCAD you can edit anytime.
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.
Design PCBs with AI
Introducing a new way to work: Give Flux a job and it plans, explains, and executes workflows inside a full browser-based eCAD you can edit anytime.
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.
Design PCBs with AI
Introducing a new way to work: Give Flux a job and it plans, explains, and executes workflows inside a full browser-based eCAD you can edit anytime.
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.

Related Content

Design Rule Checking (DRC) in PCB Design: Real-Time vs Batch, Rules, and Common Failures

Design Rule Checking (DRC) in PCB Design: Real-Time vs Batch, Rules, and Common Failures

DRC is an automated process that checks your PCB layout against manufacturing and electrical constraints, catching errors like trace spacing and drill sizes before fabrication. Modern tools run this in real-time during design, while older ones batch-check at the end, often producing overwhelming error lists.

Profile avatar of Yaneev Hacohen
Yaneev Hacohen
|March 26, 2026
Crosstalk, Reflections, and the Real Challenges of Signal Integrity

Crosstalk, Reflections, and the Real Challenges of Signal Integrity

This post explains key signal integrity issues like crosstalk and reflections in PCBs and offers simple layout tips to avoid them. A free guide is included.

Profile avatar of Jharwin Barrozo
Jharwin Barrozo
|June 20, 2025
Smarter Part Search Just Got a Major Upgrade

Smarter Part Search Just Got a Major Upgrade

Flux Copilot’s new AI-powered part search makes finding and placing components faster and easier using natural language. It eliminates tool-switching and datasheet overload. This streamlines your PCB design workflow.

Profile avatar of Nico Tzovanis
Nico Tzovanis
|June 5, 2025
Top 5 CES 2025 Hardware Projects You Can Build With Flux

Top 5 CES 2025 Hardware Projects You Can Build With Flux

This blog highlights CES 2025 showcased projects, offering insights on how to recreate them using Flux. With Flux AI-driven design tools, component library, and customizable templates, engineers and hobbyists can build inspired hardware like wearables, drones, EV components, portable chargers, and solar devices.

Profile avatar of Nico Tzovanis
Nico Tzovanis
|January 16, 2025
40+ Hardware Projects You Can Build Over the Holidays

40+ Hardware Projects You Can Build Over the Holidays

This holiday season is a great time to try something new and unleash your creativity with open-source hardware projects.

Profile avatar of Jharwin Barrozo
Jharwin Barrozo
|November 27, 2024
Design Your First PCB: 10 Popular Microcontrollers to Get Started

Design Your First PCB: 10 Popular Microcontrollers to Get Started

This article highlights 10 of the most popular microcontrollers, based on their usage in embedded systems, memory architecture, and the community support they enjoy.

Profile avatar of Jharwin Barrozo
Jharwin Barrozo
|October 11, 2024
5 Common Mistakes Working with PCB Contractors

5 Common Mistakes Working with PCB Contractors

In this post, we’ll explore five common mistakes companies make when contracting PCB design and how you can avoid them by using tools like Flux to keep your project on track, from concept to completion.

Profile avatar of Nico Tzovanis
Nico Tzovanis
|October 10, 2024
STM32: Things You Need to Know

STM32: Things You Need to Know

Learn about STM32 microcontrollers, popular series, USB OTG, SWD, UART, and development tools. Find the right STM32 MCU and kickstart your projects.

Profile avatar of Jharwin Barrozo
Jharwin Barrozo
|October 3, 2024