Zero crossing detector in PIC16F18856 - Page 1
Forum section: electronics / general

author: ocset
author_rank: Super Contributor
timestamp: July 27, 2017, 08:14:36 pm
content: Hello,Please can you confirm that the Zero Cross Detector inside the PIC16F18856 can only work with a mains input (as in the attached schematic and LTspice simulation) if there is also a resistor to ground from the ZCDxIN pin? (R5 in the attached schematic).If the resistor to ground is absent, then the current source internal to the PIC16F18856 will not operate, and therefore zero cross will not be detected….-[Both current source and current sink inside the micro must operate (alternately) in order for zero cross detection to happen].Section 21.2 of page 316 of the PIC16F18856 datasheet makes it clear that the zero cross detection needs both the internal current source, and also the internal current sink, inside the micro, to operate alternately. If one never operates then there will be no zero cross detection.As such, the resistor to ground (R5 in the attached schematic, is necessary for zero cross detection).Do you agree?The schematic in the PIC16F18856 datasheet shows a situation where the AC source and the microcontroller are both referenced to the same “ground”. But with the AC Mains setup of the attached schematic and simulation, this situation is not prevalent…and therefore the resistor to ground is needed (ie from ZCDxIN pin to ground)…do you agree?PIC16F18856 datasheethttp://ww1.microchip.com/downloads/en/DeviceDoc/40001824A.pdf

author: ocset
author_rank: Super Contributor
timestamp: July 28, 2017, 05:23:44 am
content: Basically the setting and clearing of the OUT bit in the micro has the same frequency as the zero cross points, but is phase shifted from it. How out of phase would this be given the schematic of the first post.? That is, the setting (and also the clearing for that matter) of the OUT bit will occur slightly after the zero cross, but how long after?

author: Richard Crowley
author_rank: Super Contributor
timestamp: July 28, 2017, 05:38:55 am
content: Quote from: treez on July 27, 2017, 08:14:36 pmR5 is the bottom half of a simple resistive voltage divider with R1+R2 as the upper half. Its main purpose is to keep the chip from expolding from the 100s of volts coming from the mains.

author: ocset
author_rank: Super Contributor
timestamp: July 28, 2017, 01:39:40 pm
content: Thanks, R5 indeed does that, but primarily it is to activate the current source inside the ZCD circuitry of the PIC16F18856 when the mains live is on its low-going half-cycle.If the current source doesnt get activated , then the OUT bit of the ZXDxCON register never gets cleared, and thus zero cross would never be detected.The problem is, the datasheet doesnt say what current has to flow from the current source, or to the current sink, in order for current flow to be acknowledged by the micro.That is, the PIC18F18856 datasheet doesnt tell what is the detection threshold of current. Do you know?Even microchips app note on this, fails to say what is the current detection threshold...http://ww1.microchip.com/downloads/en/AppNotes/90003138A.pdf

author: NorthGuy
author_rank: Super Contributor
timestamp: July 28, 2017, 03:08:15 pm
content: When the mains wire you're sensing goes negative, its voltage relative to the PIC ground is very small (equal to the diode drop on D5). This produces (0.75V diode drop + 0.75V sense level)/440K = 3.4 uA. This might not be big enough for zero crossing detection. The R5 resistor pulls it up to the ground and actually makes things worse.

author: ocset
author_rank: Super Contributor
timestamp: July 30, 2017, 09:53:27 am
content: Thanks, but sorry, the R5 resistor is whats needed to make the current source (inside the micro) work when the mains live goes on its low going half cycle.The problem is that theres no way of knowing when the OUT but will set or clear, and so therefore we have no way of knowing how out of phase the setting/clearing of the OUT bit is with the actual zero crossings.Microchip just dont clarify this. There should be a current threshold.(The OUT bit is in the ZCDxCON register)

author: Ian.M
author_rank: Super Contributor
timestamp: July 30, 2017, 10:50:03 am
content: Your problem is because Microchip expected the ZCD PIC's Vss to be tied to one of the mains supply wires, making the ZCD current symmetrical with respect to Vss. Running the PIC from a PSU that uses a bridge rectifier direct from mains results in a large DC offset of the ZCD currentand half wave rectification.If you remove R5 and put a capacitor between R1 and the mains, it will remove the DC offset, allowing the ZCD to detect the true zero crossing far more accurately. The capacitive reactance needs to be a couple of orders of magnitude less than the resistance R1+R2 so the voltage drop across the capacitor is negligible. 1uF 400V non-electrolytic should do nicely.Edit: see below!

