Better AC sensing circuit for ADC conversion

Ian0

Joined Aug 7, 2020
13,158
Hardware version as promised many many posts ago.IMG_2149.jpeg
input comes from the output of the system via a safety isolating transformer. (it could be full wave rectified if you want).
If the voltage is in the centre dead band the clock is inhibited. If it is below the lower threshold it counts up, and if it is above the higher threshold it counts down. The output form TC inhibits the clock if it reaches zero or 15.
The final gate of the 74HC132 forms an oscillator which provides the clock pulses. About 1Hz or slower should do.
 

Ian0

Joined Aug 7, 2020
13,158
You can then complete the project with two 100VA transformers (for 1kVA output) and four relays, and a quad ex-or gate.IMG_2156.jpegYou could replace the multi-secondary transformer with a 50VA 120V, a 25VA 60V and A 12VA 30V, so everything is off-the-shelf parts.
if you stop faffing about and order the parts today, they will arrive tomorrow and you could build it over the weekend.
 

drjohsmith

Joined Dec 13, 2021
1,614
You can then complete the project with two 100VA transformers (for 1kVA output) and four relays, and a quad ex-or gate.View attachment 362862You could replace the multi-secondary transformer with a 50VA 120V, a 25VA 60V and A 12VA 30V, so everything is off-the-shelf parts.
if you stop faffing about and order the parts today, they will arrive tomorrow and you could build it over the weekend.
I think @Ian0 the op has said this is simulation only !
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Hardware version as promised many many posts ago.View attachment 362831
input comes from the output of the system via a safety isolating transformer. (it could be full wave rectified if you want).
If the voltage is in the centre dead band the clock is inhibited. If it is below the lower threshold it counts up, and if it is above the higher threshold it counts down. The output form TC inhibits the clock if it reaches zero or 15.
The final gate of the 74HC132 forms an oscillator which provides the clock pulses. About 1Hz or slower should do.
Well, welcome back. I though you are some where @Ian0, its a point I asked for help here, I was also thinking here is some miss/match in my circuit. I will follow you now. Take a look that circuit and the truth table here, consider only post #169,#212, #177

4_bit_coded_decimal.PNG

truthtable.PNG

Make a comparison with your idea. Only read the problem in post #261, only focus on it.
 
Last edited:

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
You can then complete the project with two 100VA transformers (for 1kVA output) and four relays, and a quad ex-or gate.View attachment 362862You could replace the multi-secondary transformer with a 50VA 120V, a 25VA 60V and A 12VA 30V, so everything is off-the-shelf parts.
if you stop faffing about and order the parts today, they will arrive tomorrow and you could build it over the weekend.
Yes, it will be a good progress then. I will follow your design. May be or not it will solve my relay inactive issue.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
trying to tie down this code of yours,

you say the numberes read is not correct

so can you strip your code down to just the bit thats reading analog voltage please, and post it.
idealy the code is modular so we can once its working , it can be re used in your project.
lets get that working correctly.
Professor Smith, I was busy last week with one of my project that needs to be simulate in SIMPLIS. Running it for selecting less number of output cap in a vertical power product.

For post #265 code I want to implement a circuit, hoping to see some result.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Hi,

Yeah it might sound a little tricky but it's actually very simple.
You keep a time period stored in memory, like for a 50Hz line ideally it would be 0.010000 seconds. If your next zero crossing comes in before that (it would only be a tiny amount) then you decrease the stored time period, say 0.009900, but if it comes in after that then you change the stored period to the new value which might be 0.010100 seconds. In this manner you 'know' the next zero crossing before it even happens. In fact, you assume you know it 1/2 cycle before it actually happens. In this way you can get the timing down really close.

You can do this with a clock too that does not have perfect timing if you have another clock that has perfect timing. You store 1 second and then keep adjusting that as things change. The poor clock stays synced to the good clock within a very small margin of error, and the average time period should be really, really close, because it's being adjusted up and down relative to the average.

You don't have to do this, it's just one of those little refinements that may not even matter in your application. Get it working as is first, then think about this again later.
@MrAl , this explanation is much helpful to understand what's going on my option A code. I have not start working PLL yet.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Synchronizing the sampling is a great technique
The reason to synchronise , is to ensure you acquire a perfect single cycle . So you don't end up with say 1 1/2 cycles , which would give a varying number..
Alternatively ,
Look at the maths. If you sanoled say 128 cycles or 128 1/2 cycles and averages, the error now is 1/256 of a cycle . Probably more than good enough for your application , and you won't want to change your relays faster than that.

But
As MrAi says .
All these are after we have you sampling stable numbers.
Dr. Smith, I need to understand this issue here. The RMS averaging code gave me some numbers, but perhaps am doing wrong to select correct circuit.
 

MrAl

Joined Jun 17, 2014
13,720
Well, welcome back. I though you are some where @Ian0, its a point I asked for help here, I was also thinking here is some miss/match in my circuit. I will follow you now. Take a look that circuit and the truth table here, consider only post #169,#212, #177

View attachment 362984

View attachment 362985

Make a comparison with your idea.
Hi,

Is that supposed to be some type of rudimentary A to D converter?

I haven't seen a circuit like that in years now, brings back memories. They also make chips with built in comparators that can do that or similar, maybe 8 or 10 inputs, like the LM3914 chip. You can look into that if you think it might help.
There's the LM3914, LM3915, and LM3916 I think. One is linear, one is log, and one is for standard "VU".
The outputs are used to light up LEDs usual for a bar graph. Probably still have to convert to binary though like with a 16 to 4 line encoder chip.
 

drjohsmith

Joined Dec 13, 2021
1,614
Professor Smith, I was busy last week with one of my project that needs to be simulate in SIMPLIS. Running it for selecting less number of output cap in a vertical power product.

For post #265 code I want to implement a circuit, hoping to see some result.
Were also looking forward to seeing something .
 

drjohsmith

Joined Dec 13, 2021
1,614
Dr. Smith, I need to understand this issue here. The RMS averaging code gave me some numbers, but perhaps am doing wrong to select correct circuit.
Of
Dr. Smith, I need to understand this issue here. The RMS averaging code gave me some numbers, but perhaps am doing wrong to select correct circuit.
How's it going getting a reliable voltage reading ?.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Of
How's it going getting a reliable voltage reading ?.
@drjohsmith , @Ian0 , @MrAI referring to post #169,#212, #177 I am simulating the previous schematics and zero crossing_rms code on it. But I just change the input circuit, one at A0 sensor other is for ZCD at pin 2 .
update1.PNG

According to voltage divider calculation, at pin A0, VC1= (Vp* R2/R5+R1+R2)-0.7=3.11V (If Vp =424V, Vrms=300V, Vd=0.7V), but in simulation it looks 1.27V. I was trying to add a fuse after V1 source, but it gets burn always, and A0 showed 0.12V. So I removed it. VRMS value looks 341V but why the Relay 4 is active ? Relay 1 should active here. Zero crossing is not accurate here and AC signal is clipping.

From this simulation result, if VC1=1.28V, then Vp=220V, and in this case code loop select relay 4.
Look at this link how the input circuit is calculated. AC Voltage measurement for stabilizer

If I am wrong correct me kindly,

C:
float calculateRMS() {
  double sumSq = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {
    float voltage = (samples[i] * (3.11 / 1023.0));  // ADC to volts
    voltage -= 1.55;                                // Remove offset
    sumSq += voltage * voltage;
  }

  float vrms = sqrt(sumSq / NUM_SAMPLES);

  // Calibration factor for your sensor (adjust as needed)
  float calibration = 300.0 / 0.707;  // Example: scale to real AC RMS

  return vrms * calibration;
}
You could raise some questions for capacitor and voltage tolerances or wattage issues. But Proteus doesn't bother it. If the Fuse is troubling really then I will not consider it simulation even in @panic mode circuit. Am I violating any calibration rule in the code @MrAl ?
 
Last edited:

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Hi,

Is that supposed to be some type of rudimentary A to D converter?

I haven't seen a circuit like that in years now, brings back memories. They also make chips with built in comparators that can do that or similar, maybe 8 or 10 inputs, like the LM3914 chip. You can look into that if you think it might help.
There's the LM3914, LM3915, and LM3916 I think. One is linear, one is log, and one is for standard "VU".
The outputs are used to light up LEDs usual for a bar graph. Probably still have to convert to binary though like with a 16 to 4 line encoder chip.
I think I have use LM3914 for silly VU meter in past. I am thinking about @Ian0 circuits, definitely I will design it to see the difference with circuit I posted for 16 relay system. ( see post #79 please)
 

Ian0

Joined Aug 7, 2020
13,158
Well, welcome back. I though you are some where @Ian0, its a point I asked for help here, I was also thinking here is some miss/match in my circuit. I will follow you now. Take a look that circuit and the truth table here, consider only post #169,#212, #177

View attachment 362984

View attachment 362985

Make a comparison with your idea. Only read the problem in post #261, only focus on it.
It's fuzzy - I can't focus on it!
Screenshot from 2026-02-01 22-02-08.pngScreenshot from 2026-02-01 22-02-08.png
But I can tell you that you could lose all those inverters simply by swapping the inputs on the comparators.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
It's fuzzy - I can't focus on it!
View attachment 363041View attachment 363041
But I can tell you that you could lose all those inverters simply by swapping the inputs on the comparators.
Yes, very poor resolution . But All of them are backdated 741, the student was trying to implement it for understanding the ADC flash and signal conditioning. I will deep look on your circuit, old technology should not avoid. I think a lot of old gadgets are sill servive in market.
 

eetech00

Joined Jun 8, 2013
4,709
@drjohsmith , @Ian0 , @MrAI referring to post #169,#212, #177 I am simulating the previous schematics and zero crossing_rms code on it. But I just change the input circuit, one at A0 sensor other is for ZCD at pin 2 .
View attachment 363040

According to voltage divider calculation, at pin A0, VC1= (Vp* R2/R5+R1+R2)-0.7=3.11V (If Vp =424V, Vrms=300V, Vd=0.7V), but in simulation it looks 1.27V. I was trying to add a fuse after V1 source, but it gets burn always, and A0 showed 0.12V. So I removed it. VRMS value looks 341V but why the Relay 4 is active ? Relay 1 should active here. Zero crossing is not accurate here and AC signal is clipping.

From this simulation result, if VC1=1.28V, then Vp=220V, and in this case code loop select relay 4.
Look at this link how the input circuit is calculated. AC Voltage measurement for stabilizer

If I am wrong correct me kindly,

C:
float calculateRMS() {
  double sumSq = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {
    float voltage = (samples[i] * (3.11 / 1023.0));  // ADC to volts
    voltage -= 1.55;                                // Remove offset
    sumSq += voltage * voltage;
  }

  float vrms = sqrt(sumSq / NUM_SAMPLES);

  // Calibration factor for your sensor (adjust as needed)
  float calibration = 300.0 / 0.707;  // Example: scale to real AC RMS

  return vrms * calibration;
}
Hi

1. Can you post your proteus project file?
2. V1 should be set to VAC * sqrt(2). For 230vac = 325.27 VP.
 

eetech00

Joined Jun 8, 2013
4,709
Here's a simulation using basic code.
But I'm not using rectification of the input sine wave (and I don't understand why you want to).
Mains input is 230VAC (RMS) and I'm using a transformer (AC adapter) followed by divider to step it down.

1769996779901.png
 

MrAl

Joined Jun 17, 2014
13,720
It's fuzzy - I can't focus on it!
View attachment 363041View attachment 363041
But I can tell you that you could lose all those inverters simply by swapping the inputs on the comparators.
Hi,

Yes it looks like a bunch of comparators and a resistor divider to establish the reference voltage for each comparator.
Why they wanted to use inverters on the output I can't say for sure. Maybe they were Schmitt Triggers or something.
It's also not entirely clear if the comparators are really comparators or op amps as there are no pullup resistors that I can see.
No hysteresis either.
 
Top