> >You really need to explain the question in a little more detail to be sure > >that the answer suits your applications. > > Sorry, I didn't realise there could be so many answers to this question :) It's about to get worse :-) > >Assuming you are using the pin as an input which is pulled high by a > >mechanical single pole switch connected to Vdd and which is pulled low by > >the pull down then: > > This is almost how it works... Actually I have a zener from an external > power supply but that probably doesn't matter. > It looks something like this: > > ~12V in-|=2k=|--|----|-----|------PIC pin > | | | > Z C R > | | | > |----|-----|------ GND > > Z= 5,1V > C= 100n > R= 100k OK. I assume there is a switch there somewhere or that the AC turns off and on. The 2K is far lower than it needs to be if you use a 100K pullup. Not a problem - just doesn't need to be that low. The circuit is sensible in intent BUT has a subtle "problem" which may cause you "trouble". On +ve half cycle the zener will clamp to about 5v1 which is OK but on negative half cycles it will clamp to over - 0.7v. This means that the PIC's internal protection diodes, which conduct at around 0.6v, will almost certainly pass some current. This may cause incorrect processor operation - see below. BUT NB: Zeners typically have a slightly higher forward voltage drop * than ordinary silicon diodes OR PIC protection diodes so the PIC protection diode will get "first bite" at the current through the 2k. - and with the 2k shown and a say 10k pull down it COULD be quite significant - up to perhaps (12 * 1.414 - 0.6 )/2k = about 8 mA. ((In practice it will probably be far less but it may not be)). In actual tests using a forward biased zener and a PIC body diode in parallel I found that typically over 80% of the current is conducted by the PIC body diode. * eg a BZX79 500 mW zener has a guaranteed maximum Vforward of 0.9v at 10 mA. This is well above the forward voltage expected from a small signal silicon diode). The subject of using PIC protection diodes has been often discussed on this list and it always turns into a religious war. I will therefore simply state the facts here. If you wish to believe others who will subsequently tell you that these are NOT facts, by all means do so. But please blame them and not be when problems subsequently occur :-) - All PIC datasheeets make it clear that PIC protection diodes are NEVER intended to conduct when the PIC is operating normally. - Datasheet specs for current passing through protection diodes ALWAYS relates to worst case conditions where processor survival is guaranteed but actual processor operation is not guaranteed. - Problems may and sometimes do occur if you allow protection diodes to conduct during normal operation. Actual experience varies widely. (The body diode current flows into the die at points where its affect has not been accounted for by the designer). - The fact that at least one Microchip Application note has a circuit which allows body diodes to conduct during normal operation does not in any way alter any of the above facts :-) Caveat Emptor! (or diode equivalent). Having made all that fuss, there is an easy way out. Place a diode (eg 1N4148) in series with the 2K resistor (cathode towards PIC). Now only positive voltages will appear on the input pin, The pull down will clamp it to zero on negative half cycles. If you do add the diode do note that the discharge time constant after the switch opens will now be controlled by the pulldown plus C. Here t = RC = 100 NF x 100k = 0.01 second so this will work OK with eg 60 Hz mains. But don't forget about this factor. Let war begin :-) Russell McMahon -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body