Relay control with Raspberry Pi

Welcome to the forum!

Where you went wrong is using a voltage divider to feed the LED in the solid state relay (AKA optocoupler). An LED is a current driven device and all you want is one resistor to drop the voltage to the LED voltage based on the current you want through the LED.

To find the resistor value we use ohm’s law with these parameters:
From the SSR data sheet:
Input Control Current to Activate 5 mA Max
Input Voltage Drop 1.2V typical
Raspberry Pi power supply of 3.3V.

First we calculate how much voltage we need to drop in the resistor:
3.3V - 1.2V = 2.1V
Finally we calculate the resistor value that will drop that voltage at the LED current:
2.1V / 0.005A = 420 ohms

Since you’ve already got a 562 Ohm resistor lets see if that will work instead of a 420 ohm resistor:
562 * 0.005 = 2.81V, 3.3 - 2.81 = 0.49V way below the typical 1.2V LED forward voltage for the SSR.

However the SSR has a typical Input Control Current to Activate of 0.0008A. Reworking the calculation for that value:
562 * 0.0008 = 0.45V, 3.3 - 0.45 = 2.85V way higher than the maximum LED forward voltage of 1.5V. That would be problem except for the fact that the LED is current device not a voltage device. So what will really happen is the LED will lock the voltage at whatever its specific forward voltage is and then the current will be that divided by 562 ohms. So there is a very good probability that simply having the 562 ohm resistor in series with the SSR’s LED will work. It will only fail if the specific SSR you have has an Input Control Current very close to the absolute maximum of the data sheet.

A Big Caveat:
I just wrote and calculated all this while drinking my first cup of coffee right after waking up. Although I’ve been doing ohm’s law calculations and basic math for over 50 years I may have made a math mistake so check my values.

A few final notes
It is easy to damage a Raspberry Pi by applying the wrong voltage or drawing to much current. So it is far better to test this using an independent power source first to verify the design. Two series connected AAA, AA, C or D cell batteries will make a useful power supply to test with, new alkaline batteries will give you about 3.2V.
When you power a Raspberry Pi through the GPIO header you bypass the protection circuits of the Raspberry Pi. So any mistake such as supplying 6V will nearly instantly kill the Raspberry PI. I use the Raspberry Pi’s micro-USB power input connector during all prototyping and testing and only direct power when I’m sure everything is OK.

2 Likes