# 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 microcontrollerslabhub@gmail.com 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```

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

1. please i dont undertand when you write char ch(5) what does that 5 represent

2. please can i have the code in c++ language.

3. Hello,
thank you for this project ,please send me its code and simulation.

4. Great work!!

Please send me ALL the files (Full circuit diagram and code)?

Thanks.

• simulation and full circuit diagram is not free of cost

5. You could just use a voltage divider down to below 5V and use the PIC ADC at a 10Ksamples/s or some appropriate sample rate. This way you can do more than just get voltage……can calculate peak to peak, RMS, noise, etc…
However, certain applications have regulatory requirements to isolate 220V, such as using a transformer (or optical)….for safety reasons….i.e. A voltage divider might not be enough.

• sir please can u contact me by mail I Really need some help to find derivative using pic uc..my mail tejasshete74@gmail.com

6. Please send code and schematics, thanks!

7. please send me full code of voltage and current measurment

8. I have a question.
Why you not use bridge diode & divider voltage as beginning without potential transformer. We use another resistor values transforming 220V to less than 5V?

9. Can you please send me the full code and circuit diagram

10. Hi
while simulate , output always remain constant as 5 v
even when i change ac source voltage o/p still 5 v
or will it works by practical
help

11. please send me full code of voltage and current measurment

12. plz send me a code of this project with circuit diagram

13. Thanks for an informative lesson. I have a few questions. First, many people talk about the dangers of this kind of circuit not being isolated. Is this safe as long as the circuit is correctly implemented? If people are worried about AC voltage being connected to the CPU by a stray wire, then clearly this could happen to any design, even an optoisolated one.

Second, why the capacitor C3? What is that doing? It looks like just noise stabilization?

14. its great,
pls send me the simulation and code.

15. can i use Lm741 instead

16. can i also use rectifier to change to dc than adding 5v

17. Hello,
this is great contribution.
I want to use ur idea in geophysics.
After injecting a current from 220v ac modified sine wave with two electrode inserted in the earth, i will record the potential difference in another two electrodes that does not have any direct connection to the source electrodes.
so my questions are
1. can i use rectifier before/after the opamp to get positive voltage in micro-controller(i am using arduino uno).
2. can i replace the opamp with 741 ic
3. what do you suggest me to remove those noises(self potential of the earth) coming from the earth that does not have the same form from our modified sine wave. I saw some use synchronous rectifier tapping from the source as a reference signal.

18. anyone can explain the above mention code for voltage read?
why we multiply the ADC_read(0) value with 10/1023 and after this take minus 5 from it?
also tell me why we multiply it with 110.19 ?

thank you

19. How to choose Value Cap(C1,C2,C3)???

20. What if the input is half wave rectified AC? Then you measure the rms wrong by just processing the positive part..

21. please I need the code and simulation for AC voltage measurement

22. Hi, may I ask why is the input voltage for LM258 12v? Can I give it as 5V? Because If it is 12v, I have go put in an another DC source for this, which make it not really convinient compare to the project with transformers.

23. Hi Bilal,
Please, could you explain to me why there are “for i=0; i<= x; i++" to get voltage?

Why use x=39 or 500?

Thanks.

24. great job sir, i found your article here so intriguing and i am iterested. i will be very grateful if you send me the complete work with code for the AC voltmeter in my email @ kingsoftng@gmail.com. thank you sir! i am Kingsley, a computer engineering student from Nigeria.

25. please i need the complete code for the AC voltmeter so i can design the setup as you have already done. Thank you sir

26. Excellent help me a lot, almost to start a project about it.

27. Please send me code of the project , thanks from Buenos Aires

Alex

28. 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.
What are those reasons..?

29. Can you send me the source code and simulation?

30. Hi,
can i use above circuit for three phase voltage measurement, please give me any idea for that and thanks.

31. hi bilal i have no experience in c language but there is something i’d like to explain me .
i have a project of building an automatic voltage regulator in assembly language.

your post on ac voltage measurment using difference amplifier surpases my mind .

at the output of the op amp we should get an alternating voltage .so how can a pic read an altenative voltage ???
what should be the value of the input voltage? that should be written in the assembly program for comparison ???
what’s the value obtained after conversion in a 8bit pic ADC??? of 100v,140,180,200,220,240?

