This example shows how to use a relay module. If you are not familiar with mains wiring and the danger associated we recommend you use this relay to drive low-voltage, low-power circuits.
To switch on a device when it is normally off we will use the common terminal and the normally open terminal to connect the device as shown below. If we wanted to be able to actively switch off a device that was normally left on by default then we would use the common terminal and the normally closed terminals.
This diagram illustrates the current flows in three states. Firstly it shows that when there is no coil current (relay is off), then there is a circuit between the NC terminal and COM. Then when current flows there NC & COM circuit is broken and the COM and NO circuit is made. Once the current stops the coil is not energized and the NC and COM circuit is made and the COM / NO circuit is broken.
/* sketch
turn on a device using a relay
Connect relay module as follows:
+5V to +5V
GND to GND
IN to digital pin 2
*/
int relay = 2;
void setup() {
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
}
void loop() {
}