Relay control with Raspberry Pi


#1

I’m trying to control a door with a Raspberry Pi. All I know about the door’s wiring is as follows:

  • Crossing the white and green wire of the door causes the door to open (makes sense based on the installation manual)
  • Either 12 or 24v DC is supplied on the red and black wires (the installer chose)
  • Maximum DC current draw is 1 amp.

And here are the specs about the Pi I’m using:

  • Outputs 3.3v at 8ma by default, can be configured from 2mA to 16mA

Being overconfident in my abilities, I made a parts list and bought:

  • A 5v DC switching voltage regulator - I had read online that switching regulators produce far less heat than linear voltage regulators.
  • This relay. - I accidentally bought a NC one instead of a NO one :frowning: But otherwise this looked good to me - input voltage within the possible range (input 0.9-1.5v, with a typical input of 1.2v, and could handle a current load of 3.25 Amps between 0 and 60v).
  • Some resistors - particularly, one 562 ohm one and one 1000 ohm one. These are to be used to construct a voltage divider to convert the Pi’s 3.3v to the relay’s needed 1.2v.

The circuit is designed so the 12 or 24 volts of DC from the door’s wiring go into the voltage regulator, which outputs 5v. This 5v is then fed into the 5v rail of the Raspberry Pi. The Pi outputs 3.3v to the pin when triggered. This 3.3v is then fed into a voltage divider, reducing it to 1.2v. That 1.2v is fed into the relay, which “connects” the white and green wires.

However, when I put the relay into a test circuit, feeding it the input as configured above and using 3.3v and a GPIO input pin as the load, it would not actuate.

I have a few questions:

  • Can I complete this without ordering any new parts?
  • Where did I go wrong in my test? Would my design work?
  • Is there a better way to power the Pi?
  • Can I use an EMR? I like being able to hear the sound to know it’s actuating; however, I couldn’t find any that had a coil input of 3.3v.

#2

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.