# Measuring Voltages using MCU

1. ## Measuring Voltages using MCU

To measure VAC Voltages.
In that there are three points.
1.Phase with 230VAC
2.Neutral
3.Supply 2(VAC).Vary from 0VAC to 5 VAC.
The Neutral wire is connected to GND of MCUs(PIC16F877A) power supply for reference.Phase wire connected to AN0 by resistor network and by adding diode and Supply 2 connected to AN1 by adding diode.While running the ADC code the voltage of phase and Supply 2 are measured correctly by neutral as reference.

My doubt is how to measure the voltage between Phase and Supply 2..?

•

2. ## Re: Measuring Voltages using MCU

A good question, I like it. Lets start with this, do you know the difference between "AC" and "DC" current? Do you have 2 separate grounds, do you have an optocoupler between the VAC and MCU ADC? Do you know the difference between phase and line voltage? I am guessing your system is (phase A, phase B, phase C, ground, neutral)? Its not good to measure VAC voltage with an MCU, it will be better to use a rectifier.

- - - Updated - - -

Here is something more, remember Proteus us not your friend.

How to measure "AC" current with an MCU.

•

3. ## Re: Measuring Voltages using MCU

Hi,

Its not good to measure VAC voltage with an MCU, it will be better to use a rectifier.
I donīt agree.
I always measure VAC directly (DC shifted) with the MCU.
AC voltage is defined as V_RMS which takes care about waveform.
Rectified AC canīt care about waveform and thus it can only be an approach.

****
But there is no "must" for true RMS. If you are sure the waveform is pure, undistorted sine, then you may use any other method you like.

****
I like the direct measureusing and true RMS calculation, because the hardware is simple and the result is almost perfect.
Hardware in simplest case: 3 resistors, 1 capacitor.

The problem is, that many users find the use of interrupts for the ADC and doing RMS calculation in the ISR difficult.

***

Klaus

•

4. ## Re: Measuring Voltages using MCU

Based on this circuit,connected DC voltage with PIC18F4520.When the voltage is given to the ADC_INPUT means,values are displayed in LCD.
But when the input is removed from ADC pin means also,some values are shown in LCD.
How to avoid this one.

If compared with multimeter along with ADC pin means there is an error in LCD compared to multimeter.From .5VDC to .75VDC error in LCD.
And the values are not stable.It oscillation to lesser or greater 1VDC.

shall we use pulldown resistor at the junction of 1K resistor and capacitor..?

Code:
```#define  _XTAL_FREQ 8000000
#include "lcd.h"  // lcd in 4 bit mode

// CONFIG1H
#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

void System_init(void) {
TRISA = 0b00000001;
PORTA = 0b00000001;

CMCON = 0X07;

TRISB = 0b00000000;
PORTB = 0b00000000;

TRISC = 0b00000000;
PORTC = 0b00000000;

TRISD = 0b00000000;
PORTD = 0b00000000;

TRISE = 0X00;
PORTE = 0X00;

ADCON1 = 0b00001110; //VSS,VDD ref. AN0 analog only

}

void main(void) {

System_init();
lcd_init();

lcd_clear();
lcd_goto(1, 1);

while (1) {
__delay_ms(100);
__delay_ms(100);
}
}

unsigned long int iv_temp1, iv_temp2, iv_temp3, iv_temp4, iv_temp5, iv_temp6, iv_temp7, input_voltage;

ADCON0bits.GO_DONE = 1; //Start A/D Conversion
while (ADCON0bits.GO_DONE != 0); //Loop here until A/D conversion

input_voltage = (((iv_temp1 * 5)*1000) / 1024);

iv_temp2 = input_voltage / 1000;
iv_temp3 = input_voltage % 1000;

iv_temp4 = iv_temp3 / 100;
iv_temp5 = iv_temp3 % 100;

iv_temp6 = iv_temp5 / 10;
iv_temp7 = iv_temp5 % 10;

lcd_goto(2, 1);
lcd_putch(iv_temp2);
lcd_putch(iv_temp4);
lcd_putch(iv_temp6);
lcd_putch(iv_temp7);

}```

5. ## Re: Measuring Voltages using MCU

Hi,

And the values are not stable.It oscillation to lesser or greater 1VDC.
Either the input signal is not stable, or the reference is not stable or both are not stable.
You have to check this, be cause we canīt .

Klaus

•

6. ## Re: Measuring Voltages using MCU

Originally Posted by KlausST
Hi,

Either the input signal is not stable, or the reference is not stable or both are not stable.
You have to check this, be cause we canīt .

Klaus
VSS,VDD is taken as reference voltage.The input signal is stable(FROM SMPS),the value by ADC in LCD is not stable..

7. ## Re: Measuring Voltages using MCU

In voltmeter if we selected in VAC means,its able to measure 1VAC to 300VAC...
How to do it PIC controller..?

8. ## Re: Measuring Voltages using MCU

Hi,

There are many ways. Some with higher hardware effort, some with higher software effort.
You need to decide your requirements: voltage range, resolution, output data rate. Also true_RMS, rectified average, peak..
You need to read through a couple of discussions to find your own way.

I personally usually go the precise, true_RMS way:
* simple hardware: 3x R, 1x C, voltage reference
Software:
* continous hardware controlled sampling
* high pass filter (or fixed subtraction of DC)
* square
* average
* square-root
--> very accurate and precise, independent of waveform.
--> interrupt driven, thus there still is plenty of processing power for other tasks
--> the result is stored in SRAM, thus it is accessabke in main loop at any time, just read it.
Don't care about the whole measurement and calculation task in main loop.

With this method you get results as precise and accurate as the ADC and Ref.
No additional errors due to external circuitry (rectifier or RMS-to -DC circuits).
Choose good resistors, to avoid additional errors.

Other do this differently. Find your own way. There is no "best" solution.

Klaus

--[[ ]]--