PDA

View Full Version : Need Help On Ac Voltmeter !!



uludere72
- 19th January 2006, 08:12
Hi everyone.

I really need help wiyh a AC voltmeter. How can i measure AC voltage?
does anybody know any link ? any code sample !
thx

boboco
- 19th January 2006, 08:48
Hi Uludere,

If you're intending to use a PIC with ADC such as e.g. the 16F87* and you want to measure directly then it's only a question of the frequency you are dealing with and the speed of the PIC oscillator. If you are prepared to do a little circuit work and you don't need to read the frequency you can rectify the AC line and simply measure the DC component, which is easier and requires much less code.

It's no problem, though. Let me know what range of voltage and frequency you're interested in and I'll send you info.

Greetings,

Rob

Ruben Pena
- 20th January 2006, 02:58
Hi Rob:
I have a similar application and I need to read from 0 to 3v ac with a
16F876 at 4 mhz.
Any hints.?
Thanks in advance...
Ruben de la Pena V.

sougata
- 20th January 2006, 05:32
Hi,

The simplest way to measure a sinewave ac is to rectify the input and do an ADC. Simply multiply the result with 0.707 to get the RMS value. If your input range is too low then some amplification and active rectification through OP-AMPS maybe needed. In fact the gain of the op-amp stage can be calibrated to give you an average dc reading (no need for maths in the PIC side). That is what is done in low cost multimeters. To get best results (depending on the speed needed) do an averaging of the samples collected. Please let me know your exact requirement

1. Range of Input Voltage

2. Maximum Frequency

3. Input waveform

I will try to help.

Regards

Sougata

Ruben Pena
- 20th January 2006, 07:31
Mr.Sougata:
I am trying to read a Current transformer. It gives 1000 to 5 amps transformation. With a proper load resistor it will give you a AC voltage proportional to the current.
The range will be between 0 and 3 volts. The ac is plain 60 hertz, but the
rectification reduces the range to .7 to 3 Volts more or less.
If those voltages correspond to 0 to 1000 amps,I can only read from 233 amps to 1000.
Any simple way to overcom the rectification loss.?
Thanks in advance..
Ruben de la Pena

lab310
- 20th January 2006, 09:01
Mr.Sougata:
I am trying to read a Current transformer. ................Any simple way to overcom the rectification loss.?
Thanks in advance..
Ruben de la Pena
You can't. This is not away to measure AC current in full scale.
In one of my project I had exactly the same problem : measuring AC current using current transformer. First idea was to do ADC sampling from time to time and search for max. value in recent period (to find amplitude for ac signal) and then calculate RMS (0.707*peak). This gave poor results so I used different approach: As mains frequency should be 50Hz+/-1Hz, I used interrupt routine synchronized with zero crossing of current, then trigger another interrupt that waits that you come near the expected peak of current and then start to do ADC searching for max value.
In my case, timings were 19.6ms for start and 20.6 for finishing ADC's (50 Hz mains). Since you got approx 40us resolution, this would capture a peak value with sufficient accuracy. This actually worked pretty good, I was able to calculate RMS and from there power. Problem was that in real application, AC mains were distorted (not pure sinus wave). I tried to introduce correction factor, but couldn't (you should calculate trig function in interrupt, that was impossible task for me and poor 18F).
Solution? Well, I finally used specialized (signal->True rms converter) IC. Not a great solution, but it worked and saved me a great time.

Also, put some bigger resistor so you expand your range from 0-3V to 0-5V or you would get poor resolution.

uludere72
- 20th January 2006, 11:02
Hi Uludere,

If you're intending to use a PIC with ADC such as e.g. the 16F87* and you want to measure directly then it's only a question of the frequency you are dealing with and the speed of the PIC oscillator. If you are prepared to do a little circuit work and you don't need to read the frequency you can rectify the AC line and simply measure the DC component, which is easier and requires much less code.

It's no problem, though. Let me know what range of voltage and frequency you're interested in and I'll send you info.

Greetings,

Rob


I want to measure upto 300 V AC/ 50 Hz. I have 16f877 20 MHz ..
but any other appliction maybe helpfull !!

boboco
- 20th January 2006, 13:44
Hi All,


most has been already said, but a few points to the remarks of other posters, thanks by the way.

One safety point to start: by any sort of voltage (current) measurement please be sure that the complete supply to the PIC etc. is either floating or that grounds of both circuits (Voltmeter and Circuit to be tested) are connected to the same level.

To measure low AC voltages (< ca. 10V) accurately by rectification it is necessary to use ideal diodes. Normal diodes will have a forward drop voltage of ca. 0.6V (silicon) or ca. 0.3V (germanium). Best here is to use two OPs with diodes in the feedback, trimmed to 0V. For those who wish, I will be happy to supply circuits etc.

OP use has the additional advantage of common mode rejection and quasi-floating inputs. It also allows good frequency compensation and, of course, fine adjustment.

To avoid such circuitry it can (with a bit of fiddling) be done by setting the PIC power supply ground to have an offset to the half of the AC peak to be measured. Set a resistor divider chain so as to arrange for an AC signal of not more than +/2.5V at an ADC input.

Read this input as fast as possible at a consistant frequency. While the e.g. PIC16F877 has a 10 bit ADC, you will have an accuracy of not more than 9 bit, so if you measure 300 V you will have a maximal possible accuracy of ca. 0.6V.

With a 4 meg oscillator and a sample time from say 50 uS you could have a theorectical sample rate of 20 K/S, at 50 Hz a read of 400 per cycle. By adding all values from lowest to highest each cycle, a value can be calculated that will represent the AC voltage.

By measuring how many reads are possible in the one sweep, a rudimentery frequency value can be detected. By adding new volt and frequency values to the old and dividing by two, each value is averaged and will become more accurate with time. This accuracy will be maintained up to Nyquist, which put very simply says that if you can't have more than one sample per half-wave, you can't measure it. (Not completely right, I know, but good enough for now.)

If any are interested, let me know with details of max. V, max. F and accuracy and I will be happy to help.

Greetings,

Rob

Ruben Pena
- 20th January 2006, 21:13
Thanks Rob:
I will try with a RMS to DC converter.
Regards..
Ruben de la Pena V

uludere72
- 25th January 2006, 15:01
thx rob

i understood like this..

i am going to Read this input ,which is reduced to 2.5 V ac, every 50 uS.
then i am going to add all values at each cyle !!

thx

boboco
- 26th January 2006, 12:15
Hi Uludere,


Have you converted to DC (RMS method) or are you reading AC?

If you have converted to DC, you can read each 50 uS and average the readings, but only if you are reading lower frequencies. Taking as example 50 Hz, you have a cycle period of 20 mS. Reading at 50uS you have 400 reads per 20 mS cycle. Add each input and divide by two each time to average.


Example:

readinput:

ADCIN 0, Readvolts 'Get value

volts = volts + readvolts 'Add value to volts variable

volts = volts / 2 'Average volts variable

GOTO readinput 'Start again

With this you'll get fairly good results up to about 100 Hz, then it will start getting increasingly erratic.

If you are measuring AC directly it's a little more complicated. It also depends on if you are measuring floating AC (not swinging symmetrically around ground) or grounded (swinging symmetrically about ground).

If you have arranged so that the AC is swinging around 2.5V at the PIC ADC input you read inputs and find out the highest and lowest values, which you use to calculate the difference.

Example

Readinput:

ADCIN 0, readvolts ‘Read input

IF readvolts > highvalue THEN highvalue = readvolts ‘Find maximal value

IF readvolts < lowvalue THEN lowvalue = readvolts ‘Find minimal value

Voltsvalue = highvalue – lowvalue ‘Calculate difference to find AC value

GOTO Readinput


To be honest, neither method is especially good or accurate, but with the addition of a few components you could create a stable, accurate and precise AC voltmeter, which I think would be of more interest to you than the above two mwthods. If you would prefer that, please let me know and I’ll send you the circuit and details.


GHreetings,

Rob

uludere72
- 26th January 2006, 12:56
thx you very much rob !!

I wrote this code. for AC measurment . But i didn't check on real device!
does it work ? It worked with DC :-)
If you can send any cirtuit and detail , it will be help to us !

best regards


Define LOADER_USED 1
;Define OSC 20
'Test Kart LCD
Define LCD_DREG PORTD
Define LCD_DBIT 4
Define LCD_RSREG PORTE
Define LCD_RSBIT 0
Define LCD_EREG PORTE
Define LCD_EBIT 1


' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

adval var word ' Create adval to store result
AcVolt var word
Acvolt2 var word
volt1 var word
volt2 var word
Cnt var byte
Cnt =1

ADCON1 = 7 ' Set PORTA and PORTE to digital
Low PORTE.2 ' LCD R/W line low (W)
Pause 100 ' Wait for LCD to start up


TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result

loop:
For cnt = 1 to 400
ADCIN 0, adval ' Read channel 0 to adval
adval= (adval*10)/204 ' 0 -5 V
volt1 = adval/10
Volt2= (adval - volt1*10)

'LCDout$fe, 1 ,"Value: ",dec(volt1) ,".",dec (volt2), " Say:" ,dec say

ACVolt = Acvolt + (volt1)*(volt1)
AcVolt2= AcVolt2+ volt2*volt2

'LCDout$fe, $c0 ,"Value: ",DEC (acvolt )
'pause 250
pauseus 50
next cnt

acvolt = sqr(acvolt/400)
AcVolt2= sqr(Acvolt2/400)


LCDout$fe, 1 ,"RMS AC V: ",DEC (acvolt),".", dec acvolt2' Display value


' Pause 1000 ' Wait .1 second
adval=0

Goto loop ' Do it forever
End

boboco
- 26th January 2006, 14:28
Hi Uludere,

In general your code goes in the right direction but there are a few problems, for example Cnt is declared as byte (255) so FOR Cnt = 0 TO 400 won't work (Rollover), Cnt must be declared as word. This is not so good then in Loops, using word variables causes enormous delays compared to bytes.

I'll get something together for you this evening and send it to you tomorrow.

Best regards,

Rob

sougata
- 27th January 2006, 06:30
Hi,

For instrumentation purposes it should be kept in mind that the ADC in the PIC is poor in the lower range (Less than 2 Volts). So you need a shifting reference that actually increases the resolution.

If you measure the AC in chunks you need a zero cross sense circuit to kick your measurement routines.Also noise (motor current) would make it worse.

As mentioned by lab310 distortion and PF in the input waveform would give erroneous result. You may use a software based pseudo differential measurement technique.

I believe depending on the accuracy you need you should choose a solution. As mentioned amplifying the AC and introducing a gain of 0.707 do give results but in the lower side the accuracy is not good enough due to the conversion slope.You can handle this in software.

I would suggest that you go for a RMS converter and use a temperature compensated reference for the PIC. Do the AD in sleep via interrupt. Sample 16 times and do an average.This gives best results.

Good Luck.

Regards

Sougata

boboco
- 27th January 2006, 08:39
Hi All,

Uludere, I tried to send you circuits etc but I'm a bit of a novice to this forum and the files are too big. I'll cut them down to size this evening and send tomorrow, sorry for the delay.

Sougata, of course for voltmeters, the PIC ADC is really not suitable for any real precision. Nor is any other normal µcontroller for that matter. RMS-type measurement is easier but not better, especially for low levels and changing frequencies, which is why I even contemplate AC chunks. It isn't necessary here to check the zero cross though, assuming we're sampling offset peak to peak and calculating RMS.

It isn't necessary to measure the exact voltage value, either, and that in all cases. Of importance is that the measurement system used is consistant, that means it detects the same value of any given voltage at all times. Software correction to convert to the correct RMS values is then easy. The above statement includes your remarks about temperature correction.

A trick I have used, not for AC voltage but for reading optical sensors, was to couple a number of ADC inputs together and to let one intergrate while another converted. This allows a higher reading rate when reading chunks, and of course for all other cases too. Watch the delay requirement for switching ADCs though.

Greetings,

Rob

sougata
- 27th January 2006, 12:44
Hi,

I do not rely on the PBP ADCIN command. I setup the ADC take one reading first then start the sampling again. While sampling I do the calculation stuff then I introduce whatever more delay is required (pauseus) and repeat.

Regards

Sougata

boboco
- 27th January 2006, 13:21
Hi Sougata,

I don't use the ADCIN command either, unless I'm feeling lazy or there is no need not to. I switch the registers as required. Especially in many uses (I research in Laser Medicine and build a lot of my own equipment) a different integration time for different inputs can be of benefit.

I even connect CCD array outputs directly to the ADC input and get excellent results by using the input circuitry in the PIC as a quasi charge measurement device. Set the reference voltages to, say, +3V and +1V and we get a good resolution. We don't even need to decouple the input, the charge function of the input capacitor does that.

Regards,

Rob

uludere72
- 27th January 2006, 15:15
No problem for Delay!
we are looking forward your sch. & code sample!
thx

boboco
- 30th January 2006, 09:42
Hi Uludere,

I forgot the week-end, I got round to trying to get the data over to you but using Eagle CAD, I can't get a file under 200K, so now I'll try it in little bits. First of all the simple input for chunking AC attached.

Using a 2.4 volt zener diode D1 and R1- 1.0K, generate 2.4V as offset for the PIC input, decouple with C1, C2 from Vdd against Vss. This is also connection for one of the meter probes. Remember that both Vdd and Vss must float, the probes may not come into contact with Vss or Vdd.

The resistors R2 and R3 must be calculated as divider for the max. voltage you want to measure. If e.g. you want to measure max. 100 VAC RMS then calculate 100 x 1.42 = 142V say 150 V peak each node and select the resistors accordingly to allow not more than +/- 2.5V at the PIC input. I've put a 100nF capacitor over the PIC input to reduce noise, if you keep the frequency under ca. 100-150 Hz it won't affect the input materialy.

If you now put the probes over the voltage you want to measure, the reduced signal divided by R2 and R3 2 will appear over the ADC input offset at 2.4 V.

Let me know when or if you want to try this type, I'll send you software or the next circuit if you want to try the RMS method.

Greetings,

Rob

uludere72
- 30th January 2006, 12:22
thx for your kind response..
i want to try the RMS method, would you mind sending about it ?

if the file is big , you can send to my email adresss.. [email protected]

onur
- 2nd July 2006, 20:36
Hi
Can we see rms method?

leisryan
- 16th July 2006, 23:36
Sir:
Good day to you sir

To measure low AC voltages (< ca. 10V) accurately by rectification it is necessary to use ideal diodes. Normal diodes will have a forward drop voltage of ca. 0.6V (silicon) or ca. 0.3V (germanium). Best here is to use two OPs with diodes in the feedback, trimmed to 0V. For those who wish, I will be happy to supply circuits etc.

Sir, I would be grateful if you would provide me the schematics

Yours trully,
Ryan