AdSense

Monday, July 10, 2017

6 - Arduino: Relay


ARDUINO: RELAY

- Layout for this exercise:




1 - Introduction

A relay is an electromechanical switch that can control much more voltage and current than an Arduino pin.

- For instance, a relay could be useful to connect an Arduino to a device like a lamp or any other electrical machine of 120 V.

- A relay consists of a coil of wire and switch contacts. When power is applied to the coil it becomes magnetized and pulls the switch contacts closed. 

 A transistor is used to handle the current because in this case much more power is needed than an Arduino can provide.

-  There are 3 contact pins used to connect the device handled by the relay:

  • COM = common            <- connected to power
  • NC = Normally Closed  <- connected to the yellow LED
  • NO = Normally Open    <- connected to the red LED

- Also, there are 2 more pins for the coil used to control the relay. One side is connected to the collector of the transistor and the other one to 5 V.




- The relay can be in 2 different modes:

  • OFF = COM is connected to NC
  • ON = COM is connected to NO

- Also, a diode is used to protect the transistor from the voltage spike generated when power is disconnected from the coil, what could damage the transistor. One leg of the diode is connected to the transistor collector and the other to the coil.


2 - Code

- Arduino sketch:




- Let's analyze the code.


- First of all, the transistor base is assigned to the Arduino pin 4 in output mode:



- Now, the transistor base drives current (pin 4 is set to HIGH) energizing the coil and the switch contacts are closed. As a consequence COM connects to NO and the relay is ON for 2 seconds (the red LED lights). 






- Then, when the transistor base is LOW the COM connects to NC and the relay puts into OFF mode for another 2 seconds (the yellow LED lights):



3 - Testing the circuit and the code

- In this video the circuit and the code are tested. The 2 LEDs light alternatively every two seconds and the relay contacts "click" is heard very clearly: