# DC Voltage Measurement

Voltage measurements are easy tasks through a micro-controller. If you have the knowledge of ADC’s then you can easily measure the voltage.

There are lot of blogs present on the internet explaining ADC’s. Here, we will look on the measurement parts of DC voltage using Atmel AVR micro controllers.

I will take the example of ATmega8.

We will need only two resistors, one zener diode and one Micro-controller and a LCD for display as shown in the above diagram. If we take 100k for R1 and and 10k for R2 then according to voltage divider formula.

```V = 10k/(100k+10k)(55V)

V = 5V```

Where 55V is the maximum voltage that one can apply. The answer of the above values will be 5V which shows the maximum range of this divider. We can easily increase our voltage range through changing the resistor values accordingly.

The reason behind using the zener diode is for the protection of our micro-controller. 5V zener should be used here as the zener will nullify the surges above 5v.  The AVCC pin is specially used as the reference voltage for the ADC. To display the voltage we can useLCD and serial communication as well. We will look on the both displaying techniques simultaneously.

For serial communication and LCD interfacing read the articles

4 – bit LCD Interfacing with Atmel AVR MIcrocontrollers

## Components Needed:

Click on the component name to buy the product from reliable sources:

## Coding Part:

In the coding part we will discuss only specific areas. You can download the whole code using the link present below. The code is written on the Atmel Studio 6.2. The code can also be compile through the ‘code vision’ software with some special changes.

It is a good practice to take more than one samples for measurements and then take average of that samples as shown below.  I took 10 samples for every measurement.

```for (i=0; i <= 9; i++){
volt = volt + v[i];
_delay_us(10);
}
volt = volt /10; // Average value of samples
conv = (double)volt * 5/1024; // to know the voltage on the pin
conv = conv * 110/10; // To convert that voltage according to divider formula```

In voltage measurements mostly we need the accuracy of 3 decimal places. In Atmel Studio I didn’t find the function which can convert the float type into character type. So, I made a function by myself which can convert the data for three decimal places. By a little change we can get more decimal places as well. The below function is for the data type conversion. To understand the function you have to study the whole code carefully. If u still have any problem feel free to contact us 🙂

```void long_float(){
volt = (int)conv; // to find the value of final voltage
if (volt < 1)
i=0;
if (volt < 10 && volt >= 1)
i=1;
if (volt >=10 && volt < 100)
i=2;
if (volt >=100)
i=3;

volt = conv*1000;

ltoa(volt, buf,10);

for(j=0 ; i < 5 && j < 3;i++,j++)
arr[j] = buf[i];

volt = volt/1000;

ltoa(volt, buf,10);

// To send data through Serial Communication
string_transmit(buf);
uart_transmit('.');
string_transmit(arr);
string_transmit("\r\n")

// To send data through Serial Communication
string_transmit(buf);
uart_transmit('.');
string_transmit(arr);
string_transmit("\r\n");
// To display data on LCD
lcd_gotoxy(1,2);
lcd_print(buf);
lcd_print(".");
lcd_print(arr);
lcd_gotoxy(1,1);
}```