Arduino project: the map() function

When you acquire analog values from an analog input pin, by default they are acquired as values ranging from 0 to 1023.

This is because the analog read resolution is 10 bits, and 2^10 is 1024.

Tip: on ARM-based Arduino devices, like Arduino…


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

When you acquire analog values from an analog input pin, by default they are acquired as values ranging from 0 to 1023.

This is because the analog read resolution is 10 bits, and 2^10 is 1024.

Tip: on ARM-based Arduino devices, like Arduino Zero, Arduino Due and the Arduino MKR family, you can map up to 12 bits, but the default is 0. On those devices you can call the analogReadResolution(12) to set the resolution to 12 bits, so you can go from 0 to 4095 instead of 1023

The map() function provided by the Arduino language allows you to map that range of values to a different range.

Here’s the function signature:

int <newvalue> = map(<value>, <original_min>, <original_max>, <new_min>, <new_max>);

It’s important to note that the function returns an integer value, the decimal part is cut.

For example you might want to map the original 1024 values we mentioned you can acquire through analog input to a set of only 10 values, because you might have some logic that only handles 10 steps.

You can do so like this:

int acquiredValue = analogRead(A1);
int value = map(acquiredValue, 0, 1023, 0, 9);

Here’s a full example:

void setup() {
    Serial.begin(9600);
}

void loop() {
    int acquiredValue = analogRead(A1);
    int value = map(acquiredValue, 0, 1023, 0, 9);
    Serial.println(value);
}

Now instead of the input having 1024 possible values, you have a restricted set of 10 possible values, going from 0 to 9.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-03-29T05:00:00+00:00) Arduino project: the map() function. Retrieved from https://www.scien.cx/2021/03/29/arduino-project-the-map-function/

MLA
" » Arduino project: the map() function." flaviocopes.com | Sciencx - Monday March 29, 2021, https://www.scien.cx/2021/03/29/arduino-project-the-map-function/
HARVARD
flaviocopes.com | Sciencx Monday March 29, 2021 » Arduino project: the map() function., viewed ,<https://www.scien.cx/2021/03/29/arduino-project-the-map-function/>
VANCOUVER
flaviocopes.com | Sciencx - » Arduino project: the map() function. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/29/arduino-project-the-map-function/
CHICAGO
" » Arduino project: the map() function." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/03/29/arduino-project-the-map-function/
IEEE
" » Arduino project: the map() function." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/03/29/arduino-project-the-map-function/. [Accessed: ]
rf:citation
» Arduino project: the map() function | flaviocopes.com | Sciencx | https://www.scien.cx/2021/03/29/arduino-project-the-map-function/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.