ATmega328P/Analogue input/25. map() and voltage dividers
Analogue input · 25 of 33

map() and voltage dividers

map() rescales a count into the units you want, with whole-number maths that truncates and never clamps. Two resistors in front of A0 do the same for a voltage too big for the pin, and the sum to undo them is one line. Both keep the pin under 5 V or they read nothing at all.

Your board

map() is a ruler conversion, in whole numbers

map(value, fromLow, fromHigh, toLow, toHigh) rescales a number from one range to another. The Arduino core computes it as

(value - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow

in long integers. Two things follow from that one line:

  • It truncates. 675 × 100 ÷ 1023 is 65.98, and integer division drops the .98: the answer is 65. Arduino's own reference says the fraction is truncated, not rounded.
  • It does not clamp. Give it a value outside the input range and it carries on in a straight line. map(1023, 0, 1000, 0, 100) is 102. constrain(x, 0, 100) is the clamp, and it is a separate call.

For a percentage, a bar graph or a servo angle, whole numbers are what you want. For volts, skip map() and write the sum in floating point: n * 5.0 / 1024.0.

Bigger than 5 V needs a divider

A0 can take 0 to 5 V. A 9 V battery or a 12 V supply has to be brought down first, and two resistors do it: R1 from the voltage to A0, R2 from A0 to GND. The pin sees

V(pin) = V(in) × R2 ÷ (R1 + R2)

and the sketch undoes it by multiplying by (R1 + R2) ÷ R2. The box does not include resistors; the sketch above has R1 and R2 at the top for when you fit some, and R1 = 0 means a plain wire.

Two resistors, and the sum back
R1 22k, R2 10k
R1 on top, R2 to ground
Voltage you want to measure12.0 V
At the pin
3.75 V
analogRead
768
Worked back
12.00 V
Most it can take
16.0 V
The pair shares 12.0 V out, and A0 sees 31.3% of it. The sketch multiplies by 3.20 to get it back, so each step is now 16 mV of input instead of 4.9. The two in parallel are 6.9 kohm, under the 10 kohm the ADC is built for.

Pick the pair from the largest voltage you will ever connect, not the one you expect. 22k over 10k divides by 3.2: 12 V arrives as 3.75 V and reads 768, and the most it can take is 16 V. Each step of the count is now 15.6 mV of the input instead of 4.9, which is the price of the range.

Two more rules from the datasheet. The ADC is built for sources of about 10 kohm or less, and a divider looks like its two resistors in parallel, so 22k and 10k (6.9 kohm) is fine and 220k and 100k is not. And no pin may go more than half a volt above the supply, which includes the moment the board is unplugged and the 12 V is still connected: the divider then pushes current through the pin's protection diode into an unpowered chip. Connect the board's power first, and the source second.

The same idea, on the board alone

With one jumper wire there is no divider to build, but the percentage works: A0 on GND is 0%, on 3V3 is 65% (more when the rail is under 5 V, as on a Nano on USB), on 5V is 100%. A knob between 5V and GND on A0 later reads the same way: it is a divider whose ratio you turn by hand, and the voltage divider page has the arithmetic.

The code

Map_A0_Percent.ino

Wire A0 to GND, 3V3 or 5V and watch the count become a percentage. The sketch also maps onto a range that ends at 1000, to show map() running off the end, and constrain() pulling it back.

// Map_A0_Percent.ino — A0 as a percentage, with map() and constrain()
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: one jumper wire from A0 to GND, 3V3 or 5V. A0 is an input.
//   With a divider in front of A0, set R1 and R2 below (kohm).

const float R1 = 0;    // top resistor; 0 means no divider
const float R2 = 10;   // bottom resistor, A0 to GND

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

void loop() {
  int n = analogRead(A0);
  long pct = map(n, 0, 1023, 0, 100);     // whole numbers only
  long over = map(n, 0, 1000, 0, 100);    // 1023 gives 102
  long kept = constrain(over, 0, 100);
  // 5.0 is the rail; a Nano on USB is nearer 4.7 (see the
  // article on measuring the board's own supply).
  float vin = n * 5.0 / 1024.0 * (R1 + R2) / R2;

  Serial.print("A0 ");
  Serial.print(n);
  Serial.print("  = ");
  Serial.print(pct);
  Serial.print("%   0..1000 scale: ");
  Serial.print(over);
  Serial.print(", constrained ");
  Serial.print(kept);
  Serial.print("   input ");
  Serial.print(vin, 2);
  Serial.println(" V");
  delay(500);
}

Expect 0%, 65% and 100% on a 5.00 V rail (a Nano on USB, whose rail is lower, gives about 70% for 3V3). 3V3 is 65.98% of the scale, and map() prints 65, not 66: it drops the fraction. On the 0 to 1000 scale, 5V gives 102, which constrain() turns into 100.

When it does not work

map() gives 65 where my calculator says 66

map() works in whole numbers and drops the fraction rather than rounding it: 65.98 becomes 65. For a percentage that is usually fine. For volts, use floating-point arithmetic, count × 5.0 ÷ 1024, as the sketch does for the input.

map() returns a value outside the range I gave it

It does not clamp, on purpose: a value outside the input range maps to one outside the output range. Wrap it in constrain(value, low, high) when the result must stay inside.

My divider readings come out low or drift

The resistors may be too large. The ADC is built for sources of about 10 kohm or less, and a divider looks like its two resistors in parallel. 22k over 10k is 6.9 kohm and fine; 220k over 100k is ten times too much.

A0 reads 1023 with the divider fitted

The pin is at 5 V or more, which is over the limit. Unplug the source and check the ratio: the biggest voltage you will ever connect, times R2 ÷ (R1 + R2), must come out under 5 V.

Where this goes next

A pin that is only ever on or off, made to look half on.

What PWM is →

Edit this page — content/books/atmega328p/map-and-dividers.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →