220V detector - again

I was interested in this subject because I wanted to implement easy roll back to manual of commands implemented with Arduino. For this I needed to use the 220V appearing in the electricity box when manually switching a wall switch for example. Here is the schematic and here a waveform with 330nF. I think that with 660nF the signal could go directly to a digital input. Hope this can be useful :slight_smile:

Looks like the peak voltage is around 1.2V, which isn't even high enough for a 3.3V Arduino Digital Input -- which needs to be at minimum 60% of VCC [you didn't mention what style of Arduino we're talking about, here].

From the official Arduino documentation for the definition of "HIGH":

The meaning of HIGH (in reference to a pin) is somewhat different depending on whether a pin is set to an INPUT or OUTPUT. When a pin is configured as an INPUT with pinMode(), and read with digitalRead(), the Arduino (ATmega) will report HIGH if:

a voltage greater than 3.0V is present at the pin (5V boards)

a voltage greater than 2.0V volts is present at the pin (3.3V boards)

But the signal is "low" when there is mains. :slight_smile:

If you use a capacitive dropper instead of resistors you can drive the opto harder without generating heat.

The links just take you to a span site that wants to download Cookes to you otherwise it will not let you see anything.
If you read the how to use this forum sticky post it will tell you how to post images here.

Hi All,
I suggest the 1N4007 diode should be used as a half wave rectifier
so that only one 120K resistor is required. You do not need the
integrating capacitor(s). Just go with the square wave output.
Herb

Grumpy_Mike:
The links just take you to a span site that wants to download Cookes to you otherwise it will not let you see anything.
If you read the how to use this forum sticky post it will tell you how to post images here.

Thanks, I tried it with IE (browser I never use) and show the images w/o any problem(!). Yet Ill redo just in case imgur 'sometimes' does nasty things

succeeded to oploads the schematic but for the waveform I got
"Your attachment has failed security checks and cannot be uploaded. Please consult the forum administrator." Yet its a waveform and not porn!

septillion:
But the signal is "low" when there is mains. :slight_smile:

If you use a capacitive dropper instead of resistors you can drive the opto harder without generating heat.

Thanks,

the 240K isnt a dropper but a current limiter. Capacitive dropper wont limit the current to the ir LED in case of high freq components such as when sw1 is turned on or heavy loads (A/C etc) are started / stopped and so I would not recommend that

herbschwarz:
Hi All,
I suggest the 1N4007 diode should be used as a half wave rectifier
so that only one 120K resistor is required.
Herb

Thank you good point!

herbschwarz:
Hi All,
You do not need the
integrating capacitor(s). Just go with the square wave output.
Herb

Can you please shade some more light om this idea?

I tried it with IE (browser I never use) and show the images w/o any problem(!).

You probably let the site down load stuff to your computer, without it there is a dark filter over the image and while I can just about make out the waveform the other image was too dark to see.

What format is your picture you are trying to attach to a post?

Grumpy_Mike:
You probably let the site down load stuff to your computer, without it there is a dark filter over the image and while I can just about make out the waveform the other image was too dark to see.

no downloads whatsoever unless to somewhere out of my control and I'd be surprised that imgur does this. I asked IE to show me the downloads and there is nothing new and I think that it is not allowed to dnload cookies w/o asking first

Grumpy_Mike:
What format is your picture you are trying to attach to a post?

122,880 bytes jpg

guy_c:
the 240K isnt a dropper but a current limiter.

It's dropping the voltage isn't it? So yes, it's (also) a dropper :wink:

guy_c:
Capacitive dropper wont limit the current to the ir LED in case of high freq components such as when sw1 is turned on or heavy loads (A/C etc) are started / stopped and so I would not recommend that

That's why in a normal capacitive dropper design you combine a capacitor with a inrush limiting resistor. To get best of both world, so to say.

PS Yes, you can call cookies a form of download. But yeah, you will not see them in your download list. So I would rather call it a cookie wall what the site uses. And yes, it's annoying. But so are all free photo sites :confused:

guy_c:
Can you please shade some more light om this idea?

Simple. Remove the capacitors.

Here's a distillation of all the ideas presented so far:

Adjust the values as needed.

I set the max LED current to around 550ยตA. I set it relatively high, so the Opto's transistor gets driven way into saturation, to produce a nice square-ish wave on the output.

So, at 220VAC, that's a peak voltage of 220VAC * โˆš2 = 311V
311V minus the Opto diode's forward drop of around 1.1V = 310V
310V/550ยตA = 563k
563k - 24k = 539k which is the desired capacitive reactance of C1, at 50Hz.
Using the formula for Capacitive Reactance we get the following capacitor value:
C = 1/(2ฯ€50*539k) = 5.9 nF and the closest standard value to that is 5.6nF, or 5600pF. I would use an "Open Mode" Class-Y, fail safe AC capacitor [Y1 or Y2].

I chose 24k for the resistor to limit current to the Opto's photo diode to within it's max operating range, should a high frequency spike come along. Assuming these events are few, and fast, nothing should blow -- if you expect more noise, and nastiness, then, perhaps, bump that up, or make it a higher wattage.

Regarding the caps: Assume the transistor is off: the cap is charging.
This takes time because the resistor limits the current, so the cap
voltage ramps up. When the transistor turns on, it quickly discharges
the cap. So, without the cap, the waveform would be like a square wave
because the transistor collector current can change quickly from +5V
to almost 0V.
Herb

