Skip to content

SadeBros/RelayModule

Repository files navigation

Table of contents

  1. What is RelayModule Library? If I use it, how easier life will be ?
  2. Adding RelayModule Library to the Arduino Libraries Folder
  3. Importing RelayModule Library to the Arduino Project Source Code
  4. How to use RelayModule Library ?
    1. Creating RelayModule Instance
    2. Setting Pins and Modes of Relays
    3. All Functions of RelayModule
  5. Talk Is Cheap, Show Me The Code

RelayModule Library for Arduino

RelayModule


O. What is RelayModule Library? If I use it, how easier life will be ?

If you have made some Arduino project with Relay Modules, you probably wrote some scattered code for relays.

Scattered Code Example 1: You should initalize all the pins and modes for each relay.

//Those below codes will help you to see how bad and untidy the code looks.
int Relay1 = 5;
int Relay2 = 4;
int Relay3 = 3;
int Relay4 = 2;
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);

Scattered Code Example 2: You should know logic type of the Relay Module.
"When a relay is activated?" question will give the answer about logic type.
If a relay is activated with digitalWrite(pin, LOW), that means Relay Module is working with LOW logic.

//As an example, if you want to activate all relays on the Relay Module, you should write:
digitalWrite(Relay1, LOW);
digitalWrite(Relay2, LOW);
digitalWrite(Relay3, LOW);
digitalWrite(Relay4, LOW);

Scattered Code Example 3:
What if you have 8 Relay Module, you must do all the above stuffs for each relay.
What if you are working with more than one Relay Module, how many relay numbers will be waiting you to initalize them?
The more relay you have, the more scattered your source code will be.

If I use it, how easier life will be ?

RelayModule Library will help you to write neat code for relay modules.
It will reduce the confusion and enhance the code readability .
You can create RelayModule instance in your Arduino source code, and initalize each relay easily.
Without thinking about "Should I write LOW or HIGH to activate it?", you will make relays activate or deactivate easily.


1. Adding RelayModule Library to the Arduino Libraries Folder

After downloading RelayModule folder, you should place it to the libraries folder which should be located in the Arduino folder.

For me, this libraries folder is located in:
/Users/safakakinci/Documents/Arduino/libraries/RelayModule
Adding_RelayModule_Library





2. Importing RelayModule Library to the Arduino Project Source Code

On the menu bar, you are going to see Include Library which is located under the Sketch tab.
Click RelayModule which is at the bottom of Include Library pane.

Importing_RelayModule_Library


After clicking it, those two lines below automatically will be added to the source code.
Importing_RelayModule_Library


3. How to use RelayModule Library ?

3.1 Creating RelayModule Instance

RelayModule module(4, LOW);
We have specified that there are 4 relays on this Relay Module and it is working with LOW logic.
Working with LOW logic specifies that relays will be activated when digitalWrite(pin, LOW) is executed.

3.2 Setting Pins and Modes of Relays

module which we have created above(3.1) has 4 relays. We should set pin number and pin mode for each relay.
As an example, look at the figure below! Arduino Uno and 4 Relay Module are connected.
Arduino_Uno_4_RelayModule_Example

If we have those connections, we need to set the Relay Module's pins as:
module[1].setPinNumber(5).setPinMode(OUTPUT);     1st relay (IN1) is connected to the Arduino's Digital5 pin. Green.
module[2].setPinNumber(4).setPinMode(OUTPUT);     2nd relay (IN2) is connected to the Arduino's Digital4 pin. Brown.
module[3].setPinNumber(3).setPinMode(OUTPUT);     3rd relay (IN3) is connected to the Arduino's Digital3 pin. Yellow.
module[4].setPinNumber(2).setPinMode(OUTPUT);     4th relay (IN4) is connected to the Arduino's Digital2 pin. Orange.

module[1].setPinNumber(5).setPinMode(OUTPUT);
module[1] specifies the module's first relay. (Be careful not try to reach first relay with module[0] )
setPinNumber(5) specifies that relay is connected with Arduino's Digital5 pin.
setPinMode(OUTPUT) specifies that Arduino's Digital5 pin will be used on OUTPUT mode.

3.3 All Functions of RelayModule

Functions       Example         Explanation
module(channelNumber, logic); RelayModule module( 4, LOW ); It is 4 channel relay module which is working with LOW logic.
module.activate(relayNumber); module.activate( 1 ) ; Activates the first relay (IN1).
module.activateAll(); module.activateAll(); Activates all relays on the Relay Module.
module.deactivate(relayNumber); module.deactivate( 4 ) ; Deactivates the fourth relay. (IN4)
module.deactivateAll(); module.deactivateAll( ) ; Deactivates all relays on the Relay Module.
module.getStatus(); module.getStatus( ) ; Status: 7
7 = 0 + 4 + 2 + 1
Specifies that Relay 1,2, 3 are active and Relay4 is deactive.
module[relayNumber].getStatus(); module[3].getStatus( ) ; Returns the status of third relay (IN3) as integer.

4. Talk Is Cheap, Show Me The Code

RelayModuleExample