Better AC sensing circuit for ADC conversion

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
1. Can you post your proteus project file?
Sure, I will share with you soon. At this moment I am in office lab desk. Limited access in PC.

2. V1 should be set to VAC * sqrt(2). For 230vac = 325.27 VP.
The simulation at post #293 the V1 looks like here, I put peak to peak amplitude. do you think it should be half of the value like 212VAC ?

Skärmbild 2026-02-02 091950.png
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
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.

View attachment 363043
Yes, you showed it before and I also implemented it in my circuit, definitely this circuit has benefit. It attenuates the signal in proper level. But my true RMS code was not that good to see something better. If you would share the code it will be understandable.
I don't understand why you want to
1. High‑Impedance Divider Scaling ( R5 and R4).These feed different filters and produce separate positive and negative paths into the transistor network.

2. Clamping voltages (D5, D6). Upper half‑cycle path calculation (R5 → D5→ R1 → C4/C1)

3. Lower half‑cycle path calculation (R4 → D6 → C3), during negative cycles, the lower RC node is pulled negative → quickly discharged. During positive cycles, R4 slowly charges C3 upward.


4. The BC847B has:
  • Base‑emitter threshold: ~0.65 V
The two RC nodes drive the base:
  • When upper RC node > 0.65 VQ1 turns ON → ZCD output pulled LOW.
  • When lower RC node < 0 V (discharged by D6) → Q1 turns OFF → ZCD output HIGH.
 

drjohsmith

Joined Dec 13, 2021
1,613
@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;
}
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 ?
untill you have reliable data from your sample circuit , then what the relays do is irrelivent .
personaly id delete all the output bits till you can receive true data.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
untill you have reliable data from your sample circuit , then what the relays do is irrelivent .
personaly id delete all the output bits till you can receive true data.
ADC to volt is a problem, even you did not notice I put 3.11 at the place of 5V. Even I set V1 as 300VAC Vrms but the virtual terminal shows 341VAC or 324VAC , is not something that comes from wrong scaling ?
 

drjohsmith

Joined Dec 13, 2021
1,613
ADC to volt is a problem, even you did not notice I put 3.11 at the place of 5V. Even I set V1 as 300VAC Vrms but the virtual terminal shows 341VAC or 324VAC , is not something that comes from wrong scaling ?
your finding your mistakes, which is a great change over the last 304 posts. well done.

so what scheme did you finalise on to measure the ac voltage ?
btw : what is your Q1 doing ?
before apllying a random scalling, what do you think your answeres shoukd be ?
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
your finding your mistakes, which is a great change over the last 304 posts. well done.
The progress is not bad I would say, need to polish a bit. Post #293 is better then the result in #169, #177, #212. I wont fed up, it a good learning. I will also design the idea from @Ian0

so what scheme did you finalise on to measure the ac voltage ?
Voltage divider factor at A0 sensor pin is MUST may be. Read post # 293 carefully, see the code. I will add a 1:1 transformer after V1. Detecting ZCD is not bad idea, I can do it without writing code. @eT circuit is also good.

btw : what is your Q1 doing ?
before apllying a random scalling, what do you think your answeres shoukd be ?
Read post #302, I know you are the master of transistor.
 

drjohsmith

Joined Dec 13, 2021
1,613
The progress is not bad I would say, need toan polish a bit. Post #293 is better then the result in #169, #177, #212. I wont fed up, it a good learning. I will also design the idea from @Ian0


Voltage divider factor at A0 sensor pin is MUST may be. Read post # 293 carefully, see the code. I will add a 1:1 transformer after V1. Detecting ZCD is not bad idea, I can do it without writing code. @eT circuit is also good.


Read post #302, I know you are the master of transistor.
@Hasan2019
Your sarcasm belittles you .
What do you mean by "MUST may be" , was that a typo.
What do you think Q1 is doing ?
 

eetech00

Joined Jun 8, 2013
4,709
Sure, I will share with you soon. At this moment I am in office lab desk. Limited access in PC.



The simulation at post #293 the V1 looks like here, I put peak to peak amplitude. do you think it should be half of the value like 212VAC ?

