CYTHEIA AIOpen the editor

Voltage Reader

An Arduino reads a two-resistor divider with analogRead and prints the voltage. Change a resistor and watch the reading follow.

Open this circuit and run it

Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.

How it works

Two resistors, R1 of 10 kilohms and R2 of 4.7 kilohms, sit in a row between the board’s 5 volt pin and ground, and analog pin A0 is wired to the point between them. As in the Voltage Divider, the voltage there is the supply times R2 ÷ (R1 + R2), which is 5 × 4.7 ÷ 14.7, about 1.6 volts. A0 only listens: it draws almost no current, so it does not disturb the divider.

analogRead(A0) turns that voltage into a whole number from 0 (0 volts) to 1023 (5 volts), here about 327. The sketch turns it back into volts with raw × 5.0 ÷ 1023.0 and prints both numbers to the Serial tab twice a second. The reading comes from the circuit solver, which works out the voltage from the real resistors, so any divider you build gives the right number, not just this one.

Parts

Wiring

Arduino code

// Voltage Reader
// R1 and R2 split the 5 V supply; A0 listens to the point between them.
void setup() {
  Serial.begin(9600);
}

void loop() {
  int raw = analogRead(A0);
  float volts = raw * 5.0 / 1023.0;
  Serial.print("A0 raw: ");
  Serial.print(raw);
  Serial.print("   volts: ");
  Serial.println(volts);
  delay(500);
}

Try changing it

Open this circuit and run it

Next challenge

More projects in Analog Electronics