Alternating current or AC current measurement using a pic microcontroller. In this article, we will discuss how to measure alternating current using the current transformer/CT and pic microcontroller. After reading this, you will be able to design AC Ammeter using PIC16F877A microcontroller. It is a very important task in pic microcontroller projects. I have already posted a dc current measurement circuit with code. you can also read it.
What you will learn in this tutorial
- How to use a current transformer to measure ac current
- What is the best way to interface CT with PIC16F877A microcontroller
- Writing your first code to measure AC current
In the first section, we will provide details of the current transformer and in the next section, we will talk about measuring voltage from the secondary side of CT and after that working of code will be explained.
How to use Current transformer for AC current measurement
- CT is used to measure an alternating high current of the order of thousand Amperes.
- It steps down ac current to lower value so that it can be easily read with the help of a microcontroller. The step-down the ability of the current transformer depends on the rating of the current transformer and its current step down ratio.
- Suppose you have a current transformer with a current ratio of 100:10 Ampere. Its mean primary current of the transformer is 100 Amp and the secondary current is 10 Amp. you can not use this current transformer to measure current more than 100 Amp.
- By measuring the secondary side low current, we can easily convert it into primary current value by using the current ratio formula. I will discuss in the latter part of this discussion how to use this step-down ratio in the programming part.
How to convert current into voltage?
We cannot measure current directly. Firstly, we convert the secondary side current into voltage. We can use a known value of resistor load. We measure the voltage across this known resistor. After that, we can convert this measured voltage into the current. We can use ohm’s law formula to convert the voltage into the current.
V=IR I = V/R
As you can see in this circuit diagram, we use an R1 load resistor to convert current into voltage. In this circuit, a voltage divider is used. But we can also use an operational amplifier to step down the voltage across the load resistor.
How to Measure the Secondary Side current of CT
Current > 100 Amplere , Relay = open Otherwise if current < 100 Ampere , Relay = close
You can check about PIC16F877A pinout and its features on this link.
Hardware components
- PIC16f877A
- CT
- An operational amplifier as a Difference amplifier
AC current measurement circuit using pic microcontroller
The operational amplifier acts as a voltage level shifting circuits or difference amplifier. You can go through this guide on AC voltage measurement to understand its working.
Difference amplifier Circuit
V=IR I=V/R I=8/.1=8A
Circuit diagram with pic microcontroller
This is a circuit diagram of interfacing the current sensor with a pic microcontroller. Connect the secondary side of CT to the points shown on the schematic. After that, Connect the shunt resistor in parallel with the current sensor. The difference amplifier circuit converts voltage below 5-volt magnitude. It also shifts the level of ac voltage from the negative side to the positive side. Connect output of difference amplifier with RA1 or analog channel one of PIC16F877A.
The liquid crystal display shows the values of the current. We use PORTB to connect LCD. You can go through this LCD interfacing guide.
program of AC Current measurement using PIC16F877A
// These lines define that LCD is connected // with PORTB pins from RB2 to RB7 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 set the direction PORTB pins // All pins as a digital output pins 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; // variable to store results // This function reads the maximum voltage // across shunt resistor and finds the peack // of ac voltage which will appear // across the shunt resistor. void current_READ(void) { float max; int i,current; int t[40]; ADCON0.ADON=1; for(i=0; i<=39; i++) { v= ADC_Read(1); v =v*(10.0/1023.0); v=(v-5.0); t[i]=v*10; } ADCON0.ADON=0; max=t[0]; for(i=0; i<=39; i++) { if(max<t[i]) max=t[i]; } max=max*.707106781; // convert peak into RMS intToStr(max, txt1); // convert integer value into string Lcd_out(2,8,txt1); // displays measured current on LCD delay_ms(1000); } void main() { Lcd_Init(); // It will initialize the library of LCD which is included in Mikro C for Pic ADCON0.ADCS0=1; ADCON0.ADCS1=1; ADCON1.ADCS2=1; ADCON0.ADON=0; while(1) { Lcd_out(2,1, "Current:"); // it will display " Current " in second line of LCD current_READ(); // call current_READ() function to measure current delay_ms(1000); } }
- This code is used to measure the secondary current of CT. you can easily calculate primary current according to your CT current ratio by using the current ration formula.
- .You just have to add one more line in a current function written above if you have a basic understanding of microcontrollers programming.
- This code is written using Mikro c for pic compiler.
- Just upload this code to your PIC16F877A device and make connections according to the circuit diagram given above.
Video lecture on alternating current measurement
We hope this topic helps you with your works and projects. We see thousands of people searching for this topic on net, that’s why I have written this topic on alternating current measurement using pic microcontroller. enjoy. Remember me in your prayers in return. You may like to check this project.
V= adc_read(0);
V= v*( 10.0 /1023.0);
V= v-5.0 ;
V= v* 10 ;
Pls kindly explain thanks
v= ADC_Read(1);
v =v*(10.0/1023.0);
v=(v-5.0);
t[i]=v*10;
Please explain the equation from where it comes ??
Hi
Thanx for this blog
My project is to measure current of the output of the ups (solar and ac utility joined together) and according to this measurement i will close or open switch
What i will add to the code
جزاك الله خيرا
مرحبا استاذ وائل جزاك الله كل خير ، ان امكن ولديك معلومات بخصوص حساب التيار والقوات والحراره والضوء في pic اتمنى ان تساعدني في ذلك بارك الله فيك 🌹✨
..i want to read power factor of 50Hz 220V ac…can you help me wiith it??
I want to know if the difference amplifier can be replaced by a voltage divider to reduce the voltage below 5v?
Nice job, but can I connect this 1 ohm burden to any CT? Because my CT datasheet recommends, 125 ohms. I am using 20A:1A CT.
how to apply current ratio formula in program? if it is 20A:1A
Hi
is not working……
what is not working ?
donne le schema coomplet miseur de courant et tension avec pic16f877a
Hi,
This current measurement meter can i check battery current?
yes you can use
hi
Why multiplying 10
V= v* 10 ;
What is CT?
current transformer
wait for 2 two days and check your email
hi, thanx for the blog. I need helpon how to monitor current drawn by a 1Hp submersible pump using a PIC uC. its a single phase pump 220-240Hz. I want to monitor the current drawn so that I can protect the pump from dry running and from overload.
thanks in advance
hi can you explain the code
v= ADC_Read(1);
v =v*(10.0/1023.0);
v=(v-5.0);
t[i]=v*10;
i do not understand the code
please send me code and simulation thanks :))
Hello Bilal,
I have been given a project recently to measure primary current 10-300A using Pic 16f1787 & 3.6V battery.
My CT is having turns ratio of 2000.It means my secondary current will be between 0.005 to 0.15A rms.
I am told to use 3.6V lithium battery for powering the circuit. Your article was a bit relief for me.By reading your article I have understood that I can use difference amplifier to rectify the signal.please guide me.
Can you send me the flowchart of your code?
can i put clamp meter ??
in the place of CT ???
i do appliciate it bro
what kind of circuit can replace CT
i am a 2nd year b.tech student don’t know how to calculate primary current according to CT current ratio by using current ration formula & on’t know high level of microcontrollers programming.please help me
hi , why when i have in input 1A , it give me in lcd 0A ?
Hello, how can I use decimal values (in this case 0.707) in MikroC? Thanks in advance
IT LOOK LIKE YOU ARE NOT HELPING , NON OF YOUR EXAMPLE CODE IS NOT COMPLETE
nice project work, i doing same work but in this project i want to measure ac source of 220v 20A, what modification i should do,
can you explain where number 707106781 from?;
Hi i am required to do this design at my school but the is i dont understand its schematic diagram.
why not just adjust the gain instead of connecting an offset resistor
v= ADC_Read(1);
v =v*(10.0/1023.0); //reference has been shifted from 5v to 10v
v=(v-5.0); // offset voltage is 5v
t[i]=v*10; //inverse of (2.2k/22k) is
}
Why did you choose to go with the diff amp, which is a lot more complex? Why didnt you just use the simple voltage divider? Is there any particular advantage of the diff amp method over te divider?
WOULD YOU LIKE TO HELP ME BY SENDING HOW TO CONTROL POWER THEFT WHO STOLE POWER BEFORE ENERGY METER . I NEED TO DO PROJECT ABOUT THUS .SO WOULD YOU LIKE TO HELP ME BY SENDING MICRO C CODE ABOUT IT
can you write a pic software for pwm output?
I have very simple hardware ..
hi Sir, i wish to know more regarding the purchase of the code + simulation. thanks
Please pardon me for my ignorance. As we all know that ADC of mcu can take input as voltage and current measurement can be done by I=(V/R) where R=shunt resistor. In this code I didn’t find any thing like dividing voltage by resistor value.
I also have some difficulty in understanding this piece of code
v= ADC_Read(1);
v =v*(10.0/1023.0); // aren’t we supposed to use 5 since it can tolerate 5
v=(v-5.0); // why we subtracting by 5 here
t[i]=v*10; // is it for applying 100:10 ratio ?
If some made me understand that would be helpful.
Thank you.
The code is giving error like undefined identifiers for AD0N0.ADON & many more plz help me sir reply as soon as as possible on email plz sir
You are a wrong microcontroller while designing a project.
what is wrong bcz iam using mickro c pic compiler using pic 16f877a microcontroller. what is header file for it
You need to include libraries from the library manager
Hi bro, can you please send me the complete circuit with CT connected and the complete code with formula applied? I will be really grateful bilal bhai.
[email protected]
V= adc_read(0);
V= v*( 10.0 /1023.0);
V= v-5.0 ;
V= v* 10 ;
Pls kindly explain thanks
Hi bro, can you please send me the complete circuit with CT connected and the complete code with formula applied? I will be really grateful bilal
[email protected]
Can anyone describe design steps of opamp circuit section ?