220v AC detect problems

johnny78

Full Member level 5
Joined
Jun 28, 2017
Messages
270
Helped
1
Reputation
2
Reaction score
5
Trophy points
1,298
Activity points
3,462
hi Guys
im using this method to detect presence of mains supply (220v AC)

on the MCU side i have set the internal pull ups & detect the presence by reading the status of the input pin
Code:
  if (digitalRead(MainsDetect) == LOW) { // && (TurnOnDelay2V) == 1) {
    MainsDetectV = 1;
  }
  if (digitalRead(MainsDetect) == HIGH) {
    MainsDetectV = 0;
  }
sometimes it fails to detect mains & the MainsDetectV going 0
the main problem is at my place it works fine since years without any problem but sometimes at other places it keep doing problems detecting mains
what may get different on other places i dont know
i've noticed 2 problems
1:when mains present sometimes the mainsDetect goes 0 & re goes 1 & i've tried to use 1uf cap instead of the 22u i use & i see my detection blinking between 0 & 1 so i think using bigger capacitor may solve this issue
2: when no mains present i saw once the detection reading 1 & this is impossible to happen because i use internal pull ups & no mains present to drive the optocoupler led(is it possible that the internal pull up resistors of the MCU are damaged? or the led of the pc817 is damaged ? or maybe i have low ac mains present which may appears like that mains is present)
is there any chip to do this detection for me?
of how to enhance my circuit

i think of alot of reasons may cause this problem but actually i cant see any of this problems happens here at my place it only happens when i install my device
at another place which i dont have tools to test or try
any ideas will be great
thanks in advance

Johnny
 
Last edited:

Hi,

there are free circuit simulation tools. Did you check your circuit with it?
This helps to understand the working principle of the ciruit.
It also helps to understand what output signal to expect .... especially at varying input voltage RMS.
It also helps to improve the circuit to work the way you (your application ) needs.

But when designing a new circuit ... I mean your ATMEGA application ... the first thing you should do is to decide the inputs you need.
Every designer needs to do this.

So if I had to do this, I´d maybe define MY requirements as:
* input is rather pure sinewave
* it should be continously ON when input RMS voltage is above 190V RMS
* it should be continously OFF when input signal is below 150V RMS
* the delay should be below 100ms
(Mind: this is a description for MY fictive application and has nothing to do with your application. I don´t think this requirements could be fulfilled with your circuit)

So if we knew your requirements ... we probably could give better advice on how to achieve them. By HW as well as SW.

Klaus
 

I isn't a design I would use!
Suggestions:
1. feed it between live (line) and neutral. There is no guarantee you will see 240V RMS between line and your ground connection.
2. feed line and neutral, each through 100 K resistors
3. connect the bridge rectifier AFTER the resistors, that way you can use low voltage signal diodes or a low voltage bridge rectifier.
4. remove R3 and C2.
5. add a capacitor (~0.47uF) across the opto-coupler output and a diode from the collector to VCC to discharge it when VCC is removed.

I suspect your problem is actually not enough LED current in the PC817 but if you wire it as above it needs fewer and cheaper components.

Brian.
 
would you suggest one please?
why do i need delay ?
So if we knew your requirements ... we probably could give better advice on how to achieve them. By HW as well as SW.

Klaus
my requirements are the same as you mentioned
thanks in advance
--- Updated ---

thx for reply
i will test your suggestions right now
but i remeber that when i used this circuit the input of the MCU was blinking until i used C2
why 0.47uF at the output ?

thx for help
 
Last edited:

Hi,

would you suggest one please?
Either do an internet search, or rely on other people´s opinion, because I´m not very experienced with simulation software.
I only have very limited experience. Too low of an experience to tell whether it´s good or not compared to others.
****
When I see a circuit like yours .. I see how it works. In short:
AC comes in, gets rectified, becomes current limited, charges a capcitor and drives the optocoupler.
I also see the flaws (that a circuit simulation tool does not)
* useless C1, R4
* why two current limiting ressitors R1, R2. (Explanation could be: power dissipation, safety, voltage rating..)
* very bad V-I behaviour of C2, U1_LED because of missing series resistor. This leads to vary bad timing reliability.
* rather high ohmic, high tolerance pull up (microcontroller) leads to badly defined thresholds and timing.