author: ocset
author_rank: Super Contributor
timestamp: July 30, 2017, 02:05:41 pm
content: QuoteThanks, but sorry, i fear this doesnt work as the attached ltspice simulation of it shows.

author: ocset
author_rank: Super Contributor
timestamp: July 30, 2017, 02:07:37 pm
content: We are trying to do Zero Cross detection with the PIC16F18856. The schematic and LTspice sim of what we are doing is as attached.OUT bit:Section 21.2, page 316 of the PIC16F18856 datasheet clearly states that zero cross detection is gotten from the setting and clearing of the OUT bit. Section 21.2 states that the OUT bit is set when the current sink is active, and cleared when the current source is active.Square wave:If the waveform being checked for zero cross is a square wave, then the zero cross circuitry is obviously going to work. If a square wave, then the OUT bit is obviously going to set/clear right at the zero crossing. However, if its a sine wave, then we don’t know where on the rising sine wave the current is going to be great enough for the source or sink to be regarded as “active”. As such, we cant work out where exactly will be the zero crossing.Detecting zero current:We could pretend to ourselves that the micro will faithfully detect the exact zero of current. However, if it was capable of doing this, then why does it need the peak of the current to be 300uA? Also, concerning section 21.6, page 318 of the PIC16F18856 datasheet, why, if it is capable of detecting zero current, does it need the peak current to be at least 100uA high?Does this mean that for the detection of current in the current sink or source, at least 100uA of current flow is needed? –If not, then why is there this 100uA minimum figure?If the micro can detect the zero of current, then being sensitive to that low level of current would mean that it could easily be messed up by noise. Microchip’s TB3138 document recommends a capacitor at the ZCDxIN pin to mitigate noise, but the datasheet makes no mention of this.Sine waves:This is all relevant for sine waves, because if the detection of current is at some point above zero amps, then how far above zero amps is the detection threshold?.....because with a sine wave, if the current detection point is relatively high, then the OUT bit setting/clearing may well be a significant time interval away from the zero crossing…..and so we will not be accurately pinpointing the zero crossing. All this confusion never arises with bipolar square wave zero crossing…because a square wave immediately rises to its peak at the zero crossing. It is my impression that the ZCD circuit in the PIC16F18856 was designed for bipolar square waves. The datasheet fails to elucidate on the required parameters needed to properly use this ZCD circuit with sine waves. We actually have a microcontroller with nothing more than an “RSERIES” resistor connecting to the ZCDxIN pin and use it with sine waves as in the attached schematic. (ltspice simulation also included). The circuit appears to be working. However, it may be just that we’ve been lucky. –Maybe we have been narrowly operating at the edge of a “tolerance cliff” and are just lucky to not have gone over it……maybe with a different batch of microcontrollers we would not be so lucky, as maybe they would not be able to so closely detect the zero current point. As you can see, our current circuit is interfaced to the mains which is at the other side of a bridge rectifier, so for us, the current source barely operates at all….(it only sources some 2uA). According to section 21.2, page 316 of the PIC16F18556 datasheet, the OUT bit needs the current source to operate in order to be able to get cleared. For our situation, the ZCD circuitry would need to be able to detect 2uA of current flow in order to be able to clear the OUT bit. –Maybe the batchs of microcontrollers that we have so far used have been able to do this, but it is obviously going to be suspected that other batches will not be able to do this….and so we are running the risk that the ZCD circuit will not work.Contradiction 1:Another problem is that Microchip’s “Technical Brief” document (TB3138) contradicts the ZCD section of the PIC16F18856 datasheet. TB3138, on page 1, states that the peak current that can be sourced/sinked is 300uA. Wheras the PIC16F18556 datasheet states that this figure is 600uA. Which is it? We need to know because our mains varies from 210VAC to 265VAC, and we need to be sure that at high mains we don’t go above the maximum allowable current.Contradiction 2:Also, there is another contradiction…… the datasheet specifically states that it is current flow from the source that clears the OUT bit, and current flow into the sink that sets the OUT bit.However, TB3138, on page 6, specifically states that the OUT bit changes state when the current into the ZCDxIN pin is zero….Current into the ZXDxIN pin can become zero if only one of the current source or current sink operates. This is a contradiction. According to the TB3138, only the current source or the sink need to be monitored..not both. –This is not in accordance with the datasheet.Sine wave reference point:All of the discussion in the datasheet and the TB3138 document concerns detection of zero cross in sine waves that are referenced to the same ground as the microcontroller. In such a case, the current source and sink would obviously alternately operate….and provide similar waveforms in the current source/sink. This is not our application case, because our microcontroller is not referenced to the same ground as the mains, and the mains is at the other side of the diode bridge. The datasheet/TB3138 document makes no mention of our application case.I don’t think we can be sure of reliable operation of the ZCD circuitry in the micro, and therefore believe we should revert to using a anti-parallel diode optocoupler circuit instead.The attached simulation shows our application case in the LTspice simulator.TB3138 app note on ZCDhttp://ww1.microchip.com/downloads/en/AppNotes/90003138A.pdfPIC16F18856 datasheet (page 314- discusses ZCD)http://ww1.microchip.com/downloads/en/DeviceDoc/40001824B.pdf