View attachment 363047
Sine voltage sources require PEAK values.
Amplitude should be set to the PEAK value equal to the RMS value.
If you want the V1 voltage source to generate 230VAC, then set the Amplitude equal to 230 x (square root of 2) = 325.269.
So...Amplitude = 325.269
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Sine voltage sources require PEAK values.
Amplitude should be set to the PEAK value equal to the RMS value.
If you want the V1 voltage source to generate 230VAC, then set the Amplitude equal to 230 x (square root of 2) = 325.269.
So...Amplitude = 325.269
Then I am not doing wrong!
 

eetech00

Joined Jun 8, 2013
4,709
Yes, you showed it before and I also implemented it in my circuit, definitely this circuit has benefit. It attenuates the signal in proper level. But my true RMS code was not that good to see something better. If you would share the code it will be understandable.


1. High‑Impedance Divider Scaling ( R5 and R4).These feed different filters and produce separate positive and negative paths into the transistor network.

2. Clamping voltages (D5, D6). Upper half‑cycle path calculation (R5 → D5→ R1 → C4/C1)

3. Lower half‑cycle path calculation (R4 → D6 → C3), during negative cycles, the lower RC node is pulled negative → quickly discharged. During positive cycles, R4 slowly charges C3 upward.


4. The BC847B has:
  • Base‑emitter threshold: ~0.65 V
The two RC nodes drive the base:
  • When upper RC node > 0.65 VQ1 turns ON → ZCD output pulled LOW.
  • When lower RC node < 0 V (discharged by D6) → Q1 turns OFF → ZCD output HIGH.
Yes. but what will you use ZCD pulses for?
Is the intent to switch the relays at zero-crossing?
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Yes. but what will you use ZCD pulses for?
Is the intent to switch the relays at zero-crossing?
Still trying to synchronize with relay. Voltage stabilizers often drive motors or transformers (highly inductive loads). Switching at zero voltage prevents severe voltage spikes that occur when switching inductive loads off-sync.

May be you remember when I was involving LM324 based voltage stabilizer, some one suggested peak detection delay circuit to help the relay driver. Thinking that microcontroller can do it or not? I have another circuit to do it.
 

eetech00

Joined Jun 8, 2013
4,709
Still trying to synchronize with relay. Voltage stabilizers often drive motors or transformers (highly inductive loads). Switching at zero voltage prevents severe voltage spikes that occur when switching inductive loads off-sync.

May be you remember when I was involving LM324 based voltage stabilizer, some one suggested peak detection delay circuit to help the relay driver. Thinking that microcontroller can do it or not? I have another circuit to do it.
Yes. I remember.

Maybe is a little advanced, but you maybe can eliminate the relays and use opto-coupled ZCD IGBT or MOSFET drivers to drive TRIAC load switches. Then ZCD is handled in the HW drivers instead of in the mcu.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Yes. I remember.

Maybe is a little advanced, but you maybe can eliminate the relays and use opto-coupled ZCD IGBT or MOSFET drivers to drive TRIAC load switches. Then ZCD is handled in the HW drivers instead of in the mcu.
Could you kindly take a look on post #16, https://forum.allaboutcircuits.com/threads/better-ac-sensing-circuit-for-adc-conversion.209240/#post-2016962

Mains_ZCD_VoltageToPulseW_Opto.JPG

Is it something you are talking about ?


This is a mains Zero‑Cross Detector (ZCD) and voltage‑to‑pulse‑width converter using:
  • A highly resistive dropper network (R4, R1, R2)
  • Dual diodes for current shaping (BAV199 series)
  • A current-regulating transistor (Q1: PSSI2021)
  • A pulse‑width shaping network (R5, R3)
  • An optocoupler (PC817) for galvanic isolation.
The result is a pulse whose width is proportional to the AC mains voltage, synchronized to every half‑cycle.
 

drjohsmith

