Shop by category
Shop all componentsSensors & MCUs
map() works with whole numbers (long). It drops the fraction instead of rounding, so 512 of 1023 gives 90 for 0 to 180, not 90.09. For fractions, use the exact formula with float values.
y = (x − in_min) × (out_max − out_min) ÷ (in_max − in_min) + out_minArduino drops the fraction (integer division)A potentiometer reading of 512 mapped from 0–1023 to a servo angle of 0–180: 512 × 180 ÷ 1023 = 90.09, and map() returns 90.
map() rescales a number from one range to another. It is the usual way to turn an analogRead() value (0 to 1023) into a servo angle, a PWM value for analogWrite() (0 to 255), a percentage or a temperature from a linear sensor.
The function only does whole-number maths, so it rounds down and can return values outside the target range when the input goes past its limits. Wrapping it in constrain() keeps the output safe; swapping out_min and out_max reverses the direction.
Use map(analogRead(A0), 0, 1023, 0, 255). Dividing the reading by 4 gives almost the same result and is a little faster.
Arduino map() uses long integers, so the fraction is cut off. Write your own float version, (x − a) × (d − c) / (b − a) + c with float variables, when you need decimals.
Yes. map(x, 0, 1023, 255, 0) gives 255 at the low end and 0 at the high end, which is handy for a sensor that reads backwards.
No. A reading outside in_min to in_max gives a result outside out_min to out_max. Add constrain(y, low, high) after it.
y = (x − in_min) × (out_max − out_min) ÷ (in_max − in_min) + out_min; Arduino drops the fraction (integer division).
A potentiometer reading of 512 mapped from 0–1023 to a servo angle of 0–180: 512 × 180 ÷ 1023 = 90.09, and map() returns 90.
Geekobot builds that use it include Pico servo controlled by a knob (MicroPython), Control a servo with a knob, Servo sweep: control an angle and Seven-segment display lab. Each has the full parts list, wiring and code.
Results are estimates for design and learning. Check critical designs against the component datasheet. Made by the Geekobot Learning Team. How we write our calculators