Author 
Message 
Individual
Regular Contributor Username: Individual
Post Number: 30 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Friday, 03 February, 2006  11:00 am: 

Hi I'm looking for a circuit schematic that is used to convert ac mains voltage to a smaller dc value so the pic microcontroller can measure and display it using the internal ADC. Can anyone help? 
Zeitghost
Regular Contributor Username: Zeitghost
Post Number: 45 Registered: 012006
Rating: N/A Votes: 0 (Vote!)  Posted on Friday, 03 February, 2006  12:00 pm: 

What? Like in a powersupply? Are you attempting to measure the ac mains voltage? You could try using one of those unregulated wall wart power supply thingies: since it's unregulated the output voltage will vary as the mains voltage varies... only slower because of the smoothing capacitor in the psu. Putting a load resistor on the wall wart will improve the response by discharging the smoothing capacitor. 
Individual
Regular Contributor Username: Individual
Post Number: 31 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Friday, 03 February, 2006  03:32 pm: 

In other words, I want to build a microcontroller based ac mains voltmeter. Can anyone help? 
Grab
Frequent Contributor Username: Grab
Post Number: 123 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Friday, 03 February, 2006  06:44 pm: 

If you're using a PIC, you can do the RMS conversion directly from reading a scaleddown AC input. This is actually much simpler than messing around with external filters, and it's a much more elegant solution. How to do it... First step  reduce the voltage to a smaller value which can be handled by the electronics. A potential divider is the key here. Now if you feed the potential divider via a capacitor, you'll block the DC and be left with only the AC. Half of that will be greater than 0V and will be reported to your PIC's ADC as a nonzero value, half will be less than 0V and so will be reported as zero by the PIC's ADC. If the PIC is unhappy about voltages below 0V (I can't remember offhand), then use a railtorail opamp as a buffer. Next step  how to read values to RMS. When you're doing calculations of AC, the standard problem is working out how to detect the start and end of the cycle. But here it's easy  one half of the cycle is always reporting zero, so you can start averaging when the input goes above zero (actually best to do above some calibratable threshold, to avoid noise  maybe 2 or 3 bits) and stop averaging when the input goes back to zero, and you'll then know that you've covered one entire halfcycle. If you put the potential divider directly across the mains, then you'd have a problem with there being a voltage offset on it (neutral may be slightly higher or lower than ground, which would affect your result). But the capacitor blocks the voltage offset, leaving you with just the AC component. Now the problem is just doing the RMS calculation. The simplest approach is to use two variables for a running total and number of values. Every time you start a halfwave (ie. get your first nonzero value), zero them both. At every value from the start until the end of the halfcycle, add the *square* of each value you read to the running total, and add one to the number of values read. At the end of the halfwave, divide the running total by the number of values read to get the "mean square", and then take the square root of the average to get the "root mean square". These operations aren't built into PICs, but Microchip provides standard maths routines that'll do it. To make the sums easier, I suggest ignoring the lower 2 bits off the ADC reading. Squared, that gives you a 16bit number per sample. That's easier to accomodate than a 20bit number, and an 8bit by 8bit multiply is faster. Use a 24bit number for your running total (will handle up to 255 samples). Note that to get an accurate value, you'll want about 10 samples. That means your samples will need to run at least 20x faster than your AC wave, so at least 1000 samples per second (one sample and calculation per ms). You should be able to do a multiplyandadd in this time fairly easily, even on a 16F877. It'll take a bit longer to do the division and squareroot after that, but that doesn't matter  you've got all the next halfcycle to do that (10ms), and if it takes longer than that then no worries, you can just skip the next cycle and pick it up when you can. Also note that this approach assumes equallyspaced samples, so ensure the timing is solid by using a timer. And remember that the more samples you get per halfwave, the more accurate your calculation will be. At the end of it, you've got an RMS in bit scaling. You then just need to apply some scaling factor to get it into volts (do the sums, it ain't too hard). Another note on this. You might find that the reading is a bit jittery, because you're limited to the accuracy of your sampling and ADC. You can get around that by averaging the last few halfwave values to get a more accurate result. Graham. (Message edited by grab on 03 February, 2006) 
Zeitghost
Regular Contributor Username: Zeitghost
Post Number: 46 Registered: 012006
Rating: N/A Votes: 0 (Vote!)  Posted on Friday, 03 February, 2006  11:17 pm: 

Don't forget that messing around with mains voltages is dangerous and may kill you if you get it wrong... :o) The wallwart approach may well be inelegant, but oddly enough, it is relatively safe. 
Obiwan
Frequent Contributor Username: Obiwan
Post Number: 95 Registered: 122005
Rating: N/A Votes: 0 (Vote!)  Posted on Saturday, 04 February, 2006  12:12 am: 

They used to have some RMS to DC converter chips, Analog Devices I think. Use like an opamp, just put your AC in the input, and a RMS DC value is returned at the output. They were popular in early digital multimeters. But I haven't seen them out there in a while, but I haven't been looking either. There are analog methods of doing this, but I can't remember what all is involved. Could be more complicated that you would want to get into, that's why AD came out with the chip. (yes, I also a fan of not messing with the mains, that's just asking for trouble, plus, is a wire were to short out, it could kill somebody later on. If you can't use a wallwart, maybe you can use an inductive method, so you don't have to attache anything to the actual mains circuit) May the Force be with (most of) you.....

Rich1608
Member Username: Rich1608
Post Number: 9 Registered: 012006
Rating: N/A Votes: 0 (Vote!)  Posted on Saturday, 04 February, 2006  08:43 am: 

Forgive my ignorance but what is a wall wart? I've never heard of this before. Or maybe I have but not by that name. 
Terrym
Frequent Contributor Username: Terrym
Post Number: 144 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Saturday, 04 February, 2006  01:46 pm: 

Wall Wart = Plug Pack = one of those little black boxes you plug into the wall and whatever voltage/current it says on the label (supposedly) comes out the lead attached to it. TM 
Zeitghost
Frequent Contributor Username: Zeitghost
Post Number: 53 Registered: 012006
Rating: N/A Votes: 0 (Vote!)  Posted on Saturday, 04 February, 2006  04:17 pm: 

Yup. That's the very thing... I must apologise for the Americanism. I lurk far too much on EngTips and it rubs off. I called petrol "gas" in a post the other day... it keeps the cousins happy. It'll be "aluminum" next. Don't know that there's a great advantage in going to the trouble of true rms. It all depends on what you want to measure & how fast you want to measure it. The other thing to remember is that there are humungous spikes on the mains supply caused by tap changing and other network funnies. I seem to recall that stuff is supposed to survive 10 6kV spikes in 30 secs, or something similar. That was for things like electricity meters. Not really my field. 
Grab
Frequent Contributor Username: Grab
Post Number: 124 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Monday, 06 February, 2006  01:16 am: 

Just remembered last night, re the maths in RMSing. Suppose your AC wave goes 0 3 5 6 5 3 0 3 5 6 5 3 0 Then a halfwave is *not* just "3 5 6 5 3"  it's actually "0 3 5 6 5 3". So when you do the average, you have to divide the total by the number of nonzero elements *plus* *one*. If you don't, your RMS calculation will be too high. Graham. 
Grab
Frequent Contributor Username: Grab
Post Number: 125 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Monday, 06 February, 2006  01:30 am: 

For completeness, the analogue version (to do what you actually asked for. ;). You still need your potential divider to drop the voltage down to something more reasonable. Then you want a precision rectifier, which is two opamps connected together as an inverting (gain 1) and noninverting buffer (gain 1), giving out a precision fullwave rectified value. See Google for precision rectifier circuits  it's simple enough. Then you put that through a lowpass filter with a time constant of slower than 0.02s (1/50Hz). That smooths out the peaks and troughs to give a fairly steady value. Then you just feed this into your ADC input. The main downside of this is that this is just the "mean" rather than the "root mean square". If you can assume a true sine wave then it's not such a big deal though, but it won't do true RMS for more complex waveforms. You also need to apply a scaling factor  IIRC this level is 0.65 times the peak AC voltage, whereas the RMS is 0.71 times the peak AC voltage. I've got the sums somewhere if you're interested. If you still want to do it this way, this is the simplest way to do it. As I said before though, if you're feeding all this into a micro anyway then you're better doing the whole thing in software. Doing things in discrete hardware just because it's possible is *not* good engineering. ;) Graham. 
Grab
Frequent Contributor Username: Grab
Post Number: 126 Registered: 052005
Rating: N/A Votes: 0 (Vote!)  Posted on Monday, 06 February, 2006  09:30 am: 

Just found my sums, so here's the scalings. Suppose you've got a 1V RMS sine wave (ie. it's sqrt(2) V peak, or 2*sqrt(2) V peaktopeak). Then the mean of fullwave rectified 1V RMS sine wave is (2*sqrt(2)/pi) V. Do the sums, it works out that the mean comes out at 0.9V. Graham. 
Zeitghost
Frequent Contributor Username: Zeitghost
Post Number: 56 Registered: 012006
Rating: N/A Votes: 0 (Vote!)  Posted on Monday, 06 February, 2006  03:36 pm: 

Which is why the AVO 8 has engraved upon its scale: "average responding, rms calibrated" or some such. 
