# 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.

## 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:

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;

Difference amplifier to step down voltage

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

LCD displaying voltage value..

Complete circuit diagram:

Alternating voltage measurement 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[5];
char txt1[5];
{
float max;
int i;
int t[40];
for(i=0; i<=39; i++)
{
v =v*(10.0/1023.0);
v=(v-5.0);
t[i]=v*110.1909091;
}

max=t[0];
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 bilalmalikuet@gmail.com After sending payment, send me email, I will send you code and simulation in your email. thanks

### 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.

How to measure ac voltage using microcontroller

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.

How to measure ac voltage using pic microcontroller

### 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 pic microcontroller:

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[5];

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

## ESP32 programming with Arduino VIDEO COURSE 73% DISCOUNT

1. tahir August 24, 2014
• masood March 24, 2016
• BILAL March 9, 2018
2. uday October 5, 2014
3. Prashant October 6, 2014
4. idreesmuhamed October 9, 2014
5. Visitor October 11, 2014
• BILAL Malik October 12, 2014
6. Deepak October 12, 2014
• BILAL Malik October 12, 2014
• Wak March 13, 2017
7. prabakaran October 12, 2014
• BILAL Malik October 12, 2014
8. Abuzur October 27, 2014
9. pulsetronics October 28, 2014
10. pulsetronics October 28, 2014
11. vu bao November 10, 2014
12. Aby Francis Wilson November 17, 2014
13. DHURGHAM November 19, 2014
14. naveen December 1, 2014
15. med December 26, 2014
16. ateeb akmal January 2, 2015
17. Mrunal January 6, 2015
18. Vijay January 7, 2015
• BILAL Malik January 7, 2015
• Hashim Ashraf March 30, 2017
19. Mrunal January 8, 2015
20. frendy January 12, 2015
21. Mrunal January 13, 2015
22. Mrunal January 17, 2015
• BILAL Malik January 17, 2015
• Mrunal January 20, 2015
• Nitz June 15, 2017
• Mho April 5, 2018
• Nasaii Mustafa March 14, 2017
• Bilal Malik March 14, 2017
23. Mrunal Ahirrao January 21, 2015
24. Tung January 22, 2015
25. Justin Jose February 2, 2015
26. Ayotomiwa Adekahunsi February 5, 2015
• BILAL Malik February 5, 2015
27. mpandi February 7, 2015
28. mahdi February 12, 2015
29. Asfand Sattar February 12, 2015
30. Mase Ashipala February 18, 2015
31. Pandi February 22, 2015
32. joynal March 2, 2015
• Bilal Malik May 7, 2015
33. abu shaid sujon March 15, 2015
34. Ajay Rai March 18, 2015
35. TAnvir Mahtab March 23, 2015
• BILAL Malik March 24, 2015
36. RaviKumar March 25, 2015
• BILAL Malik March 25, 2015
37. sajan March 27, 2015
38. mahdi March 29, 2015
39. kalyan April 4, 2015
40. Jalal Khan April 7, 2015
41. Abhishek c April 8, 2015
42. lacika April 8, 2015
43. chitra April 9, 2015
• BILAL Malik April 9, 2015
44. Prashanth April 9, 2015
45. vasyl April 16, 2015
46. shukri April 17, 2015
47. Justin April 24, 2015
48. trinh dat May 1, 2015
49. trinh dat May 1, 2015
50. quoc dat May 1, 2015
51. R K May 10, 2015
• BILAL Malik May 10, 2015
52. R K May 10, 2015
• BILAL Malik May 10, 2015
53. R K May 11, 2015
• BILAL Malik May 12, 2015
55. Iro June 4, 2015
56. Bracoch June 9, 2015
58. JOEL June 13, 2015
59. chinnathambi.J June 23, 2015
60. Jowah Lewis July 5, 2015
61. ajikumar July 5, 2015
• BILAL Malik July 5, 2015
62. shiva July 7, 2015
• BILAL Malik July 7, 2015
63. ali July 8, 2015
65. Mahreen July 13, 2015
66. alireza July 16, 2015
68. Morsi Jaber August 8, 2015
69. Mayuri August 10, 2015
70. Mayuri August 10, 2015
71. abdullah August 12, 2015
72. ayat August 26, 2015
• Bilal Malik September 6, 2015
• ayat September 7, 2015
73. Gourab September 5, 2015
• Bilal Malik September 6, 2015
74. chand hanif September 16, 2015
75. chand hanif September 16, 2015
76. althaf k October 1, 2015
77. Paccy October 6, 2015
• Bilal Malik October 8, 2015
78. jemshihas October 14, 2015
79. Kivutha Danson October 21, 2015
80. Tiệp Lê November 8, 2015
• Bilal Malik November 13, 2015
81. safeedeen November 11, 2015
• Bilal Malik November 13, 2015
82. joe li November 30, 2015
83. ‫حسان محمد الشيخ سعيد‬‎ December 5, 2015
84. ‫حسان محمد الشيخ سعيد‬‎ December 5, 2015
85. Nigromante December 12, 2015
86. Azlan rizal December 25, 2015
• BILAL Malik January 9, 2016
87. rocksstar January 9, 2016
88. alif osman January 10, 2016
89. omer January 14, 2016
90. Rolando January 18, 2016
91. KPBS February 1, 2016
92. Richard February 9, 2016
93. Jagatheeswaran February 9, 2016
94. sura February 13, 2016
95. king mayor February 13, 2016
96. Sarfaraj February 22, 2016
• BILAL Malik February 22, 2016
97. Vinoth February 23, 2016
• parthi February 26, 2016
98. fix February 28, 2016
99. fix February 28, 2016
100. Ali Farkouh February 29, 2016
• Ali Farkouh February 29, 2016
101. Thushara March 1, 2016
102. che kinsley neba March 6, 2016
103. che kinsley neba March 6, 2016
104. Sayantan Ghosh March 12, 2016
105. jaffar March 15, 2016
106. raouh March 15, 2016
107. marie March 15, 2016
108. Ajlal March 16, 2016
• BILAL Malik March 17, 2016
109. ryu March 21, 2016
• ryu March 21, 2016
110. Donald J Murray March 31, 2016
• tejas shete February 22, 2017
111. Kevin Sekeres April 1, 2016
113. Hai Luong Nhu April 20, 2016
114. Dheepu April 25, 2016
115. kamal April 28, 2016
116. Foma May 10, 2016
118. raj May 30, 2016
119. Dov June 5, 2016
120. yahya June 6, 2016
• BILAL Malik June 7, 2016
121. yahya June 6, 2016
122. yahya June 6, 2016
123. yahya June 7, 2016
124. saheed mustopha June 27, 2016
126. keerthi July 7, 2016
127. Dien July 14, 2016
128. Dien July 14, 2016
129. Sarp July 15, 2016
130. Oluwatobi July 17, 2016
131. franka July 22, 2016
132. ashwati July 30, 2016
133. pip September 27, 2016
134. Kingsley September 28, 2016
135. Kingsley September 28, 2016
136. albert November 1, 2016
137. Alex Hunter December 14, 2016
138. Alex Hunter December 14, 2016
139. Rahul December 14, 2016
140. New Tan December 18, 2016
141. kumaresh December 21, 2016
142. tamil December 30, 2016
143. Ravi N January 3, 2017
144. tamil January 7, 2017
145. tamil January 7, 2017
• José Miguel March 31, 2017
• satheesh February 23, 2018
146. burak January 11, 2017
147. Jagdeep kataria January 14, 2017
148. saheed January 17, 2017
149. Pawan Meshram February 2, 2017
150. Jay March 1, 2017
151. francis March 7, 2017
152. Rohan March 9, 2017
153. Sandar Lwin March 12, 2017
154. Nasaii Mustafa March 13, 2017
155. Ramtec March 14, 2017
156. José Miguel March 31, 2017
• José Miguel March 31, 2017
157. Snoc April 5, 2017
158. Ali April 12, 2017
159. Nishit sehgal July 19, 2017
160. kang,yonkkoo July 23, 2017
161. Hossein September 10, 2017
• Hossein September 14, 2017
162. usha October 20, 2017
163. ayush kothari October 26, 2017
164. Joachim October 27, 2017
165. rohit October 29, 2017
• rohit October 29, 2017
167. Ayanda November 17, 2017
168. Milos December 22, 2017
169. gokula krishnan January 17, 2018
170. hosjiu January 19, 2018
171. chathuranga March 23, 2018
172. Mho April 3, 2018
173. s.vinoj April 12, 2018
174. omayma April 13, 2018
175. oumayma April 13, 2018
176. Bhautik April 13, 2018
177. Sanjay Dalvi April 17, 2018

#### Learn ESP32 programming with Arduino IDE with 73% DISCOUNT

LIMITED TIME OFFER