Monday

Controlling a 2 Channel Relay with Arduino

Hi friends, I am writing this post to reduce the length of the video I am posting on youtube. This article works as a companion of my videos on the same topic.

In this article let us learn how to use a 2 Channel Opto coupler relay with an Arduino Uno


This is a Typical 2 Channel opto coupler relay available on Amazon India. At least that is where I bought one. This is a 5v relay. That means you need to give it a supply of 5v for it to operate.  This relay needs supply on two pins.


The opto coupler has two distinct electric circuits isolated by glass or transparent material. Or coupled optically. That means one circuit makes contact with another circuit only when the Infrared LED on the one side (that is on the GND, IN1, IN2, VCC side) sends a light signal to the light activated transistor, that is part of the JD-VCC - GND side.

This opto isolation does not complete if the JD-VCC and the VCC are joined by a Jumper, as is the default case when you receive your 2 Channel Relay.  This jumper can be used to test the working of the relay and to test if it is working and functional, if you just connect its VCC and GND (of the right hand side of the image) to 5V, and Gnd on the Arduino, and then just connect another port of GND from Arduino to IN1 and then to IN2 briefly. You should notice the LED on the Relay lit and a tick sound of the relay being activated. This happens because this is a Active LOW relay, as are most of the relays that are available for use with Arduino on Amazon India.

Once you have checked your relay and everything is working fine, then the next step is to remove the jumper from the JD-VCC VCC pins and keep it aside.



Never put the jumper pin over the VCC and GND pins. That is equivalent to inserting a wire, one end in one pin and another in other pin of a electric socket.

If you put the jumper over the VCC and GND pins ( I think it is a design mistake/ overlook on the part of the designers/ manifacturers to place the pins in close proximity where they are not expected to be used with a jumper. increasing the distance between the VCC pin and the GND pin slightly so that the jumper could not be placed over them could/ or can prevent accidental mishap. If you continue with your experiment while the jumper over the wrong pins, the 20mA current that the 5V pin of the Arduino will pass through the entire circuit while short circuited. It may damage any trace on the PCB either on Arduino or on the Relay. Although I have never attempted it nor have I seen what would actually happen and how much damage it could cause, but it is my learned guess that it may end up ruining your board.

Take a look at the items that I used in this example.


Arduino board, with a USB cable.
Some Male to female dupont wires. I strongly recommend you to stick to some kind of color code for your convenience. So that you do not insert wires in wrong pins, and it becomes easy for you to check and troubleshoot your circuit in case things are not working the way they are expected to.
An external breadboard power supply will come handy to power the JD-VCC pin of the Relay module.

I will show you a crude circuit diagram that is easy to understand.


Connect 5v and Gnd from Arduino to GND and VCC of the Relay ( at the input side)
Connect any pins ( form pin2 to pin13) to the IN1, and IN2 of the relay. ( You will have to define these pin numbers as Relay1 and Relay2 in the program as explained later below)
Then connect the JD-VCC and the adjacent VCC pins to an external DC 5v supply and Ground.

After that you can connect the 12v adapter to the breadboard power supply , then switch it ON (The breadboard power supply has a On/Off switch on it) . After that connect the USB cable to the Arduino and the computer. Then open the Arduino IDE software and start coding in it.

Controlling 2 Channel Relay with Arduino Uno
These are the variables that you declare at the beginning of the program. Relay1, Relay2 are          variables. And 7 and 8 are the port number or pin numbers on Arduino Uno which we will be using to connect to the IN1 and IN2 of the relay module.

int Relay1 = 7;
int Relay2 = 8;


This is the setup() function of the Arduino. Whatever code you write here will be executed once in the beginning when the program runs. After that only the code in the loop() function will run over and over again till you disconnect the power supply of the Arduino. The program will resume as soon as you power the Arduino again.

The Serial.begin(9600) function opens the serial port and sets data rate to 9600 bps. We will be opening the Serial communication only if we need to check the output on the connected computer. If you intend to  run this program without connecting to the computer, after the program and the circuit are tested. You can reduce the overhead by commenting out all the lines that begin with "Serial."
You do not need Serial () functions to be consuming your processing power if you are not looking at the output on the Serial Monitor a computer.

There is more to understand here. We will go into it in a later article.

We declare the pins, that is the Variables Relay1 and Relay2 to be outputs. Then immediately we turn them High. When we turn a pin HIGH using the digitalWrite() function, the LED is turned on if connected  with that port, but that is not the case with our relays. We are using Active Low type of relays. And we will turn then Off, to do that we neeed to send HIGH on the port.

void setup() {

Serial.begin(9600);

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);

digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
}


Now we come to the loop() function. Whatever code we write in this function runs over and over again till the Arduino has power. Now we will Turn the Relay On for 1 second one after the other  and then turn them off in the reverse order. You can notice the LEDs of the corresponding Relays turned on and the Clicking sound of the Relays as they take their turns.

void loop() {

  digitalWrite(Relay1, LOW);
  Serial.println("Relay 1 On");
  delay(1000);
  digitalWrite(Relay2, LOW);
  Serial.println("Relay 2 On");
  delay(1000);
  digitalWrite(Relay2, HIGH);
  Serial.println("Relay 2 Off");
  delay(1000);
  digitalWrite(Relay1, HIGH);
  Serial.println("Relay 1 Off");
  delay(1000);

}

In the last experiment, while working with DS3231 RTC, none of the libraries seem to work. I tried all the libraries of ds3231 available, then read all the possible error messages posted on the forums, after that I wondered if the  RTC module is faulty or what and I ordered another module and tested with the same results.

Then I ended up writing to all the authors of the respective ds3231 libraries asking them about my issues. Posted my questions on major forums but could not get any help.

And guess what in the end, just the Dupont wires that I connected the ds3231 RTC module with the Arduino were somehow loose/ no connection. I found that out when I decided to replace all the cables with a fresh set of cables, and that solved my problem for good.

This has prompted  me to create a section with my videos and articles where I will be posting some common errors and their symptoms so that you can easily detect and rectify the issue. There is one such blooper in this experiment also. I will add a GIF file that I have created here.




This happens when one of the wires that supply 5v from the external power supply to the relay module is not inserted properly or is faulty.  You will see the relay lights turning On and Off as you programmed, and the Serial monitor will display the output of your program, but you will not hear the Click of the Relays. It means the relays are not getting the power they need to make them work. Either try to re-insert the wires or try to replace them with another set of wires and your issue will be resolved.


Download the code from this link 



Featured Post

Creating Games in Scratch for ICSE Class 6

An Introduction to Creating Games in Scratch  for ICSE Class 6 Hello friends, I hope you have already read the previous post on An Int...