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

Use the Arduino map() function to scale numbers, control sensors, and optimize projects effortlessly with examples and advanced tips in this guide.

Written by [Jharwin Barrozo](https://www.flux.ai/jharwinbarrozo)

Published Jul 2024 - 4 min read

[![arduino board and text suggesting this is all about the arduino map () function](https://www.flux.ai/p/assets/blogs/arduino-map-function/main.webp)](#)

## 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.

## About the author

[Jharwin Barrozo](https://www.flux.ai/jharwinbarrozo)

Electronics Engineer at Flux

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](https://flux.ai/jharwinbarrozo)

Let’s start building

Build your big idea together with Flux. All you need to do is describe what you want to make, or what problem you want to solve. Flux will work with you to make it a reality.

![Close-up photo of a PCB made with Flux, with an ESP32 module, buttons, and a microSD slot.](https://www.flux.ai/p/assets/blogs/miscellaneous/sidebar-cta-pcb.webp) [Try it Free](https://www.flux.ai/signup)

## More in Component & Platform Guides

[

![A 3d model of arduino uno board](https://www.flux.ai/p/assets/blogs/arduino-getting-started/main.webp)

### Arduino Uno Basics: Beginner's Guide to Getting Started

15 Min Read](https://www.flux.ai/p/blog/arduino-uno-basics-beginners-guide-to-getting-started)[

![ESP32 Board with its pinout label and diagram, each pin label and function on different colors](https://www.flux.ai/p/assets/blogs/esp32-pinout/main.webp)

### ESP32 Pinout Guide: Everything You Need to Know

11 Min Read](https://www.flux.ai/p/blog/esp32-pinout-everything-you-need-to-know)[

![A raspberry pi 3B and zero boards laid out together showing graphical GPIO pinout](https://www.flux.ai/p/assets/blogs/raspberry-pi-guide/main.webp)

### Raspberry Pi Pinouts: A Comprehensive Guide

8 Min Read](https://www.flux.ai/p/blog/raspberry-pi-pinouts-a-comprehensive-guide)[

![An arduino uno microcontroller board with its sketch code on Arduino IDE being used as background in a monitor.](https://www.flux.ai/p/assets/blogs/arduino-code-guide/main.webp)

### Arduino Code for Beginners: How to Write Your First Sketch

8 Min Read](https://www.flux.ai/p/blog/arduino-code-a-comprehensive-guide-for-beginners)[

![Meet the Arduino Nano R4: Why Is This Tiny Upgrade So Big for Makers?](https://www.flux.ai/p/assets/blogs/arduino-nano-r4/main.webp)

### Meet the Arduino Nano R4: Why Is This Tiny Upgrade So Big for Makers?

6 Min Read](https://www.flux.ai/p/blog/meet-the-arduino-nano-r4-why-is-this-tiny-upgrade-so-big-for-makers)[

![A microcontroller board with the words Arduino Pro Micro on it.](https://www.flux.ai/p/assets/blogs/arduino-pro-micro/main.webp)

### What is Arduino Pro Micro: a Comprehensive Introduction for Novices

5 Min Read](https://www.flux.ai/p/blog/what-is-arduino-pro-micro-a-comprehensive-introduction-for-novices)[

![Schematic diagram of arduino uno board with all of its passive and active components](https://www.flux.ai/p/assets/blogs/arduino-uno-schematic-diagram/main.webp)

### Arduino Uno Schematic Diagram: A Comprehensive Guide

8 Min Read](https://www.flux.ai/p/blog/arduino-uno-schematic-diagram)[

![Side by side comparison of the 3 powerful and most common Arduino microcontroller boards; Arduino Mega 2560, Arduino Uno and Arduino Micro.](https://www.flux.ai/p/assets/blogs/arduino-mega-uno-micro/main.webp)

### Arduino Mega vs. Uno vs. Micro: Key Differences

6 Min Read](https://www.flux.ai/p/blog/what-is-the-real-difference-arduino-mega-vs-micro-vs-uno)[

![Different packages or footprint of famous atmega328p microcontroller with its pinout](https://www.flux.ai/p/assets/blogs/atmega328p-still-top-choice/main.webp)

### Why Atmega328p is Still a Top Choice for Microcontroller Projects in 2023

8 Min Read](https://www.flux.ai/p/blog/why-atmega328p-is-still-a-top-choice-for-microcontroller-projects-in-2023)[

![ESP32 Chip in a dark background: A powerful microcontroller with integrated Wi-Fi and Bluetooth, driving IoT innovation with seamless connectivity.](https://www.flux.ai/p/assets/blogs/esp32-history/main.webp)

### Exploring ESP32: from History to Application

3 Min Read](https://www.flux.ai/p/blog/exploring-esp32-from-history-to-application)[

![3d rendered of an ESP8266 microcontroller board, showing its compact size with the Wi-Fi module, chip antenna, and GPIO pins clearly visible, ideal for embedding wireless internet connectivity into DIY electronics projects.](https://www.flux.ai/p/assets/blogs/everything-about-esp8266/main.webp)

### ESP8266 Pinout Guide: Complete GPIO Reference & IoT Project Tips

10 Min Read](https://www.flux.ai/p/blog/esp8266-pinout-everything-you-need-to-know)[

![Attiny85 chip in two different packages viewed on top, also showing pinout diagram](https://www.flux.ai/p/assets/blogs/attiny85/main.webp)

### Introduction to the ATtiny85 Microcontroller

12 Min Read](https://www.flux.ai/p/blog/introduction-to-the-attiny85-microcontroller)[

![STM32 microcontroller chip overview – Why choose STM32? Discover STM32F4 ARM-based processors, optimized for performance with USB OTG, SWD, and UART support](https://www.flux.ai/p/assets/blogs/stm32-things-you-need-to-know/main.webp)

### STM32: Things You Need to Know

10 Min Read](https://www.flux.ai/p/blog/stm32-things-you-need-to-know)[

![Arduino Nano Guide](https://www.flux.ai/p/assets/blogs/arduino-nano-guide/main.webp)

### Arduino Nano Guide

10 Min Read](https://www.flux.ai/p/blog/arduino-nano-guide)[

![A top view photo of raspberry pi zero 2 w board](https://www.flux.ai/p/assets/blogs/pi-zero-2w/main.webp)

### Raspberry Pi Zero 2 W Review: a Powerful and Affordable Mini Computer

9 Min Read](https://www.flux.ai/p/blog/raspberry-pi-zero-2-w-review-a-powerful-and-affordable-mini-computer)[

![RP2350 A4 Stepping: Why Raspberry Pi Pico’s Latest Upgrade Changes Everything](https://www.flux.ai/p/assets/blogs/rp2350-a4-stepping/main.webp)

### RP2350 A4 Stepping: Why Raspberry Pi Pico’s Latest Upgrade Changes Everything

8 Min Read](https://www.flux.ai/p/blog/rp2350-a4-stepping-why-raspberry-pi-picos-latest-upgrade-changes-everything)[

![Photos of Raspberry Pi Pico 2 PCB board and its predecessor Raspberry Pi Pico boards laid out with a text suggesting their key differences](https://www.flux.ai/p/assets/blogs/pi-pico-vs-pi-pico-2/main.webp)

### Raspberry Pi Pico 2 vs Pico: Key Differences and Upgrades

8 Min Read](https://www.flux.ai/p/blog/whats-new-in-the-raspberry-pi-pico-2-a-showdown-with-the-original-raspberry-pi-pico)[

![Choosing the Right Platform to Build Your Project](https://www.flux.ai/p/assets/blogs/choosing-the-right-microcontroller/main.webp)

### Choosing the Right Platform to Build Your Project

9 Min Read](https://www.flux.ai/p/blog/choosing-the-right-platform-to-build-your-project)[

![Raspberry pi boards and arduino boards laid out side by side.](https://www.flux.ai/p/assets/blogs/arduino-vs-raspberry-pi/main.webp)

### Arduino vs. Raspberry Pi: Key Differences and Which to Choose

8 Min Read](https://www.flux.ai/p/blog/arduino-vs-raspberry-pi-comparison)[

![ESP32 boards and Arduino boards side by side, highlighting the comparison between these popular IoT microcontroller platforms.](https://www.flux.ai/p/assets/blogs/esp32-vs-arduino/main.webp)

### ESP32 vs Arduino: Key Differences & Which to Choose?

8 Min Read](https://www.flux.ai/p/blog/esp32-vs-arduino)[

![A simplified schematic diagram illustrates how pull-up and pull-down resistors are utilized in microcontroller applications](https://www.flux.ai/p/assets/blogs/pull-up-down-resistors/main.webp)

### Understanding Pull-up and Pull-down Resistors: A Guide for Arduino and Microcontrollers

7 Min Read](https://www.flux.ai/p/blog/understanding-pull-up-and-pull-down-resistors-a-guide-for-arduino-and-microcontrollers)[

![What Is an Open Circuit? Causes, Examples, and Troubleshooting](https://www.flux.ai/p/assets/blogs/what-is-an-open-circuit/main.webp)

### What Is an Open Circuit? Causes, Examples, and Troubleshooting

10 Min Read](https://www.flux.ai/p/blog/what-is-an-open-circuit)[

![An HX711 load cell amplifier board connected to a load cell and an Arduino.](https://www.flux.ai/p/assets/blogs/hx711-load-cell-amplifier/main.webp)

### HX711 Load Cell Amplifier: Wiring, Calibration, and PCB Design Guide

8 Min Read](https://www.flux.ai/p/blog/hx711-load-cell-amplifier)[

![A 7-segment display wired to an Arduino with current-limiting resistors.](https://www.flux.ai/p/assets/blogs/7-segment-display-arduino/main.webp)

### How to Connect a 7-Segment Display to Arduino: Wiring and Code

9 Min Read](https://www.flux.ai/p/blog/7-segment-display-arduino)

See All (24)

## Let’s start building.

Let’s build your idea together. All you need to do is describe what you want to make, or what problem you want to solve. Flux will work with you to make it a reality.

[Try it Free](https://www.flux.ai/signup)

 ![Close-up of a Flux-designed printed circuit board](https://www.flux.ai/p/assets/homepage-v2/mission-visual.webp)
