# AC Voltage measurement using PIC16F877A microcontroller

AC voltage measurement using pic microcontroller and op-amp.You have came across many online tutorials  on various websites about voltage measurement using different microcontrollers. But all these tutorials are about measurement of low  DC voltage.In this project, you will learn how to measure high AC voltage using PIC16f877A micrcontroller.

In this tutorial, We will discuss ac voltage sensing with two method:

1.  Using difference amplifier method
2. Using potential transformer method

## Alternating voltage measurement

To measure 220V AC, you need to step down this voltage. Because microcontrollers can’t measure voltage greater than 5V. If you give voltage more than 5V to the analog input of microcontroller, it will get damage permanently. To assure protection of microcontroller, you will need to step down 220 volt AC into AC  voltage whose peak value should be less than 5V. For example , 220V AV mean RMS voltage and its peak value is equal to 311 volt. similarly you have to step down high AC voltage of in such a way that its peak value should not be greater than 5 volt.

There are two methods to step down 220 alternating voltage into low alternating voltage which peak value should not be greater than 5 volt.

• Potential Transformer ( All Electrical Engineering students must know about P.T and its use)
• Difference amplifier  ( I will discuss difference amplifier method  in this project.)

Potential Transformer can also be used to step down 220 Alternation voltage.But why you want to spend more money? when you can do this with the help of cheap operational amplifiers and few resistors. Difference amplifier method is more economical than potential Transformer when you want to step down voltage less than 400 volt AC.

NOTE: Difference amplifier method is economical for voltage measurement less than 400 volt. Because above 400 volt, this method become expensive than potential Transformer. There are reasons behind it. I am not going to discuss reasons here.This method is suitable for final year students who want  to measure Alternating voltage and current.

## Difference Amplifier circuit

Difference amplifier is used to amplify voltage from two voltage levels.In case of Alternating voltage we have two voltage level one is positive with respect to neutral and other is negative with respect to neutral. I will recommend you to Google about difference amplifier and its use.

you can adjust the gain of difference amplifier according to our requirement by selecting proper  values of resistors.In this project gain is equal to :

Gain=  R8/(R1+ R2+ R3)  ;

In Alternating voltage case second voltage level is zero. Because during positive cycle and negative cycle other side is considered zero or neutral. So output voltage will be

vout = gain * Vinput;

In Above picture, resistor R1, R2, R3 R4, and R5 have high values which do not allow high voltage to appear across op-amp. Because high input resistors are used that’s why micro ampere current will low and this way power loss will be in mili watts. According to difference amplifier gain formula our gain will be:

gain=   (22K)/( 1.2M + 1.2M + 2.2K) = 0.0091

NOTE:  you must make calculation according to peak value of sine wave. Because peak voltage is the maximum voltage input to micrcontroller analog pin.So with a .0091 gain, with respect to peak voltage of sine wave output voltage from op-amp is :

Vout = .0091 * 311 = 2.8301 volt (peak output voltage)

As you see above figure we have connected other terminal of R7 to 5 volt instead of  ground as we do while using difference amplifier in many applications. R7 resistor is used to increase DC voltage level op-amp output. Sine wave have zero DC voltage level and  negative voltage cycle. Microcontroller can not read negative voltage.So we increase the DC level of sine wave by 5 volt.In this way negative voltage will not appear across  microntroller. Now output peak voltage from op-amp is 5 + 2.8301 = 7.8301 volt. But as I have mentioned , microcontrollers can not measure voltage greater than 5 volt.So as shown in above figure we have used voltage divider to divide voltage by 2.Hence output voltage is:

Vout = 7.8301/2 =  3.90155;

Capacitors C1, C2, and C3 are used to filter harmonics from input voltage and to provide protection to micrcontroller from harmonics.Now AN pin can be connected to microcontroller analog pin to measure voltage easily.

### Video lecture on AC voltmeter design

To know about how to measure analog voltage using analog module of PIC16F877A microcontroller go through PIC microcontrollers tutorials..

PIC MICROCONTROLLERS TUTORIALS

## COMPLETE CIRCUIT DIAGRAM

To know about LCD interfacing with PIC micronctrollers, go through pic microcontrollers        tutorials

PIC MICROCONTROLLERS TUTORIALS

Complete circuit diagram:

### AC voltage measurement code

