Making a solid-state relay with I2C interface

As [Mic] often got requests to make high-power switching boards, he recently finally gave in and designed the one shown above based around a solid-state relay. Some of our readers that already play with mains power know that switching should normally occur when the voltage crosses zero volts. The ‘TRIAC BLOC’ is able to do so, which also allows mains frequency measurement. [Mic] then tuned to the internal oscillator of his ATtiny microcontroller with this 50Hz by adjusting its OSCCAL register value, so the switching command can be sent at the ideal moment. Zero crossing detection is implemented by feeding the mains into an AC optocoupler. [Mic] discovered that the optocoupler diodes are not identical, so he had to adjust his firmware to account for the time differences.

All the resources are available on github, we would be interested to hear your detailed analysis of the circuit implemented with the passives R3/C1/L1/R8/C3.


  1. rj says:

    Nice rigor on all the math!

    Shouldn’t you expect the off time to be the same as the twice-previous zero crossing? Or when you said “the same length as the previous one” that’s what you meant? (same length as the previous one in the same direction)

    At some point in the past I’d sat down and characterized a specific incandescent bulb to get a mapping of “% duty” to “perceived brightness”; human photopic response and resistance-as-a-function-of-temperature.

    • miceuz says:

      Yes, exactly, each pulse was similar with the one two ago. In other words, pulses for the same leds are similar in length. But it depends on pullup value used. For a bigger pullup I don’t get two distinct peaks in hystogram, so they are appearently close enough to each other.

      Regarding perceived brightness – i’ve checked on the stuff recently – interestingly enough it goes back to greece and perceived star brightness. I should put my power values thru another function get a perceived linear dimming. I dont’ remember exactly now, it has something to do with 2.5^x.

    • Rollyn01 says:

      I liked to math too. It took me a second to understand he was using trig identities. I hate math. /facepalm

  2. Don says:

    The only thing I hate is the mains connection. Why bolts? There are plenty of IP20 PCB terminal connectors you can get…

  3. rainsbury says:

    The idea with the bolts is that you use ring crimp terminals which you fit to your mains lead before connecting and then fix in place with bolts.

    This simplifies construction and makes it easier to change the mains wire if it gets damaged.

  4. skdsdfhjds says:

    Not sure about the quality of solder joint on that resistor to the left

  5. Karl Miller says:

    Hey author @MathieuStephan , get your titles correct. This is not a post about making a relay, it is a post about making a relay board. Thanks for wasting my time.

  6. Trui says:

    You can avoid the huge resistors for the optocoupler by using a series capacitor to limit the current. Also, I would just use an optocoupler with a single LED, and put an anti-parallel diode on the outside (to avoid reverse voltage breakdown of the LED). The CPU can measure the mains frequency, and calculate zero crossings with a timer.

    • miceuz says:

      I was considering that, but wouldn’t I get a phase shift because of a capacitor?

      • Trui says:

        Yes, but you can compensate that in the firmware.

        • Rollyn01 says:

          Wouldn’t he also have to worry about the harmonic distortion (read: added noise) to the circuit that would make it harder to detect the zero-crossing event?

          • Trui says:

            I have used this method with a series capacitor in a couple of products, and never had any trouble with distortion. Of course, you can’t count on the mains frequency to be a clean sine wave anyway. There could be distortion from heavy or non-linear loads on the same circuit, as well as big transients, so a suitable low pass filter is recommended.

  7. The oscillator calibration trick is cool, but I’m not sure what it buys you. Assuming that you are running on the internal RC oscillator at ~8 MHz and your mains frequency is 50 Hz, each mains half cycle is ~100k clocks. Your worst-case misalignment without calibration (1 clock) is ~3e-5 radians. Assuming 240V RMS, you are, at worst, 10 mV off the zero crossing. What about the application makes scrubbing out that 10 mV worth the effort?

    I really like the work you did on sussing out the errors with the zero-crossing detection.

    • Rollyn01 says:

      If he’s trying to trigger at zero-crossing, but there’s still voltage detected, then wouldn’t his timing be off? Correcting for the voltage error would allow him to properly detect when the voltage would be zero just before it occurs. This would help prevent any wasted power dissipation.

      • You will never switch perfectly at 0V, since nothing is perfect. The proper engineering question to ask is how close to 0V is good enough? Now, if the uC clock really is the limiting factor on your precision, you can figure out how much the mains voltage changes in 1 clock… and it happens to be 10 mV. That’s out of a 240V RMS/340V peak. It hardly seems worth the effort, so I was asking why.

        If you want to be achingly precise about correctly estimating the exact instant of zero crossing, the right approach is likely a single LED optocoupler (so you only get half the waveform) combined with a PLL to synthesize the zero crossings.

    • Trui says:

      I’m guessing that the cycle-to-cycle variation in mains frequency is already bigger than the error from the uncalibrated oscillator.

  8. Elmars says:

    If I understand this circuit correctly, it can also be used to switch american two phase 220v loads, with L1 on phase and L2 on Neutral. Something I have not been able to do with regular SSRs.

    • Tom Hargrave says:

      Why not? With a SSR you only interrupt current anyways which means you only need to switch the hot leg & leave the neutral leg connection. There is only a need to switch all legs in a three phase circuit.

      • Elmars says:

        For North American 220, both legs are hot. When a regular SSR tries to switch the load, it fails to find the zero crossing. One leg of the SSR is direct to hot, the other hot via the load. It tends to work ok for a resistive load, but when you try to switch a motor, nothing happens. Something is messing with the SSR’s ability to find the zero crossing point. With this circuit, it switches one leg, but sees both sides direct without intervening load. Place the second power leg on the neutral post.

        • Septillion says:

          It has nothing to do with the North American 220V. It’s only a matter of reference if a leg is hot or not. As long as the neutral (or ground) isn’t connected the SSR had no way of telling it’s connected hot or not. The problem then is in the fact it’s an Inductive load. The TRIAC stops conducting when no current is flowing. With a inductive load that’s not at the zero crossing of the voltage…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 96,657 other followers