Using the map() Function

Master Arduino's map() function to easily convert values between different ranges.

ARDUINO LESSONSProgrammingFunctions
S
Shaun Sosi
How to Use the map() function on the Arduino

Understanding the map() Function

The map() function is a powerful tool in Arduino that allows you to convert values from one range to another, making it easier to work with sensors and create intuitive outputs.

Arduino map() Function Visualization
Understanding how map() converts values between different ranges

Think of map() Like This:

Imagine you're converting temperatures between Celsius and Fahrenheit. Just as you use a formula to convert between temperature scales, map() helps you convert between any two ranges of numbers.

Get Your Arduino Kit

To follow along with this mapping tutorial:

How map() Works

Function Syntax:

MapSyntax.txt
map(value, fromLow, fromHigh, toLow, toHigh)
    1. Parameters Explained

      • value - The number to convert
      • fromLow - Lower bound of current range
      • fromHigh - Upper bound of current range
      • toLow - Lower bound of target range
      • toHigh - Upper bound of target range
    2. Return Value

      • Returns the mapped number in the new range
      • Result is a long integer
      • Can handle negative numbers and reversed ranges

    Common Use Cases

    The map() function is particularly useful for:

    • Sensor Readings - Convert raw sensor values to meaningful units
    • LED Control - Scale values for brightness control
    • Servo Control - Convert angles to pulse widths
    • Display Scaling - Adapt values for different display ranges

    Real-World Examples:

    • Potentiometer (0-1023) → LED brightness (0-255)
    • Temperature sensor (0-1023) → Celsius (-40 to 125)
    • Joystick (0-1023) → Servo angle (0-180)
    • Light sensor (0-1023) → Percentage (0-100)

    Practical Examples

    Here are some practical implementations of the map() function:

    PotentiometerToLED.ino
    // LED brightness control using potentiometer const int potPin = A0; const int ledPin = 9; void setup() { pinMode(potPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { int potValue = analogRead(potPin); // Map potentiometer range (0-1023) to LED range (0-255) int brightness = map(potValue, 0, 1023, 0, 255); analogWrite(ledPin, brightness); Serial.print("Pot Value: "); Serial.print(potValue); Serial.print(" → LED Value: "); Serial.println(brightness); delay(100); }
      TemperatureSensor.ino
      // Temperature sensor reading with mapping const int tempPin = A0; void setup() { Serial.begin(9600); } void loop() { int sensorValue = analogRead(tempPin); // Map sensor range to temperature in Celsius int tempC = map(sensorValue, 0, 1023, -40, 125); Serial.print("Temperature: "); Serial.print(tempC); Serial.println("°C"); delay(1000); }

        Advanced Techniques

        Here are some advanced ways to use map():

        • Floating-Point Mapping - For more precise conversions
        • Constrained Mapping - Combine with constrain() function
        • Reverse Mapping - Use inverted ranges for reverse control
        • Multi-Stage Mapping - Chain multiple map() calls
        AdvancedMapping.ino
        // Floating-point mapping function 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; } // Example usage with constrain int value = analogRead(A0); int mapped = map(value, 0, 1023, 0, 255); int constrained = constrain(mapped, 0, 255); // Ensures value stays in range // Reverse mapping example int reversed = map(value, 0, 1023, 255, 0); // Inverted output range

          Common Pitfalls and Solutions

          Watch out for these common issues:

          • Integer Rounding - Use float mapping for precision
          • Out-of-Range Values - Add constrain() for safety
          • Zero Division - Ensure input range isn't zero
          • Overflow - Use long integers for large numbers

          Important Considerations:

          • map() returns a long integer, which may need casting
          • Input values outside the from range will be extrapolated
          • The function doesn't constrain results automatically
          • Integer math may cause rounding errors

          Project Ideas

          Try these projects to practice using map():

          • Light-Responsive LED - Map light sensor to LED brightness
          • Custom Thermometer - Convert sensor readings to temperature
          • Servo Controller - Map potentiometer to servo positions
          • Battery Monitor - Convert voltage readings to percentage

          Coming Up Next:

          Stay tuned for more exciting Arduino tutorials where we'll explore more functions and create even more interesting projects!

          Connect With Me

          Support Our Work

          Help us create more amazing content

          $

          Your contribution helps us create more amazing content. Thank you! 💖