Arduino Lesson #109: measure voltage with the Arduino

In this post, I want to show how to measure the voltage up to 5V with an Arduino.

Arduino measure voltage
Arduino measure voltage

Review

In the article Arduino Lektion 4: LED mit Fotowiderstand I showed how to read the value of a photoresistor and “visualize” it via a light emitting diode. What we actually did is to measure the voltage that the photoresistor lets through and then map this value to a PWM signal.

Arduino UNO with connected photoresistor via analog pin
Arduino UNO with connected photoresistor via analog pin

Alternatively, we can also build a small circuit with a rotary potentiometer, in which we can connect the voltage of 5V (which the Arduino supplies via pin 5V) and change it manually with a screwdriver.

Depending on the quality of the microcontroller, the value can be slightly below or above the value of 5V. However, if you don’t use a clone but an original Arduino UNO, it will deliver almost exactly 5V.

compare 5V voltage at Arduino UNO original and Keyestudio UNO
compare 5V voltage at Arduino UNO original and Keyestudio UNO

Sample code

In the following sketch I read the value from analog pin A0 and map it first to the possible PWM signal (0..255) and then to a value between 0 and 50 for calculating the voltage.

I deliberately choose the values from 0 to 50 here, so that I can later divide this by 10 and thus get a floating point value.

#define rotaryResistor A0 
#define led 9 
void setup() { 
   Serial.begin(9600); 
   pinMode(rotaryResistor, INPUT); 
   pinMode(led, OUTPUT); 
} 
void loop() { 
   int resistorValue = analogRead(rotaryResistor); 
   int ledValue = map(resistorValue, 0, 1023, 0, 255); 
   analogWrite(led, ledValue); 
   double v = map(resistorValue,0,1023,0,50); 
   Serial.print(v/10, 2); 
   Serial.println("V"); 
   delay(100); 
}

measure voltages greater than 5V

If you want to measure voltages higher than 5V, you have to use a voltage divider circuit.

In the following I show you the circuit diagram, if the input voltage “VCC” is up to 25V.

Circuit voltage sensor (voltage divider)
Circuit voltage sensor (voltage divider)

In this case a voltage of maximum 5V is output at “S” & “-“, which we can then measure again with our Arduino at the analog input.

Building the circuit with a breadboard

Components needed for the circuit

First we want to build the circuit on a breadboard. This has the advantage that we could still make a few modifications.

Layout

Structure of the voltage divider circuit on the breadboard
Structure of the voltage divider circuit on the breadboard

Structure of the circuit on a breadboard

Components needed for the circuit

After we have created and tested the circuit on the breadboard in the first step, we want to “perpetuate” it is on a breadboard. For this purpose, we need an additional small breadboard.

If you now connect the finished board to the Arduino UNO (yellow > analog pin A0, black > GND) you can see the voltage on the serial monitor of the Arduino IDE.

With this circuit, as already mentioned, voltages up to a maximum of 25V are possible!

DIY voltage sensor on Arduino UNO
DIY voltage sensor on Arduino UNO

Alternative, a ready sensor

Of course, it is easier with a ready-made voltage sensor.

Voltage sensor
Voltage sensor

I have already presented this sensor in the article Arduino Lektion 54: Spannungssensor.

And if I am to be completely honest, I have taken the circuit from exactly this sensor and rebuilt.
The sensor costs at ebay.de just under €4 incl. shipping costs (This time it is even, no matter whether you have the goods delivered from China or Germany.).

Leave a Reply

Your email address will not be published. Required fields are marked *