herbschwarz:
Regarding the caps: Assume the transistor is off: the cap is charging.
This takes time because the resistor limits the current, so the cap
voltage ramps up. When the transistor turns on, it quickly discharges
the cap. So, without the cap, the waveform would be like a square wave
because the transistor collector current can change quickly from +5V
to almost 0V.
Herb

Yes but how do you convert this square wave into a logic state of the switch which is what -to the best of my understanding- the program needs.
I am aware of statements like if(digitalRead, somepin)==HIGH but is there an if(somethingRead, somepin)==SQUARE_WAVE?

septillion:
It's dropping the voltage isn't it? So yes, it's (also) a dropper :wink:

with the smily I must agree

septillion:
That's why in a normal capacitive dropper design you combine a capacitor with a inrush limiting resistor.

I do not know what 'inrush' one should take but the one limiting the current to the max specified by the spec minus the impedance of the capacitor which may be null.

I once plugged the ac adapter of my $2k asus lap and the 63A physical fuse blow and I had to call the electric company. They must have miscalibrated the 'inrush' resistor in their supply :frowning:

guy_c:
no downloads whatsoever unless to somewhere out of my control and I'd be surprised that imgur does this. I asked IE to show me the downloads and there is nothing new and I think that it is not allowed to dnload cookies w/o asking first

This is what I see when I click your links.

This is why your posted links are unacceptable.

guy_c:
I am aware of statements like if(digitalRead, somepin)==HIGH but is there an if(somethingRead, somepin)==SQUARE_WAVE?

Like this:

/********************************************************************************
 *                     220VAC Detector Support Firmware
 *                     
 *                    !!!!!!!!!!! UNTESTED !!!!!!!!!!!!!!
 */

#define AC220_SENSE 3
const unsigned long AC_GONE_GAP = 30;  // # of milliseconds to wait before AC Gone determination 

bool is_AC_first_rising_edge = false;
bool is_AC_present           = false;
unsigned long ac_rising_edge__millis = 0;


void setup() {
  pinMode(AC220_SENSE, INPUT); 
  attachInterrupt(digitalPinToInterrupt(AC220_SENSE), ac_sense_int, RISING);
   
}

void loop() {
  if (is_AC_present  &&  is_AC_first_rising_edge)
  {
    // Do yer 'AC is present' leading-edge thing, here!
    // i.e., that stuff that needs to happen only when AC first appears. 
    // There, of course, will be a slight delay from the actual first
    // rising edge.  If timing is an issue, then do this in the 
    // Interrupt service routine [ac_sense_int()]. 

    // reset this boolean, so this function is only called on the first rising edge.
    is_AC_first_rising_edge = false;  
  }

  if (is_AC_present)
  {
    // Do yer 'AC is present' thing, here!
    // i.e. that stuff that needs to happen over and over while AC is present.
  }

  // This looks for the timeout that flags the disapperance of AC.
  check_for_ac_gone();
}

/** 
 *  @Description:
 *    When this Interrupt starts firing, that means an AC induced squarewave 
 *    is present at the INT pin, and a rising edge occured there.  The first 
 *    rising edge is recognized and captured when previous to this event, a 
 *    "no AC Present" state is in effect. After that, all subsequent rising 
 *    edges are considered NOT the first.
 *    
 *    The current millis are captured, on each Rising Edge, for external 
 *    determination of the "AC presence" state.  A state change occures if it's 
 *    determined that no rising edge occured for longer than the AC_GONE_GAP 
 *    number of milliseconds.
 */
void ac_sense_int() {
  if (!is_AC_present)
  {
    // Then this is the first rising edge since the last determination 
    // that AC was not present.
    is_AC_first_rising_edge = true;
    is_AC_present = true;
  }
  ac_rising_edge__millis = millis();
}

void check_for_ac_gone() {
  if ((millis() - ac_rising_edge__millis)  >  AC_GONE_GAP)
  {
    // Then deem it true that AC is no longer present.
    // Reset everything, so we're ready for the next appearance of AC
    is_AC_present = false;
    is_AC_first_rising_edge = false; 
  }
}

Grumpy_Mike:
This is what I see when I click your links.

This is why your posted links are unacceptable.

it is telling you that it uses cookies and that if this is not acceptable to you than it wont do with you. Most sites I am aware of uses cookies and since this new low became internationally adopted, they are obliged to ask for permission before using cookies what they never did before the adoption of this low.

This is just in case you were not aware which I doubt seriously

I have no difficulties letting sites writing / reading their cookies as sessions becomes statefull and hence more user friendly

ReverseEMF:
Like this:

/********************************************************************************

*                    220VAC Detector Support Firmware
*



/quote]

Thanks,
This is not immune to noise and if you want to make it immune you'll have to add holdoff timers and nearly write a sw pll. the ac line runs all over the house an you'll find plenty of rising edges.

Anyway this is not the way I'd go

guy_c:
once plugged the ac adapter of my $2k asus lap and the 63A physical fuse blow and I had to call the electric company. They must have miscalibrated the 'inrush' resistor in their supply :frowning:

Damn, that should not happen. Normally* a primary house fuse is of the type very (very) slow. So or that fuse was terrible in the first place or that suplly was faulty i would say.

About inrush limiting etc, ReverseEMF made a good schematic for that in reply #11.

And yes, AC is full of noise. You could address this with a small cap. But it's also not to hard in software I think. You don't have to trigger on a single edge. If you react in say 100ms that's fast enough for us humans. Which gives you at least 10 edges. Which is easy to combine with a minimal width detection of a pulse of say 5ms will eliminate probably all noise.

  • Can't speak for all countries but here (Netherlands) it is. And it does make a lot of sense as well.