AC Voltage measurement using PIC16F877A microcontroller (original) (raw)

In this tutorial, we will learn to measure AC voltage using a pic microcontroller with two methods. One is using a potential transformer and the second one is using an op-amp as a difference amplifier. That means, in the first method, we will use a voltage transformer to step down 220V AC and in the second method, we will use an operational amplifier as a difference amplifier to step down high AC voltage. But for both methods, we will use pic microcontroller ADC to take AC voltage samples. For this tutorial, we will use the PIC16F877A microcontroller. However, the logic used in this tutorial can be used with other microcontrollers such as 8051, AVR, Arduino, Raspberry Pi, Beaglebone, etc.

You have come 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 methods:

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

Alternating Voltage Measurement using Difference Amplifier Method and Pic Microcontroller

To measure 220V AC, it is necessary to step down the voltage as microcontrollers are unable to measure voltages greater than 5V. Applying a voltage higher than 5V to the analog input of a microcontroller can result in permanent damage. Thus, in order to protect the microcontroller, it is crucial to step down the 220V AC voltage to an AC voltage with a peak value lower than 5V. For instance, 220V AC represents the RMS voltage, with a corresponding peak value of 311 volts. Therefore, it is essential to reduce the high AC voltage to a level where its peak value does not exceed 5 volts.

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

The Potential Transformer is capable of stepping down 220 Alternating current voltage. However, why would you want to spend more money when you can achieve this using inexpensive operational amplifiers and just a few resistors? The difference amplifier method proves to be more economical than using a Potential Transformer when stepping down voltages below 400 volts 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

The difference amplifier is a circuit used to amplify voltage between two different voltage levels. When dealing with alternating voltage, there are two distinct voltage levels: one is positive relative to neutral, and the other is negative relative to neutral. For more detailed information on the difference amplifier and its applications, I would suggest conducting a search on Google.

You can adjust the gain of the difference amplifier according to your requirements by selecting the proper values of resistors. In this project, the gain is equal to:

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

In the case of alternating voltage, the second voltage level is zero. This is because during the positive and negative cycles, the other side is considered to be zero or neutral. As a result, the output voltage will be zero during these cycles.

vout = gain * Vinput;

Difference amplifier to step down voltage

In the above picture, resistors R1, R2, R3, R4, and R5 have high values, which prevent high voltage from appearing across the op-amp. The use of high input resistors ensures that the current is in the microampere range, resulting in low power loss in the milliwatt range. According to the difference amplifier gain formula, the gain can be calculated as follows:

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

NOTE: Please make sure to calculate the peak value of the sine wave, as the peak voltage is the maximum voltage input to the microcontroller’s analog pin. Therefore, with a gain of 0.0091 in respect to the peak voltage of the sine wave, the output voltage from the op-amp is:

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

In the figure above, we can observe that the other terminal of R7 is connected to the 5-volt supply instead of the ground, which is typically done when using a differential amplifier in various applications. The purpose of the R7 resistor is to raise the DC voltage level at the output of the op-amp. Since a sine wave has a zero DC voltage level and negative voltage cycle, it becomes important to increase the DC level of the sine wave by 5 volts. By doing so, we prevent any negative voltage from appearing across the microcontroller. Consequently, the output peak voltage from the op-amp becomes 5 volts plus 2.8301 volts, resulting in a total of 7.8301 volts. However, it is important to note that microcontrollers are unable to measure voltages greater than 5 volts. To address this, as illustrated in the figure above, a voltage divider is employed to divide the output voltage by 2. This ensures that the output voltage level is within the microcontroller’s measurement range.

Vout = 7.8301/2 = 3.90155;

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

Video lecture on AC voltmeter design

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

Circuit Diagram

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

LCD displaying voltage value..

LCD displaying voltage value..

Complete circuit diagram:

Alternating voltage measurement circuit diagram

Alternating voltage measurement circuit diagram

