Built-in project · Arduino Uno R3

DC voltmeter on an Arduino Uno

A 0 to 25 V voltage sensor module across a bench supply, and the voltage on a 16x2 I2C LCD: two resistors make it a fifth, and the sketch multiplies by five. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $35. On Mokxi they're free.

Step by step, with the wiring and the common mistakes: Arduino 0-25 V Voltage Sensor: Wiring and Code

DC voltmeter · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// DC voltmeter: a 0 to 25 V voltage sensor module across a bench supply,
// and the voltage on a 16x2 I2C LCD and the serial monitor.
//
//   A0      the sensor's S
//   -       the sensor's header minus, to GND
//   VCC GND the sensor's screw terminal, across the supply's V+ and V-
//   LCD     SDA -> A4    SCL -> A5    VCC -> 5 V    GND -> GND
//
// The module is two resistors and nothing else: 30 k from the terminal's VCC
// to S and 7.5 k from S to its GND. So S is a fifth of the voltage on the
// terminal, and the sketch multiplies by 5 to get it back.
//
// The terminal's GND and the header's minus are one trace on the board, so
// wiring minus to the Arduino ties the supply's minus to the Arduino's ground.
// That is what lets it measure at all, and why it cannot measure anything
// that must not share a ground with the Arduino.
//
// On a 5 V Uno, S can go up to 5 V, which is 25 V on the terminal: that is
// the "0 to 25 V" on the listing. One converter step is 5 / 1023 V at S, so
// 24.4 mV on the terminal. (On a 3.3 V board the top is 16.5 V.)
//
// Press the supply's VOLTAGE buttons while it runs and watch the reading
// follow.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int SENSE = A0;
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(115200);
  Serial.println("Mokxi Arduino Uno: DC voltmeter");
  lcd.init();
  lcd.backlight();
  lcd.print("DC voltmeter");
}

void loop() {
  // Average sixteen readings: the sum fits a long easily, and the average
  // smooths a step of noise on a real supply.
  long sum = 0;
  for (int i = 0; i < 16; i++) sum += analogRead(SENSE);
  int raw = (sum + 8) / 16;

  float atS = raw * 5.0 / 1023.0;  // the voltage at S
  float volts = atS * 5.0;         // the divider is 5 to 1

  lcd.setCursor(0, 1);
  lcd.print(volts, 2);
  lcd.print(" V  ");
  lcd.print(raw);
  lcd.print("    ");

  Serial.print("ADC: ");
  Serial.print(raw);
  Serial.print("  volts: ");
  Serial.println(volts, 2);
  delay(250);
}

Parts list

4 parts, plus the jumper wires and a bench instrument. The parts retail for about $35; on Mokxi they're free, and so are the instruments.

How it is wired

7 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; Character LCD with I2C backpack pin VCC
  • Ground: Arduino Uno R3 pin GND; Voltage sensor, 0 to 25 V pin -; Character LCD with I2C backpack pin GND
  • Arduino Uno R3 pin A0; Voltage sensor, 0 to 25 V pin S
  • Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
  • Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
  • Bench power supply pin V+; Voltage sensor, 0 to 25 V pin VCC
  • Ground: Bench power supply pin V-; Voltage sensor, 0 to 25 V pin GND

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.