GF159 William Grill: 110V AC PWM Final 01-25-10 1 PWM Line-Voltage Interface Brightens Your Outlook William Grill, Riverhead Systems Several of William Grill's designs have included pulse-width modulator (PWM) circuits that control LEDs. But what about the control of line-powered lamps and fixtures? You can find several commercial PWM controllers, but build one yourself, save money and learn how to implement a microcontroller-based design. This circuit is no flash in the pan. This circuit accepts a PWM signal from an external source, processes the signal, and uses the result to turn on an optically coupled triac (triode for ac) in sync with 60-Hz 120V ac line power. The circuit provides both an educational and an affordable project for those Blue Light Special gadget freaks and model-railroad enthusiasts who want to add a bit more control in illumination projects. CAUTION The circuit derives its power directly from a 120V AC line connection without a step-down power supply or any electrical isolation, so exposed components are electrically "hot" and could pose a shock hazard. Neither William nor Design News accepts any responsibility for your safety or the safety of others who build or use this circuit. We cannot be responsible for how you build, GF159 William Grill: 110V AC PWM Final 01-25-10 2 fabricate, use, or package this circuit to get it into a form you can use. Work on a nonconductive surface. Keep the unpackaged circuit away from children and others. During testing, connect test instruments or probes to the UNPOWERED circuit and then with one hand in your pocket, plug in, or turn on, the circuit and read your measurements. Package the final circuit in a non-conductive package. If you must use a metal enclosure, ensure that you have carefully grounded the metal case to the ground terminal (green wire) on your line-power plug. Circuit Description This circuit uses an inexpensive microcontroller (MCU) to read the a filtered PWM signal that comes via an isolated input. The MCU then uses that signal to synchronize the PWM input with the phase of the 110V AC line power. The synchronization ensures careful control of a load device. See the schematic diagram below. For a larger schematic diagram, go to: www.gfreak.com/GF159/GF159_Schematic.jpg. One optical coupler (U1) isolates the external PWM-Control inputs from the rest of the circuit. This PWM signal could come from another circuit, a microcontroller's PWM output, or a similar source. A second optical coupler (U2) includes an optically driven diac (diode for AC) that connects to the triac to control the line-powered load device. An inexpensive Microchip Technology 12F675 MCU (IC1), provides the data-processing functions. The lamp-related hardware uses a triac (TRIAC) and an optically coupled MOC3010 diac (U2) of the types commonly used in lamp-dimmer and AC motor-control circuits. The remainder of the circuit hardware isolates the PWM Control inputs and provides the circuit with power directly from a 110V AC outlet. (Jameco Electronics sells the MOC3010 as part number: 26278. It is a non-stock part at Allied Electronics.) PIC12F675 Pin # Pin Name Connection 1 Vcc To 5V from 78L05 2 GP5 Not Connected GF159 William Grill: 110V AC PWM Final 01-25-10 3 PIC12F675 Pin # Pin Name Connection 3 GP4 Jumper to Ground, connects to 5V through a 10Kohm resistor 4 GP3 Input from 120V AC through 1-Mohm resistor 5 GP2 Output to MOC3010 6 Vref Not Connected 7 AN0 Input from MCP6002 8 GND Ground The GP4 input at pin 3 on the MCU lets users "invert" the sense of the PWM input. If you leave the jumper in place so it grounds (logic 0) the GP4 input, a 0-percent PWM input at the PWM Control points results in no power to the load through the triac. A 100-percent PWM input causes the circuit to put 100-percent power through the load. Depending on your application, you might choose to reverse these conditions so a 0-percent PWM input at the PWM Control inputs causes the circuit to apply 100% power to the load and a 100% PWM input causes the load to receive 0-percent power. In this case, remove the jumper between the MCU's GP4 input (pin 3) and "ground" (the 120V AC white wire) so the MCU senses a logic 1 at this input, provided via the 10 Kohm resistor that connects to the +5V supply. When you apply power to this circuit, the MCU causes a short delay. Then the control program uses the MCU's internal clock and a counter to measure the period of the AC-line signal present on the MCU's GP3 input at pin 4. This signal comes directly from the "hot" side of the 110V AC signal through a 1-Mohm resistor. The 5.1-V zener diode (D1) prevents the GP3 input from going above 5.1 volts, which could damage the MCU. The PWM control signal reaches the circuit through a CNY17-1 optical coupler (U1). The output of this coupler passes through the MCP6002 operational-amplifier circuit and to the resistor and capacitor on the op-amp's output, which create a low-pass filter. Next, the MCU reads the filtered voltage at pin 7 (AN0) and converts it to a 10-bit digital value. The analog-to-digital converter (ADC) measures this input signal once per AC line-voltage cycle. You should provide a continuous PWM signal for the PWM Control input with a voltage that swings between 0 and 2 to 5 volts and with a current of at least 8 mA. Apply this signal to the PWM Control + input and ground the PWM Control - line. The CNY17-1 device can handle a maximum reverse voltage of 6V and a maximum forward current of 60 mA. For complete specifications and a data sheet for the Vishay Semiconductors CNY17 family, visit: <A HREF = "http://www.vishay.com/docs/81863/81863.pdf">www.vishay.com/docs/81863/81863.pdf</A>. The 60-Hz line frequency and the effect of the PWM period and of the op-amp-based filter, limit the controller’s output pulsed-phase response. I recommend you use a PWM frequency of 900 Hz or greater at the PWM Control input. PWM frequencies above 900 Hz reduce the filter-related ripple on the voltage applied to the ADC at the MCU's AN0 input. The MCU code uses the 10-bit PWM Control value and the measured AC-line period to define the corresponding delayed output control pulse. In the circuit shown in the schematic diagram, and with the MCU running the supplied program, a small PWM Control signal would cause the MCU GF159 William Grill: 110V AC PWM Final 01-25-10 4 to place pulses very near the end of each half cycle. (See the oscilloscope images below.) The maximum PWM Control signal would produce pulses at the very start of each half cycle. Thus, the MCU synchronizes its triac-control pulses with the AC-line cycle times. The MCU's GP2 output (pin 5) generates the pulses that control the MOC3010 diac (U2) that turns on the triac and delivers power to the load device. William coded these pulses for a width of about 50 μsec. A pulse causes the triac to turn on. A triac device turns off on its own when the voltage across it reaches, or transitions through, zero volts. The MCU translates the filtered PWM Control signal into a linear phase across each half cycle of the AC-line. Internal timing loops, used in the controller code, each have a resolution cycle time of 50 μsec. The code uses only eight bits of the digitized 10-bit PWM Control value, so the code resolves each 180-degree half cycle to within about 1.4 degrees. In the prototype, William found that minor differences in the code's loop-execution times contributed to some additional jitter, resulting in a total error of up to 2.8 degrees. This 2.8 degrees is the phase variation you might observe during each half cycle (180 degrees), given a constant PWM Control input. (The minor differences come from sources of errors in the translated phase position and include the multiple branches associated with the code execution. Depending on the dynamic variables processed in the code, the position of the output control pulse ‘product’ may appear to be offset and thus ‘jitter’ in time. The controller’s internal timing is synchronized to the 60Hz.) Power Supply Because this circuit obtains power directly from the 110V AC line, you must pay careful attention to component values and ratings. The 0.47-μF 250V capacitor (C1) acts like a "ballast" that supplies current to the circuit. The larger the capacitance, the more current available. The two 1N4003 diodes, shown near C1 in the schematic diagram, provide a positive AC voltage to the 22-μF 50V capacitor (C2) that helps filter the AC signal that goes into the 78L05 voltage regulator (U3). This regulator produces steady 5V DC power for the MCU and the devices that connect to it. (For 120V AC applications, you must use a 0.47-μF capacitor and diodes rated for AT LEAST 200 volts. Diode ratings include a peak-inverse-volts, or peak reverse-voltage, specification, and the two 1N4003 have a PRV or PIV of 200V.) For the capacitor at C2, do not use a 22 μF capacitor with less than a 50V working-voltage rating. A higher voltage rating will work, though. The 5.1 Kohm load resistor (R1) and the LED in series with it ensure the circuit draws enough current to load the 0.47 μF ballast capacitor and maintain a voltage less than 25V across the 22 μF capacitor (C2). The 0.47 μF value of the ballast capacitor was chosen so the 78L05 voltage regulator could provide about 6 to 8 mA for the circuit. This design could use a 0.27 μF ballast capacitor for a load of between 4 and 6 mA. Mismatching the capacitance value to the load might cause a voltage higher than the 50-V specified for the 22 μF capacitor (C2). In most circuits you want a power source that provides an isolated low voltage. You can substitute a plug-in power cube that supplies DC power to the input of the 78L05 voltage regulator. (The input voltage to the 78L05 regulator should be between about 8 and 20 V DC.) You still must connect the MCU to the 120V AC line with the 1-Mohm resistor and with the 5.1V zener diode (D1) so the MCU can detect the line-voltage timing. Remember, you have NO ELECTRICAL ISOLATION between the controller, other components and the AC line. In this project, the CNY17-1 optical isolator (U1) electrically isolates the external PWM Control input from the line voltage. Consider everything else electrically ‘hot,' and thus a potential shock hazard. One Design News Gadget Freak described an MCU-based model-railroad light set that uses PWM control. I used the model-railroad circuit to demonstrate this PWM phase controller. I have GF159 William Grill: 110V AC PWM Final 01-25-10 5 taken the beacon output from that circuit and uses its modulated PWM output, as shown in the short video for this Gadget freak design. Waveforms from the Circuit In the scope photos below, the upper trace shows the voltage across the triac, so the flat "plateaus," or 0-volt portions of the trace indicate the triac is on and conducting current. Thus, the voltage across it comes close to zero volts. The bottom trace represents either the phase-control signal from the MCU or the PWM Control signal from the user's external PWM device. Each photo identifies the lower-traces source. The video for this Gadget Freak case includes dynamic scope images. Scope screen 1. With 0-percent PWM, shown in the lower trace, the triac has only a minimal on time--she short 0-volt plateaus. GF159 William Grill: 110V AC PWM Final 01-25-10 6 Scope screen 2. This image shows the voltage across the triac (upper trace) and the MCUgenerated control pulse (lower trace) delivered to the diac switch (U2 in the schematic diagram). The upper trace represents about a 20-percent pulse-width modulation at the circuit's PWM Control input. GF159 William Grill: 110V AC PWM Final 01-25-10 7 Scope screen 3. This image shows the same result across the triac for ~20-percent PWM. Note the triac turns on in each half cycle. The lower trace shows the PWM input signal measured at the PWM Control input to the circuit. GF159 William Grill: 110V AC PWM Final 01-25-10 8 Scope screen 4. At a PWM Control of ~70 percent (lower trace), the triac stays on (upper trace) for a longer time. GF159 William Grill: 110V AC PWM Final 01-25-10 9 Scope screen 5. A change of the PWM Control input signal to ~50 percent (lower trace) turns on the triac (upper trace) for approximately half of each AC line cycle. The information used to time the placement of the triac-on pulse in each half cycle of the 60-Hz line power depends on the filtered voltage applied to the MCU's AN0 input the reference voltage set for the MCU's ADC. In this circuit, William used a programmable option to select the MCU's supply voltage (the 5V output from the 78L05 regulator) as the ADC's reference. For a complete data sheet for the PIC12F675 MCU, visit: <A HREF = "http://ww1.microchip.com/downloads/en/DeviceDoc/41190F.pdf">ww1.microchip.com/download s/en/DeviceDoc/41190F.pdf</A>. You can use this PWM circuit with a fader application, found in EDN magazine's 12 November 2009 issue. Other applications include an illumination ring (EDN, 9 July 2009) and a programmable table-driven PWM sequence controller (EDN, 20 July 2006). Find EDN's archives at www.edn.com/archives. Build Instructions You can use a small piece of perf-board (holes on 0.1x0.1-inch centers) to mount the components and make the connections. This PWM circuit's triac can carry several amperes, so use appropriate gauge wire that matches the current drawn by your load (motor, lamp, etc.). The larger the wire the better. (Remember, smaller wire-gauge numbers indicate larger wire cross sections.) William did not include a fuse on the output, so you might include one when you package your implementation of the circuit. Place the fuse in the circuit between the 120VAC connection and your line-power plug. For questions about compatibility, code or circuit modifications, contact William Grill at: contact@riverheadsystems.com. Bill of Materials Amt. Description Allied Part No. 1 CNY17-1 Optical Isolator 431-0064 1 MOC3010 Optical Isolator See Text 3 47-ohm, 1/8W Resistor 296-4733 2 2.2 Kohm, 1/8W Resistor 296-4725 1 5.1 Kohm, 1/4W Resistor 296-6517 1 10-Kohm, 1/8W Resistor 296-4721 1 1-Mohm, 1/8W Resistor 296-6647 1 Green LED 405-0106 1 0.47 μF, 250V Capacitor 862-0602 1 4.7 μF, 50V Capacitor 852-7070 2 10 μF, 10V Capacitor 541-0585 1 22 μF, 50V Capacitor 852-7074 GF159 William Grill: 110V AC PWM Final 01-25-10 10 2 1N4003 Diode 411-0003 1 L6004L5 Triac 846-0051 1 12F675 PIC Microcontroller 383-0292 1 MCP6002 Op Amp 383-0227 1 1N5231 Zener Diode 431-0365 1 78L05 Voltage Regulator 288-0630 1 2-Pole Terminal Block 502-0421 2 2-Pole Terminal Block 502-0492 2 8-Pin DIP Socket 374-4541 1 6-Pin DIP Socket 374-4540 Programming the MCU William used a Microchip PICSTART Plus programmer but other PIC MCU programmers will do the job, too. The figure below shows the configuration screen provided by Microchip's free MPLAB integrated-development environment software that will work with the assembly-language code included below the figure. Details of the controller configuration settings: Code Listing ; FROM SCRATCH AND derived from PWDIM16Z_GREATEST.ASM 11/9/09 ; PW TO DIMMER CONTROLLER this is now a pwm to analog to dimmer controller ; makes the app a bit more flexible and eliminates 2nd controller ; ; F675 bit A/D ;********************************************************************** *** GF159 William Grill: 110V AC PWM Final 01-25-10 11 GPIO equ 5; 6 STATUS equ 3 PC equ 2 trisr equ 0x85 ; reserved equ 7 dwell equ 0x30 index1 equ 0x31 ;minimum equ 0x0A ; min # keyservice loops : sequence 2*23/60 min ;keyimage equ 0x32 ; masked key image ;savephase equ 0x33 ; max # keyservice loops : sequence 10*23/60 max temp1 equ 0x34 ; temp2 equ 0x35 ; cnt equ 0x36 datareg equ 0x37 ; ;keyloop equ 0x20 ; keyservice/debounce loop servie counter temp equ 0x21 bits1 equ 0x22 dwelltimr equ 0x23 ; maintains active count of dwell for each tabled value newcount equ 0x24 mresult equ 0x25 ; product of result sub service pointer equ 0x26 ; pointer of the tabled value cyclecnt equ 0x33 ; saved value of phasecount representing number of timing loops in poitive half cycle phasecount equ 0x28 ; working counter of timing loops in poitive half cycle bits equ 0x2A phase equ 0x2B ; working counter of number of loops to assert output newphase equ 0x2C ; saved value recovered from the tabled profile resindex equ 0x2D ; maintained indexed count of the dwell per the key inputs number equ 0x2E timer equ 0x2F ad0 equ 0x1F ad1 equ 0x9F addata equ 0x1E ; #define b0 STATUS,5 #define output GPIO,2 #define input GPIO,3 ; bits #define outflag bits,0 #define debounc bits,1 ; state flag for key service #define firstpass bits,2 #define pflag bits,3 #define shutdwn bits,4 #define up bits,5 #define down bits,6 ; #define sflag bits1,0 ;#define pretest bits1,1 ; #define carry STATUS,0 #define zero STATUS,2 GF159 William Grill: 110V AC PWM ; Final 01-25-10 12 *********** START ******************** goto s1 ; reset vector nop nop ; intpr vector nop nop s1: clrf GPIO movlw 0x11 ; 0001 0001 bsf b0 movwf ad1 movlw 0x3B ; 11 1011 movwf trisr bcf b0 movlw 1 ; enable movwf ad0 ; ******************************************************** topp: goto realstart keyserv: goto xkeyserv result: goto dresult delay10: nop nop nop nop nop nop retlw 0 ; ******************************************************** realstart: clrf bits clrf bits1 clrf temp movlw 0xFF movwf temp1 movwf temp2 clrf cyclecnt real3: decfsz temp1 ; wait ~2 sec 254*254*16 goto real3a decfsz temp2 goto real3a goto real3b real3a: decfsz temp goto real3a movlw .4 movwf temp goto real3 ; ******************************************************** real3b: ; recall the full scaled phasevalue would be interpreted as 0 degr bsf firstpass topb: GF159 William Grill: 110V AC PWM Final 01-25-10 13 btfsc input ; wait for - phase goto topb topq: btfss input ; wait for + phase goto topq ; ************************44 instructions *********************** topa: ; all loops are 50 instructions btfsc firstpass ; test state flag for special process complete goto topa41 ; 6 ; non calibration + phase movfw phasecount movwf phase ; phase is working counter to desired phase btfsc zero ; qual for maximmum value goto topa11 topa3: call delay10 call delay10 call delay10 call delay10 decfsz phase goto topa2 goto topa1 topa2: btfss input ; test/qual input hasn't transistioned goto topa5 nop nop goto topa3 ; topa11: bsf output call delay10 call delay10 call delay10 call delay10 call delay10 topa1: bsf output topa4: ; coast to negative transistion btfss input ; wait for - phase goto topa5 nop nop nop nop nop nop nop nop bcf output ; makes a 10 us pulse goto topa4 topa41: ; 5 nop nop nop nop call delay10 GF159 William Grill: 110V AC PWM Final 01-25-10 14 call delay10 call delay10 call delay10 incf cyclecnt bcf firstpass btfss input ; wait for - phase goto top10a1 nop goto topa41 ; ****************************************************** topa5: ; 3 in negative phase bcf output ; just in case movfw phasecount movwf phase btfsc zero ; test for maximuum goto topa10 ; normal - cycle btfsc phase,7 ; test if phase is >127 loops goto topa8 goto topa6 ; 10 loop to pulse topa7: nop nop nop nop nop nop nop nop nop topa6: ; 12 nop nop nop nop nop call delay10 call delay10 call delay10 nop decfsz phase goto topa7 topa10: bsf output call delay10 call delay10 call delay10 call delay10 call delay10 top10a1: call keyserv movfw newphase ; save value 'newphase' movwf phasecount ; set up for next cycle goto topq topa8: ; 9 call keyserv GF159 William Grill: 110V AC PWM Final 01-25-10 15 movfw newphase movwf phasecount ; assumes call plus routing equals 150 instructions decf phase decf phase decf phase ; 150 topa9: call delay10 call delay10 call delay10 call delay10 nop nop nop nop nop decfsz phase goto topa9 btfsc input goto topa bsf output nop nop nop nop nop nop nop nop nop bcf output goto topq ; ****36************ xkeyserv: ; THIS INTENDED TO PROVIDE SERVICES FOR BUTTON BASED DIMMER bsf ad0,1 ; start conversion nop f1: bcf output nop movwf temp call delay10 ; call delay10 ; ; movfw addata ; movwf temp ; rrf temp pre- buffer offset adjust ; rrf temp ; rrf temp,w ; andlw 0x1F ; addwf addata ; bcf carry rrf addata ; movlw 2 ; subwf addata GF159 William Grill: 110V AC PWM btfss incf carry addata movlw movwf btfsc decf 0x7F temp addata,7 addata Final 01-25-10 16 ; ; movfw addata btfsc GPIO,4 ; test/adjust for inversion at opticoupler 0%PWM->no power out subwf temp,w ; 7F-addata movwf temp ; **********************equalized 114 instructions************ dresult: clrf mresult movlw 7 movwf index1 movfw cyclecnt movwf number rlf temp a: bcf carry rrf number rlf temp btfsc carry goto ba nop nop nop nop c: decfsz index1 goto a call delay10 c2: movfw mresult movwf newphase retlw 0 ; product in mresult and newphase ba: movfw number addwf mresult goto c stop: end