Go Down

Topic: 220V detector - again (Read 863 times) previous topic - next topic

Grumpy_Mike

#15
May 21, 2019, 06:29 pm Last Edit: May 21, 2019, 06:29 pm by Grumpy_Mike
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.

ReverseEMF

I am aware of statements like if(digitalRead, somepin)==HIGH but is there an if(somethingRead, somepin)==SQUARE_WAVE?
Like this:
Code: [Select]

/********************************************************************************
 *                     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;
  }
}
"It's a big galaxy, Mr. Scott"

Please DON'T Private Message to me, what should be part of the Public Conversation -- especially if it's to correct a mistake, or contradict a statement!  Let it ALL hang out!!

guy_c

#17
May 21, 2019, 08:06 pm Last Edit: May 22, 2019, 09:15 am by guy_c
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

guy_c

#18
May 21, 2019, 08:12 pm Last Edit: May 22, 2019, 09:12 am by guy_c
Like this:
Code: [Select]

/********************************************************************************
 *                     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


septillion

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 :(
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.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Grumpy_Mike

Quote
I have no difficulties letting sites writing / reading their cookies as sessions becomes statefull and hence more user friendly
A strange new usage of the word friendly. This is why we have rules about going off site. I don't want my computer infected with this spam.

guy_c

A strange new usage of the word friendly. This is why we have rules about going off site. I don't want my computer infected with this spam.
Ok dear, of you call cookies spam i prefer to stop here the conversation.

It does not encourage to insert pics: ext site tabou, own site refuse to upload. I wanted to share and possibly help and be helped and i end up with this sad conversation. Again I won't come back in this subj

Grumpy_Mike

Quote
Ok dear, of you call cookies spam i prefer to stop here the conversation.
Fine by me you are not being particularly respectful to the rules we have here about posting images. Maybe you can find a place where they don't mind you ignoring rules. Rules designed to make exchange of ideas flow smoothly.

Quote
own site refuse to upload
Then you are doing something wrong but simply can't be bothered to find out what. Images upload fine here, we do it hundreds of times a day.

Quote
Again I won't come back in this subj
Good bye.

guy_c

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
after some more thinking, why? 'normal' 1/4W resistors are rated 300V and pick of 220rms is 311...

But this lade me to envisage a diode bridge: the output transistor would saturate every smaller time intervals so that the exponential charge would last less and as a result the positive pulses would be smaller. Furthermore, as their frequency would double, the filtering capacitor can be smaller and as a consequence the detection of switch off condition can be faster. Critics please?

septillion

If you really don't want to do in software (which is pretty straight forward), yes, a full bridge will give you extra on time. But it also makes the hardware more complex as a trade off ;)
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

guy_c

If you really don't want to do in software (which is pretty straight forward), yes, a full bridge will give you extra on time. But it also makes the hardware more complex as a trade off ;)
Thank you Septillion,

First do we agree that the half rectification suggested above does not alleviate the need for tp series resistors
Second, regarding sw implementation, imagine a source generating noise in sync with the mains, e.g. a stuff injecting a pulse every zero cross, this may even be a dimmer. The software who's looking for a 50Hz signal will find it and will consider the switch as being closed. Now suppose you write your code, test it and find it rock robust. One day you (or you neighbor) buy a new appliance which, during night (e.g.) sometimes has it's synced noise exceeding a threshold. Should the switch in question command stores or a gateway (my case) they will open while I am away and this is really undesirable and should justify spending some more drachmas on the hw . The reason I said I won't go this way is that I have no mean to insure that my algorithm will remain valid in the future.



septillion

But that pulse will not last as long as "real mains" ;) So simply check that. Like suggested in reply #19 :)

Aka, check for x pulses in y time with minimum duration of z.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

guy_c

But that pulse will not last as long as "real mains" ;) So simply check that. Like suggested in reply #19 :)

Aka, check for x pulses in y time with minimum duration of z.
Heumm yes.
I must postpone the the discussion until Sunday. Meanwhile thanks for your involvement

guy_c

#28
May 26, 2019, 03:02 pm Last Edit: May 29, 2019, 11:53 am by guy_c
So we need to wake up in sync with the middle - to make sure its not too early nor too late - of the square pulse at the collector's and then wake up, say every millisec five times to make sure we have the 'real mains' and not synced parasit. The location of the mid of the pulse - assuming 50% duty cycle - can be found by sampling the wave say every milli to find five 'lows' followed by five 'highs' and adding 2.5ms to the previously found transition. Now we are well centered and if all - or most, say 3 out of 5 are high and that this occurs for 100 ms (5 mains cyclec) then we decide 'switch closed'. Conversely, if centered, by a 20ms translation of above center we find mostly 'lows' then we decide that the switch is open. While the switch is closed, We will need to redo the centering every now and then to compensate for the drift of cpu's clock relative to the 50 Hz and do the all thing for every switch. I said wake up because obviously, it must be done in interrupts. This may work but, then, why not going the orthodox way with a DSP and replace the unfortunate capacitor by an FIR low pass? Say with constant coefficients all ones and one bit ADC? Could be implemented as circular buffer where samples are inserted at the head pointer and extracted from the tail=head+1 (modulo) and FIR output = sum + *head -*tail is compared to a threshold [edit: with hysteresis] and the results is the switch state? 

septillion

I don't really get the complex approach and the need for the middle. But if you want to put the uC to sleep then you, external filtering might be preferred to wake it up less frequency. Although it might still benefit from software filtering as well. And you don't need the middle and stuff. Just keeping time between a rising and a falling edge and counting them.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Go Up