The Arduino Uno, like many microcontrollers, has a built-in Analog to Digital Converter (ADC) that can convert an analog voltage on a pin to a digital number. However, the maximum analog input pin voltage is limited to 5V. You may find this limit inconvenient if your project requires measuring voltages exceeding 5V. In such cases, you could create a voltage divider using discrete resistors.

But there’s an easier way to measure voltages, especially if they’re lower than 25V: use a Voltage Sensor. It is a pre-made voltage divider circuit that uses precision resistors to provide accurate readings.

In this tutorial, we’ll explore how to use a voltage sensor with your Arduino to make your voltage measuring tasks simpler and more efficient. Let’s get started!

## Hardware Overview

The Voltage Sensor, in essence, is a simple voltage divider circuit composed of two resistors–nothing fancy.

The schematic of the Voltage Sensor is illustrated in the following image.

There are two resistors in this circuit. The resistor (R1) closest to the input voltage, has a value of 30 KΩ, and the resistor (R2) closest to ground, has a value of 7.5 KΩ. The voltage drop across R2 is our divided voltage. This signal is broken out to a header pin labeled S.

This simple circuit divides the input voltage by a factor of 5. That’s why this voltage sensor can help you measure voltages that are less than 25 volts with an Arduino.

Reading the voltage sensor or any voltage divider, for that matter, is very easy. We can use the voltage divider equation.

The voltage divider equation assumes that you know three values of the above circuit: the input voltage (Vin), and both resistor values (R1 and R2). Given those values, we can use this equation to find the output voltage (Vout):

However, in our case, we will be measuring the output voltage (Vout) from the voltage divider circuit using Arduino’s ADC. Therefore, the value we do not know is Vin.

Let’s rearrange the above equation to solve for the input voltage (Vin).

This equation tells us that the input voltage (Vin) is the output voltage (Vout) divided by the fraction of the second resistor’s resistance (R2) over the total resistance (R1 + R2) in the circuit.

## Voltage Sensor Pinout

Now let’s have a look at the pinout.

### Input Terminal

VCC is connected to the positive terminal of the voltage source you want to measure. The recommended voltage range for this pin is 0 to 25V.

GND is connected to the negative terminal of the input voltage source.

S is the signal output pin of the voltage sensor module. It provides an analog voltage that is proportional to the input voltage level. It’s usually connected to one of the analog input pins on the Arduino.

+ is not connected to anything.

is the common ground pin.

## Hardware Hookup

Connecting a voltage sensor to an arduino is a breeze.

To begin, connect the voltage source that you want to measure to the input screw terminal. Then, connect the ‘S’ pin on the voltage sensor to the ‘A0’ analog pin on the Arduino and the ‘-’ pin to ground.

The image below shows how to connect everything.

## Arduino Example Code

Here is a simple sketch that reads the analog voltage on the analog pin A0, calculates the input voltage using voltage divider equation and prints the results to the Serial Monitor.

``````// Define analog input
#define ANALOG_IN_PIN A0

// Floats for ADC voltage & Input voltage
float in_voltage = 0.0;

// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;

// Float for Reference Voltage
float ref_voltage = 5.0;

void setup(){
// Setup Serial Monitor
Serial.begin(9600);
}

void loop(){

// Determine voltage at ADC input

// Calculate voltage at divider input

// Print results to Serial Monitor to 2 decimal places
Serial.print("Input Voltage = ");
Serial.println(in_voltage, 2);

// Short delay
delay(500);
}``````

To verify the accuracy, measure the actual voltage using your digital multimeter. The value displayed on the multimeter should match the reading shown on the serial monitor.

The following result is obtained when 5V is applied to the voltage sensor:

And when 12V is applied:

### Code Explanation:

This is a fairly simple sketch, so you should have no trouble following it. The sketch begins with the definition of a few global variables that will be used throughout the program.

The first line defines the analog input pin on the Arduino that we are using to read the voltage from the voltage divider circuit. In this case, we are using pin A0.

``#define ANALOG_IN_PIN A0``

Next, two floats, `adc_voltage` and `in_voltage`, are defined to store the voltage readings at the ADC and the input of the voltage divider, respectively.

``````float adc_voltage = 0.0;
float in_voltage = 0.0;``````

The `R1` and `R2` variables store the resistance values for the voltage divider. If you’re using a different set of resistors to make your own voltage divider, you’ll need to change them.

``````float R1 = 30000.0;
float R2 = 7500.0;``````

`ref_voltage` is the reference voltage for the ADC, typically 5V for an Arduino Uno.

``float ref_voltage = 5.0;``

The last variable `adc_value` is used to store the raw digital value read from the ADC.

``int adc_value = 0;``

In the setup(), we set up the serial communication at a baud rate of 9600.

``````void setup(){
Serial.begin(9600);
}``````

In the loop(), the `analogRead()` function is used to read the voltage on the A0 pin. The returned value is stored in the variable `adc_value`.

``adc_value = analogRead(ANALOG_IN_PIN);``

This value is then converted to a voltage (`adc_voltage`) by multiplying with the reference voltage and dividing by 1024 (as the Arduino has a 10-bit ADC, hence 2^10 = 1024 different values).

``adc_voltage  = (adc_value * ref_voltage) / 1024.0;``

The input voltage to the voltage divider is then calculated using the formula for a voltage divider:

``in_voltage = adc_voltage*(R1+R2)/R2;``

Finally, the calculated input voltage is printed to the Serial Monitor, displaying up to 2 decimal places. After this, the Arduino waits for 500 milliseconds before repeating the loop.

``````Serial.print("Input Voltage = ");
Serial.println(in_voltage, 2);

delay(500);``````