32. Hi
Wish You Happy New Year.
Nice Explanation. Great Work.
Can you Please share the complete code for the Measurement of AC voltge & Current.
ravirke@gmail.com
Thanks& Regards
Ravi

33. Hi
V= (10.0/1023.0); why 10?
V =(v-5.0); v-5.0 howw?
t[i]=v*110.1909091;
Tamilbig@gmail.com

34. hi how can a pic measure with accuracy the Ac voltage that comes from the op amp?
can you also explain it with some formulas??
in addition to that may you explain also
v =v*(10.0/1023.0); why 10????
v=(v-5.0); how v-5.0?????
t[i]=v*110.1909091;

• v =v*(10.0/1023.0); why 10???? because of the voltage divider -> v =2*v*(5/1023.0)
v=(v-5.0); how v-5.0????? Remove offset
t[i]=v*110.1909091; Inverse Differential gain

Regards

• Why we inverse differential gain?

• @José Miguel

v =v*(10.0/1023.0); why 10???? because of the voltage divider -> v =2*v*(5/1023.0)
v=(v-5.0); how v-5.0????? Remove offset
t[i]=v*110.1909091; Inverse Differential gain

v =v*(10.0/1023.0); why 10???? because of the voltage divider -> v
no way the maximum output from the ac setup can only amount to 3.915V given that the voltage divider halfs the output of the 7.831,
it doesn’t make sense why you would equate it to 10!!!!

t[i]=v*110.1909091; Inverse Differential gain ??? How is it calculated or the values just landed here from no where?

• Late reply but i hope this will help someone someday.

1. Why He used 10. Originally, the resolution should be 5000mV/1023 (5V/1023) but because the negative half cycle was previously ignored since PIC cannot measure negative voltages, to compensate for that, you multiply by 2. i.e from +5v to 0V to -5V for normal sinusoidal signal. Note the 5000mV is actually the 5V supply to PIC. It has been used because the ADC module is using the supply voltage to the PIC as its reference voltage (Vref).

2. About the V-5 ==>> Recall the DC level of the Stepped down signal was raised by 5V to eliminate negative voltages to the PIC Pin. Since the negative part of the cycle has now been adequately compensated in V = V * (5.0*2/1023.0), it is necessary to subtracted the added DC level raising voltage which is 5V. Thus, V-5.

3. On t[i] = V * 110.1909091; I believe he used the already stated formula
Vout = gain * Vin
Since we now want to measure Vin,
Vi = V/gain
which is same as
Vi = V * (1/gain)

1/gain = 1/0.0091 = 109.190909143.
Thus,
Vi = V * 109.190909143.
Can’t tell why he used 110.1909091 instead.
Note, t is a variable (placeholder for Vi) and it is logging 40 different Vi levels (i.e. from 0 to 39) with respect to i that will make for better measurement accuracy and precision…
Hence,
t[i] = V * 109.1909091

Thank you!

35. Hi Bilal,

Now for one of my project I decided to use above explained circuit.
I noticed one strange behaviour of above circuit which I am not able to understand fully.

Circuit gives exact calculated gain, when R1 is connected to neutral of grid & live wire to R4.
But moment we inter change the input connection gain of circuit comes totally different then previous case. Simulation does not show any behaviour like this.
did you observe anything like this ???

36. Thanks sir,more power to your elbow
I will like to have the full code sir

37. can u send me complete circuit diagram and code

38. Is a difference amplifier the same thing as a differential amplifier or are you referring to a method of measurement with an op amp? This was unclear to me.

Thanks,

Jay

39. I love this.please send me complete details on this.

40. Hello, I have a query. In the difference amplifier, I am giving the Amplitude of 311V (for 220v) but the LCD is showing 220v (No Fault) and for for the amplitude of 340V (240V), the LCD is showing 240v (No Fault).
Then I realized that the circuit trips (Shows fault) when the voltage reaches to 260V in the LCD.
I want to change this part. Can you please point out that which part of this code is needed to change?
Your project is really awesome, but if you help me a little, it will be very much appreciable.

41. great work,, pls send me the code and schematic of voltmeter and ammeter projects in mikroC.
thank you.

42. Hi Mr. Bilal Malik. actually im interested to the “Alternating voltage measurement circuit diagram(with complete code)”.Which is it can measure 240Vac and current flow. So how can i get the code and circuit? if need pay or anything. Please reply me on nasaiieng@gmail.com. Thank You Mr Bilal Malik

