Measure voltage with the Arduino

FunduinoLast updated on

Bis zu 25 Volt Gleichspannung mit dem Spannungssensor messen

Die analogen Eingänge des Arduino vertragen höchstens 5 Volt. Mit einem Spannungssensor misst du trotzdem größere Spannungen – bis zu 25 Volt Gleichspannung.

In dieser Anleitung schließt du den Sensor an, liest den Messwert ein, rechnest ihn in Volt um und lässt ihn im seriellen Monitor anzeigen.

Circuit diagram with voltage sensor on the Funduino UNO R3
Schematic: the voltage sensor on the UNO R3

You need this

0-25 V Voltage Sensor - 0-5 V analog output, optocoupler 0-25 V Voltage Sensor - 0-5 V analog output, optocoupler 1,11 € *Item no. F23106636About the product — 0-25 V Voltage Sensor - 0-5 V analog output, optocoupler
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 4 parts together 16,87 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

What a voltage sensor is used for

The Arduino’s analog inputs can handle a maximum of 5 volts. If you want to measure more, you need a voltage sensor. The one used here covers a range of 0 to 25 volts DC.

The module is essentially a voltage divider: It maps 0 to 25 volts at its input to 0 to 5 volts at its output - exactly the range the Arduino can read.

The sensor measures DC voltage only, up to a maximum of 25 volts. Do not use it to measure mains voltage.

Voltage sensor module with screw terminal and three connection pins
The voltage sensor for up to 25 volts

Connect the sensor

The wiring is simple, as only three connections lead to the Arduino:

  • S – to A1 (Analog 1)
  • + – to 5V
  • − – to GND

On the other side of the module are the GND and VCC terminals. Connect the voltage you want to measure there.

Circuit diagram with voltage sensor on the Funduino UNO R3
Schematic: the voltage sensor on the UNO R3

Read voltage

The sketch reads the value at the analog input and converts it to a voltage using the map command. Because map only works with whole numbers, the value is first mapped to the range 0 to 250 and then divided by 10 - this produces the decimal place.

Measure the voltage and display it in the serial monitor.
float reading;
float voltage;

void setup()
{
  // Start the serial connection. This is necessary to send the results later to the
  // serial monitor using the Serial.println command.
  Serial.begin(9600);
}

void loop()
{
  // Read the voltage value at analog input 1
  reading = analogRead(1);

  // Convert the sensor value using the map command. Since map does not return decimal places,
  // larger numbers are used here for now.
  voltage = map(reading, 0, 1023, 0, 250);

  // Divide the result by 10 so it can be output as a correct value with one
  // decimal place.
  voltage = voltage / 10;

  // Display the final voltage value in the serial monitor. The number 2 in parentheses
  // ensures that the result is displayed with two decimal places. A 1
  // would give one decimal place.
  Serial.println(voltage, 2);

  // Wait one second so that too much data does not arrive in the serial monitor.
  delay(1000);
}

Check result

After uploading, open the Serial Monitor. A voltage reading will appear every second. As long as no voltage source is connected, it should read 0.00.

To test it, measure the Arduino's own 3.3 V pin. The Serial Monitor should then show a reading of around 3.3.

Arduino IDE serial monitor with voltage readings output every second
The readings in the serial monitor
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.