Using Pic Microcontrollers To Control Triacs 2053
Using Pic Microcontrollers To Control Triacs 2053
May 1, 2003
Abstract
This application note describes how to control triacs with Microchip PIC Microcontrollers.
Special emphasis is placed on phase control of triacs. Phase Control is a cycle by cycle control
capable of delayed turn-on in each half cycle, which offers fast response and fine resolution.
Comparisons are made with other forms of triac control, such as burst mode and on/off, and
with variable switching power supplies.
A technical description of triacs is offered and two sample phase control applications. One
uses opto isolation, the other direct drive. The isolated sample application has debugged circuitry
and code. The PIC used is a 16C63. The code, written in C, is transportable to any of the
Microchip PIC16Cxx, 16F8xx, or 18Cxxx parts.
The non-isolated example also has debugged circuitry and code. It uses a 12C508. The code is
short and is in assembly language.
In depth descriptions of the calculations necessary for phase triac control is included, with a
step by step description of how to calculate and use triac firing angles and triac firing times.
Problems are addressed. These include controlling inductive loads, triac overheating, and
power line symmetry. Some myths about triac control are addressed.
Finally a list of related articles is included for those who wish to learn more about using
triacs or wish to use triacs with lower end PIC microcontrollers.
Definition of terms
Before getting started we need to get clear on some terms that are commonly used to
describe the electrical behavior of triacs.
Zero Cross - the two places in an AC wave form where the voltage goes to zero. When Zero cross
is stated alone, voltage zero cross is assumed. A current zero cross also exists. In resistive loads it
is coincident with voltage zero cross. In inductive loads it will not be.
Half Cycling - an error condition. This is a condition where the triac will act as a rectifying diode,
allowing the full wave of either the negative or positive side of the wave form to pass to the load,
but suppress the other side of the wave form. In this situation the triac has ceased to control the
energy to the load.
Commutate - The condition where the current through the triac has gone to zero. This normally
occurs at some time after zero cross. How much after zero cross this occurs depends on how
inductive the load is.
Delay Time - is the time between zero cross and gating the triac.
Delay Angle - is the same as delay time but it is expressed in degrees making this expression line
frequency independent. Zero degrees means no delay, 180 degrees is the maximum delay and
means the triac is not being gated at all.
Gating Time - is the time the triac gate is being actively driven. Gating is to start current flow
through the triac.
Dead Time - is the time just before zero cross that the triac should not be gated in a phase control
system. Gating in Dead Time runs the risk of the triac not commutating when zero cross does
happen. The triac could then pass current for the next half cycle, half cycling.
On/Off Control
On/Off is using the triac in place of a relay. In that case use the circuit in Figure 1. Control
the triac on and off just as one would control an LED. Typically no calculations are involved. Zero
cross sensing is not necessary because U2, the triac output opto-isolator takes care of the zero cross
synchronization.
Without the zero cross detection a burst of noise can be generated with each turn on of the
load. This noise can be managed. However, the more cost effective approach, typically, is to
eliminate the generation of noise at the source using this type of opto-isolator.
Burst Mode
Also known as proportional control, Burst Mode allows single cycles to pass either
completely or not at all. Every alternate zero cross creates an opportunity for turn off or turn on of
the triac. Thus for 50% burst mode, Half of the cycles are let through and half are not. The
disadvantage of this over phase control is response time and granularity. Changes in power level
take longer to implement and fine changes take even longer.
Whether the triac will be turned on or kept off is controlled by the power ratio. If for
example the requested power ratio is 3/4 (75%), then three of every four cycles are passed.
The gate length can be fixed, since it will always fire right after zero cross. Firing near zero
cross will prevent noise generation. Note: if you do use the zero cross triac output opto-isolator,
you must fire before zero cross to have the next half cycle turn on. Also for this reason Burst Mode
control requires monitoring of zero cross.
Burst mode provides a quieter system than phase control and easier implementation. The
disadvantage is performance.
Figure 1- Triac triggering circuit using a zero cross
opto-isolator for On/Off or
burst mode, to minimize switching noise.
Phase Control
Phase Control is the control of power to a load on a cycle by cycle basis. Control is
achieved by blocking current flow after zero cross. At the proper time in the half cycle, the
microcontroller will gate the triac allowing current flow. By blocking the current flow for a short
time after zero cross, more power is delivered to the load. A delay for a longer time delivers less.
Delay for the full half cycle will deliver no power to the load.
Fine granularity of control and fast reaction time are achieved. Granularity is only limited
by the number of bits in the microcontrollers timer, its clock rate, and the uncertainty in the
response time of the system. Response to changes can be implemented 50 times a second in a 50
Hz system and 60 times a second on a 60 Hz system. (This will be less in inductive loads.)
In phase control gating near zero cross must be avoided. The circuit in Figure 1 will not
work. Ordinary triac output isolators, ones without zero cross circuits in them, are best for this (see
figure 2).
Because of its performance capabilities phase control is the main focus of this application
note. Two operational examples are included.
For cost sensitive applications see the non-isolated sample application. It shows how to
control a triac without the use of an opto isolator. For performance see the isolated sample
application.
What is a triac?
A triac is a three terminal electronic device designed to control large alternating currents
with a small current. It is a member of a family of electronic components known as Thyristors.
SCRs are another member. The names of the terminals on a triac are MT1, Gate, and MT2.
Specifications of the triacs operation are referenced to MT1.
Simply stated, a current flow between GATE and MT1 will allow a larger current flow
between MT2 and MT1. Once current flow is established between MT2 and MT1, current flow
will continue even if the current flow between GATE and MT1 stops. Thus, controlling the gate
cannot turn the current flow off. When current flow stops on its own the triac commutates. Further
current flow between MT2 and MT1 will be blocked. This typically happens at zero cross. Current
flow cannot restart until another gate pulse occurs.
The current flow through the gate can be in either direction to start current flow between
MT1 and MT2.
Because the current flow between GATE and MT1 can be either direction and because
current flow between MT2 and MT1 can be of either direction four triggering conditions exist for
the triac. The specifications for each of these four conditions, also known as quadrants, are all
different. By convention they are defined as follows:
GATE-MT1 MT2-MT1 Quadrant
Positive Positive QUADRANT 1
Negative Positive QUADRANT 2
Negative Negative QUADRANT 3
Positive Negative QUADRANT 4
The triac’s operation can be defined by a set three specifications. These are Gating current
(Ig), Latching Current (Il), and Holding current (Ih). Each Quadrant has its own set and all are
affected by temperature.
Gating Current (Ig) - this is the current that must flow between GATE and MT1 for the triac to fire.
Latching Current (Il) - this is the current that must be flowing between MT2 and MT1 when the
gate current turns off to maintain current flow. It can be compared to the “pull in” specification of a
relay.
Holding Current (Ih) - This is the current that must be flowing between MT2 and MT1 to prevent
commutation and the stopping of current flow. It can be compared to the “must release”
specification for a relay.
Quadrant 1 operation will always be the most sensitive. Sensitive means that the triac is
easy to trigger but hard to commutate. Quadrant 4 is the least sensitive. Quadrants 2 and 3 are in
between but are not the same. Manufactures strive to make quadrants 1, 2, and 3 as similar as
possible.
Triac Selection
Triacs come in three types. There are ordinary triacs, triacs optimized for sensitive gating,
and snubberless triacs.
Sensitive gate triacs, also called logic triacs, should only be considered for direct drive
applications as in figure 3. In systems using opto-isolation drive current will not be an issue, but
false triggering can be. In systems using opto-isolators sensitive gate triacs should never be used.
The choice between snubberless triacs and ordinary triacs will be made based on the price
sensitivity of the product, current, and the level of inductance in the load.
Snubberless triacs differ from regular triacs in three ways. First, they are a little more
expensive. Second, they cannot be fired in the fourth quadrant, making them less susceptible to
false firing. Third, they tend to be available in the higher current ratings, rather than low current
ratings. Having more robust commutation and surge current capability, they also feature fast turn
on/off speeds and of course, do not need snubbers, often paying for the price difference.
Circuit description
A full schematic is shown on figure 4. The circuitry that includes the push buttons, display,
clocking, and power supply are all quite ordinary and not described here.
Triac Firing Circuitry - U1 the microcontroller drives the triac using RC2 through the opto
isolator, U5. Any port could have been used. R5 limits the gate voltage swing so as to not stress
the gate junction. This is a more detailed example of the circuit in figure 2.
R4 and C1 are a snubber. Sizing of the snubber is dependent on load and is sometimes not
necessary. Capacitors should be of the Metalized Polypropylene type. These have the high dV/dt
specification needed for snubbers. The snubber needs to act at turn off, when the voltage across the
snubber is changing quickly.
The simplicity of this circuit in terms of part count is one of the great advantages of triac
control.
Zero Cross Circuitry - R23 and U4A are the zero cross sensing circuit. The output of U3A
goes to the RB0 interrupt. Usually zero cross needs to be read by an interrupt. An exception is the
first example which uses a 12c508. Rapid polling replaces the interrupt in that example.
The internal pull up resistor is enabled in the code to pull up the open collector output of the
U4A opto-isolator.
R23 is often two series resistor to allow easier passing of UL.
Test Circuitry - TP1 allows for measurement of the voltage across the triac. TP2 allows for
measurement of current through the triac.
Figure 4 - Schematic Triac Phase control example
Operation of the test board
The triac test board is designed to control the triac and present important parameters on the
three digit display. Power to the load can be controlled in terms of gating delay time or percent
power. Important parameters can be read such as line frequency, delay time, on time, and dead
time.
An Auto/Manual switch is on the test board to determine if the triac delay angle will be
controlled in terms of triac delay time in milliseconds (manual) or percent power (auto).
The six buttons around the display allow parameters to be changed. Only line frequency is
not changeable.
The single push button beneath the arrows selects the parameter that will be viewed and
available for modification.
Code Description
Two sets of source code are included. The first is C source compiled using Hi-Tech C and
designed to be used with the hardware described above. It implements the phase control of a load.
The second set of source code is written in PIC 17C75x assembly code and designed to
work with the simulator. It is designed to verify the tables used in calculating triac firing angle.
C Source Example
Flow charts are included to describe program flow. At reset the PIC initializes its ports and
resources then enters the main loop, see figure 5.
In the main loop it reads the buttons and updates the display every 1/8 second. If a change
in requested power has resulted, a new triac firing angle is calculated using the look up tables.
On every pass through the loop, a state machine variable, zx_state, is checked to see if a
zero cross has recently passed. If a zero cross has recently passed, new triac delay and on times are
calculated. TMR1 is then updated immediately to improve response.
Figure 6 - Triac timing calculations
When a new zero cross occurs, a new triac delay time and a new triac gating time need to be
calculated. A flow chart of the calculation routine is shown in figure 6.
Triac delay time changes with zero cross, although zero cross time should not change very
much. This algorithm allows the system to track changes and accommodate them.
Once the timings have been calculated, they are ready for use in the interrupt routines. The
zero cross interrupt starts the TMR1 timer timing the triac off time.
It would be nice to do the calculations for the timings in this interrupt routine. Two issues
argue against this. First, interrupt routines need to be short. If much time is spent in the interrupt
routine it may prevent urgent main loop tasks or other interrupts such as communications from
getting through. The second reason is that doing mathematics in the interrupt will require the
compiler to generate two copies of every mathematical routine needed in the interrupt, one for the
interrupt and one for the main loop, since the main loop needs them, too. This requires a lot of
program and file memory.
The flow chart for the zero cross interrupt routine is shown on figure 7.
Figure 7 - Zero cross interrupt servicing routine
The TMR 1 interrupt is designed to occur twice every half cycle. On the first time out, the
delay time has just expired. The triac is gated. On the second time out, the gate time has just timed
out and the triac gating signal is turned inactive. Timer TM1 is then left to continue clocking. This
over count will be collected in the zero cross interrupt and used to calculate the zero cross time.
If the triac is not to be gated, this interrupt will not come due. In that case, TMR1 is loaded
with 0 at zero cross. The next zero cross will occur before the full count of TMR1 is reached.
A flow chart of the TMR1 interrupt is shown on figure 8.
Figure 8 - TMR1 interrupt
Control Details
Phase control is a five step process. It starts with the determination of the power required
and ends with values for gate delay time and gating on time.
P = desired power
Pmax = the full power available
F = the delay angle, See the description below
P = PI, approximately 3.14159... This is also the maximum angle in radians for a half
cycle.
The Delay Angle (F) is an expression of how much the triac is delaying the current flow on
each half cycle. If the delay is zero, then P = Pmax. If the delay is P radians (180 degrees), then
the power will be zero. Delay angle is an intermediate value in calculating the time between zero
cross and triac firing.
Normalizing the desired power to the maximum power available changes the above
equation to:
Pr = P / Pmax = 1 - F/P + (sin2F) / 2P (4)
At this point we know the power that we want, we know the power that is available in the
system, and of course we know P. We would like to solve the above equation for F. Unfortunately
this equation is not solvable for F. Even if it was the resulting number crunch would be beyond
what most microcontrollers could do in a timely manner.
The most efficient solution at this point to use a look up table. In the code this table can be
found in file ttable.h. The table was generated using MathCadâ, which exported values for F for
each of the 255 power ratios between 0/255 to 255/255. These were exported in two tables, a
whole number part and a fractional number part. From a ‘C’ point of view it would be cleaner to
have a single table declared ‘const unsigned int’ type. The table, however, was originally exported
to two assembly language look up tables where they were tested and then ported to C. Although it
makes the C source longer, it does not waste any program memory space.
In the code this value F, Delay Angle, is referenced as the variable, triac_angle. It can range
from 0 to 46,080 where the least significant bit is 1/256 of a degree and 46,080 represents 180
degrees.
DT = the delay time. This is expressed in terms of TMR1 increments. In the code it is
referenced as triac_delay_time.
F = Delay Angle, 180 degrees maximum, in terms of 1/256 degrees. In the code it is
referenced as triac_angle.
ZX = the full zero cross time. Two readings are taken every cycle. In the code it is
referenced as full_zx_time, and is in terms of TMR1 increments.
Three things are important when handling loads with inductive components. These
precautions are best done with any type of load.
1. Gate the triac with a pulse of no less than 10 microseconds. Use a 1 millisecond pulse if
possible. This is needed for the reason stated above and because it takes longer to get enough
current flowing through an inductive load to get above the Latching Current(Il) specification.
2. Size the Dead Time so that the firing pulse is off long enough to ensure that half-cycling
will not come from the pulse being too close to zero cross. This could cut back on the low power
end of the control.
3. Snubber the triac if voltage spikes are retriggering it. Size the snubber for the expected
load or use a snubberless triac.
Over Heating
The three main parameters Gating current (Ig), Latching current (Il), and Holding current
(Ih) are all temperature dependent. As temperature rises the gate becomes more sensitive. The
hold current goes down, too, which can eventually keep the triac in conduction right through zero
cross, resulting in half cycling or full cycling.
Suspect overheating if the system comes up fine, but then starts to fail after operating a
while. Spray some freeze mist on the triac. If the problem goes away for a while, you know that
overheat is the issue and heat sinking is not adequate.
Code Bugs
It is very easy to upset the zero crossing interrupt. Be careful before disabling interrupts.
Disabling interrupts can cause jitter in the triac firing and mismeasurement of the full zero cross
time.
Line Symmetry
The algorithm used to control the triac firing angle, controls on a full cycle by full cycle
basis. It could control on a half cycle by half cycle basis, but does not. This is because symmetry
between positive and negative halves of the AC wave form must be maintained to accommodate
any inductive component of the load. Any asymmetry in the AC wave form effectively introduces
a DC component on to the wave form and will cause trouble in the form of half-cycling and loss of
control.
Triac Myths
Noise at Zero Cross
This is not a problem. If you think you have a problem with noise at zero cross, look
somewhere else.
The zero cross circuitry is low impedance. Low impedance circuits have low susceptibility
to noise.
On two of the designs that I have done using triacs, I was explicitly warned about noise at
zero cross. During development this was never an issue. One design is a product commonly used
in China. Never has there been a noise at zero cross issue.
The myth of narrow pulses may be based on two things that are indeed true. First, even
though the pulse should not be short, it should have a fast rise time. Rise time should be less than 1
microsecond. For this reason Diacs are sometimes employed in the gate circuit of triacs. This is
usually not an issue with microcontroller based designs, so Diacs are not recommended. Second, if
the current of the gate pulse is too high, it can stress the GATE-MT1 junction causing overheating.
The fix is not to shorten the pulse but to ensure that the current of the pulse is not excessive. Use
equation 7 above to size the gate current limiting resistor.
Motor Control
The short answer for using a triac to control a motor is, don’t try it.
Of course a triac can be used to turn a motor on and off. Snubbering is most likely needed
and turning on and off at zero cross is also recommended.
Phase control of a motor will not work. The reason is that the process of phase control
introduces harmonic frequencies into the power waveform that are much higher in frequency than
the wave forms base frequency. Phase control is good at controlling voltage, but not at controlling
frequency. Since torque is proportional to V/F, and F becomes a complex waveform when using
phase control, torque essentially becomes undefined.
The motor’s reaction to the frequencies in the complex waveform will not be specified or
controlled by the manufacturer. The result is usually bad noises, overheating, and inefficient
operation of the motor.
Triacs have been used to control motors on aircraft. This has been done using Burst Mode
(proportional control). The result will be a an uneven operation, since some cycles will be missing,
but very little EMI will be generated. In aircraft the power wave form is 400Hz, which makes the
uneven operation a little more forgivable. Performance at 50 or 60Hz is unlikely to be considered
adequate under any product specification.
Summary
Phase triac control can provide a very cost effective solution to controlling significant
power to a load. Although problems exist with EMI and inductive components in the load, these
problems can be managed. Keep the gating pulses wide and don’t gate too close to zero cross. If
you are not stingy with line input filtering, you will get your CE mark, too.
Who could ask for more.
Related Articles
Microchip Reference Design:
PICREF-4 - “PICDIM Lamp Dimmer for the PIC12C508,” by Scott Fink, 1997, Microchip
document number 40171a. schematic in Teccor Application Note 1003
ST Micro Ap-Notes:
AN308 - “Control by a Triac for an Inductive Load How to Select a Suitable Circuit,” by X.
Durbecq, Feb. 1989.
Teccor AP-Note 1003 has schematics and has a predecessor to the first example of this Ap-
note. The COP Note 6 describes how control was done before the invention of opto-isolators.
The ST Micro AP-Note 308 has a good description of half cycling in inductive loads. If the
description here confuses, try that one.
Acknowledgments
The author would like to thank Richard Norris of Teccor for reviewing and correcting this
Application Note and Mark Heyman of TA Instruments for driving the Math Cad to derive the look
up tables for linearizing the firing angle in the code.