Joined Dec 13, 2021
1,613
@Hasan2019
how we doing at apturing a good voltage reading ?
last i read you were coming to a conclusion, but we as far as I know, and I could have missed it, have not seen you final circuit and code to meassure the mains.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
@Hasan2019
how we doing at apturing a good voltage reading ?
last i read you were coming to a conclusion, but we as far as I know, and I could have missed it, have not seen you final circuit and code to meassure the mains.
Are you talking about voltage capturing ? At post #293, I already showed to you! Vp =424V, Vrms=300V, Vd=0.7V was the condition. Yes, I have fixed it with some adjustment.

My last simulation is one of the solution for 4 relays but I will go for 8! I am talking to @eT now for improving more. But I wont avoid @lan0 suggestions. But I can tell you the circuit @panic mode mode was simulating is the corrected version what I have posted at #1.
 

drjohsmith

Joined Dec 13, 2021
1,613
Are you talking about voltage capturing ? At post #293, I already showed to you! Vp =424V, Vrms=300V, Vd=0.7V was the condition. Yes, I have fixed it with some adjustment.

My last simulation is one of the solution for 4 relays but I will go for 8! I am talking to @eT now for improving more. But I wont avoid @lan0 suggestions. But I can tell you the circuit @panic mode mode was simulating is the corrected version what I have posted at #1.
Thank you
Sorry with so.many hundreds of posts , it's easy to get lost.

So your happy with your AC reading circuit and code.
That's great .
Have you tried it with the range of AC values in your expecting to work over ?
Is the voltage value you decode updated in your code on a fixed period , or randomly as the main loop runs ? I.e. if you add to the main loop , the period between readings would get longer

What is the plan for the output code ?
This I think has to take as input the voltage value , and output control lines to select which relays to activate or not.
Sounds to me like a state machine ?
 

MrAl

Joined Jun 17, 2014
13,716
Could you kindly take a look on post #16, https://forum.allaboutcircuits.com/threads/better-ac-sensing-circuit-for-adc-conversion.209240/#post-2016962

View attachment 363144

Is it something you are talking about ?


This is a mains Zero‑Cross Detector (ZCD) and voltage‑to‑pulse‑width converter using:
  • A highly resistive dropper network (R4, R1, R2)
  • Dual diodes for current shaping (BAV199 series)
  • A current-regulating transistor (Q1: PSSI2021)
  • A pulse‑width shaping network (R5, R3)
  • An optocoupler (PC817) for galvanic isolation.
The result is a pulse whose width is proportional to the AC mains voltage, synchronized to every half‑cycle.
Hi,

Just wondering, why such an elaborate zero crossing detector?
 

panic mode

Joined Oct 10, 2011
5,013
no kidding... 300+ posts.

the first circuit shown in post #16 is a naive attempt to measure and sync to mains. chosen part is an AC optocoupler with low CTR. that is the reason for lower series resistor and output that resembles rectified AC sine. this optocoupler is meant for only checking AC presence, not measuring amplitude. the good news is that one can sample this signal by ADC and determine voltage as well as zero crossing points. there are also bunch of useless parts (C1,C2,R2). the bad news is that already bad CTR is only going to get worse and this means need for repeating calibration over time. the same happens if it ever need to be replaced. also since signal is rectified sine wave, there are no steep edges to sync onto. and syncing on peaks is not reliable doe omnipresent noise. so not something i would consider a first choice.

the second circuit has more parts but... it is a better, more mature design. it also contains useless parts (R4,D3,D5) thate are only making clutter. one of the advantages of this circuit is that current draw is small due to much larger value of resistor. this optocoupler is DC type but with much higher CTR, so with moderately high value of R3 transistor easily saturates. this means output is digital signal and the shape will be true even with aging. and current limiter sets the current to some 0.4mA so average current is the same but no high peaks. capacitor is charged and discharged slowly which translates to pulse width. falling edge is nice and rather sharp - great for syncing. it is drifting slightly depending on mains voltage but this is negligible (15-20uS over 20ms is under 0.1%) and if needed can be compensated by measured voltage. rising edge is not as steep but measurement will be consistent since sampled by same GPIO. and - no ADC is needed. post #314 talks about synchronisation to every half-cycle which is not true. this circuit only synchronizes to positive half-periods. which is once per period, not half-period.
 
Top