What is a relay? How can it be used in your project?

Introduction

A relay is an electrically controlled switch that can be switched on or off, letting current through or not. It can be controlled with low voltages, such as the 5V supplied by arduino pins.

It can be used to control high-voltage components, such as light bulbs on an Arduino board, using an external power supply.

It can also be used to control components for which the Arduino board does not have the necessary power.

If you’re into home automation, the relay can be very useful for controlling high-voltage components such as light bulbs, or even a coffee maker with an arduino board!

The relay is very easy to use, as it requires no library. Just declare the pin and you’re ready to control it!

What's the difference between a transistor and a relay?

A transistor and a relay can both be used to control a component that requires a higher voltage than the Arduino board can supply, i.e. above 5V. However, unlike a transistor, the relay can’t vary the signal at half its power, for example. You won’t be able to light your LED at half its intensity with a relay, for example. For that, you’ll need to use a transistor.

Always handle your circuit with the power off!

The relay is a component that is often used with an external power supply. You must therefore take care when handling your circuit to make sure that your voltage sources are disconnected.

Relay pins

The right-hand side of the relay connects to the Arduino board.

  • Ground: connects to the ground of the Arduino board.
  • Vcc: connects to the 5V of the Arduino board
  • Signal: connects to a GPIO on the Arduino board

The left-hand side of the relay corresponds to your component and the external power supply.

Depending on your needs, you’ll use the ground with the pin normally closed or normally open, or both.

The two relay modes

The relay operates in two possible modes: normally open and normally closed.

 

  • Normally open mode

To use this mode, you need to connect the ground of your high-voltage component to the pin on the far right. The external voltage source will be connected between the relay ground (middle pin) and the power supply of your high-voltage component.

In this configuration, if the pin is low, the relay will be an open circuit, i.e. your component will be switched off.

Conversely, if the pin is high, the relay is closed, i.e. the component is switched on.

  • Normally closed mode


To use this mode, you need to connect the ground of your high-voltage component to the relay’s ground. The external voltage source is connected to the relay’s “normally closed” pin.

In this configuration, if the pin is low, the relay is closed, i.e. your component will be switched on.

Conversely, if the pin is high, the relay is an open circuit, i.e. the component is switched off.

Blinking light

Now let’s see how to make a bulb light up and blink. To do this, you’ll need a power supply suitable for your bulb. In general, a bulb lights on 220V, so you’ll need to connect your relay to the mains.

Here’s the program to make the LED flash:

int pin_relay=2 ;
void setup(){
  pinMode(pin_relay, OUTPUT) ;
}

void loop(){
  digitalWrite(pin_relay, HIGH) ;
  delay(1000) ; // Wait 1 second

  digitalWrite(pin_relay, LOW) ;
  delay(1000) ; // Wait for 1 second
}

Starting a motor

Now we’ll see how to switch on a motor using a relay.

Here is the program to run the motor:

int relay =2 ;
void setup() {
pinMode(relay,OUTPUT) ;                  
}

void loop() {
digitalWrite(relay,HIGH) ;        
delay(1000) ;                    
digitalWrite(relay,LOW) ;            
delay(1000) ;            
}