Arduino Mega - Relay

This guide shows you how to use an Arduino Mega and a relay to turn high-voltage devices on and off. These devices include light bulbs, fans, electric locks, and linear actuators. Here’s what we will learn:

Arduino Mega and relay

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Relay
1×LED Strip
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires

Or you can buy the following 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 you can program. It is controlled by devices like Arduino Mega or other small computers. It lets you turn things on or off automatically, especially things that use a lot of power or high voltage.

A relay is a link between an Arduino Mega and high-voltage devices.

WARNING

When you work with power from the mains, you need to know what you’re doing to avoid getting an electric shock. Safety is very important. If you’re not sure about what you’re doing, don’t try to handle anything. Instead, ask for help from someone who has experience. We recommend using a DC device (up to 24V) for testing, even though some relays can work with both DC and AC.

Relay Pinout

Relay Pinout

A relay has two groups of pins: the input pins use low voltage, and the output pins use high voltage.

  • Input pins connect to the Arduino Mega. There are three pins:
    • DC- pin: connect to ground (0V).
    • DC+ pin: connect to +5V (VCC).
    • IN pin: receives the control signal from the Arduino Mega.
  • Output pins connect to the high-voltage device. There are three pins (usually in a screw terminal):
    • COM pin: the common pin used in both normally open and normally closed modes.
    • NO pin: the normally open pin. Used in normally open mode.
    • NC pin: the normally closed pin. Used in normally closed mode.

    Usually we don't use all the pins in the high-voltage group. We only use two of them.

    • In normally open mode, only the COM and NO pins are used.
    • In normally closed mode, only the COM and NC pins are used.

    Also, if the relay can use both low-level and high-level triggers, there is usually a small jumper to choose between them.

    ※ NOTE THAT:

    Relays from different makers may place the pins in different orders. Always check the labels on the relay and connect it the right way. Look carefully!

    How to Connect the High Voltage Device to Relay

    How to connect relay

    How It Works

    A relay can work in different ways, depending on who makes it and how you install it.

    The input mode: There are two input modes that make the relay work in opposite ways.

    • Low-level trigger mode
    • High-level trigger mode

    Output mode: There are two modes that make the relay work in opposite ways.

    • Default open mode
    • Default closed mode

    The word "normally" means the situation when the IN pin is connected to LOW (0V). Let's start with some quick information:

    • Normally open and normally closed modes work in opposite ways.
    • Most relay modules support both normally open and normally closed modes.
    • 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 time, the relay module can operate in only one mode, either low-level trigger or high-level trigger.

    Using input modes and output modes gives many uses. For beginners, we recommend choosing high-level trigger mode and normally open mode.

    The low level trigger and high level trigger modes work in different ways. Next, we will explain the high level trigger mode in detail. The low level trigger works in the opposite way.

    HIGH Level Trigger - Normally Open Mode

    To enable this mode, connect the high voltage device to both the COM and NO pins.

    • If the input pin is low (0 volts), the switch is open and the device is off.
    • If the input pin is high (5 volts), 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 high-voltage device to the COM and NC pins.

    • When the input is at low (0 V), the switch is closed. The device is ON.
    • When the input is at high (5 V), the switch is open. 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 eight use cases. This may seem like a lot. 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 mainly covers these two cases.

    Arduino Mega - Relay

    The Arduino Mega uses a relay to control a device that runs on high voltage.

    It is easy to control a relay. We only need:

    • Connect the Arduino Mega pin to the relay's IN pin.
    • Set the pin to LOW or HIGH to turn the relay on or off.

Wiring Diagram

The wiring diagram between Arduino Mega Relay

This image is created using Fritzing. Click to enlarge image

How To Program For Relay

  • Set a pin on the Arduino Mega as an output using the pinMode() function. For example, pin 3:
pinMode(3, OUTPUT);
  • Set the pin to low (0 volts) using the digitalWrite() function.
digitalWrite(3, LOW);
  • Set the pin to 5V using the digitalWrite() function.
digitalWrite(3, HIGH);

Arduino Mega Code

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-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 steps one by one.

  • Connect the parts following the diagram.
  • Connect the Arduino Mega board to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the correct board (Arduino Mega) and the COM port.
  • Copy the code and paste it into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • Check the LED strip; it should blink.

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!