In this tutorial, we will learn how to measure voltage using Arduino. By using Arduino and this voltage measuring sensor, we can measure voltages up to 25V.
We also display the voltage reading on an LCD 16×2 Display. In this article, we measure external voltage using Arduino. You can also call it a digital voltmeter.
For measuring the voltage we use an analog pin of Arduino.
Circuit For Voltage Sensor
Below is the circuit of the voltage sensor.
Note:- If you are measuring the voltage of 25V use R1 = 35Kohms and R2 = 7.5Kohms. It will save your Arduino Analog Pin.
Interfacing a Voltage Sensor with Arduino
It is very simple and straight forward to measure the voltage. Follow the below circuit diagram and make sure you use the correct resistors.
Circuit Diagram for Measure Voltage with Arduino
Code for Measure Voltage with Arduino
Upload the below code and open the serial monitor to view the voltage readings.
This sketch relies on the fact that the analogRead value is a ratio of the measured voltage to the reference. But because the measured voltage is divided by two dropping resistors, the analogRead value needs to be multiplied to get the actual voltage.
Display Voltage Readings on an LCD Display 16×2
Now we will display the voltage readings on an LCD display. So for this, we have to connect the LCD display with Arduino. Follow the below circuit diagram.
Circuit Diagram of Voltage sensor and LCD 16×2
Code for Voltage Sensor and LCD Display
Responding to Change in Voltage
Monitor one or more voltages and take action when the voltage rises or falls below a threshold.
For example, to flash an LED to indicate a low battery level, perhaps to start flashing when the voltage drops below a warning threshold and increasing in urgency as the voltage drops further.
This example starts flashing an LED at 1.2 volts and increases the on-to-off time as the voltage decreases below the threshold. If the voltage drops below a second threshold, LED stays lit:
Code for Responding on Change in Voltage
The line if (val < (warningThreshold * 1023L)/5000) in this sketch calculates the ratio of the value read from the analog port to the value of the threshold voltage.
For example, with a warning threshold of 1 volt and a reference voltage of 5 volts, you want to know when the analog reading is one-fifth of the reference voltage.
The expression 1023L tells the compiler that this is a long integer, so the compiler will promote all the variables in this expression to a long integer to prevent overflowing the capacity of int (a normal 16-bit integer).
When reading analog values, you can work in the units that are returned from analogRead, ranging from 0 to 1023, or you can work in the actual voltages they represent.
As in this sketch, if you are not displaying voltage, it’s simpler and more efficient to use the output of analogRead directly.
Some More Articles For You: