alternating current measurement using pic microcontroller

Alternating current or AC current measurement using pic microcontroller. In this article, I will discuss how to measure alternating current using current transformer/CT and pic microcontroller. After reading this, you will be able to design AC Ammeter using PIC16F877A microcontroller. It is 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 current transformer to measure ac current
  • How to interface CT with PIC16F877A microcontroller
  • How to write code to measure AC current.

In first section, we will provide details of current transformer and in next section, we will talk about measuring voltage from secondary side of CT and after that working of code will be explained.

Current transformer for AC current measurement

  • CT is used to measure 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 microcontroller. Step down ability of current transformer  depends on the rating of current transformer and its current step down ratio.
  • Suppose you have a current transformer with current ratio of 100:10 Ampere .Its mean primary current of transformer is 100 Amp and secondary current is 10 can not use this  current transformer to measure current more than 100 Amp.
  • By measuring secondary side low current, we can easily convert it into primary current value by using current ratio formula. I will discuss in later part of this discussion how to use this step down ratio in programming part.

How to measure secondary side current of CT

Now we will see how to sense ac current with secondary side. There are many methods to measure low alternating current you can also measure it using AC ammeter. But if you want to perform some control operation and want to send measured current value to other place.You have to use some kind of intelligent system.
For example you want to make a current protection circuit and circuit specification are followings
if current flowing through a line is greater then 100 Ampere, a control action  should be performed to operate a rely
if current is greater than 100 Ampere relay=open
if current is less than 100 Ampere relay=close
To make such kind of intelligent system, we may use analog and digital electronics,but its better to use digital electronics as far as cost issue is concern. In this tutorial, we will use PIC microcontroller to measure alternating current that is PIC16F877A.
You can check about PIC16F877A pinout and its features on this link.

Hardware components of alternating current measurement

1. PIC16f877A

2. CT

3.difference amplifier

AC current measurement circuit working

Difference amplifier:

To measure this current with pic microcontroller,we have to use ADC module of PIC microcontroller. To use ADC module,  we will convert current into voltage form by using a .1 ohm shunt resistor across CT and we will measure this voltage drop across shunt resistor.Then this voltage drop can be easily converted into current again.For example voltage drop across .1 ohm shunt resistor =8v
then current according to ohm law


but the problem is ADC of pic microcontroller can never measure voltage greater than 5 to solve this problem,  we can use difference amplifier.By adjusting gain of difference amplifier,  we can reduce voltage lower than 5 volt.Following diagram shows the circuit of CT and difference amplifier.For more information about difference amplifier how to adjust its gain etc, you can search on check this link.

Video lecture on alternating current measurement

Circuit diagram of alternating current measurement:

  • This is a circuit diagram of interfacing current sensor with pic microcontroller.
  • Connect secondary side of  CT to the points shown on the schematic.
  • After that shunt resistor is connected in parallel with current sensor.
  • Difference amplifier circuit is used to step down voltage below 5 volt magnitude and it is also used to shift the level of ac voltage from negative side to positive side.
  • Connect output of difference amplifier with RA1 or analog channel one of PIC16F877A.
alternating current measurement
circuit diagram of AC voltmeter using pic microcontroller 
current measurement complete circuit
current measurement using pic microcontroller complete circuit

program of alternating current measurement using microcontroller

// 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];
for(i=0; i<=39; i++)
v= ADC_Read(1);
v =v*(10.0/1023.0);

for(i=0; i<=39; 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

void main()
Lcd_Init();      // It will initialize the library of LCD which is included in Mikro C for Pic

Lcd_out(2,1, "Current:");   // it will display " Current " in second line of LCD
current_READ();               // call current_READ() function to measure current 
  • This code is used to measure secondary current of can easily calculate primary current according to your CT current ratio by using current ration formula.
  • .You just have to add one more line in current function written above if you have 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 circuit diagram given above.

I hope this topic help you in your works and projects.I found 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 these project.

39 thoughts on “alternating current measurement using pic microcontroller”

  1. 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
    جزاك الله خيرا

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

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

  4. i am a 2nd year 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

  5. 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,

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

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


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


Leave a Comment