# Voltmeter and Ammeter using PIC Microcontroller

## Voltmeter and Ammeter using PIC Microcontroller

Voltmeter and Ammeter can be easily made using PIC Microcontroller having ADC (Analog to Digital Converter). I am using PIC16F877A and the result is displayed on an LCD Display. PIC16F877A is enough if you do this project only for testing purposes. I suggest to use PIC with low pin numbers and multiplexed 7 segment display if you wish to use this as your measuring instrument.

# Prerequisite

If you don’t know the basis of PIC ADC and LCD Interfacing please read the following articles.

ADC module of PIC Microcontroller converts the Signals on its analog pin to 10 bit binary data and it has software selectable high and low voltage reference input to some combination of VDD, VSS, RA2 and RA3. The analog input to PIC is limited to VSS and VDD voltages (0 – 5V) of PIC.

This circuit is designed to measure 0 to 30V. So we will map 0 to 30V to 0 to 5V by using a voltage divider. Current through a circuit can be measured by introducing  a 1 ohm resistor and measuring the voltage across it. To minimize the path resistance we will use .47 ohm special resistor with fuse (shown in figure) and current is calculated. Voltage and Current Sampling circuit is shown below. When the Input voltage is 30V (max) the voltage across 20K ohm resistor becomes 5V which is feedback to the analog pin RA2 of the PIC Microcontroller.

The voltage across .47 ohm resistor is also feedback to the analog pin RA3 via 100K ohm resistor. 5.1V Zener Diode is added in parallel to these analog input pins to protect PIC from over voltages.

The ADC module of PIC converts analog input to 10 bit digital number. We want to convert this digital to corresponding voltage in decimal.

• 0v  =  0 0 0 0
• 5v  =  1  1  1  1

Resolution = (Vref+ – Vref-)/(1024-1)   (as it is 10 bit ADC)

= 5/1023

= 4.887 mV

Thus it means that for a change in 4.887mV, the binary output changes by 1.

So voltage input to the analog pin of PIC can be calculated as follows…

```v = ADC_Read(2);  // ADC value of channel 2 (voltage)
V = v*4.89;       // Converting ADC value to mV
I = i*4.89;       // Converting ADC value to mV```

By using values V and I we can calculate the Input Voltage and Current across the Load (Connected across Output terminals).

Voltage across 20K resistor = V

Current through 20K = V/20K

Input Voltage = Current through 20K * 120K  (Current flowing to PIC can be neglected)

Thus,

`V = (V/20)*120;`

Voltage across 0.47 ohm resistor = V

Current through Load = Current through 0.47 ohm resistor = V/0.47

Thus,

I = I/0.47;

To display the results in LCD Display we need to convert these readings into string, we use the user defined function look() for it. It converts each digit in the reading to corresponding character (see the source code).

# Circuit Diagram

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

# MikroC Code

```// LCD module connections
sbit LCD_RS at RB5_bit;
sbit LCD_EN at RB7_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;

sbit LCD_RS_Direction at TRISB5_bit;
sbit LCD_EN_Direction at TRISB7_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
// End LCD module connections

char look(int a)
{
switch(a)
{
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
default:
return '.';
}
}

void main()
{
unsigned int v,vp,ip,i;
char *volt = "00.0";
char *current = "0.00";
CMCON = 0x07;
TRISA = 0xFF;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);

do
{
i = (i*4.89)/0.47;
v = ((v*4.89)/20)*120;
if(v!=vp || i!=ip )
Lcd_Cmd(_LCD_CLEAR);
vp = v;
ip = i;
volt[0] = look(v/10000);
volt[1] = look((v/1000)%10);
volt[3] = look((v/100)%10);
Lcd_Out(1,1,"Voltage = ");
Lcd_Out(1,11,volt);
Lcd_Out(1,16,"V");

current[0] = look(i/1000);
current[2] = look((i/100)%10);
current[3] = look((i/10)%10);
Lcd_Out(2,1,"Current = ");
Lcd_Out(2,11,current);
Lcd_Out(2,16,"A");
Delay_ms(250);
} while(1);
}
```

You may also use IntToStr() to convert integer to string