if (digitalRead(MainsDetect) == LOW) { // && (TurnOnDelay2V) == 1) {
MainsDetectV = 1;
}
if (digitalRead(MainsDetect) == HIGH) {
MainsDetectV = 0;
}
would you suggest one please?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.
why do i need delay ?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)
my requirements are the same as you mentionedSo if we knew your requirements ... we probably could give better advice on how to achieve them. By HW as well as SW.
Klaus
thx for replyI 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.
Either do an internet search, or rely on other people´s opinion, because I´m not very experienced with simulation software.would you suggest one please?
I don´t know. YOU have to decide YOUR application´s requirements.why do i need delay ?
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.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 ?
no actuallyLooks like this circuit was drawn with proteus. Did you run a simulation with that software?
hi BrianTry 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.
what you mean by this?Hi,
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:
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
I wrote what I meant.what you mean by this?
I'm not sure what you mean by that.the only thing common in the 2 places caused this issue to appear is the old electrical wiring
what is different with old wires ?
no it was a mistake im sorryI'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.
is there a different of a new or old electrical 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.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?