Code for this project is written using Mikro C.To download code for AC voltage measurement click on link below :  [sociallocker]

```sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
float v;
char txt;
char txt1;
{
float max;
int i;
int t;
for(i=0; i<=39; i++)
{
v =v*(10.0/1023.0);
v=(v-5.0);
t[i]=v*110.1909091;
}

max=t;
for(i=0; i<=39; i++)
{
if(max<t[i])
max=t[i];
}
max=max*.707106781;
intToStr(max, txt);
Lcd_out(1,9,txt);
delay_ms(1000);
}

void main()
{
Lcd_Init(); // Initialize LCD

while(1)
{

Lcd_out(1,1, "Voltage:");
}
}```

Don’t forget to subscribe us to get info about next useful projects in your inbox. if you want to purchase simulation in 15\$ US. Contact me at [email protected] After sending payment, send me email, I will send you code and simulation in your email. thanks

[button-brown url=”http://store.microcontrollerslab.com/product/ac-voltage-measurement-using-pic16f877a-microcontroller/” target=”_blank” position=”left”]Buy code and proteus simulation in 15\$[/button-brown]

## Second method to measure AC voltage

In that article I have discussed each and everything you need to know to make ac voltage measurement project. In that article, I have used operational amplifier as a difference amplifier to step down ac voltage level from 220 volt ac to less than 5 volt ac. But I have seen many people struggling with that method. So I have decided to write a article on how to measure ac voltage using potential transformer and pic microcontroller with digital lcd display.

### How to measure ac voltage:

Ac voltage can be measure with following methods:

• AC voltage measurement using digital millimeter
• AC voltage measurement using analog voltmeter
• AC voltage measurement using microcontroller
• AC voltage measurement using potential transformer and digital display (I will discuss this method in this tutorial)
• AC voltage measurement using difference amplifier and pic microcontroller

As I have already discussed in this project, I will be using potential transformer to step down 220volt ac voltage to less than 5 volt ac. I will discuss it later why we need to step down ac voltage to measure it with the help of pic microcontroller.

### Main components of ac voltage measure project:

Followings are the main components of ac voltage measurement project. Brief descriptions of all components are also given below:

### What is potential transformer (PT)

Potential transformer is a type of transformer which is used to step down ac voltage. It is used to measure ac voltage by stepping down AC voltage. For example in this project, potential transformer is used to step down 220volt ac voltage to 12 volt ac. Potential transformer secondary winding have less turn than primary winding’s. So according to below mentioned turns ratio formula, it step down ac voltage.

Ns/Np = Vs/Vp

### What is bridge rectifier?

Bridge rectifier is electronics circuit used to convert ac voltage into pulsating dc voltage. In other words it converts, negative cycle of AC voltage into positive cycle. Now the question is why we need bridge rectifier in this project? Because microcontrollers can’t reads negative voltage.  Therefore we need to convert negative half cycle of AC voltage into positive cycle. Bridge rectifier is made using rectifier diodes connected in a form to form a bridge. 1N4007 rectifier diodes are used to make H Bridge.

### Voltage divider circuit:

Voltage divider circuit as it name suggests is used to divide voltage. Two resistors are used to as voltage divider. Potential transformer step downs 220 volt AC in to 12 volt AC. After that bridge rectifier is used to convert 12 volt ac into pulsating dc.  But microcontroller cannot read voltage more than 5 volt. Therefore voltage divider circuit further divide the voltage in two parts and less than 5 volt appear across analog to digital converter pin of pic microcontroller. I will discuss later about analog to digital converter.

### Liquid crystal display:

Liquid crystal display or LCD is used to used to display value of measured ac voltage. 16X2 LCD is used in this project.  LCD is interfaced with pic16f877a microcontroller. IF you don’t know how to interface LCD with PIC16F877A microcontroller, check following article:

LCD interfacing with pic16f877a microcontroller

PIC16F877A microcontroller:

PIC16F877A microcontroller is used in this project. PIC16F877A microcontroller is belongs to 16F family of pic microcontrollers. It have built in analog to digital converters module. Some of basic features of PIC16F877A microcontroller is given below:

For more information about pic16f877a microcontroller features and if you are new to microcontroller’s worlds, check following article.

Getting started with PIC16F877A microcontrollers

## Circuit diagram of how to measure ac voltage using microcontroller

Circuit diagram of how to measure AC voltage project is given below. I have already explained all the components of this project above.

Input to circuit is 220 volt AC voltage. Potential transformer step downs 220 volt AC voltage in 12 volt AC. After that bridge rectifier converts step down AC into pulsating dc voltage. Voltage divider further divides the voltage into two parts. Voltage less than 5 volt appear across analog to digital converter pin of pic16f877a microcontroller. Microcontrollers are basically small micro computers which understand only digital values. Built in analog to digital converter module of pic16f877a microcontroller converts analog values of AC voltage into digital values. These digital values are used in processing of data with in microcontroller. Pieces of instructions written in the form of coding told microcontroller what to do. Microcontroller itself do not do anything. You tell him to do whatever you want him to perform by writing pieces of information called program.

### Simulation result of how to measure ac voltage project:

Diagram below shows simlation results of ac voltage measurement project. LCD displays 220 volt AC which is measured with the help of microcontroller and necessary components connected with the microcontroller and potentioal transformer.

### List of components :

List of required componensts for how to measure ac voltage is given below:

```Category,Reference,Value,Order Code
Capacitors,"C1",22pF,
Capacitors,"C2",22pF,
Resistor R1 = 30K , R2 = 10K, R3 = 10K;
Integrated Circuits,"U1",PIC16F877A,
Diodes,"D1",1N4007,
Diodes,"D2",1N4007,
Diodes,"D3",1N4007,
Diodes,"D4",1N4007,
Miscellaneous,"TR1",TRAN-2P2S PT,
Miscellaneous,"X1",CRYSTAL 8MHZ,
```

## Program for how to measure AC voltage using PT

program given below is written using MIkro C compiler.

```// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

float maxpoint = 0;
int i;
unsigned int temp=0;

char ch;

void main()
{

TRISA = 0XFF;// All input
TRISB0_bit = 1;//set as input
TRISB1_bit = 1;//set as input

// Initialize LCD configuration...
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

while(1)

{
Lcd_Out(1,1,"AC voltage");

for(i=0;i<500;i++)
{
{
maxpoint = temp;
}
}
maxpoint = ( maxpoint * 5 )/ (1023) ;
maxpoint = maxpoint * 4;
maxpoint = maxpoint + 1.4;
maxpoint = maxpoint * 18;
maxpoint = maxpoint * ( 1 / sqrt(2) );
intToStr(maxpoint, ch);
lcd_out(2,1, Ltrim(ch));
maxpoint = 0;
}// while
}// void mai```

### 257 thoughts on “AC Voltage measurement using PIC16F877A microcontroller”

1. tahir

Hi,
can you send me its code and simulation file……………?
thanks ,

• BILAL

Hello can you help me for the code.

2. uday

hi bro please send me code for this AC voltmeter and ammeter projects

Thank you

3. idreesmuhamed

hello , iam idreesmuhamed , in the fact , your project is very important because iam electric engineer
, please send to me the code and iam be thankfull for you .

4. Visitor

Dear i am new here on your blog.Can you please send me the code of this alternating voltage measurement project.

• BILAL Malik

5. Deepak

The information provided on your page here turns very useful for me and i thanks you for this.
I have made the simulation but having a little problem with the code so if you send me the code then i am very grateful to you.
I am an electrical engineering student in Delhi Technological University and the topic here on this page is similar to my minor topic so please help me and send the code as soon as possible.

• BILAL Malik

• Wak

Please I need to measure three phase voltage line to line and three. Phase current by using pic18f4550

6. prabakaran

good tutorial can u send me the simulation

• BILAL Malik

Use pictures given in article to draw circuit diagram

7. Abuzur

Dear Bilal,
Good work keep going, send me the source code in .c file if possible.

8. pulsetronics

9. pulsetronics

Pls try to elaborate on your ADC conversion using differential amplifier in the software… Thanks

10. vu bao

hi! can you explain me how to find the formular “for” look of source code:

v=v*(10.0/1023.0);
v=(v-5);
t[i]=v*110.1909091;

thanks you so much!

• Amanti

Hi please send for me the code
thanks so much!

11. DHURGHAM

thank u so much Mr.bilal

12. Mrunal

Nice work! you have shown a easy way to interface and measure high voltage using microcontroller. Thank you. but I am having some doubts, after calculating current through resistors, it is 9.57×10^-5 so by calculating voltage drop across R1, so (9.57×10^-5) x(2.2M) is 219.34V it should be low isn’t it? please guide me.

13. Vijay

Thank you for sharing with us! I have assembled this ckt on bread board, without interfacing it with PIC. So the output voltage should be 7V DC(without voltage dividing) isn’t it? but mine is getting 5V DC so what might be the problem? or its normal ?

• BILAL Malik

check your circuit connections and its also depend on maximum input voltage to difference amplifier

• Hashim Ashraf

If you are using a multimeter to measure the o/p voltage it shows the average voltage . use dso instead.

14. Mrunal

Hi, will you please explain your code of voltage_Read routine? I didn’t get how you have done calculation.. Thank you.

15. frendy

Hi Bilal Malik

I would like to ask is this circuit is also applicable to 110Vac. I was able to try your circuit in 220Vac and I was successful on it but when I try to 100Vac it shows different voltage readings. I appreciate if you reply on my inquiry.

thanks
frendy

16. Mrunal

my LCD is showing 388V always, though I remove input from LM258! what might be the problem?

17. Mrunal

Now its working! thanks man! but reading is not constant! fluctuating

• BILAL Malik

you are checking it on hardware or software ?

• Mrunal

I am checking it on hardware. And now reading is constant. I added one 0.1uf cap.

• Nitz

• Mho

please is it working alright for you? where did you add the capacitor?

• Mho

Please where did u add the cap? I am also having the same fluctuation issues, and I eally need this project urgently.

• Nasaii Mustafa

Is it this project for measure voltage and current ? or voltage only

• Bilal Malik

voltage only you can buy complete code for current and voltage both

18. Mrunal Ahirrao

19. Tung

That is great, Please send me the code and simulation file for voltmeter and ammeter project

Thank you so much!

20. Justin Jose

Please send me the code and schematic of voltmeter and ammeter projects.

great work,, pls send the code in mikroC. would love to see this work on my pc.

• BILAL Malik

code link is given in article. check the article again

22. mpandi

Nice post Please send schematics and code .i am also interested in MCU projects Kindly help me..

23. mahdi

hi dear

24. Mase Ashipala

Hi there please send me your code for this AC voltmeter and ammeter project. I am very much interested in this project .
Thanks a million

25. Pandi

I am very much interesting to do PIC mcu projects with mikroC compiler. Especially AC voltage measurement by PIC mcu.Kindly send me the complete circuit diagram and Source code for 16F 877A AC voltage measurement projects.

• Bilal Malik

Code link is given at the end of post

26. TAnvir Mahtab

Hi,
I am Tanvir Mahtab from Bangladesh. Is this circuit work in 220VAC ?

e-mail: tanvirbl[email protected]

Thank you.

• BILAL Malik

yes it can measure 220volt AC

27. RaviKumar

Can you send the complete source code with proteus file

• BILAL Malik

code link is given at the end of post

28. kalyan

please send the ckt and source code of pic for voltmeter and ammeter

regards
kalyan

29. Jalal Khan

Plz send me the mickroC code and proteus file. I am an electronics engr

30. lacika

Hi,
can you send me its code and simulation file……………?
thanks ,

31. chitra

Sir,
will you pls mail me code of pic16f877a for closed loop voltage regulation of a converter in MPLAB if possible
Regards,
Chitra

• BILAL Malik

I will try to write article on it

32. Prashanth

Plz send me a code..i cant open voltage code.txt in ur link provided..plz help me..

33. shukri

hello siir..could you sent me the code programming for this project?i need your..thank you…

34. Justin

Your post is a life saver, thanks. Can you send me the code buddy 🙂

35. trinh dat

very good
please send me its code and simulations
thanks you

36. R K

Hi Friends,
Can you explain this step on the source code:

v=v*(10.0/1023.0);
v=(v-5);
t[i]=v*110.1909091;

please explain how to find 110.1909091 value
Thank U

• BILAL Malik

It is inverse of gain of difference amplifier

37. R K

Thank u BILAL Malik friend ,

How to change the source code to the measure 3phase voltage (0-600v )

• BILAL Malik

try to adjust the gain of difference amplifier

38. R K

Hi friend
I am try to adjust the gain of difference amplifier to change R8 value change to 15KOHm
gain =15K(1.2m+1.2m+2.2K)
gain = .0062443

source code
v=v*(10.0/1023.0);
v=(v-5);
t[i]=v*160.1467

input voltage 14V but display voltage 21V
how to sl the err plz tell frd ……..

i want source code

• BILAL Malik

source code is given in post

40. Iro

Dear Bilal,

I’m having proplems with the circuit. When I plug in the mains , the fuse turns off.
Have you any idea what the problem could be?

Iro

41. Bracoch

Thenk’s!!!

thank you it is good project

43. JOEL

hi,
thanks for such an interesting and nice tutorial. please send me the source code and simulation.
[email protected]

44. Jowah Lewis

Good day Sir,
I cant find the circuit for the input AN2 to the microcontrollers pin 3. thanks in advance

45. ajikumar

plz share code

• BILAL Malik

code is given in post

46. shiva

sir
we are using pic18 . how to provide input to pic18 from potential divider circuit. ? code is not clear please explain in detail. please reply to my email id.thank you……….

• BILAL Malik

this code is not for potential divider. you have to make little bit changes in code to use directly with voltage divider second method can be used

47. ali

hi
please send me code for this AC voltmeter and ammeter projects
Thank you

Hello Sir

Thanks a lot

49. Mahreen

sir can you please send me the MPLAB code using potential divider to this mail address.
[email protected]

50. alireza

Hello
Thank you.

Hi please send perfect project to me
Tanks

52. Morsi Jaber

please send me code and isis simulation thank you very much

53. Mayuri

Kindly share the complete code with comments.I am not able to understand the code.

54. abdullah

i need code project

55. ayat

hi any one have model related this topic ”
automatic load sharing of transformer by using gsm technique

• Bilal Malik

you can purchase code from me fot automatic load sharing of transformer

• ayat

is there any matlab or proteous simulation ? if there is how much its price?

56. Gourab

Dear sir, Thanks for the tutorials. Can you send me the code for Atmega16 microcontroller. Thanks in Advance. My Email Id: [email protected]

• Bilal Malik

pic code is given. You can hire me to write code using Atmega16

57. chand hanif

Dear Sir, can we use lm358 in place of lm258. will it work while measuring 230 VAC.

58. chand hanif

dear sir, Thanks for a fruitful tutorial. can lm358 work in place of lm258 fo measuring ac 230 V

59. althaf k

sir,please give me complete coding and circuit diagram of this project(AC voltage measuring )
my Email id: [email protected]

60. Paccy

HELLO SIR,
Current value is also shown in the simulation pic. sir can you please explain how that current is measured.

• Bilal Malik

61. jemshihas

hi
i appreciate for your great work…
sir what is the relevance of 40 in int t;

• Bilal Malik

Everything is given in article, What else you need?

62. safeedeen

Hi, Do you measure the peak of Pulse?

• Bilal Malik

Yes you can measure it using same concept

63. ‫حسان محمد الشيخ سعيد‬‎

Pleassssssss Can you sent me the simulation file to [email protected]

64. Nigromante

Please send code and circuit diagram the difference amplifier method’s to [email protected]. thank you

65. Azlan rizal

Hai,
For the first method using difference amplifier, what is the wattage for resistor R1,R2,R3 and R4? Is it 10w,5w or 1/4w ?

• BILAL Malik

quarter watt resistors are enough

66. rocksstar

waw you ara amazing

67. alif osman

hello sir,
can you send me the complete file for this project.
my email: [email protected].
thank you.

68. omer

Hello,
Will you please send me the code and Simulation of the difference method. @ Bilal Malik
email: [email protected]

69. Rolando

Great project.

Please send me the schematic diagrams and codes.

70. KPBS

Can you please send me the voltage and current full circuit and full code to display both current and voltage as shown in the post.

71. Richard

[email protected]
Please send me the whole circuit and full code. If you have the Python code that will be really nice!

72. Jagatheeswaran

I just did simulation in proteus and I dont know how generate the sine how you generated. Can u please help me??

My mail id: [email protected]

73. sura

Comment Text*i prefer to do it .. so it will be helpful for me receiving ur full code n cct

74. king mayor

I love this , Pls elaborate on code using lm258
And kindly pls send me the full code on my email

75. Sarfaraj

can i make the same project using 8051 microcontroller?

• BILAL Malik

yes it can be designed

76. Vinoth

Hi,
can you pls send me its full code ……………?
thanks ,