The function:
* when AC is 0 the output of the optocoupler will be HIGH
* With rising input voltage there will be a level where the output starts toggeling with 100Hz (twice the mains frequency), high duty cycle
* with more rising input voltage the duty cycle becomes smaller
* until a certain input voltage .. where the output becomes a stable LOW (if this ever happens )

There are several issues I see with your application / expectation.
Your software: Did you expect the 100Hz toggeling at all? and did you take care of this with software?
The levels where it begins (from HIGH to) toggeling ... is not reliable. It may vary a lot with: from device to device (part tolerance), temperature, aging...
The same is true for the threshold from toggeling to staedy LOW.

The voltage thresholds are not reliabe, the timing is not reliable. Thus the output (function) is not reliable.
It has a lot to do with "hopy" and "believe" and "maybe". This is not how I design my electronics. I rely on part specifications (including tolerances and drift) and math.

Here I have to tell you are the limits of a simulation tool. Such a poor design also gives poor results in a simulation. Whil in the simulation the function may look OK ..in the real world it may not work reliably. If the simulation tool could simulate ageing, thermal drift, tolerance (I guess there is software that can do this) .. The user needs to input alle these informations and needs to run the simulations with all the se parameters. In the end such a software becomes too complicated to be used by newbies ..

To say it in short: I recommend you to use a simulation tool to play around, modify part values,, ... to see how the circuit behaves. But in the end the result still needs to be taken with a grain of salt, because the simulation can not tell you ALL the truth of a real circuit.


Back to your circuit:
why do i need delay ?
I don´t know. YOU have to decide YOUR application´s requirements.
As said: these are fiktive requirements and maybe not possibel to achieve with this circuit.

*****
When I siad: this is not how I do electronics designs ... this does not mean the circuit does not work.
If an application is fine with "coarse" function, thresholds, timing ... this is OK and may work on million devices.

Thus it´s so important that you define your application requirements. No other persons can do this for you.
For example:
* you may build a hardware that generates clear ON/OFF signals for mains voltage valid/invalid. (there will be no 100Hz toggeling at all)
* other designs may accept toggeling inputs but the software detect the toggeling input as "invalid" and only 0% duty cycle is considered as valid. (pin change interrupt, timer interrupt)
* other designs may accept the toggeling and the software measures the duty cycle to "estimate" the input voltage ... and the valid/invalid threshold may be done with the use of the duty cycle. (As a poor input voltage measurement) (using pin change interrupt or input capture function). The benefit may be that the threshold is flexibly adjuastable by software and does not need hardware adjustment.

So all depends on your decisions, your ideas, your software skills ....
but we all don´t kow about this all.

Btw: Software:
The snippet you show ... leads to questions:
* why did you comment out the "&& turnOnDelay2V" command? (If correctly coded it could result in a toogeling = invalid, 0% dutyCycle = valid function)
* why two inputPinReadings while a single reading works (using preset value or ELSE). I see no benefit in fucnction nor safety or sth else.
* why this function at all ... why not simply read the portState inline

Klaus
 
but i remeber that when i used this circuit the input of the MCU was blinking until i used C2
why 0.47uF at the output ?
From your code it seems you only want to see if AC is present and do not want to see individual cycles. If you feed the optocoupler through resistors and a bridge it will give an output that pulses near zero mains crossing. The 0.47uF capacitor in conjunction with the internal pull-up source will smooth the pulses so it looks more like a DC level. The diode is to protect the MCU in case the capacitor is charged up and the power is removed,, it will discharge the capacitor to the supply line to avoid risk of reverse polarity on the MCU input.

Brian.
 

