Bird
Raised Fist0
Arduinoprogramming~10 mins

Mapping analog values with map() function in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Mapping analog values with map() function
Read analog input
Apply map() function
Get mapped output
Use mapped value in program
END
The program reads an analog value, uses map() to convert it to a new range, then uses the mapped value.
Execution Sample
Arduino
int sensorValue = analogRead(A0);
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(9, outputValue);
Reads analog input from pin A0, maps it from 0-1023 to 0-255, then writes PWM output on pin 9.
Execution Table
StepActionsensorValuemap() Input Rangemap() Output RangeoutputValueOutput Action
1Read analog input from A05120-10230-255No output yet
2Apply map() to sensorValue5120-10230-255128Mapped value calculated
3Write outputValue to pin 95120-10230-255128PWM signal with value 128 sent
4End of cycle5120-10230-255128Program waits for next loop
💡 Mapping done once per loop; outputValue is used to control PWM signal.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
sensorValueundefined512512512512
outputValueundefinedundefined128128128
Key Moments - 3 Insights
Why does outputValue become 127 when sensorValue is 512?
Because map() converts 512 from the 0-1023 range to the 0-255 range proportionally, so 512 maps roughly to 128 (halfway). See execution_table step 2.
What happens if sensorValue is 0 or 1023?
If sensorValue is 0, outputValue becomes 0; if sensorValue is 1023, outputValue becomes 255. This is the mapping range limits shown in execution_table step 2.
Does map() change the original sensorValue?
No, sensorValue stays the same. map() returns a new value assigned to outputValue. See variable_tracker showing sensorValue unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the outputValue when sensorValue is 512?
A128
B512
C255
D0
💡 Hint
Check the outputValue column at step 2 in execution_table.
At which step does the program write the PWM signal to pin 9?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Output Action column in execution_table.
If sensorValue was 1023, what would outputValue be after map()?
A0
B512
C255
D1023
💡 Hint
Refer to key_moments explanation about mapping limits.
Concept Snapshot
map(value, fromLow, fromHigh, toLow, toHigh)
- Converts value from one range to another
- Keeps proportion between ranges
- Useful for analog input to PWM output
- Does NOT change original value
- Output can be used directly in analogWrite
Full Transcript
This example shows how Arduino reads an analog input from pin A0, which gives a value between 0 and 1023. The map() function converts this value to a new range, here 0 to 255, which is suitable for PWM output. The mapped value is stored in outputValue and then sent to pin 9 using analogWrite. The execution table traces each step: reading input, mapping value, writing output, and waiting for the next loop. Variables sensorValue and outputValue are tracked to show their values at each step. Common confusions include understanding that map() returns a new value without changing the original, and how the proportional mapping works. The visual quiz tests understanding of these steps and values. The quick snapshot summarizes the map() function usage and behavior.

Practice

(1/5)
1. What does the map() function do in Arduino programming?
easy
A. It converts a number from one range to another range.
B. It reads analog sensor values from pins.
C. It controls the speed of a motor.
D. It stores data in the EEPROM memory.

Solution

  1. Step 1: Understand the purpose of map()

    The map() function takes a number and changes it from one range to another, like converting sensor values to a different scale.
  2. Step 2: Compare with other options

    Reading analog values, controlling motors, or storing data are different functions, not what map() does.
  3. Final Answer:

    It converts a number from one range to another range. -> Option A
  4. Quick Check:

    map() changes ranges = C [OK]
Hint: Remember: map() changes number ranges fast [OK]
Common Mistakes:
  • Confusing map() with analogRead()
  • Thinking map() controls hardware directly
  • Assuming map() stores data permanently
2. Which of the following is the correct syntax to map a value val from range 0-1023 to 0-255?
easy
A. map(val, 0, 255, 0, 1023);
B. map(0, 1023, val, 0, 255);
C. map(val, 0, 1023, 0, 255);
D. map(val, 255, 0, 1023, 0);

Solution

  1. Step 1: Recall map() function parameters

    The correct order is: map(value, fromLow, fromHigh, toLow, toHigh).
  2. Step 2: Match parameters to the question

    We want to map val from 0-1023 to 0-255, so the call is map(val, 0, 1023, 0, 255);.
  3. Final Answer:

    map(val, 0, 1023, 0, 255); -> Option C
  4. Quick Check:

    map(value, 0-1023, 0-255) = D [OK]
Hint: Remember parameter order: value, from range, to range [OK]
Common Mistakes:
  • Swapping from and to ranges
  • Putting value in wrong parameter position
  • Reversing range limits
3. What is the output of this Arduino code snippet?
int sensorValue = 512;
int outputValue = map(sensorValue, 0, 1023, 0, 255);
Serial.println(outputValue);
medium
A. 0
B. 255
C. 512
D. 127

Solution

  1. Step 1: Understand the mapping calculation

    Mapping 512 from 0-1023 to 0-255 scales it roughly to half the output range.
  2. Step 2: Calculate mapped value

    512 is about half of 1023, so output is about half of 255, which is 127.
  3. Final Answer:

    127 -> Option D
  4. Quick Check:

    512 maps to 127 in 0-255 range [OK]
Hint: Half input maps to half output in linear map() [OK]
Common Mistakes:
  • Using input value directly as output
  • Confusing output range limits
  • Rounding errors ignored
4. Identify the error in this code snippet:
int sensorValue = analogRead(A0);
int outputValue = map(sensorValue, 0, 1023, 0, 255)
Serial.println(outputValue);
medium
A. Missing semicolon after map() function call.
B. Incorrect analogRead() usage.
C. map() parameters are in wrong order.
D. Serial.println() cannot print integers.

Solution

  1. Step 1: Check syntax line by line

    The line with map() is missing a semicolon at the end.
  2. Step 2: Verify other lines

    analogRead(A0) and Serial.println() are used correctly.
  3. Final Answer:

    Missing semicolon after map() function call. -> Option A
  4. Quick Check:

    Missing semicolon = A [OK]
Hint: Check every statement ends with a semicolon [OK]
Common Mistakes:
  • Forgetting semicolons after function calls
  • Misordering map() parameters
  • Assuming Serial.println() can't print ints
5. You want to map a sensor value from 0-1023 to a motor speed range of 100-200. Which code correctly maps and constrains the output to this range?
hard
A. int speed = map(sensorValue, 100, 200, 0, 1023);
B. int speed = constrain(map(sensorValue, 0, 1023, 100, 200), 100, 200);
C. int speed = map(sensorValue, 0, 1023, 0, 255);
D. int speed = constrain(sensorValue, 100, 200);

Solution

  1. Step 1: Map sensorValue to motor speed range

    Use map(sensorValue, 0, 1023, 100, 200) to convert sensor reading to speed between 100 and 200.
  2. Step 2: Constrain output to avoid out-of-range values

    Wrap with constrain(..., 100, 200) to keep speed within limits.
  3. Final Answer:

    int speed = constrain(map(sensorValue, 0, 1023, 100, 200), 100, 200); -> Option B
  4. Quick Check:

    Map then constrain for safe range = B [OK]
Hint: Map first, then constrain to keep values safe [OK]
Common Mistakes:
  • Reversing map() range parameters
  • Not constraining output causing invalid speeds
  • Using constrain() without mapping first