tutorial
Arduino Relay Control
One of the most
useful things you can do with an Arduino is control higher voltage
(120-240V) devices like fans, lights, heaters, and other
household appliances. Since the Arduino operates at 5V it can’t control
these higher voltage devices directly, but you can use a 5V relay to
switch the 120-240V current and use the Arduino to control the relay.
Introducing the
Relay Module
A
relay is an electrically operated switch of the mains voltage. It means that it
can be turned on or off, letting the current go through or not.
Controlling
a relay with the Arduino is as simple as controlling an output such as an
LED.
The
relay module is the one in the figure below.
This module has
Four channels (those blue cubes). There are other varieties with one, two and
eight channels.
In relation to mains voltage, relays have 3 possible connections:
1. COM: common
pin
2. NO (Normally open): There is no contact between the common pin and the normally open pin. So, when you trigger the relay, it connects to the COM pin and supply is provided to a load. (through electromagnetic induction)
3. NC (Normally closed): There is contact between the common pin and the normally closed pin. There is always a connection between the COM and NC pins, even when the relay is turned off. When you trigger the relay, the circuit is opened and there is no supply provided to a load.
If you want to control a lamp, for example, it is better to use a normally-open circuit, because we just want to light up the lamp occasionally.
Pin wiring
The connections between the relay module and the Arduino are really simple:
GND: goes to ground
IN1: controls the
first relay (it will be connected to an Arduino digital pin)
IN2: controls the
second relay (it should be connected to an Arduino digital pin if you
are using this second relay. Otherwise, you don’t need to connect it)
IN3: controls the
third relay.
IN4: controls the
fourth relay.
VCC: goes to 5V
Diagram:
Code:
Its very easy to
code the relay,here we’re going to glow led that is connected with Relay board.
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
delay(1000);
digitalWrite(4, HIGH);
delay(1000);
digitalWrite(4, LOW);
delay(1000);
digitalWrite(5, HIGH);
delay(1000);
digitalWrite(5, LOW);
delay(1000);
}
Connect the
pinMode 6 to IN1, pinMode 7 to IN2, pinMode 8 to IN3 and pinMode 9 to IN4
OUTPUT:
The four light’s will on and then it’ll off.
Hi
ReplyDelete