Lesson 80: Using map() Function
Learn how map() scales values from one range to another for sensor-to-output control logic.
Progress indicator
Lesson 80 of 80
Learning Objectives
- Understand what map() does mathematically and why range scaling is common in embedded systems.
- Learn map() syntax and how input/output boundaries control scaled results.
- Scale sensor values into output domains like PWM, speed, and thresholds.
- Understand map() vs constrain() and when to combine both for safe control behavior.
- Avoid common mistakes like wrong calibration ranges, no clamping, and integer rounding confusion.
Concept Explanation
What is map()
map() converts a value from one numeric range into another numeric range. It is commonly used when sensor range and output range are different.
Think of map() as a proportional translator. If input is at 50% of its range, output will be near 50% of the target range.
map() Syntax
Syntax: map(value, inMin, inMax, outMin, outMax)
Example: map(sensorValue, 0, 4095, 0, 255) converts ADC-style input to PWM.
value: current input reading.inMin,inMax: source range.outMin,outMax: destination range.
How map() Works
- Compute relative position of input inside input range.
- Apply that same relative position to output range.
- Return scaled result as integer math in classic Arduino behavior.
Conceptually: subtract input minimum, scale by output/input range ratio, then add output minimum.
Input Range vs Output Range
Input range is where your source value comes from (for example sensor 0..4095). Output range is where you need to send value (for example PWM 0..255).
If either range is inaccurate, mapped output will feel wrong even if your code is syntactically correct.
Scaling Sensor Values
Sensor data often uses one resolution, while actuators use another. map() lets you keep behavior proportional across these different numeric scales.
- ADC input to LED brightness (PWM).
- Potentiometer input to motor speed.
- Joystick axis to servo angle-like value range.
map() vs constrain() (Comparison)
map()scales value from one range to another.constrain()clamps value into min/max limits.- Common pattern: map first, then constrain if needed for safety.
map() can produce out-of-range results when input goes outside expected bounds, so constrain() is often used as a safety gate.
When to Use map()
- Converting ADC readings to PWM brightness.
- Converting joystick/sensor values to movement or speed values.
- Any proportional scaling between two known ranges.
- Converting protocol or UI numeric ranges into hardware-friendly ranges.
Example Code
This example maps simulated sensor values to PWM and drives LED brightness.
const int LED_PIN = 2;
int sensorValue = 0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
// Simulated sensor value sweep from 0 to 4095.
sensorValue = sensorValue + 350;
if (sensorValue > 4095) {
sensorValue = 0;
}
int pwmValue = map(sensorValue, 0, 4095, 0, 255);
pwmValue = constrain(pwmValue, 0, 255);
analogWrite(LED_PIN, pwmValue);
Serial.print("sensorValue: ");
Serial.print(sensorValue);
Serial.print(" | mapped pwm: ");
Serial.println(pwmValue);
delay(600);
}Example Code Explanation
sensorValueis incremented each loop to simulate an analog sweep.- Wrap logic resets value after max to keep a repeating demo cycle.
map(...)converts 0..4095 input into 0..255 output proportionally.constrain(...)ensures mapped result never exceeds valid PWM range.analogWrite()applies resulting PWM to LED brightness control.- Serial prints raw sensor and mapped PWM so scaling is visible step-by-step.
- Delay slows updates so learners can observe mapping progression clearly.
What Happens Inside
- CPU takes input value and computes relative position in source range.
- Relative position is projected into destination range.
- Scaled result is returned (typically as integer in classic Arduino map behavior).
- Optional constrain stage ensures safe boundary protection.
- Output is written to hardware control layer (PWM in this example).
- Serial monitor logs provide validation for calibration and tuning.
Common Mistakes with map()
- Using wrong input range values that do not match real sensor limits.
- Assuming map() clamps automatically (it scales, does not clamp).
- Ignoring integer rounding effects when high precision is required.
- Using unstable calibration points, causing mapped output drift.
- Forgetting to update ranges when hardware resolution changes.
Best Practices for map()
- Use accurate input/output ranges from datasheet or measured calibration.
- Combine map() with constrain() when inputs can exceed expected range.
- Print raw and mapped values during tuning to verify scaling behavior.
- Keep calibration constants centralized to avoid mismatched scaling across files.
- Re-check mapping after changing sensors, board resolution, or voltage reference.
Try it now
Open the simulator workspace and observe map() scaling from sensor range to PWM output.