How to use a relay module with Arduino
Hello everyone, this is a tutorial on how to use a relay module with Arduino, here I used a 2 channels relay module with an Arduino Uno, you can use the same thing to control other relay modules with 4, 6 or plus channels modules.
Why using a relay? A relay permits you to easy control “high” voltage appliances, by high voltage I mean 110VAC/220VAC, depending on your relay type, it’s module that will permit you to control lights, lamps and other appliances in you house in case you want to make things automated or programmed and controlled by your Arduino.
Using a relay module is far better than a relay on its own, when used with Arduino, with module you actually control an optocoupler, which means the relay circuit that contains a coil is isolated from the Arduino circuit, of course only when powered externally, check the N.B below.
Wiring:
There’s isn’t that much going on with the straight usage, the module has multiple pins the important are GND/IN1/IN2/VCC, the first and last are for power, meanwhile the IN1/IN2 are for controlling the relays (Activate or not), the relays have also two positions, NO/NC: Normally Open and Normally Closed pins.
Important N.B:
Here I kept the jumper between Vcc and Jd-vcc wich mean that the optocouplers and the coils are wired together and they are powered from the arduino directly, if you are using other sensors in the same time you can may face some power issues, so you have to remove the jumper, and then wire “JdVcc” and the other “Gnd” from another power source, for example a 9V battery.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//This is code is to control 2 channels relay module //It turns on on channel for 3 seconds then turn it off and vice versa //This code will help you to know how the relays work and how to activate each one //FB/Youtube: @SurtrTech int in1 = 2; //Declaring pins used int in2 = 3; void setup() { pinMode(in1, OUTPUT); pinMode(in2, OUTPUT); } void loop() { digitalWrite(in1, LOW); digitalWrite(in2, HIGH); delay(3000); digitalWrite(in1, HIGH); digitalWrite(in2, LOW); delay(3000); } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
One thought on “How to use a relay module with Arduino” Leave a comment ›