author: Ian.M
author_rank: Super Contributor
timestamp: July 30, 2017, 02:30:38 pm
content: Sorry about that. The voltage on one side of the AC in, with DC blocked by a capacitor looks good unloaded but collapses on negative half cyclles as soon as you load it even with 1Meg.I didn't check it loaded . . .With a realistic load on the bridge and with the ZCD input current flowing, I don't see any way of fixing the half wave rectification of the ZCD input signal and resulting DC offset.I think the best option is to totally ignore the built-in ZCD module and use this circuit:http://www.dextrel.net/diyzerocrosser.htmOn the subject of discrepencies between the Microchip application note and a specific PIC datasheet - the application note is generic for a range of PICs with similar ZCD modules, and its electrical specs are based on the earliest PICS Microchip released with a ZCD module. The 5 digit PIC16F1xxxx range is considerably newer than the 4 digit PIC16F1xxx range referred to in TB3138. Its not surprising that the ZCD silicon has been changed. Any limits or recommended operating points in TB3138 are superseded by the specific values given for the specific PIC's ZCD module in the electrical specifications section of its datasheet.

author: ocset
author_rank: Super Contributor
timestamp: July 31, 2017, 04:50:33 am
content: Thanks for the ZCD circuit.Its unusual about microchip because usually, in fact almost always, they are far and away the best at explaining technical stuff

author: Richard Crowley
author_rank: Super Contributor
timestamp: July 31, 2017, 05:32:34 am
content: Then there is this Atmel App Note: "AVR182: Zero Cross Detector"http://www.atmel.com/Images/Atmel-2508-Zero-Cross-Detector_ApplicationNote_AVR182.pdf

author: Ian.M
author_rank: Super Contributor
timestamp: July 31, 2017, 06:56:34 am
content: Quote from: treez on July 31, 2017, 04:50:33 amYes, generally their datasheets and application notes are pretty good(apart from a tendency to copy/paste errors: when they release a new datasheet its not unusual to find whole paragraphs that refer to features of a previous device that haven't been checked for applicability to the new one)., but sometimes they screw the pooch and release misleading or even dangerous low grade crap as an application note, (e.g the infamous TB008), that leads one to suspect that peer review by professional engineers is not part of their dicument release process.I wasn't too impressed by their TB3099 Extending Relay Life by Switching at Zero Cross. See discussion here:http://www.microchip.com/forums/FindPost/828889

author: ocset
author_rank: Super Contributor
timestamp: August 05, 2017, 09:28:25 am
content: Fig 21.2 on page 315 of the PIC16F18856 datasheet shows optional externally attached pull-up and pull-down resistors.Please can you confirm that in our setup, with AC mains, the pullup resistor (pulling up to Vdd) would never be needed?This is because the pullup resistor would simply bring the current source into action, and that would be pointless, because the high-going half cycle of the AC mains will already have brought the current source into action (the “current source” being the current source inside the PIC16f18856).Schematic and LTspice simulation of our setup is as attached.PIC16F18856 datasheet:http://ww1.microchip.com/downloads/en/DeviceDoc/40001824B.pdfPS none of these posts are anti-microchip, i personally wouldnt look at any other micro maker, as microchip appears to be the best certainly for people like me who only rarely do software/micro stuff.