An Arduino library for zero-cross solid state relays
ACZCPWM
Precise, scheduled heat for a glass kiln’s annealing profile, from inexpensive parts instead of a dedicated controller.
github.com/wmacevoy/ACZCPWM · MIT license · Arduino Library Manager: ACZCPWM
1 / 14 ACZCPWM stands for AC zero-cross PWM. It was designed around a glass kiln: the kiln has to follow a firing and annealing schedule, and the goal was to get smooth, accurate control from a hobby microcontroller and a plain zero-cross solid state relay instead of an expensive dedicated kiln controller.
Why a kiln needs precise heat
Annealing is a schedule, not a setpoint
Ramp
Bring the glass up to its working temperature at a controlled rate, then hold while it fuses or slumps.
Soak
Drop to the anneal temperature and hold it steady, so stress inside the piece can relax evenly.
Cool
Come down slowly through the strain range. Rushing or wandering here leaves stress that can crack the piece later.
The controller has to follow a curve for hours, holding each step without overshoot or droop.
2 / 14 Glass work is unforgiving about the anneal. The kiln must follow a time and temperature profile, not just reach a target. Steady holds and smooth ramps matter more than raw power.
The firing profile in the Oven example
One table in the sketch drives the whole firing
1400 °F process hold
920 °F anneal soak, 0.9 h
Ramp from 100 °F
Cool
0 h
1 h
2 h
3 h
3 / 14 This is the time and temperature table from examples/ACZCPWM_Oven. The sketch interpolates between the points, a PID loop compares the thermocouple reading with the target once a second, and ACZCPWM turns the PID output into relay timing. Editing the table changes the schedule; no controller programming panel needed.
The catch
A zero-cross relay only switches when the AC voltage crosses zero
That makes it clean and cheap: no inrush, no electrical noise. But power now comes in whole cycles, and ordinary PWM timing fights that.
8 %
power error from cycling 10 times a second on 60 Hz mains, depending on where the zero crossings fall
8.3 s
PWM window plain timing would need to reach 0.1 % accuracy
4 / 14 A zero-cross solid state relay waits for the next zero crossing before it turns on or off. That is exactly what you want for a heater, but it means any PWM period that is not lined up with the mains cycles aliases. You either accept large errors or very long, lumpy windows.
The idea
Dithered whole-cycle PWM
Each block of period cycles is on for ⌊duty × period⌋ cycles, or one more. A bit-reversed counter decides which blocks get the extra cycle, spreading them as evenly as possible.
3 on
2 on
3 on
2 on
period = 4, dither = 2, duty 60 %: 10 of 16 cycles on. That is 9.6 rounded to the nearest whole cycle, the closest any relay can get.
5 / 14 Filled squares are AC cycles where the relay is on, outlined squares are off. Within a block the relay is on at the start. The extra cycle alternates between blocks because the counter steps through binary fractions in bit-reversed order: one half, then the quarters, then the eighths, and so on.
The one rule
dither cycle = period × 2^dither
Make the dither cycle a factor of the window your process averages over. Every window of one dither cycle then gets the requested energy rounded to the nearest whole cycle: the best whole cycles allow.
6 / 14 This is the design intent of the library. Pick the window you care about, for a kiln something comparable to how fast it responds, and choose period and dither so that period times two to the dither divides it evenly. Over each such window the error is at most getResolution(), which is one over period times two to the dither plus one. Windows that are not whole dither cycles carry no such guarantee.
Settings on 60 Hz mains
Slow kilns get fine resolution
| Use | period | dither | Dither cycle | Resolution |
|---|---|---|---|---|
| Oven example | 4 | 5 | 128 cycles ≈ 2.1 s | ±0.39 % |
| 1 s window | 60 | 0 | 60 cycles | ±0.83 % |
| 1 s, spread out | 15 | 2 | 60 cycles | ±0.83 % |
| 1 min window | 225 | 4 | 3600 cycles | ±0.014 % |
| ~18 min window | 1 | 16 | 65536 cycles | ±0.0008 % |
A kiln responds over minutes, so a long window costs nothing and buys precision. Resolution is getResolution() = 1 / (period × 2^(dither+1)) of full power, checked by simulating the library’s exact algorithm.
7 / 14 All rows use the default full-cycle switching. On 50 Hz mains the same settings make windows 20 percent longer. The Oven example row is what examples/ACZCPWM_Oven ships with.
Default: full-cycle switching
Whole cycles: no DC in the load
Half-cycle switching
600 : 0
positive to negative half-cycles in 10 s at 50 % duty: half-wave rectified current
Full-cycle switching
300 : 299
the same 10 s: balanced, never more than one half-cycle apart
The library decides once per cycle and holds it for both halves. DC is harmless to kiln elements but saturates transformers and motors and loads the supply. Resistive-only builds can pass fullCycle = false for twice the resolution.
8 / 14 Numbers are from simulating the library with period 1 at 50 percent duty. With half-cycle switching the relay turns on every second half-cycle, which is always the same polarity. Full-cycle mode makes every on period a whole number of complete cycles.
Option: sigma-delta
When the PID keeps changing the duty
Dithered PWM (default)
19 cycles
drift in total energy over 100 s of once-a-second PID changes: each setting’s rounding is lost
heater.setSigmaDelta()
½ cycle
at most, always: an accumulator carries the remainder from one setting to the next
Same zero-cross timing, full cycles and fail-safe, with a 16-bit duty. The same idea as GyverDimmer’s DimmerBres (8-bit, half-cycles) and ESPHome’s sigma_delta_output (timer-clocked).
9 / 14 Sigma-delta adds the duty to an accumulator every cycle and switches on whenever it reaches one whole cycle, like Bresenham’s line algorithm. The energy delivered since start is always within half a cycle of what was asked for, however often the duty changes. Dithered PWM stays the default because it is optimal over a window you choose and switches less often. The 19-cycle figure is a simulation of period 60 with a PID-like duty; the integral term would correct it eventually, sigma-delta just never lets it build up.
The build
Four parts from thermocouple to heat
Thermocouple
K-type probe, read by a MAX31855 amplifier
Arduino
Firing table, PID loop and ACZCPWM
Zero-cross SSR
Switches only as the voltage crosses zero
Kiln elements
Resistive heat, in whole AC cycles
H11AA1
Optocoupler zero-cross detector
Tells the Arduino (pin 2) when each cycle begins, with the mains side fully isolated from the low-voltage side.
10 / 14 The thermocouple and MAX31855 give a digital temperature. The Arduino runs the firing table and a PID loop, and ACZCPWM turns the PID output into relay timing locked to the mains cycles reported by the H11AA1. The SSR does the switching. There is no dedicated kiln controller in the chain.
Inexpensive parts
Common parts, precision in software
| Part | Job | Why it is enough |
|---|---|---|
| Arduino Uno or Nano | Runs the profile, PID and ACZCPWM | Any AVR board with Timer0 works |
| MAX31855 + K-type probe | Reads kiln temperature | Digital output, no analog design |
| H11AA1 + 3 resistors | Isolated zero-cross signal | A handful of small parts |
| Zero-cross SSR + heat sink | Switches the elements | No timing smarts needed in the relay |
The expensive part of a kiln controller is the control. Here that is a library.
11 / 14 A dedicated ramp and soak kiln controller bundles temperature measurement, scheduling and power control. This build gets the same functions from parts that are common in hobby electronics: the Arduino does the scheduling and control, the MAX31855 does the measurement, and ACZCPWM gets precise power out of an ordinary zero-cross relay. Size the SSR and heat sink for the kiln's current.
Safety is built in
A kiln left alone must fail off
Isolated detector
Keeps mains off the Arduino, its USB cable and the computer.
Detector fail-safe
No zero cross for 30 ms and every SSR output turns off.
Sensor fault
Thermocouple fault: heater off; the PID restarts cleanly on recovery.
Safe power-up
Outputs stay off until two real zero crosses arrive.
Mains wiring can kill. Build in a proper enclosure and get qualified help.
12 / 14 The first three come from the library and the Oven example. A failed or unplugged zero-cross detector can no longer leave the elements stuck on, a broken thermocouple cannot make the PID heat blind, and a single stray edge at power-up cannot switch the load on.
The code
The kiln’s side of the sketch
ACZCPWM heater;
void setup() {
// zero cross on pin 2, SSR on pin 13
// period 4, dither 5: 128 cycles, about 2.1 s at 60 Hz
heater.setup(2, 13, 4, 5);
heater.setSigmaDelta(); // optional, for a PID-driven duty
}
void loop() {
// once a second: goal = profile(t); pid.Compute();
heater.setDuty(powerOutput / 255.0);
}
13 / 14 That is all the library asks of the sketch: one setup call with the zero-cross pin, the relay pin, period and dither, an optional switch to sigma-delta, and a duty between zero and one whenever the PID updates. The full kiln version, with the firing table, thermocouple and auto-tune, is examples/ACZCPWM_Oven.
Get it
ACZCPWM
Install
Arduino Library Manager: search for ACZCPWM.
The kiln
The full controller it was written for: github.com/wmacevoy/kiln
Source
github.com/wmacevoy/ACZCPWM, MIT license.
Read the README’s wiring section before connecting anything to mains.
14 / 14 The README covers the isolated H11AA1 wiring with part values for 120 and 230 volts, choosing period and dither for your window, and the full-cycle default.