Sub topic 9
Scaling Readings with `map()`
Assume you can read an analog input with analogRead() and understand the basic setup() and loop() structure. This chapter shows how to convert that raw reading into a useful output range, such as the brightness value required by analogWrite().
Why scale a reading?
On an Arduino Uno, analogRead() normally returns a value from 0 to 1023. PWM output, however, normally expects a value from 0 to 255. A potentiometer turned halfway might produce a reading near 512, but analogWrite(LED_PIN, 512) is outside the normal PWM range.
The map() function converts a value from one range to another:
map(value, fromLow, fromHigh, toLow, toHigh)| Argument | Meaning |
|---|---|
| `value` | The number to convert |
| `fromLow`, `fromHigh` | The input range |
| `toLow`, `toHigh` | The output range |
For a potentiometer controlling an Uno LED:
int brightness = map(raw, 0, 1023, 0, 255);A raw value of 0 becomes 0. A raw value of 1023 becomes 255. A value near 512 becomes about 127.
Mapping a potentiometer to LED brightness
The following sketch reads the potentiometer on A0 and controls an LED connected to PWM pin 9.
const int POT_PIN = A0;
const int LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
int raw = analogRead(POT_PIN);
int brightness = map(raw, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
delay(10);
}analogWrite() does not create a smoothly varying analog voltage on most Arduino boards. On a PWM pin, it rapidly switches the output on and off. The brightness appears to change because the LED receives power for a different fraction of each cycle. That fraction is called the duty cycle.
Integer arithmetic and limits
Arduino's map() function uses integer arithmetic. It returns a long, although assigning the result to an int is suitable for the 0-to-255 PWM range. Fractional results are discarded rather than rounded.
For example, mapping 512 from 0–1023 to 0–255 produces approximately 127, not 127.5. This one-step difference is usually unnoticeable.
map() does not automatically restrict values to the input range. If the input is outside the source range, the result can also be outside the destination range:
long result = map(1100, 0, 1023, 0, 255);
// result is greater than 255This matters when a sensor or calculation can exceed its expected limits. Use constrain() when you need a guaranteed output range:
int raw = analogRead(POT_PIN);
raw = constrain(raw, 0, 1023);
int brightness = map(raw, 0, 1023, 0, 255);
brightness = constrain(brightness, 0, 255);A potentiometer reading from analogRead() already fits the expected range on an Uno, so the first constrain() call is not necessary in the basic example.
Reversing the output
You can reverse a mapping by reversing the destination limits. This makes the LED dim when the potentiometer reading increases:
int brightness = map(raw, 0, 1023, 255, 0);
analogWrite(LED_PIN, brightness);The same technique works for other controls. For example, mapping 0–1023 to 20–200 can keep a motor or lamp within a safer operating range.
flowchart LR
A[Potentiometer reading 0 to 1023] --> B[map]
B --> C[PWM value 0 to 255]
C --> D[LED brightness]Exercise
What map() expression converts an Uno reading named raw from 0–1023 into a reversed PWM range where 0 becomes 255 and 1023 becomes 0?
Show answer
int brightness = map(raw, 0, 1023, 255, 0);