43. Hello. Thank you very much for your helpfull ac measurement circuit.
I built it in an pcb and make my own program. It work fine, but i am having a problem with the linearity.
I adjust the voltage for 120V and it look fine, but when the voltage is low than 60V, the voltage measured is more or less 4 to 5V different. When the voltage is 180V, the measurement is more or less 10V Different.

What could be the problem.?

• Can i get help about it?

44. And the divider voltage was not considered in the calculations….

v =v*(10.0/1023.0);
…………
v = v*2; -> divider voltage was not considered
…………
v=(v-5.0);
t[i]=v*110.1909091;

• Jajaja ok, I have paid more attention to C code.

v =v*(10.0/1023.0); // Here is v*2
Above is the same this:
v =v*(5/1023.0);
v =v*2;
Regards

• Hi can i get full c code of this code . Thanks so much!!

45. Very nice design and tutorial. But I would really appreciate it if I get the the complete file on the difference applifier type via my email.
Thanks.
Cyytechs@gmail.com

46. Hi,
why you are running for loop for only 39 times and where you are using t value, it is confusing me,
i am also not able to change the value of power supply to amplifier and to R11 resistor, it is by default Vcc, does that make any difference- dont know how to make 12v or 5v.

How i can measure voltage being supplied by AC voltage source.

Awaiting response

47. Hello dear,
thank you for this teach, I create this circuit but I have 2.5 volt in op-amp output(after R9), so I measurement input voltage, after R1,R4 I have 98.5V AC, after R2,R5 I have ~0.3 VAC and after R3,R6 I have ~0 VAC, so when I remove input 220 volt Output not change!!!
Can you help me for knowing where is my mistake???
thanks a lot.

• Hello,
I use LM358, and couldn’t give a normal output, so when I remove the Op-Amp. I can see 3.01 VAC after 2.2K resistors. but when op-amp is in circuit output voltage is 0!!!!

48. hey ,
osm teach
how you maintain it to show current values on lcd

• please send me other codes and details

49. can i get the simulation please

50. Hi, please send me the complete code and circuit diagram.

51. pic 16f877a controllers adc can measures dc pulsating voltage or not…… if not how can i convert into steady dc… (i.e my input 2.5vdc pulsating voltage am required same voltage in steady dc voltage)….pls help me….

• Input is not received in constant way , how we can use the formula.

52. Sir, please send me complete code and circuit diagram. thank you

53. Nice tutorial. I want to ask concerning the linearity of the circuit. Has anyone built it in real life? Is the linearity recommendable? I just ask because I tried it on proteus, and I didn’t get very impressive results, so I am curious if anyone has actually built it.
Thanks.

54. could you please send me the complete circuit diagram.

55. Can you send me a code for AC Voltmeter

56. please i need the complete code for the AC voltmeter so i can design the setup as you have already done. Thank you sir

57. I see that you have used Separate 5v source for incresing dc level and powering pic. What can you suggest if you want to achieve same just by using AC input . Could you Explain that

58. hi

59. help please i am getting a fixed -388v in my lcd.. i am using proteus and 16f876

60. Firstly thank you sir for ur support.
I did as u instructed but at output of my LCD it was giving me 388v please what is d solution sir.

61. Hi, I am looking for low cost ac voltage sensing circuit. would you pls email me the method using difference amplifier??

62. hello please send me code

63. Good day sir, I am Okoye Franklin from Enugu Nigeria, I what to no if I can get this project from you, How to Read AC voltage with comparator, Am using PIC16f886 MikroC.

• Just use the code, and change the registers to match with the ic u are using. I did the project using pic16f886, and it works quite well. Nice tutorial. I learnt a lot.

64. Hello can you please send me the C code for this project

65. Differential amplifier still needs a ground reference through a resistor or a voltage divider in your case if want to DC bias the output. Without this the output will get saturated due to supply limitation and voltage difference b.w the inputs. This will probably never give you a true representation of 50hz sine wave on the output.

66. Hi,

Pl send me the circuit diagram & code.

Thnaks

67. please send me the ckt and code for Alternating voltage and current measurement

68. Hi, please send me the code and circuit diagram .

69. i need to measure 220v ac using pic16f676 without comparator please send me the whole code and the schematic circuit
thank you