AC Voltage Measurement using ATmega8

AC Voltage Measurement using Atmel AVR Micro-Controllers

There are many different methods to measure the voltage. But I will give you a method through which you can do this job just like 1 2 3. You just have some idea of ADC’s of micro-controller.

1.  Step down the AC voltage
2. Convert the AC voltage into DC
3. Give it to micro-controller for calculations

I told you it is easy. I am going to discuss these steps one by one.

Components Needed

Click on the component name to buy the product from reliable sources:

Step1: Step down the AC voltage

Stepping down an AC voltage is really an easy thing due to transformers. We use Potential Transformers for measuring purposes. Most people wonder that potential transformer (PT) must be something that is very difficult to find or something really different than simple transformer but that is not true.  A PT is a simple transformer with less current rating. As you know we buy transformers according to their voltage rating plus current rating. In the case of PT you just have to buy the smallest step down transformer. In case you are not able to buy a 100 milli-Amps transformer then don’t worry. You can use a simple 1 Amp or 1.5 Amp transformers as well.

What about voltage rating?

Same as current rating, buy a transformer with minimum secondary voltage. In my country, we have 220 volts AC at domestic level. So, I bought a 220V/6Vtransformer to step down the voltage to the minimum level.

Step 2: Convert the AC voltage into DC

Conversion of AC into DC is another simple step. Take a rectifier and capacitor and put it after the potential transformer. In my case I just put a simple diode instead of using Rectifier Bridge to convert the AC voltage because we just need to convert it for measuring purpose.

Why are we converting the voltage?

There must be a question in your mind that why are we converting. Just give the AC voltage to the micro-controller. The answer is we cannot give negative cycles of AC to the micro-controller. As the ADC of most micro-controllers are not able to detect negative cycles. Moreover, it  is  very  dangerous  to  give  a  negative  cycles  of  voltage  to  the microcontroller because it can damage it after some threshold value.

Before going into details, we should discuss the circuit diagram/simultion of whole system.

AC Voltmeter using Atmel (smart Energy meter)
Figure1: Circuit Diagram/Simulation

After seeing the circuit diagram you can now easily understand the above steps. As you can see we used a 16×2 LCD for display and ATmega8 as a processor. I did not energize the LCD here because Proteus can work without the supply as well. I used diode for rectification and a capacitor for filtering. After capacitor a voltage divider is present to cut off the half voltage and zener diode is for the protection of ATmega8.

Note: There is a “ground” beneath the 10uF capacitor. I didn’t put that ground to make it cool or something. That symbol has much importance in this circuit. As there are two different blocks in the circuit and only connection going from one block to other is the wire connected with zener and ADC0 of micro-controller. So both blocks do not have same reference point.  To bring them on same reference connect ground of the micro-controller with the negative terminal of capacitor.

Step 3: Give it to microcontroller/ ATmega8

Third step involves the coding part of the micro-controller. I’ll explain here only important and tricky part of code. You can download whole code from the link below.

for(i=0; i<=39; i++){
 adc_int[i] = adc_read(0); // read adc value at PORTC.0
 for(i=0; i<=39; i++){

In code, I inserted two loops. In first loop, it will take 40 samples for the voltage. Then in the second loop out of 40 samples it will give out the maximum value from those 40 samples. The reason behind inserting two loops is when we convert the AC voltage to DC then there will be some ripples there. But we want to get the highest peak of that ripples so we used two “for loops”.

I think that’s enough for voltage measurement. If you still need any help don’t hesitate to ask and don’t forget to like our facebook page. 🙂

To Download the whole code and simulation click the buttons below:

 Complete Code Proteus File

19 thoughts on “AC Voltage Measurement using ATmega8”

  1. Vai,
    Assalamu alaykum.I need AC voltage & current measurement meter in same circuit,both disply in 16X2 LCD display,if u have please post it or email me..

  2. You probably have noise in your circuit. You need a 100nF capacitor and a 100uF 63V capacitor between 5v and ground in the supply line to reduce noise. And also a 100nF capacitor between the output of adc and ground.
    After the fluctuation slows down or even disappears, the next problem is accuracy. To have the highest accuracy, you need to fine tune the input signal to be exactly 5V and make sure all the resistors are 1%.

  3. After some change in code :
    for(sampleCount=0; sampleCount<32; sampleCount++){
    freshSample[sampleCount] = readADC1(5);
    newSample = freshSample[0];
    for(sampleCount=0; sampleCount<32; sampleCount++){
    if(newSample<freshSample[sampleCount]){ //Step-3
    newSample = freshSample[sampleCount];
    finalSample = newSample;

    95% result is ok but some time result goes to "0" why ?

  4. Hello Sir,
    I’m working on same type h/w.but I’m not getting fix value in result.value fluctuating in every time.
    I’m very confused with my result.Please help me for this.

    Controller : stm8s003f3.
    Code Detail :
    for(sampleCount=0; sampleCount<32; sampleCount++){
    freshSample[sampleCount] = readADC1(5);
    newSample = freshSample[0];
    for(sampleCount=0; sampleCount<32; sampleCount++){
    if(newSample<freshSample[loop_1]){ //Step-3
    newSample = freshSample[loop_1];
    finalSample = newSample;

  5. Hello sir ,
    I am working on digital voltmeter i ref your logic
    But i found some errors value moves 0 to hi & hi to low on 7seg display pls guide

  6. hello sir, please provide me full circuit diagram of your AC Voltmeter using ATmega8, is there any crystal used or fuse bite to write, i made circuit according to your Proteus diagram but its not working please suggest me what is the problem, my email Add : “” Thanks

    1. Hi Tariq,
      I shared already full circuit diagram. And default fuse bit setting is used with internal 1Mhz crystal. Check your circuit and implementation step by step. You must making mistake somewhere.

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.