Controlling 120 VAC lights with a Stamp · Dimming lights with a Stamp is relatively easy.· With the help of some additional circuitry, it is possible for the Stamp to perform some very fun feats of lighting control. The nice feature of this circuit is that it is easily expandable and provides very fine (225 useful steps) control over the light level. · Background · I was looking for some method of controlling the lighting in Halloween haunted house dioramas.· I found an article titled “All-digital circuit fires triac” that appeared in the July 21, 1994 issue of “Electronic Design News”, and can be seen at the link below. · http://www.edn.com/archives/1994/072194/graphs/15di3fg1.htm · I made some minor additions to the circuit and one change to the timing as shown in the original article.· This circuit works dependably and is great for setting the mood of an event. · Possible uses for this circuit · Animated holiday light displays Party lighting Halloween haunting, lightning effects Strobe effects · AC sine wave · In the USA, single-phase alternating current has a frequency of 60 cycles per second.· The voltage curve varies from zero up to +150 volts and back to zero volts and then to -150 volts and returns to zero volts.· The curves are sinusoidal, not circular.· The root-mean-square (RMS) of all points on either curve equals 120 volts.· However, for this circuit, being aware of the voltage at various points in the sine wave is important. · General Description · The light dimmer circuit consists of 3 functional blocks: timing, control, and 120 VAC switching.· The objectives of the dimmer circuit are: · 1.·················To be able to pick any point in the positive or negative AC half-wave to switch the current on, 2.·················To repeat turn-on at this point indefinitely until a new turn-on point is selected. 3.·················To electrically isolate the 120 VAC portion of the circuit from the rest of the circuit. · 120 VAC Devices and AC switching · Triac · The triac is the device that will switch the AC. The terminals on a triac are labeled Main Terminal 1 (MT1), Main Terminal 2 (MT2) and the Gate (G). · From a black box perspective, the triac has four important functional characteristics (assuming the device is operating within its maximum ratings): ·· 1.········ The triac will conduct in either direction of current flow. This means that current can flow from MT1 to MT2 or from MT2 to MT1. · 2.········ The triac will only begin conducting when triggered by a current applied to the Gate.· · 3.········ The triac automatically ceases conducting when (only if) the voltage across and current through MT1 and MT2 goes to zero, after which the triac must be re-triggered to begin conducting again. · 4.········ Once triggered, the triac will continue conducting until the next zero voltage crossing point in the AC sine wave regardless of the voltage/current applied to the gate. So, once triggered, the Gate input is in a “don’t care” state until after the next zero crossing point. · To summarize, the triac will only begin conducting when triggered and will continue conducting regardless of the state of the gate until the next zero voltage crossing point. The triac can be triggered in either the positive or negative part of the AC sine wave. · A helpful article titled “Thyristors and triacs - Ten golden rules for success in your application” can be found here: · http://www.web-ee.com/primers/files/AN_Golden_rules.pdf&e=7620 · Triac Trigger (MOC3010M) · The triac trigger is a device that provides 5 V inputs to an internal LED (through a suitable current-limiting resistor) on one side and a 120 VAC optically triggered triac on the other side.· This device electrically isolates the 120 VAC side of the circuit from the logic level side of the circuit. Using a triac to trigger the main triac solves the problem of triggering the main triac on the positive or negative voltage portion of the AC sine wave. · Zero Crossing detector (H11AA1) · This device provides notification of when the AC voltage is zero.· It consists of two cross-coupled LEDs on the AC side and a phototransistor on the logic level side.· Thus the one or the other LEDs is on during any non-zero portion of the AC sine wave. ·Both LEDs are off at the zero voltage point. By adding a 10K pull-up resistor to the photo transistor output on the logic level side of the device, the zero crossing detector will pull the output low during the non-zero portion of the sine wave and the 10 K resistor will pull the line up to 5 V when the photo transistor turns off.· The zero crossing detector will produce a positive going pulse every 8.333 milliseconds (1/120 =· .0083333).· This device electrically isolates the 120 VAC side of the circuit from the logic level side of the circuit. · Simple 1-channel circuit (Attachment 1) · This simple 1-channel circuit can be built using a Stamp for control and timing.· In this configuration, the Stamp has one pin connected to the trigger input of the triac trigger and one pin connected to the zero cross line. The Stamp must poll the zero crossing clock line and when the zero cross line goes high, initiate the change to the triac.· This circuit is not practical for dimming because retriggering the triac at a specific point in the sine wave will totally consume the Stamp and leave no time left for other computing.· The circuit will function adequately to turn the triac full-on or full-off.· The following sample code will blink a lamp at 1 second intervals: · Trigger············· PIN·········· 0 ZeroCross········PIN·········· 1 · Main: DO · GOSUB··········· TriacOn · PAUSE············ 1000 · GOSUB··········· TriacOff · PAUSE············ 1000 LOOP · ‘·········· [noparse][[/noparse]Subroutines] · TriacOn: DO WHILE (ZeroCross = 0) LOOP HIGH Trigger RETURN · TriacOff: DO WHILE (ZeroCross = 0) LOOP LOW Trigger RETURN · This circuit does not meet the requirement of being able to trigger the triac at any point in the AC half-cycle and repeatedly turn it on at this point, but does implement simple on-off control. · Circuit component costs: Triac-4Amps······················ $1.87 Heatsink·························· 1.00 MOC3010M triac trigger············ 0.86 H11AA1 zero cross detector········0.42 4 ¼ watt resistors················0.20 · Total·····························$4.35 · Fully functional 1-channel dimmer (Attachment 2) · This circuit offloads all timing and control from the Stamp to external circuitry.· By adding a 1MHz TTL crystal and a 14-bit ripple counter with reset, the timing issues are solved.· By adding a comparator that compares two 8-bit terms labeled P and Q and provides a low output when P>Q, the control problem is solved. · The 1 MHz crystal output is fed directly into the ripple counter.· The ripple counter is free-running. The first 5 stages (bits 0 through 4) of the ripple counter are not used.· The next 8 output stages (bits 6 through 13) of the counter are the term P fed into the comparator. The first 5 stages of the ripple counter serve to divide the crystal clock rate by 32.· This results in a clock rate of 31,250 Hz fed into the next 8-bit counter stage that makes up the P term. Since the ripple counter’s reset pin is connected to the zero cross line, the counter is reset every 120th of a second. There are 260 clock pulses (31,250 / 120 = 260) going into the 8-bit counter stages that make up the P term. Thus the 8-bit P term counts from 0 to 255, rolls over to zero and continues counting ·to 4, when it is reset to zero by the zero cross line.· This repeats for each AC half wave.· The counter roll over slop does not affect the performance of the circuit, since the triac is likely already triggered by this point in the cycle, and any changes on the triac trigger are ignored until the triac resets itself at the next zero crossing point. · The 8-bit output of the counter is connected to the P inputs of the comparator.· Pins 0 through 7 of the Stamp are connected to the Q inputs of the comparator.· When the count value on the P inputs is greater than the trigger value on the Q inputs, the triac trigger line is driven low, which fires the triac. · So by manipulating the trigger value with the Stamp, you can control the trigger point in the AC half-wave, and this circuit will continually retrigger the triac at this point until the trigger value is changed. · In order for the Stamp to know when to change the Q value, it must poll the zero cross line and only change the Q inputs when the zero cross line is high.· Otherwise, changes in Q inputs can occur in mid AC cycle, resulting in lamp flicker. · Notice that the trigger values are reversed from what one might intuitively expect.· For example, a trigger value of zero is full-on, while a trigger value of 255 is full-off. This is because the triggering condition occurs when the counter value exceeds the trigger value. So if the trigger value is zero, when the counter value reaches 1, the counter value is greater than the trigger value and the triac is triggered.· Conversely, if the trigger value is 255, the count never is greater than 255 and the triac is not triggered. · The following subroutines can be used to implement this dimmer circuit. · TrigValue········ VAR··· Byte···························· ‘Trigger value ZeroCross······· PIN···· 8································· ‘Pin attached to zero cross line · Main: ‘·········· This loop will ramp the light on in 225 steps - notice that the count proceeds from 225 to 0 FOR TrigValue = 225 to 0 · GOSUB SendTriac · PAUSE 100 NEXT END · ‘·········· [noparse][[/noparse]Subroutines] · SendTriac: DO WHILE (ZeroCross = 0) LOOP · OUTL = TrigValue RETURN · The terminal count of 225 is simply the number that represents the last value for which I could observe filament illumination.· So, even though values greater than 225 do indeed turn the triac on during the down-slope of the sine wave, the voltage at that point is insufficient to illuminate a lamp filament.· Thus I reduced the maximum trigger value to 225.· At 225 no visible light is produced, but at any value less than 225 the filament will glow. · Circuit component costs: Triac-4Amps····························$1.87 Heatsink·································1.00 MOC3010M triac trigger···············0.86 H11AA1 zero cross detector···········0.42 4 ¼ watt resistors···················0.20 1Mhz TTL crystal·····················1.70 CD4020N 14-bit ripple counter········ 0.60 74LS682 8-bit magnitude comparator··· 1.10 · Total·······························$7.75 · This circuit works fine but uses a lot of precious stamp pins.· The next circuit adds two additional devices to reduce the number of Stamp pins needed. · Fully functional 1-channel dimmer that uses only three Stamp pins (Attachment 3) · This circuit adds a serial to parallel shift register and an 8-bit latch.· This relieves the Stamp of presenting the 8-bit trigger value on eight of its own pins and offloads the zero cross line polling.· The stamp can send the 8-bit trigger value serially.· The serial to parallel shift register will present the 8-bit trigger value to the 8-bit parallel latch.· The latch is used to control the timing of when the trigger value is presented to the comparator, which must by synchronous with the zero cross line to avoid flicker. · · SerialClock················· PIN····· 0····················· ‘Serial transmission clock line SerialData··················· PIN····· 1····················· ‘Serial data line TriacLatchEnable········· PIN····· 2····················· ‘8-bit latch enable pin · TriacTriggerValue········ VAR··· BYTE············· ‘byte for holding trigger value · Main: ‘·········· Ramp the light on LOW·· TriacLatchEnable································· ‘initialize state of latch enable pin · FOR TriacTriggerValue = 225 to 0 · GOSUB SendTriac NEXT END · ‘·········· [noparse][[/noparse]Subroutines] · SendTriac: SHIFTOUT· SerialClock, SerialData, MSBFIRST, TriacTriggerValue· ‘Send data to shift register PULSOUT· TriacLatchEnable, 5000················································ ‘hold pulse for 10 mSecs RETURN · This circuit is simple to program and uses only three Stamp pins.· It is a fully synchronous circuit and will provide flicker free operation. · Circuit component costs: Triac-4Amps···························· $1.87 Heatsink······························· 1.00 MOC3010M triac trigger···············0.86 H11AA1 zero cross detector··········· 0.42 4 ¼ watt resistors··················· 0.20 1Mhz TTL crystal····················· 1.70 CD4020N 14-bit ripple counter········ 0.60 74LS682 8-bit magnitude comparator··· 1.10 74LS164 Serial-to-parallel ·shift register······················ 0.45 74LS377 Octal D register············· 0.70 · Total······························· $9.10 · Eight-channel light dimmer (Attachment 4) · By adding a 3-line to 8-line decoder and three more Stamp pins for addressing, the circuit can be expanded to an eight channel dimmer. The serial-to-parallel shift register is connected to all 8-bit latches.· The three address bits are used to select the specific 8-bit latch to store the trigger value. The following code will drive this circuit. · TriacAddr··················· VAR··· Nib····························· ‘Triac latch address (0 through 7) TriacAddr0················· VAR··· TriacAddr.Bit0 TriacAddr1················· VAR··· TriacAddr.Bit1 TriacAddr2················· VAR··· TriacAddr.Bit2 · TriacPin0····················· PIN····· 0································· ‘Triac addr pin 0 TriacPin1····················· PIN····· 1 TriacPin2····················· PIN····· 2 · TriacLatchEnable········· PIN······3····················· ‘8-bit latch enable pin · TriacTriggerValue········ VAR··· BYTE············· ‘byte for holding trigger value · Main: ‘·········· Ramp all lights on HIGH· TriacLatchEnable FOR TriacTriggerValue = 225 to 0 ··FOR TriacAddr = 0 to 7 ······GOSUB SendTriac ··NEXT NEXT END·· · ‘·········· [noparse][[/noparse]Subroutines] · SendTriac: ‘ ········· Send data to shift register SHIFTOUT· SerialClock, SerialData, MSBFIRST, TriacTriggerValue· ‘Send data to shift register · ‘·········· Set up address of latch TriacPin0 = TriacAddr0 TriacPin1 = TriacAddr1 TriacPin2 = TriacAddr2 · ‘·········· Latch the data PULSOUT· TriacLatchEnable, 5000············· ‘hold pulse for 10 mSecs RETURN · Circuit component costs: Triac-4Amps······························$1.87 Heatsink································1.00 MOC3010M triac trigger···············0.86 H11AA1 zero cross detector···········0.42 4 ¼ watt resistors··················· 0.20 1Mhz TTL crystal····················· 1.70 CD4020N 14-bit ripple counter········ 0.60 74LS682 8-bit magnitude comparator··· 1.10 74LS164 Serial-to-parallel ·shift register······················0.45 74LS377 8-bit latch ·················0.70 74LS155 Dual 1 line to four line ·Data distributor····················0.35 Total·······························$9.25 · This final version of the circuit is the best, because of the number of channels supported.· Each additional channel requires addition of: · Triac with heat sink················$2.87 MOC3030M triac trigger,··············0.86 2 ¼ watt resistors·················· 0.10 74LS377 8-bit latch················· 0.70 74LS682 8-bit magnitude comparator··· 1.10 · Incremental cost of each channel·····$5.63 · I used Teccor triacs.· These are available with isolated tabs in a TO-220 style case. This keeps the AC off of the Heatsink.· Current ratings range from 4 to 15 Amps.· All can be triggered with the MOC3010M triac trigger.· Review the manufacturer’s data sheet regarding heat sink requirements. · Attachment 5 is a collection of Parallax USB Oscilloscope images of the zero cross detector output· (blue) and the triac trigger line (red).· The images show the following cases: Full off, ¼ on, ¾ on and full on. Each image shows the activity in one-half of the AC sine wave, the period between zero crossing events. · Final notes · This circuit can ONLY be used for dimming lights.· Inductive loads cannot be used.· This includes electric motors and fluorescent lamp ballasts. The reason is that the reverse EMF produced by inductive loads produces an out-of-phase wave back to the triac so that the triac never sees zero volts.· Thus the triac never ceases conducting, resulting in total loss of control.· There are snubber circuits that can be added to filter this reverse EMF and restore order to the triac. · As with all circuits that use 120 volt AC mains, the circuit should properly fused for the loads to be used.· Due care must be taken when working AC Mains voltage to protect from shock and injury.· Never work on the circuit with Mains voltage applied to the circuit.· Do not bring Mains voltage onto a breadboard.· It is too easy to forget where on the breadboard these voltages are.· · You can put the AC side of the circuit on a perfboard or prototype board and bring the logic level signals to/from a breadboard.· Just remember to use adequately sized wire in the AC side for the anticipated current. Corrections: Changed Attachment 4 to correct address and data inputs on device 74LS155 (had them reversed) and changed polarity on Latch_Enable line from LOW at rest to HIGH at rest.· This is an active LOW signal.·PULSOUT will invert the output state of the pin for the specified time and then restore to its original state.