Arduino UNO R4 - Relay

This tutorial instructs you on how to use an Arduino UNO R4 and a relay to control the on and off states of devices that use high-voltage power, such as light bulbs, fans, electromagnetic locks, and linear actuators. In detail, we will learn:

Arduino UNO R4 and relay

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Relay
1×LED Strip
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino UNO R4
1×(Recommended) Breadboard Shield For Arduino UNO R4
1×(Recommended) Enclosure For Arduino UNO R4

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Relay

A relay is a switch that you can program, controlled by devices like Arduino UNO R4 or other micro-controllers. It lets you turn devices on or off automatically, especially those that use high voltage or high current.

The relay is a bridge between Arduino UNO R4 and high-voltage devices.

WARNING

When working on projects connected to main electricity, it is critical to have proper knowledge to avoid shocking yourself. Safety is very important. If you are not completely sure about what you are doing, please do not attempt to handle anything. Instead, seek help from someone experienced.

We recommend using a DC device (up to 24V) for testing, even though some relays can work with both DC and AC devices.

Relay Pinout

Relay Pinout

A relay consists of two sets of pins: the input pins, which use low voltage, and the output pins which use high voltage.

  • The pins in the input group are connected to the Arduino UNO R4 and include three pins:
    • DC- pin: connect this to GND (0V).
    • DC+ pin: connect this to VCC (5V).
    • IN pin: this accepts the control signal from the Arduino UNO R4.
  • The pins in the output group connect to the high-voltage device and also have three pins (usually found in a screw terminal):
    • COM pin: this is the common pin used in both the normally open mode and the normally closed mode.
    • NO pin: this is the normally open pin. It is used in the normally open mode.
    • NC in: this is the normally closed pin. It is used in the normally closed mode.

    In practice, we usually do not use all the pins in the high voltage group. We only use two of them.

    • For normally open mode, we use only the COM pin and the NO pin.
    • For normally closed mode, we use only the COM pin and the NC pin.

    Also, if the relay can use both LOW and HIGH level triggers, there is typically a jumper to choose either LOW level trigger or HIGH level trigger.

    ※ NOTE THAT:

    Different manufacturers may arrange the pins on the relay module in different orders. Always check and follow the labels on the relay to connect it correctly. Make sure to look carefully!

    How to Connect the High Voltage Device to Relay

    How to connect relay

    How It Works

    A relay might works in different ways depending on the manufacturer and how the user installs it.

    The input mode: There are two input modes that make the relay function oppositely:

    • LOW level trigger mode
    • HIGH level trigger mode

    The output mode: There are two output modes that make the relay work oppositely:

    • normally open mode
    • normally closed mode.

    The term “normally” refers to the condition when the "IN pin" is connected to "LOW (0V)". Let's start with some quick information:

    • The normally open mode and normally closed mode work in opposite ways.
    • Most relay modules support both normally open and normally closed modes.
    • The LOW level trigger and HIGH level trigger modes work in opposite ways.
    • Not every relay module supports both LOW level trigger and HIGH level trigger modes.
    • At any given time, the relay module can operate in only one mode, either LOW level trigger or "HIGH level trigger."

    The combination of input modes and output modes leads to many use cases. For beginners, we suggest choosing HIGH level trigger mode and normally open mode.

    The LOW level trigger and HIGH level trigger modes function in opposite ways. Next, we will describe the HIGH level trigger mode in detail. The LOW level trigger operates in the opposite manner.

    HIGH Level Trigger - Normally Open Mode

    To set this mode, connect the high voltage device to both the COM pin and the NO pin.

    • When the IN pin is connected to LOW (0V), the switch is open and the device is OFF.
    • When the IN pin is connected to HIGH (5V), the switch is closed and the device is ON.
    How Relay Works - Normally Open

    HIGH Level Trigger - Normally Closed Mode

    To use this mode, connect the device with high voltage to the COM pin and the NC pin.

    • When the IN pin is connected to LOW (0V), the switch is closed. This means the device is ON.
    • When the IN pin is connected to HIGH (5V), the switch is open. This means the device is OFF.
    How Relay Works - Normally Closed

    Summary

    Input modes Output Modes IN pin (programmable) Output pins Relay state Device state
    HIGH Trigger Normally Open LOW COM and NO pin ⇒ open OFF
    HIGH Trigger Normally Open HIGH COM and NO pin ⇒ closed ON
    HIGH Trigger Normally Closed LOW COM and NC pin ⇒ closed ON
    HIGH Trigger Normally Closed HIGH COM and NC pin ⇒ open OFF
    LOW Trigger Normally Open LOW COM and NO pin ⇒ closed ON
    LOW Trigger Normally Open HIGH COM and NO pin ⇒ open OFF
    LOW Trigger Normally Closed LOW COM and NC pin ⇒ open OFF
    LOW Trigger Normally Closed HIGH COM and NC pin ⇒ closed ON

    There can be as many as 8 use cases. This might seem overwhelming. But if you are a beginner, you only need to focus on the first two cases. These involve the HIGH level trigger and normally open settings. This tutorial will mainly cover these two cases.

    Arduino UNO R4 - Relay

    The Arduino UNO R4 uses a relay to control a device that operates at high voltage.

    To control a relay is easy. We just need:

    • Attach the pin from the Arduino UNO R4 to the IN pin on the relay.
    • Program the pin to be LOW or HIGH to operate the relay.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Relay

This image is created using Fritzing. Click to enlarge image

How To Program For Relay

  • Set up the digital output mode for an Arduino UNO R4 pin using the pinMode() function. For instance, for pin 3:
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
digitalWrite(3, HIGH);

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-relay */ #define RELAY_PIN 3 // The Arduino UNO R4 pin connected 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(500); digitalWrite(RELAY_PIN, LOW); delay(500); }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code and open it in Arduino IDE
  • Click the Upload button in Arduino IDE to send the code to Arduino UNO R4
  • Check the LED strip: it should be blinking

Video Tutorial

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!