The .gif attached is a circuit from Electronics Today International (UK) of March 1996, part of a 9 page article called "Driving Triacs With The PIC Microcontroller" by Bart Trepak. Much of the text is relevant to the ongoing thread of lighting control. If the halogens talked about this week can run OK with AC this circuit could be easily adapted to work with a lower AC voltage. The circuit is for a 240VAC line voltage. Note that Triac terminal MT1 is connected to Phase/Live and MT2 to Neutral. Two push buttons are in the circuit as up/down dimming controls. Suggestions and points (and there were a lot of them) raised in the article include ; Using 0 and -5V as the PIC supply because the triac triggers better with a negative voltage pulse The internal static-limiting diodes of the PIC CMOS inputs are used to clip the input mains voltage. Two 2M2 are better used than a 4M7 because of the voltage rating of 1/4W resistors. The diode in series with the gate resistor is to prevent the AC that comes out of the gate, when the triac is conducting, getting back into the PIC. Although the author says it's already limited by the gate resistor (100R) it's better to be cautious and block it altogether. RTCC input requires an external diode to V+ (in this circuit 0V line) To use RTCC as a zero-crossing detector, set it to FFh. The next positive transition on RTCC will cause it to rollover to 00h. Check it with mov RTCC,1 and test Z flag. (My take on this is that RTCC can't be used to detect both positive- and negative-going crossings) Detect both the rising and falling approach to the zero voltage point. This way, integral numbers of both positive and negative half cycles can be used. For example, loads with high inertia such as heating elements would be better suited being supplied with complete half cycles. With motors and lights this would cause jerking or flickering and they are best supplied with a part of every cycle. Put a choke in series with the triac to minimise EMI and suppressor cap(s) across the load. If you assume the threshold for the CMOS inputs is -2.5V (with this PSU 0V is high and -5V is low) then the positive- and negative- going zero crossing points are slightly assymetrical, which should be accounted for if very fine control is needed. To detect the zero crossings - When reading the input pin a change from a stream of 1's to 0 is a negative-going zero crossing ie the mains is heading into its negative half cycle. A change from a stream of 0's to 1 is a positive-going crossing. The 470n (250V) cap is there for continuous gate current with a large triac (50mA gate) in integral cycle applications. If a stream of micro- pulses is used instead (the author suggests 100 every half cycle) the current going out of the PIC is reduced and the cap can be cut back to 100n. To reduce PIC power further use sensitive gate triacs. A transistor can be used to boost the PIC pulse current. From PIC o/p pin an 8k2 to npn base, emitter to -5V, collector to gate via 390R. Trigger early in the cycle when using whole half cycles to minimise current rush and electrical noise. However with inductive loads, the current and voltage through the triac may not be enough to cause it to latch properly and the trigger pulse should be lengthened or a series of micropulses sent throughout the cycle. For dimming, divide a mains half cycle into a finite number of equal time intervals, say 50 or whatever is convenient for the frequency you have the PIC running at. The triac will continue to conduct after being triggered so the power to the light will come from the moment the triac is triggered until the next zero voltage point. For 100% power send a gate pulse out as soon as a rising or falling voltage is detected after a zero point. For other brightnesses, start counting the time intervals after the zero point and then send a gate pulse. As this means the power will be applied at a non-zero voltage, steps will have to be taken to cut noise, such as chokes or suppressor caps. This may be difficult with high wattage applications. I hope this will help anyone with mains interfacing, I know I learned a thing or two. Mains is not my favourite stuff to play around with and the article has given me a bit more confidence than I had before. Jinx Attachment converted: wonderland:picdimmer.gif (GIFf/JVWR) (00011413)