Opto-Isolated
Relay Module
Circuit : Andy Collinson
Email :

Description
A very simple dual channel optical isolated relay module. The input can be driven from 3.3V or 5V sources. The output is isolated via changeover relay contacts. The relays are 5 Volt making this compatible with Arduino and Raspberry Pi.

2 chanel opto-isolated relay circuit Notes
The circuit above is a two chanel relay switch using opto-couplers. The opto-coupler is a sealed four pin device containing a light emitting diode (LED) and a spatially separated photo transistor. This arrangement gives physical separation between the input (the LED) and the output photo transistor.

The inputs labelled as IN1 and IN2 are the input to the opto-coupler. The input voltage can be as low as 3.3V making this circuit compatible with lower voltage boards such as Raspberry and Banana Pi. The output of the opt-coupler is passed through a current limit resistor R2 and then drives the transistor and output relay. A back emf diode 1N4007 protects the transistor from the relays back emf voltage and to indicate each channel is on another LED is wired across each output channel.

Opto-Couplers
Opto-couplers such as 4N25, EL817 are encapsulated and hermetically sealed. They are light proof, waterproof and and available as surface mount packages (SMD) ao 4Pin DIP packages. Both LED and photo transistor are contained within the same monolithic block but physical and spatial separation between the two components.
You can read the EL817 Datasheet here.

Single Channel Relay Power Isolation
The circuit below is identical to the dual channel except that the power for the relay JD-VCC and power to the opto-isolator VCC are separated by a link. When the link is removed a separate power supply is required to JD-VCC and GND for the relay. With the link in place, both the opto-isolater and relay can be powered by the Arduino or Raspberry Pi and there is no isolation between power supplies. The "Link" is a plastic 0.1 inch PCB header, usually nlue or black.


The relay has a 5 Volt coil and the Vcc and Ground can be powered from the 5V supply of Arduino or Raspberry Pi with the link between JD-VCC and VCC. The Raspberry Pi output pins, when active only provide 3.3V and can not be used to power the module, but can be used on the input pins marked IN1 and In2 in the dual channel circuit. The relay contacts are single pole changeover type, the pin designations refer to COM, NO1 and NC1. These are the common or changeover contact, normally open and normally closed contact pins. More on relay contacts in this article.

These boards are available on Ebay, Ali Express and many other vendors, a search on google will find many suppliers. The boards are now so cheap that they cost less than you can buy the individual components for. They come in single, dual, and quad versions. The dual version circuit is shown above Left.

Fritzing Layout

Sample Arduino Code
In the Fritzing layout above, the Arduino digital output pins 8 and 9 are setup as outputs and connected to channel 1 and 2 respectively, The sample Arduino code below is very basic. The code is wrote in C and setup function runs the code between the braces { }. This code initialises pins 8 and 9 as outputs. The "void" keyword must be present because the function setup returns nothing. In C language, all variables and functions must be declared, meaning that you must tell the program the type of variable, whether integer or string etc. The loop function also returns no value so is a void loop and again runs through the statements between the braces. The command "digitalWrite" is used to set an output pin HIGH or LOW, and will remain in this state until otherwise defined. The "delay" is a built in Arduino function and sets a delay in milliseconds, hence 5000 ms is 5 seconds. You can see that the loop statement turns one output on, the other off, then pauses for 5 seconds, then reverses the outputs. At the end of the loop, the last delay(5000) the loop repeats forever, or until power is removed.

To upload to an Arduino Uno, right click and copy the Code , then paste into the Arduino editor. The Arduino boards are programmed using a USB cable and once the code has been checked for errors, the Arduino software will compile the C code into machine code for the Arduino micro-controller. The compiled code is compact and is executed very quickly, in microseconds. However a relay is a slow device in comparison and may take 25 milliseconds for the coil to energise the armature and contacts to close.

save file
/*
Dual Relay Module Test. Code for Arduino Uno
Alternates outputs on pins 8 and 9.
Process Repeats every 5 seconds
*/

//Setup runs once only. Pins 8 & 9 as outputs

void setup() {				
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}

// Loop repeats all commands between { } 

void loop() {				
digitalWrite( 8, HIGH);	
digitalWrite( 9, LOW);
delay(5000);	
digitalWrite( 8, LOW);
digitalWrite( 9, HIGH);
delay(5000);				
}

Links and Where to Buy
Ali Express
Ebay
Sunfounder
2 Chanel 5 Volt Relay Wiki
Circuit Exchange International Return to Switching Circuits https://www.cxi1.co.uk/