# Measuring voltage on an Arduino Being able to measure voltages automatically with a micro-controller is a very useful feature. Although the ADC on a micro-controller is limited to measure between 0V and 5V, this range can be extended quite easily using a special type of resistor network called a resistor divider.

In this tutorial I will be explaining how to read a voltage on an Arduino into an integer and how to convert that to a voltage. In future tutorials I will show you how to expand or narrow this measuring range.

## Measuring a voltage on an Arduino

Reading a voltage on an Arduino input pin is a relatively simple task. Three actions have to be taken to complete a measurement.

1. Configure an IO pin for analogue reading
3. Convert this integer to a voltage

### Configure an IO pin for analogue reading

Six of the IO pins on the Arduino UNO are capable of reading analog values. These pins are named A0 through A5. In this example I will be using A0. You can change the pins function to input by using the pinMode() function. I will put this inside an empty application in the setup function because it only has to run once.

```void setup()
{
pinMode(A0, INPUT);
}

void loop()
{
}
```

The ADC in the micro-controller converts the voltage it senses on the input pin into an integer values ranging from 0 to 1023. 0V will be 0 and 5V will be 1023. We read this values by using the function analogRead().

```int value;

void setup()
{
pinMode(A0, INPUT);
}

void loop()
{
}
```

Now we have stored the value into the variable called ‘value’. I want to have a look at that value. Because I want to read these values on my computer, I will add Serial.begin() to the setup() function. This will enable the Arduino to communicate to the PC over a serial connection. This serial connection is emulated by the USB driver.

The number 9600 is a speed setting for the communication. Both the computer and Arduino have to use the same speed in order to properly communicate.

The second addition will be in the loop() function. After every ADC reading I will send this value through the serial connection to the PC using the Serial.print() function.

```int value;

void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
}

void loop()
{
Serial.print(value);
Serial.println();
}
```

## Testing with a potentiometer

Because a potentiometer is a resistor divider, I attached one to the Arduino to use as a variable voltage source. The outer leads go the +5V and ground and the slider lead is attached to A0, the pin I will do a reading on.

While connected to the computer, I opened the serial monitor windows in the Arduino IDE. This gives me a stream of endless reading from the A0 pin.

The same can be done with the serial plotter, also included in the IDE. This gives me a nice graph while turning randomly on the potentiometer.

## Convert this integer to a voltage

```int value;
float voltage;

void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
}

void loop()
{
voltage = map(value, 0, 1023, 0, 500) / 100.0;