Difference between revisions of "Relay"

From Interaction Station Wiki
Jump to navigation Jump to search
Line 34: Line 34:
  
 
==Code example==
 
==Code example==
<code>
+
{code}
 
/*
 
/*
 
  * Created by ArduinoGetStarted.com
 
  * Created by ArduinoGetStarted.com
Line 57: Line 57:
 
   delay(1000);
 
   delay(1000);
 
}
 
}
</code>
+
{code}
 
 
  
 
[[Category:Electronics]]
 
[[Category:Electronics]]

Revision as of 11:14, 3 October 2024

Relay

Introducing the Relay Module

Relay02.jpg

A relay is an electrically operated switch of 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.

Notice the writing on the module terminals

COM: common pin
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
NC (Normally Closed): there is contact between the common pin and the normally closed pin. There is always 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.


Wiring

1channel-relay-fritzing-1.jpg

it is very straightforward as you can see. But always double check the relay module connections before you plug things together.

GND: goes to ground
VCC: goes to 5V
IN1: controls the relay (it is connected to an Arduino digital pin)

OK, but maybe you wan to control more than one thing??


then no porblem
6-Channel-12V-Relay-Module.jpg here are 16 channels:) Wiring is the same, you will just need 16 digital pins on the Arduino

Code example

{code} /*

* Created by ArduinoGetStarted.com
* This example code is in the public domain
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-relay
*/

// constants won't change const int RELAY_PIN = 2; // Arduino pin, which connects to the IN pin of relay

// the setup function runs once when you press reset or power the board void setup() {

 // initialize digital pin as an output.
 pinMode(RELAY_PIN, OUTPUT);

}

// the loop function runs over and over again forever void loop() {

 digitalWrite(RELAY_PIN, HIGH);
 delay(1000);
 digitalWrite(RELAY_PIN, LOW);
 delay(1000);

} {code}