Skip to content Skip to sidebar Skip to footer

Arduino Map How to Mapping Range Int and Float Values

Arduino map() For Int and Float Values

Arduino Map How to Mapping Range Int and Float Values as he said is mapping. The term mapping is used to get a value of two value ranges.

Arduino Map How to Mapping Range Int and Float Values
 Ilustrasi Arduino Map


By default, this map() function is used for integers, both positive or negative. Basic writing MAP function is:

data = map(val, fromLow, fromHigh, toLow, toHigh)

To be able to understand how it works, now we make a small project where we will make a simple voltmeter using Arduino.

1. Map for Integer (INT)

Arduino has 6 pin ADC. We just take the ADC0 pin. Later this ADC pin will connect to the power supply source.

Arduino has an ADC feature with a 10-bit resolution. This means that the incoming data will be broken / samples to 1024 times. But in the program writing, 1024 starts from 0. So programmed we use the value of 0-1023.

1 time the sample will read the 0.004volt voltage. And so on.

If the voltage changes, of course the ADC reading value also changes. To make this conversion change quickly then use the MAP function.

Now, we will use the Arduino Map Basic Program. Because by default the map is used for integers, the voltage will appear only integer.

Note on the data line = map (Val, 0, 1023, 0, 5);

  • Val is a value that is read from ADC.
  • 0 and 1023 is Val's value limit.
  • 0 and 5 are boundaries of 0V to 5V.

For the program are as follows:

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

void loop() {
  int val = analogRead(A0);
  int data = map(val, 0, 1023, 0, 5);

  Serial.print(data);
  Serial.println(" V");
  delay(100);
}

In the program above, try connecting the ADC0 PIN to the 3.3V voltage source, then Bukia Serial Monitor. Then the one that is read only 3V.

Now what if we want to measure a more precision voltage? Of course we need a value.

2. Map for Float

Because we measure the 3.3V value in Arduino and we want to appear also 3.3V when measurement, from that we must map the ADC value into a value or float.

Next I have included the basic program. There is little modification on the MAP program. Please upload the following program:

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

void loop() {
  int val = analogRead(A0);
  float data = mapf(val, 0, 1023, 0, 5);

  Serial.print(data);
  Serial.println(" V");
  delay(100);
}

float mapf(float value, float fromLow, float fromHigh, float toLow, float toHigh) {
  float result;
  result = (value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow;
  return result;
} 

Then you will look at the series of monitors, a measurable voltage is 3.3V, or greater little or less less.

Thank you for visiting the website Chip Piko. May be useful.

Post a Comment for "Arduino Map How to Mapping Range Int and Float Values"