Continue to Site

### Welcome to EDAboard.com

#### Hayee

##### Member level 3
Hi All,
I want to measure the Grid voltage in microcontroller and for this purpose I am using opto coupler for isolation and output of opto coupler will be based on input current. following is the circuit

I am using real hardware not proteus for results. also I am using pic18f452 microcontroller and MPLAB for programming
I am getting some results but they are not accurate. following is the chart where I showed the grid voltage "column C" and voltage at uC pin "column D" after optocoupler output. "Column F" is the ADC value which I am getting.
Then divide this ADC value with scaling factor 2.91 to get real voltage value "column H".
"column G" shows the voltage if we apply formula on "column F" that is Vout = (ADC value *5)/1023, it means voltage at ADC pin and voltage calculated in microcontroller are different. What is the cause of this? Am I missing something here?

Also "Column J" shows the actual ADC value according to the "Column D" value. after multiply "column J" value with scaling factor 2.91 I am getting "column K" values.
Hope you guys understand what I am trying to convey.
Need your suggestions to improve the accuracy of this

Hi,

For me as a professional designer ... it's one of the worst methods I ever have seen.
Why:
* it can not be "accurate" at all. Accuracy means the deviation from the expected value. But can you calculate what value to expect?
Even with a lot of experience - this would be very hard for me. And there is a lot of tolerance.
So all you can exoect is some kind of "precision". This means how much the output value deviates even with identical input.
But even here you will get variations caused by: time, temperature, waveform....
* it is some kind of "rectified average". But with a lot of unlinearities an offset.
* then you have an output filter. With less than 0.5Hz it is slow and will take more then 2s for less than 1LSB deviatiin on DC input.
* then the filter suppresses 2x mains frequency by a factor of 200, means you still have max. 5 LSBs toggling on a 10 bit ADC.
* then you seem to use VCC as reference. This usually is neither accurate nor precise. VCC is noisy and will drift with time, temperature, and VCC current.

Thus what I usually do:
* using proper VCC decoupling (not as reference)
* using a true voltage reference and the according capacitors .. to get a stable and reliable VRef.
* build a VRef/2 circuit (in simplest case using two resistors and a capacitor), but a Opamp buffer will improve
* use a high voltage divider for each mains line - connect the lower leg to VRef/2. (Mind to use high voltage resistors)
* connect the center of the dividers to the differential ADC inputs.
* use a capacitor at each ADC input as low pass filter (anti aliasing filter)
So you see - besides the VoltageReference - there are only acoule if Rs and Cs. The signal at the ADC inputs is the mains voltage (AC), added by VRef/2 (DC). Simple hardware, but with almost perfect signals at the ADC. Very low drift with time and temperature, very predictable signal (easy to calculate) at the ADC inputs. Still it is safe to use and thus allowed by safety regulations.(as long as the high ohmic resistor value is correct and the voltage rating)

Now all you have to do in the microcontroller:
Use a known, fixed sampling rate (using interrupt) and do the calculation according to what value you want to get (rectified average, but most probably RMS)
--> optimized data processing may take less than 1% of your processing power. And you get accurate, precise, reliable and fast responding information about mains voltage, even on distorted waveform
* Accuracy is determined by: resistor tolerance, ADC accuracy, VRef accuracy.
* Precision is less than 1LSB
* Resolution depends on software (useful: up to 1/8 LSB)
* delay: depends on software. Close to 1 period time in best case

But for sure you are free to go your own way.
My solution is focussed on "accuracy", which seems to be your major issue.

Klaus

Can you please provide me the diagram which you are suggesting.
I also want to measure AC current and battery voltage and temperature using ADC. so If I use differential input of ADC then can I achieve all the tasks using one microcontroller or not?

Hi,

Klaus

What is your target accuracy goal ? 1%, .1%,.......?

Do you want galvanic isolation between mains and solution ? Problem
with couplers are really poor linearity. There are isolation amps with
high accuracy, Analog Devices comes to mind. Some stuff pretty pricey....

https://www.analog.com/en/index.html Amp, even isolated A/Ds.......

Regards, Dana.

Last edited: