Voltage reading from a rectifier+voltageDivider - Page 1
Forum section: electronics / general

author: VEGETA
author_rank: Super Contributor
timestamp: April 24, 2013, 09:33:21 pm
content: hi, now I am trying to read the voltage (dc) from a rectifier + filtering cap + voltage divider circuit like the following:the means voltage (or any 220v rms ac/50hz sinewave signal) is fed into the rectifier bridge (4 diodes here) then i connected a 100uF filtering cap in parallel to filter the signal... then put a voltage divider as shown... after all that i took that wire to the RA0_pin in PIC16F877A.voltage divider is: 115k and 1.47k -----> Vout = Vin (1.47/116.47) and hence at 220 approximately 3.9v (the max of 5v happens @ 280v rms nearly so there's enough gap).the problem is that the voltage divider isn't connected to ground because of the rectifier so when I took the reading between at the 1.47k resistor, it was correct.BUT, when connected to PIC, it doesn't! it gives about 2.6v or so... also tried connecting that node (1.47k) to ground and it didn't change nor work.PIC's voltage source comes from a 7805 regulator which is fed by a charger that gives about 9v dc, this charger's ground is the circuit ground.I made a voltage follower like this:Vin = 8-9vground = ground of the 9v charger.this follower made a problem which is the output voltage of the charger (and the regulator) gets halved, thus PIC stopped working cuz low voltage is now connected at Vcc. AND, it didn't work as a follower which gave the same result as the node connected to ground.the reason i chosen this voltage follower is to make an isolation between PIC and divider, and to enhance input impedance is it shouldn't be high.can you tell me what to do to be able to deliver the correct voltage from the 1.47k resistor to PIC's ADC pin (or that voltage follower OP-AMP)?thanks.

author: Unknown
author_rank: Unknown
timestamp: April 24, 2013, 10:15:34 pm
content: I don't see the point of the voltage follower, the input impedance of the PIC should be much higher than 1.47 kOhm. Connect the negative terminal of R2 in your first schematic to the PIC's circuit ground (not earth!), then the PIC should measure the correct voltage assuming the power supply is floating (eg. battery, double isolated power brick). Make sure you treat the whole circuit (including the power brick you use to power it) as live. The insulation on the wire from the power brick is probably not rated for the mains voltage you connect it to, so the whole power brick should be protected by something rated for mains voltages.

author: amspire
author_rank: Super Contributor
timestamp: April 24, 2013, 10:23:05 pm
content: If the circuit is directly powered by the mains, you cannot have the circuit connected to ground - if that is what you want to do. If you want to have a grounded circuit, you have to add a transformer before the rectifier.If you want to use a grounded circuit to measure the voltage across the rectifier mains, you can use a high impedance diffential amplifier. You will want to have it protected for impulse voltages up to a few KVs.

author: VEGETA
author_rank: Super Contributor
timestamp: April 24, 2013, 11:16:32 pm
content: Quote from: alm on April 24, 2013, 10:15:34 pmthe PIC Vss (ground) is connected to the ground of the 9v dc charger. do you mean i should connect that ground to the R2 -ve terminal?and then connect +ve R2 to ADC pin? "I don't have an earth"Quoteyes, as you can see, I want to measure mains voltage (and another voltage source too).I don't have a transformer, so I can't connect it ^_^. The whole idea of the voltage divider and the bridge is to have a 5v max at 280v rms and thus about 3.9v at nominal 220v rms so i can connect it.... do you have any idea about how this can be done using a transformer? what transformer?and if I don't wanna use a transformer, how can i read the voltage the correct way?QuoteI have the LM358 OP-AMP.the rest stuff you wrote i don't know about them. I don't insist on having a grounded circuit, but I wanna have the voltage read the correct way.BTW, using a transformer can make an isolation right? so is it better to have it?

author: amspire
author_rank: Super Contributor
timestamp: April 24, 2013, 11:33:37 pm
content: You definitely cannot have a bridge rectifier connected to the mains with the negative connected to ground.That just doesn't work and it should trip your earth leakage breakers at the switchboard instantly. The whole idea is very unsafe.It is possible to make an amplifier circuit with a differential input. A LM358 probably has too much input bias current. Something like a TL074 would be a better choice. But, as I said, both positive and negative inputs to the differential amplifier circuit have to be able to safely handle impulses up to a few thousand volts and you want an input resistance on the positive and negative inputs of something like 10MOhms. This is not something that is easy to test if it is safe.The only simple solution is to get a small mains transformer. They only cost a few dollars if you only want a low power one for measuring the mains, and you can then safely ground your micro circuit. You can even connect your in-circuit programmer/debugger to the micro with the mains on without blowing up your computer.

author: VEGETA
author_rank: Super Contributor
timestamp: April 24, 2013, 11:40:52 pm
content: Quote from: amspire on April 24, 2013, 11:33:37 pmOK, now this differential amp thing can't be done cuz i never tested it, plus i only have LM358.the simple solution is to have a transformer as you said, so what ratings should i get? remember I want a precise measurement.after that transformer I can put the bridge rectifier and the filtering cap right? here i don't need the voltage divider cuz it will be 5v max right?means voltage will be 220v rms typically, so transformer should be rated a little bit higher right? I have a zener diode to make extra protection in case anything happen. ^_^the connection will be +v of the transformer's secondary terminal to PIC ADC pin and -v terminal to 9v dc charger's ground?

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 12:20:19 am
content: OK, now i made a little research and came up with this circuit:note that simulation was too much heavy and couldn't get a result from it.but when i connect the transformer's secondary -ve terminal to ground, simulation works great but gives false results... I don't really care about Proteus' simulation, i just need to know if the circuit work or not. I have less than 2 weeks to complete the project and the only remaining thing is this measurement.

author: amspire
author_rank: Super Contributor
timestamp: April 25, 2013, 12:21:40 am
content: Quote from: VEGETA on April 24, 2013, 11:40:52 pmBasically whatever you can get your hands on. A transformer salvaged from an old bit of electronic gear or an old-style power pack (the pre-switching supply power packs). Once calibrated, any mains transformer - if lightly loaded - should manage much better then 1% accuracy. The higher the voltage, the less error from the diode voltage drop.QuoteI would make it simpler - a single diode to a capacitor attached to a voltage divider. Look at the micro specs and make the divider as high as impedance as possible. So it the micro allows for up to 10K without loss of accuracy, and the rectified transformer output is 15V, I would use something like a 27K + 10K to make the divider. You then look at the speed you want to be able to want the converter to be able to react to changes in the mains. If you want it to be able to react in 5 seconds, choose a capacitor of about 270uF. When you are converting the A/D reading to volts, add about 0.6V x 10K/(10K + 27K) = 0.16 volts to compensate for the diode voltage drop, then use a calibration factor to calibrate the A/D reading to the mains volts.One of the beauties of transformers is the division ratio never drifts, unlike other parts like resistors.QuoteAll 220V mains transformers should be properly designed to safely handle the expected range of voltages, and will have a safe isolation. For piece of mind, I like to get the transformers with primary and secondary windings separated by a plastic insulator on the bobbin, as you can see there is no way you can get a primary to secondary breakdown.In the circuit I described above, one of the secondary transformer leads goes to ground. The other one to the diode. The diode can be a power diode like a 1N4004 or a signal diode like a 1N4148. Even better, a Schottky diode will have a lower voltage drop, but as I mentioned above, you can compensate for a diode voltage drop successfully.If you are after better then about 1% accuracy, you will have to look at an opamp based rectifier circuit rather then just the diode (yes, you can use the LM358) to eliminate the diode drop offset. If you look up the TI LM324 data, it will probably have suitable circuits in the application circuits. For the best accuracy, you may need a Analog Devices RMS converter chip, but that is probably overkill for your needs.

author: amspire
author_rank: Super Contributor
timestamp: April 25, 2013, 12:39:03 am
content: Quote from: VEGETA on April 25, 2013, 12:20:19 amIt will work. You will have to calibrate the transformer - you cannot rely on its rated output voltage, but as long as your micro allows for calibrating the voltage, it will work great.

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 12:54:16 am
content: Quote from: amspire on April 25, 2013, 12:39:03 amwhat is the calibration you are talking about? for example, I assume that max means rms voltage is 280v... the transformer is rated to give say 6v @ 220v rms... what to do here?your sentence "When you are converting the A/D reading to volts, add about 0.6V x 10K/(10K + 27K) = 0.16 volts to compensate for the diode voltage drop, then use a calibration factor to calibrate the A/D reading to the mains volts." BUT, compensating factor is what?I have some resistors I can use... like 100k and 15k. divider will be using these 2 values and the output will be:Vout = Vin (15/115)so @ 220v RMS, Vout = 6(15/115) = 0.782vthis is too low value, but if the transformer's secondary is higher like 12 or 24... it will be bigger.so, I have 2 things to worry about:1- transformer's ratings to handle >220v rms.2- what output of the transformer's secondary windings will be.3- connection of the 4-diodes (bridge rectifier) will have an effect for sure. Here I think I should correct it in software but what value to add? should I just add 1.4v to the resultant RMS value?my program reads the rectified (and divided) peak voltage then divide it by sqrt(2) to get the rms of the sinewave.4- I don't really care that much about response time, I have two 100uF/400v caps. are they ok?

author: amspire
author_rank: Super Contributor
timestamp: April 25, 2013, 02:12:46 pm
content: Quote from: VEGETA on April 25, 2013, 12:54:16 amThere always has to be a conversion factor, and you just want to make sure you can change it rther then having a fixed valua based on calculations. So if you A/D goes from 0 to 1023, then when you apply 220V, you will get a reading - say 738 (depends on the resistors and the transformer). You might then multiply this by a calibration factor of 298 to give 219924 in a long integer variable to correspond to 219.9V. In this case, 298 is the calibration factor.The diode in the rectifier will mean the A/D converter always reads low by a fairly constant count. Assume it is a 0.6 v reduction of the rectified mains. If the divider reduces the voltage to 20%, then reduce the 0.6V to 20% (0.12V) at the A/D input. If the A/D reads 1023 at 5V in, then 0.12 = 1023 x 0.12/5 = about 25. So in this example, you would add 25 to the A/D reading to compensate for the diode drop before converting to voltQuoteI wouldn't use a bridge rectifier - that involves 2 diode drops instead of one diode drop for no good reason. For measurement, the less error caused by diode drops the better. Just use a single diode which as I said can be a common signal diode like a 1N4148.The 100uF cap may be OK, but first find a transformer and then we can worry about resistors. If you find a transformer that is labelled as 15V AC out - that is at full load. Under no load (which is what you want for measuring the mains), the output may be 20V AC. And you want a margin so if you are trying to measure 220V, you might want the resistors to allow for a measurement up to 300V.Any 220/240 transformer you find will probably handle overvoltage fine. Don't worry to much about that - just take what is at hand, or buy a transformer. A small one will cost perhaps $5.Richard

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 05:07:18 pm
content: I will 15k and 100k resistors which is suitable for the required range.if the transformer gives 9v AC (rms here right?) from 230v.. then:Vout = Vin(15/115) = 0.1304*Vinso, 230v -> 9v -> Vout = 0.1304*9 = 1.1736v rms @ ADC pinbut this is for RMS, when I connect that rectifier bridge (for full-wave) I will get the Peak value of the 9v rms which will be about 12.726v.now, 12.76*0.1304 = 1.659v........ HERE, I will get the ADC value of it and it's not required to multiply it by sqrt(2) to get the rms value... Is that correct?so if voltage @ secondary windings is about 15v.... how much is the input rms voltage?Vout = (15*1.414)*0.1304 = 2.765v @ ADC pin // 15*1.414 cuz it's gonna be rectified to get the peak value.so, the input voltage needs to be measured is:Vout = Vadc * (9/1.659) = Vadc*5.425Vin = Vout * 25.55 // 25.55 is the turns ratio 230/9now, Vin = Vadc*5.425*25.55 = Vadc*138.64.....so the final equation is Vin = Vadc*138.64 // here, 138.64 is the same compensating factor u explained xD. 230/1.659 = 138.64so if Vsecondary = 5v -> Vout_rectifier = 5*1.414 = 7.07v (peak)so, Vadc = 7.07*(15/115) = 0.922vVin = 0.922*138.64 = 127.85v rmsto check it out:Vin = N*VoutVin = 25.55 * 5 = 127.75v rmshow do you see my calculations?for 15v secondary voltage of transformer...Vout_rect = 15*1.414 = 21.21vVadc = 21.21 * (15/115) = 2.766vVin = 2.766v * 138.64 = 383.55v rms..... to check it, N=25.55 so Vin = 25.55*15 = 383.25v rms which is correct!so in software, I should write a function like this:////////float RMS_calculator (unsigned short source) {channel = source;adc = ADCRead(channel); // a function that returns 10 bit adc result 0-1023v_adc = (adc*5)/1023; // gives a 0-5v floating number to represent voltage at adc pinv = v_adc*138.64; // rms value for input voltage// or can i use v = adc*0.6776; ? because (5/1023)*138.64 = 0.6776here it will be faster right?return v;}///////please give me your opinion in all that ^_^ I need to finish this thing as fast as possible.

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 05:15:50 pm
content: but, for the bridge rectifier, there will be 1.4v drop from the rectified voltage right?so we need to make things like:Vout_to_adc = Vout_rect - 1.4V_adc = Vout_to_adc *(15/115)and then continue...but this 1.4 is the rms or peak? cuz it will make a significant effect on calculations.

author: mariush
author_rank: Super Contributor
timestamp: April 25, 2013, 06:28:31 pm
content: The bridge rectifier will have different voltage drop depending on its rating, the temperature it gets to and so on.Since you're not planning to have a load on the transformer, you can use any small bridge rectifier or even a single diode (do half wave rectification), a half-wave rectifier or a single diode will have 0.5-0.7v per diode (so 1-1.5v on bridge, since the bridge rectifier has 2 diodes passing current through them with every cycle). Bridge rectifiers rated for 6-10a or more, like the one on power supplies, may have up to 1-1.2v per diode but the actual drop varies with temperature (and the datasheet has a graph with drop vs current/temperature)Get any transformer, you can always play with the resistors to get you a value closer to your 5v. 9v rms and 12v rms should be very easy to find. The PIC can use the 5v from the 7805 as voltage reference or you can use the internal 4.096v voltage reference and tune the voltage divider to get about 4.096v for 256v and you'll have the 4.096-5v as headroom in case the AC voltage goes up. 4.096v at 10bit gives you 256v / 1024 bits = 0.25v per bit, so you have easy math.Determine the ratio of the transformer, see the peak voltage without load (remember, transformers can have 10-15% extra voltage without load) so you may not have 9v rms or 12v rms, you may have 1-2v more. Since you're not gonna put load on the transformer, you don't care about this since it's pretty much going to be a constant, you just want to know this so that at 300v you won't go over 5v with the voltage divider.When you rectify the voltage, the dc voltage becomes 1.41 x Vac - Vdiode_dropSo let's say you have 9vrms transformer ... that's 230:9 ratio ... so let's say you make this thing for maximum 270v, then you'll have 270 / ( 230/9) = 10.5v but remember that 10-15% extra voltage without load that you have to determine for your particular transformer... so you may have 10.5 + about 2v = 12.5v in worst case scenario.Now take out the voltage drop of diode of about 0.5v - 0.8v if you half wave rectify it, twice with bridge rectifier and you have 11.5-12v max at 270vSo do the math for the voltage divider so that 12v would equal to about 4.3-4.5v if you use 4.096v internal reference (just pick the resistors so at 256v you'll have about 4.096v). Now you have 1024 digits ... 250vThe 100uF capacitor will however affect the measurements as it smoothes the sine wave. If you want to just measure the AC voltage, it will work fine, but if you want to take n samples and calculate a true rms value or something I think it affects things.

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 07:55:44 pm
content: well, things got complicated for me a little bit.I have resistors values of 15k, 100k only... unfortunately I can't get others.let's define few stuff:V1: transformer's primary voltageV2: transformer's sec voltageVr: voltage after bridge rectifier (across capacitor)Vpin: voltage across ADC pin and ground (to be measured) which is across the 15k resistor.transformer rating for example: 230:9 which turns ratio = 25.55now for maximum Vpin of 5v, it will be:Vpin = Vr (15/115) -> Vr=38.34and, V2 will be:Vr = (V2*1.414)-1.5 -> V2 = (Vr + 1.5)/1.414 = 28.175v (rms)and thus maximum input voltage:V1 = 28.175*25.55 = 719.88 = 720v rms^ above is theoretical calculations, true values will never read 720v rms @ input ^_^.now, I chosen Vref for ADC to be 5v (Vcc) and Vref- to be ground, so it's 0-5v = 0-1023.5/1024 = 0.00488v per bit.And, 5v = 720v rms right? so 720/1024 = 0.703v rms per bit .OK?now let's try our calculations:V1 = 200v rmsV2 = 200/25.55 = 7.827Vr = 7.827*1.414 - 1.5 = 9.567vVpin = 9.567(15/115) = 1.2479vadc = 1.2479/0.00488 = 255.7 = 256now, V1 = adc*0.703 right? it will be 256*0.703 = 180v rms NOT 200v rms!^that's what confused me!!for sure, I have something wrong... can you please point it out in these calculations? < make sure res values are a must.

author: mariush
author_rank: Super Contributor
timestamp: April 25, 2013, 08:45:52 pm
content: The transformer inputs and outputs are RMS values:http://www.learnabout-electronics.org/ac_theory/ac_waves02.phpA 9 Vrms transformer will output a PEAK voltage of 1.414 x V rms or about 12.7v .This is BEFORE considering that a transformer may output up to 10-15% (sometimes even more) under low load, which is always the case here. Exactly how much over it's up to you to determine, this percentage varies from transformer to transformer, based on several things (constructions, va rating etc). But once you determine this percentage, it will be pretty much the same no matter how much the input voltage varies.So let's just say as an example that the transformer has without any load a peak voltage of 13.5v (106%, so just 6% over the spec) or 9.55 VrmsNow your ratio isn't 230 :9 or 25.5, it's 230:9.5 or about 24.2Now you still have AC voltage at the output, so you can either use a single diode to do half wave rectification, or a bridge rectifier to do full wave.Half wave is like this:http://www.williamson-labs.com/powersupply.htmIf you use a big enough capacitor, the voltage will be the peak voltage, minus the voltage drop of that single diode which at low loads should be 0.5-0.7v. So with our fictional transformer, it will be 13.5v - 0.7v = 12.8vIf you use a bridge rectifier, you have something like this:http://www.williamson-labs.com/images/pwr-supply-fullwave-287.gifhttp://www.williamson-labs.com/images/wf-hum_120hz-03.gifAnd you can use a smaller capacitor to get the DC as smooth as possible because now you have 120 Hz, twice as many pulses. But, you have 2 diodes in the circuit for every cycle, so now there's 2 x 0.5-0.7v voltage drop, so the output will be 13.5- 1.4v = 12.1vThe capacitor here is a problem, because if you have a variation like 228-230v at the input for a couple of seconds, the variation at the output of the transformer is very small. If you use any capacitor there, or a capacitor that's a bit too big, the capacitor may hold enough charge to keep the voltage change unnoticeable for those couple of seconds.If you just want to read the AC voltage value, then use a capacitor, smooth out the DC and use the ADC. But if you want to do the true rms stuff, I think you kinda need to get as much as a sine wave as possible so that you'll read lots of voltages and then do that math :https://en.wikipedia.org/wiki/Root_mean_squareSo anyway, for that fictional transformer, we have 12-14v at the output after using the 230v - 9v rms transformer and diode or bridge rectifierYou can put three 15k resistors in series to get a 45k resistor, so now you can use 100k and 45k and you have a voltage divider that will give you 5v for about 16v inputGoing in reverse, 16v input + 1.4v that was dropped on the diodes is 17.4v ... the Vrms would be 0.707 x 17.4v = 12.3v and since you determined the ratio to be 24.2 (approx.) you know the aC voltage at input is about 12.3x24.2 = 297.66 VrmsIf you only have ONE 15k and ONE 100k resistor, just change the math but don't expect to get exactly 5v

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 09:46:22 pm
content: yes, I have one 15k and one 100k res.I understand what you said, but how can I write the software for it?I have a function that gives the 10-bit adc value called adcread(). and another function to read that 0-1023 value and use it to calculate the rms voltage, this is the math problem now.If I used 15k and 100k I will get 2.0869v @ 16v. and the ref voltage is 5v so 5/1024 = 0.00488v/bit.2.0869/0.00488 = 427.65 < this is the ADC value @ 2.08v @ 16v... that is correct till now, right?now, to calc rms IN SOFTWARE I will go like this and you tell me if it's good or not:float RMS_Calculator (){v_adc = adcread(0); // read adc value and output 0-1023, this function is pre-defined and tested/*for 230v rms -> 12.8v peak (after bridge) -> after voltage divider:12.8*15/115 = 1.669vthis 1.669: 1.669/0.00448 = 342.12 adc <<<-- 0.00488 because of adc pin ref of 5v.now, V_pin = (adc*5)/1023; < will output 0-5vand, 230/1.669 = 137.8 which can be a comp. factor.now, V_rms = 137.8*V_pin; <<< 137.8*adc*5/1023 .... if adc=342: V_rms = 230.34 which is correct.ALL this can be summed as follows:*/V_rms = (V_adc)*0.6735; // 5*137.8/1023=0.6735// so, for 342: 342*0.6735=230.337return V_rms}I only need to calc the rms value of the ac without any sampling. Is the previous code good enough for my specs?I noticed that in this code, I don't have to worry about converting any value from peak to rms... do you find it true?

author: mariush
author_rank: Super Contributor
timestamp: April 25, 2013, 10:19:41 pm
content: Depending on what PIC you have, the PIC may have an internal voltage reference that can be configured at 2.048v or 4.096vIf you set it at 2.048v , you'll get 1024 values for 2.048v which you get for 15.7v or more. Anything over 15.7v at input will not kill the PIC as you still have way up to the 5v but the ADC will give you 1023.You get the value from the ADC, a 10 bit number. You multiply that by your voltage per division - for 2.048v reference, that's 0.002 v.Let's say you get 980 ... then you know the voltage read is 980x0.002 = 1.96vYou go backwards and since you know the voltage divider ratio, you can determine that the voltage before the voltage divider was 15v. This is the voltage at the capacitor, AFTER the voltage is rectified to DC.So now you have to add the voltage that was dropped across the diode (if you rectify using half wave, a single diode), or across the two diodes in the bridge rectifier.You measure the diode or the bridge rectifier before, and you store the voltage drop in a variable. Let's say 0.7v for both cases.So:if you used a single diode, add 0.7v .. now your voltage before the diode, right between the output of the transformer and the diode, is 15.7vif you used a bridge rectifier, add 1.4v .. now your voltage before the diode, right between the output of the transformer and the diode, is 16.4vNow this is the peak value of the transformer output. So to get the rms value of the secondary, you do Vpeak x 0.707 , and you get 15.7v x 0.707 , or 16.4 x 0.707 ... that's 11.099-11.594.Once you have this rms value, you can multiply it by the ratio factor you determined for the load you put on the transformer.Again, it's not 230/9 because at basically no load, your particular transformer will output a bit more than that. But the ratio will be somewhat preserved ... if the input has 220v the output will get a value, if your input has 250v the output will have another value, but the ratio will be almost if not exactly the same. It's just unlikely to be exactly 230/9.It may be 230 / 9.5 with no load... if you put a 470 ohm resistor on the secondary to make it consume about 20mA, the Vrms will not be the same, it might be slightly less, let's say 9.35v .. but if the input voltage goes up or down, so will the output voltage.It really depends on the transformer you have, how much it will be above 9v rms with no load.A 9v 10VA transformer will probably have lower output with no load, compared to a 9v 50VA transformer with no load - I actually have a 9v 50VA transformer on my desk, weighs almost 1 Kg.. I can do some measurements for you if you want, just to see the unregulated output with various loads on it.So when you determine the ratio, let's say 230/9.5 = 24.2 .. you just multiply by 24.2 and you should get the value.so something like this:v= adcread();result = ((v * 0.002) * 7.66 + 1.4 ) x 0.707 x 24.2v = adc result0.002 - 2.048 v reference, so 0.002 v per bit)7.66 - the multiplier for 100k/15k voltage divider -- (100k + 15k)/15k1.4 - the voltage drop over the 2 diodes in the bridge rectifier)0.707 - convert the peak voltage to Vrms24.2 - the transformer ratioexamples :800 => (800x0.002 * 7.66 +1.4) x 0.707 x 24.2 = 233.64875 => (875x0.002 * 7.66 +1.4) x 0.707 x 24.2 = 253.304

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 10:43:57 pm
content: aha nice, I will try such code. But I told you that the ref is 5v so I will stick with it for the moment.I will do the following steps:1- connect the circuit. connect AC source to the transformer's primary and leave secondary floating for the moment.2- measure V1 (primary) and then measure V2 (secondary)... now N=V1/V2 which is the turns ratio... It will be someway 24.2-25.5.3- for software, I will stick with 5v ref for now so the calculations will be:v= adcread();result = ((v * 0.00488) * 7.66 + 1.4 ) x 0.707 x 24.2return v/*0.00488 - 5 v reference, so 0.00488 v per bit7.66 - the multiplier for 100k/15k voltage divider -- (100k + 15k)/15k1.4 - the voltage drop over the 2 diodes in the bridge rectifier0.707 - convert the peak voltage to Vrms24.2 - the transformer ratio which will be the measured one.*/4- connect the +ve terminal of the 15k res to PIC ADC pin, and -ve terminal to ground (which is from the 9v dc charger).5- have fun.___thank you for all your help... really saved me.still I will ask you about the 2.048v reference thing... will it have more benefit to me than 5v?If the voltage became 3v, what will be the output?you mentioned 2.048v ref voltage will have about 15.7v because of the divider... this thing helped me. 15.7v is 10.11v rms at secondary windings which equals 244.66v rms at primary... Is that correct?SO, you are trying to tell me that 2.048v reference voltage is more accurate than 5v for this application?

author: mariush
author_rank: Super Contributor
timestamp: April 25, 2013, 11:34:42 pm
content: Oh .. went a second time through your codeQuoteYou can't use a compensation factor like that, because the voltage drop on diodes in the rectifier remains constant, while the transformer output varies. You're not taking that 1.4v (or around that value) into consideration.It's just a bit silly to use the 5v reference, because then your maximum adc value will probably be always under 4-500, so a 9 bit value out of 10 bits. You're wasting precision.. it's much better to use 2.048v or 4.096v because you get more precision, which matters if you actually want more accurate results.In addition, when you have 2mV per bit or 4mV per bit, you can do some math tricks , like saying 2 / 1000 or 4 / 1000 instead of 0.002 or 0.004 and that may help you a lot if you need to do lots of measurements in a second because multiplying by two is basically shifting a bit to the left, multiplying by 4 is shifting 2 bits to the left and so on.Floating point multiplications will waste quite a lot of cycles.So if you have (v * 0.002 * 7.66 + 1.4) x 0.707 x 24.2 you could write it like this :convert 1.4v to 0.18276 x 7.66 -> (v x 0.002 * 7.66 +0.18276 * 7.66) * 0.707 * 24.2convert 0.002 to 2/1000 -> (v x 2/1000 * 7.66 + 0.18276 * 7.66) * 0.707 * 24.2multiply that 1.4 with 1000 to get both to be /1000 - > (v * 2/1000 * 7.66 + 182.76 * 7.66/1000 ) * 0.707 * 24.2now that both have 7.66/1000 you can add them - > (v * 2 * 7.66/1000 + 182.76 * 7.66/1000 ) * 0.707 * 24.2=>[( v * 2 * 7.66 + 182.76*7.66) / 1000 ]* 0.707 * 24.2=> now all those floating point numbers are constants so you can pre-multiply them => [(v*2 +1820 ) * 7.66 * 0.707 * 24.2 ] / 1000=>(v * 2 + 182.76) * 131.05 / 1000=>(v << 1 + 182.76 ) * 0.13105so 400 =>(400x2 +182.76) * 0.13105 = 128.79v(400*2 + 183 ) * 0.13105 = 128.82vand you can further round up 182.76 to 183 because it has minimal effect on the results.If you use 4.096 v as reference, it's basically just shifting two bits to the left (v<<2), that's all that changes.You reduced that whole thing to a shift to left and a multiplication, saving lots of cpu cycles.QuoteYour input pin accepts voltages between 0 and 5v and tolerates a bit more for short periods of time. The ADC converts a voltage into a binary value, based on a voltage reference. If you use 2.048 as voltage reference, then 1023 will be 2.048v.If you put 3v or more than 2.048v, the adc will report 1023, the maximum it can write in 10 bits.QuoteThe voltage divider is basically (100k + 15 k ) / 15k .. so about 7.66. So anything before the voltage divider, is divided by 7.66 ... that's why 15.68 becomes 2.048 (15.68 / 7.66 = 2.048)...Just the same 25v would become 25 / 7.66 = 3.26v and since the value is higher than the 2.048 voltage reference you set for the ADC, the adc will give you the value 1023. It's like seeing "overload" on digital multimeters, if you put the multimeter on a small range.QuoteI think you forget to add the voltage lost in the bridge rectifier. Don't forget that the voltage before the voltage divider is less than the voltage before the bridge rectifier, by 2 times the drop on a diode.----->  <----> [secondary ] ----- > [bridge rectifier+capacitor] -------> [voltage divider] ---------->If  is 2.048v, then  is 2.048 x 7.66 = 15.68v, is  + 1.4v , 15.68v + 1.4v = 17.08v which is the peak voltage.Then the Vrms is 0.707 x  , 0.707 x 17.08 = 12.07vSo if your transformer ratio is 24.2, then Vrms at [ 0 ] is 24.2 x  = 24.2 x 12.07 = 292v

author: VEGETA
author_rank: Super Contributor
timestamp: April 25, 2013, 11:57:12 pm
content: my PIC is PIC16F877A and I think it has some voltage reference options... because i once tweaked it to be 5v Vcc.I understand everything you said right now, I still didn't tried it in simulation yet... I will do it tomorrow.I will follow the 5 steps I wrote in my previous post, so I won't do that compensating factor.

author: mariush
author_rank: Super Contributor
timestamp: April 26, 2013, 12:34:58 am
content: http://ww1.microchip.com/downloads/en/devicedoc/39582b.pdfpage 141You have two ways to set the internal reference voltageRef = 0 - 15I : Ref / 24 * VinII : Vin / 4 + Ref /32 *VinSo you probably can't get exactly 2.048v or 4.096v, but you can get something like 2.5v.. (12/24)* 5v = 2.5v . You won't be able to shift bits but instead of shifting you just multiply by 2.5 ... basically the same, only you do 2 multiplications instead of a shift and multiply.

author: VEGETA
author_rank: Super Contributor
timestamp: April 26, 2013, 01:46:57 am
content: I think what you wrote is about the comparator not ADC. If you want to adjust ADC reference values, you should check the previous chapter.looks like I will stick with 5v ref for now, I can make a voltage divider to Vref+ to make it about 2v, but my Vcc is about 4.8v so it's not reliable i guess... if I put 2 equal resistors like 470 ohm as a divider, it will be 4.8/2 = 2.4v. but it won't be stable because the Vcc itself isn't (not a good regulator ^_^).

author: VEGETA
author_rank: Super Contributor
timestamp: April 26, 2013, 03:01:05 pm
content: well, I ran the code in simulation and it worked well!still I have 2 problems:1- in this equation: v=(adc*0.00488*7.66+1.4)*0.707*25.55) I noticed that if adc was 0, we will get about 25v rms output because of adding 1.4.and it really affected low voltages...what to do here?2- Is it necessary to put a pull down resistor to each adc pin. Just for the sake of not getting a floating condition as I got once... I connected 5v to AN1 and nothing (no 5v and no ground) to AN0... the result in the program was as it read 5v on AN0...I put 100k pull-down resistor and it worked, but people suggested 1M. Now in simulation 100k affected rms output by 24v! when i connected 1M it was 3v... any advices?Is it enough to connect just the +v output of the divider so that if it was 0v it's already grounded?

author: mariush
author_rank: Super Contributor
timestamp: April 26, 2013, 03:55:26 pm
content: Well think about it... IS 0v really a sine wave? Is it rms, an average, is there a peak voltage? No, what you see there it's an extreme situation.Your minimum Ac voltage that you can measure, would be 1 bit x 0.00488 + voltage drop on diode multiplied by that other stuff , so about 26v AC rmsOf course you'll have problems measuring low AC voltages, you're converting ac high voltage to lower ac voltage so you already lose some accuracy, then you further lose some accuracy using the voltage divider ...so what do you expect ... it's not the 1.4v that give you problems, it's the consecutive decreases that give you low accuracy at low voltages.There's not much you can do.If you want more accuracy across the whole range, you'll just have to keep the accuracy loss to a minimum .. no bridge rectifiers, no capacitors, as little conversions as possible...For example, get a transformer that would give you 5v or 10v at peak at let's say 300v (and use a 50/50 voltage divider in this later case) and drop the whole bridge rectifier and capacitor and everything. You just use a single diode to block the negative pulses.ex:http://uk.farnell.com/vigortronix/vtx-120-006-5045/transformer-6va-2-x-4-5v/dp/1712730240v primary, 4.5v secondary with 25% regulation, so it might go up to 5.5-6v rms, which would give you a peak of about 8.5vBut with such small secondary voltage you could possibly get a 10 ohm 5-7w resistor as a load and waste 2-3w in the resistor, that would be enough to get the transformer more in line, it's basically at 50% load.Anyway, even without, use a voltage divider with 2 100k resistors and you have a peak of about 4.2v for 240v, closer to 5v at 300v acUse a shottky diode to block the negative voltages so you only drop about 0.3v which you can ignore as it won't give you much error in the final results.Now you have half the sine wave, so you read the adc lots of times and you get the voltages as the sine wave goes up and down ... ex you start when your adc reads a value above 0, do as many readings until the adc reads 0 or very close to 0 again, and you take the peak value measured and you multiply that by 0.707 and that's your rms voltage on the secondary side.

author: VEGETA
author_rank: Super Contributor
timestamp: April 26, 2013, 05:23:23 pm
content: yes, that's what I thought... the minimum AC voltage I can measure is about 26v rms and this is proven in Proteus simulation.well, I think I can go with this because the normal operation of the Automatic Transfer Switch I'm designing is going to be @ nominal 220v rms and say voltage of one of the sources got drastically decreased and become 150v rms or so... it's still accurate at that range.so, it's gonna be OK.I told my partners to buy a transformer with low VA rating, about 100VA. Is that good? I thought, for measurements I don't need high current.

author: mariush
author_rank: Super Contributor
timestamp: April 26, 2013, 05:42:20 pm
content: 100VA is A LOT, and will be expensive. My 9v 50VA is almost 1 Kg heavy, and big and it was somewhat expensive, at about 25$ (in local currency).The smallest you're probably going to find is about 6VA, so any 6-12VA transformer should be good as long you use it just to measure current.

author: VEGETA
author_rank: Super Contributor
timestamp: April 26, 2013, 07:33:39 pm
content: aha, but my concern is that will it withstand input current?there are 10VA ones... about 5$ or so.