Code for this project is written using MikroC. To download code for AC voltage measurement click on the link below:

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];
void voltage_READ(void)
 {
 float max;
 int i;
 int t[40];
 ADCON0.ADON=1;
 for(i=0; i<=39; i++)
 {
 v= ADC_Read(0); //Digital value convert
 v =v*(10.0/1023.0);
 v=(v-5.0);
 t[i]=v*110.1909091;
 }

 ADCON0.ADON=0;
 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
 ADCON0.ADCS1=1;
 ADCON0.ADCS1=0;
 ADCON0.ADON=0;
 
 while(1)
 {

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

AC Voltage Measurement using PT and Pic Microcontroller

In this section, we will see how to measure AC voltage using a potential transformer and Pic Microcontroller. In the last section, we have seen how to use an operational amplifier as a difference amplifier to step down AC voltage level from 220 volts AC to less than 5 volts AC. Here, we will delve further into the process of measuring AC voltage with the help of a potential transformer and a Pic Microcontroller. Understanding the intricacies and the practical applications of this setup is essential for accurate voltage measurements. By following the steps mentioned in the previous section, we can ensure that the AC voltage is accurately measured with the help of the potential transformer and Pic Microcontroller.

How to measure ac voltage?

Ac voltage can be measure with following methods:

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.

Components required

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

What is a potential transformer (PT)?

A potential transformer, also known as a voltage transformer, is a specific type of transformer that is used to decrease or “step down” the magnitude of AC voltage. Its primary purpose is to measure high voltage levels by transforming them into lower, more manageable values.

In this project, the potential transformer plays a crucial role in reducing the voltage from 220 volts AC to 12 volts AC. By utilizing a carefully calculated turns ratio, the secondary winding of the potential transformer consists of fewer turns than its primary winding, resulting in a significant decrease in voltage.

The turning ratio formula serves as a fundamental guideline for determining the appropriate number of winding turns, allowing the potential transformer to effectively step down the alternating current voltage and facilitate safe and accurate voltage measurement.

Ns/Np = Vs/Vp

What is bridge rectifier?

The bridge rectifier is an electronic circuit used to convert AC voltage into pulsating DC voltage. Essentially, it transforms the negative cycle of AC voltage into a positive cycle. So why do we need a bridge rectifier in this project? It’s because microcontrollers are unable to read negative voltage. Consequently, we must convert the negative half cycle of AC voltage into a positive cycle. The bridge rectifier is constructed by connecting rectifier diodes in a specific arrangement to form a bridge. For this purpose, we employ 1N4007 rectifier diodes to create an H Bridge.

Voltage Divider Circuit

The voltage divider circuit, as the name suggests, is used to divide voltage. It employs two resistors to achieve this. In this setup, a potential transformer steps down the 220V AC voltage to 12V AC. To convert the 12V AC into pulsating DC, a bridge rectifier is used. However, it is important to note that microcontrollers can only read voltages up to 5V. Therefore, the voltage divider circuit further divides the voltage into two parts, ensuring that less than 5V appears across the analog-to-digital converter pin of the PIC microcontroller. We will discuss the analog-to-digital converter in more detail later.

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:

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

The circuit diagram for measuring AC voltage is presented below. In this section, we have discussed all the components of this project.

How to measure ac voltage using microcontroller

How to measure ac voltage using microcontroller

The input to the circuit is a 220-volt AC voltage. A potential transformer is used to step down the voltage from 220 volts AC to 12 volts AC. After that, a bridge rectifier converts the stepped-down AC voltage into pulsating DC voltage. A voltage divider is then used to further divide the voltage into two parts.

The voltage of less than 5 volts appears across the analog-to-digital converter pin of the PIC16F877A microcontroller. Microcontrollers are essentially small microcomputers that only understand digital values. The built-in analog-to-digital converter module of the PIC16F877A microcontroller converts the analog values of the AC voltage into digital values. These digital values are then used in processing the data within the microcontroller.

Instructions written in the form of code instruct the microcontroller on what to do. The microcontroller itself does not perform any tasks on its own. You need to provide it with instructions by writing a program that outlines what you want it to do.

Proteus Simulation

The diagram below shows the simulation results of an AC voltage measurement project. The LCD displays a reading of 220 volts AC, which is measured using a microcontroller and the necessary components connected to it, including a potential transformer.

How to measure ac voltage using pic microcontroller

How to measure ac voltage using pic microcontroller

Code AC voltage Measurment using PT

The program given below is written using MikroC 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
ADC_Init();

// 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++)
{
if(temp = ADC_Read(0),temp>maxpoint)
{
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

These lines define the connections of the LCD module to the microcontroller’s pins. sbit stands for “single bit” and is used to specify the pin connections for the LCD control and data lines.

// 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;

These lines define the direction of the LCD pins. TRISBx_bit are used to configure the corresponding pins as input (1) or output (0). These lines specify that RB2 (LCD_RS), RB3 (LCD_EN), RB4 (LCD_D4), RB5 (LCD_D5), RB6 (LCD_D6), and RB7 (LCD_D7) are configured as outputs, so they will be used to control the LCD.

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;

These lines declare some variables used in the main function. maxpoint is a floating-point variable to store the maximum voltage value measured. i is an integer variable used as a loop counter. temp is an unsigned integer variable used to store the ADC reading. ch is an array of characters used to store the string representation of the voltage value.

float maxpoint = 0;
int i;
unsigned int temp = 0;
char ch[5];

In the main function, after initializing the LCD, it enters an infinite loop (while(1)). The loop does the following:

In summary, in this tutorial, we have learned how to measure AC voltage using two different methods and a PIC microcontroller.

You may also like to read: