I think it is important to use the 2N2222 based logical level shift up and buffer to take the 10~20mA sinking current from the optocoupler. If Rpi GPIO is directly connected to the optocoupler, the 15mA current might damage the GPIO pin circuitry.
My suggested wiring is as below.
I am assuming OP's relay module is similar to this below.
sainsmart 8ch 5-v relay board
And the photocoupler similar to below.
Below is a summary of the photocouopler.
But I don't understand why Sainsmart says that their module can be controlled directly by Rpi, though at the same time each channel takes 15~20mA.
I found a relay module similar to Sainsmart, and has optocoupler, and with a limiting resistor of 1K. In other words, input drive current is only 4~5 mA. So I can use rpi to directly control this module.
I have also connected all the grounds together, just check out if there is any bad effects.
Measuring Relay Switch Noise/Interference
After skimming through a couple of EE SE and electro-tech-online.com forum discussions on relay noise, I have the feeling that relay noise is a complicated thing. The hardware guys suspect the noise feeds through the AC line. I have been playing only 5V/12V motors, and I dare not to play with big 220VAC motors for now. So I will start with light DC 1A loading, to see if I can find any spiking noise.
# tlfong01 2018may13hkt1228
toggleRelay(relayPin = 17, activateLevel = 'High', \
activateMilliSeconds = 20, deActivateMilliSeconds = 20, \
toggleCount = 100000)
I did not find any spiking noise at switch on and off. But the signal noise is around 1V pp. I don't know if these noise level is big or small. But the rpi program works OK, switch on and off at about 40mS period or 1/40mS = 25cps.
Varistor for Relay Surge Absorption
Now I have found a varistor with a broken leg
These appendices are for relay beginners.
A. SainSmart 8-Channel 5V Relay Module Ad
B. SainSmart 8-Channel 5V Relay Module Rpi Wiring Diagram
C. SainSmart 8-Channel 5V Relay Module Schematic and Spec
Power Supply: 5V DC / 400mA (relay all ON)
Input control signal voltage:
0V - 0.5V Low stage (relay is ON)
2.5V –5V High state (relay is OFF).
Input control signal LOW state current:
JD-VCC or VCC: Power supply input, 5V DC. (JD-VCC RELAY POWER VCC:SYSTEM VCC)
GND: Power supply ground and control signal ground.
CH_x: Control signal input, Low: relay ON, High: relay OFF.
COM / NO / NC: (C1=COM1, C2=COM2)
Control signal state low, the relay ON, COM - NO disconnected, COM - NC connected.
Control signal stage high, the relay OFF, COM - NO connected, COM - NC disconnected
D. How to wire a Raspberry Pi to a Sainsmart 5v Relay Board
D1. Pi and MCP23017 Based Heating Control system
E. Simple relay circuit getting noise spike form AC line
F. Relay noise problem although flyback diode is used
G. Transient Supressor - michaelkellett
The fan may be a much more difficult load than the lights - but if switching the relay makes it go wrong only if it's the fan that is switched then it is probably something to do with the characteristics of the fan/regulator combination.
I should mention that you need to be very very careful using the Sainsmart board to switch mains voltages - you must be very careful with your wiring and make sure things are in a box and firmly screwed down. You could try putting a transient suppressor across the relay contacts for the fan - something like:
Transient suppressor EPCOS 1004329 B72205S151K101
H. Introduction to Transient Voltage Suppressors
I. Mechanical Relay Primer
J. Relay Arc Suppression
For AC powered applications, you can put a Metal Oxide Varistor (MOV) across the load terminals of the relay in order to protect it from voltage spikes. An MOV will not remove the entire arc, but it will be helpful in circuits up to several hundred volts. You can buy an MOV from any electronics distributor, like this MOV from Digikey.
For low-voltage AC applications, a bi-directional transient voltage suppression diode ("transil diode") that is rated for higher than the AC voltage of the circuit can be placed across the relay terminals in order to protect it from voltage spikes. You can buy one from any electronics distributor, like this Transil Diode from Digikey.
K. Sain Smart Story
L. Sain Smart relay manuals
M. RLTS80 Transformer
N. Low Electromagnetic Noise Emission Solid State Relay news
Two kinds of emissions:
Radiated emissions - low with standard solid state relays
Conducted emission noise: with standard Solid State Relays, conducted emission noise is emitted by thyristors and at 150KHz you reach the highest level.
Standard low noise SSR on the market are limited to low switching current of 5A for domestic applications or to low inductive loads, while lots of pure resistive loads applications have high emission noise.
EMC stands for Electromagnetic Compatibility.
O. AC-DC Power Supplies - Using Wall Warts
Page 5 of 9 - Output Voltage vs Percentage Load (Switching Supplies)
P. Varistor ZOV10D471K Datasheet
Q. 20mA coil operating current relays
R. Circuit Design of 2N2222 Driving a Relay