Looks like this circuit was drawn with proteus. Did you run a simulation with that software?
 

Had to eliminate C in order to get decent output, as well as reduce 100Ks to 10K to get Iled up :



Note slow Trise into MCU, possible issue ?

Now add 20V RMS noise (had to alter 22 uF to get output) :



If I elim filter across LED, no noise enabled, good results. But adding 20 V RMS noise output quite
noisy. One trades off filter for min led shutoff current, eg. generate a logic 1 to MCU.


Regards, Dana.
 
Last edited:

Try it the way I stated - I do not have Simetrix to simulate in the same way.

Basically, both inputs through 47K - 100K resistors, putting one in each leg makes it safer and less prone to noise coupling. The other side of the resistors go directly to a bridge rectifier, it will only have a few volts across it so small signal diodes can be used. Connect the optocoupler LED directly across the bridge output. Then put the filter capacitor across the transistor and ideally add a pull-up resistor to increase charging current and a diode, across it, cathode to VCC to protect the MCU when power is removed. It might be useful to add a small resistor, a few hundred ohms in series with the opto transistor collector pin.

I notice the simulator schematic shares ground with the AC input and the output signal, this is not wise unless the whole thing is well insulated. If it really should share a common connection, there is a simpler way to do the operation that doesn't use an opto-coupler at all.

Brian.
 

Caution using small signal diodes, make sure reverse breakdown V is adequate.

The prior sim was not done as a solution, just to show OP what his original solution
looked like. And to deal with power line noise as an important consideration.

Simetrix is free for base version, and far superior to LTC spice, and was Analog Devices goto
sim until they absorbed LTC. Shame decision got made that way.


Regards, Dana.
 

Wiring the resistors before the bridge means the voltage across + and - will never be more than Vf of the optocoupler LED, typically less than 2V so small diodes are fine.

Simetrix may be free but does it work under Linux? I gave up on Windows years ago and never looked back.

Brian.
 

@betwixt, I made the mods you suggested, or so I think, sans output diode,
output cap, and had to add R across LED to get it to shut off. I had to raise the 100Ks to
get the led to shutoff as well.

Tr and Tf horrible though.



With 20V of noise :



One concern is CTR can be as low as 50%, so maybe 5K pullup too low.....


Regards, Dana.
 
Last edited:

hi Brian
im testing it now & i think its ok two 100k resistors & the output of the bridge is connected to the optocoupler & accross the emmiter collector i've added 4.7uF capacitor which gives me some delay

the only thing common in the 2 places caused this issue to appear is the old electrical wiring
what is different with old wires ?

thanks for help
--- Updated ---

what you mean by this?
 

the only thing common in the 2 places caused this issue to appear is the old electrical wiring
what is different with old wires ?
I'm not sure what you mean by that.
In your original post, one side of the input voltage is also connected to the MCU ground. Normally, if you are sensing AC mains voltage you do it across the live and neutral wires, not live and ground. The MCU ground is normally isolated or connected to Earth. There are two scenarios:
1. if you really are measuring live to ground, your voltage detection may be unreliable and the opto-coupler isn't needed.
2. if you are measuring live to neutral, you might be shorting neutral and live together and the MCU could potentially have dangerously high voltage on its pins. I'm not sure where in the World you are but imagine what happens if your wall plug can be reversed and your MCU ground pin connects to 240V AC!

The advantage of my method:
1. the resistors provide a degree of extra safety by limiting current into the rest of the circuit
2. the diodes only ever have a few volts across them so they are cheaper and less prone to damage from voltage spikes
3. the impedance at the output point is higher so a smaller capacitor can be used to achieve the same time constant.

Brian.
 

no it was a mistake im sorry
no connection between MCU ground & earth& im just detecting presence of mains voltage
 

I'm not sure what you mean by that.
is there a different of a new or old electrical wires ?
what would change with old aging ?
 

Cookies are required to use this site. You must accept them to continue using the site. Learn more…