DEV Community

Cover image for Arduino UNO R4 - 2-Channel Relay Module
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino UNO R4 - 2-Channel Relay Module

A 2-channel relay module lets your Arduino UNO R4 switch two high-voltage devices, like a pump and a fan, from code. Instead of wiring up two separate single relay modules, you get both channels on one board, each controlled by its own digital pin.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino UNO R4 - 2-Channel Relay Module

What you need

Wiring

Arduino UNO R4 - 2-Channel Relay Module wiring diagram

The module has three groups of pins: power, signal and output. DC+ and DC- take power in, from the board's 5V and GND for a quick test. IN1 and IN2 take the control signal, wired to D2 and D3 on the Arduino UNO R4. Each relay also has three output pins, NCx (normally closed), NOx (normally open) and COMx (common), where you connect the actual high-voltage device.

For a final build, swap the bare jumper wires for a screw terminal shield, which turns the header pins into screw terminals. If the 5V pin on the board already powers other parts, give the relay module its own 5V adapter instead, and still tie its GND to the Arduino's GND.

How it works

Each relay is really a switch flipped by a small electromagnet, which is why it can control a high-voltage circuit from a low-voltage signal pin without the two ever touching directly. The module also has two jumpers, one per relay, that set whether a LOW or a HIGH signal turns that relay on, so you can configure each channel differently.

Code

The sketch sets both relay pins as outputs with pinMode(), then loops forever: it turns both relays on with digitalWrite(), waits 2 seconds with delay(), turns them off, waits 2 more seconds, and prints a message to the Serial Monitor at each step so you can follow what's happening without watching the hardware.

arduino-uno-r4-2-channel-relay-module.ino

/*
 * 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-2-channel-relay-module
 */

#define PIN_RELAY_1  2 // The Arduino UNO R4 pin connected to the IN1 pin of relay module
#define PIN_RELAY_2  3 // The Arduino UNO R4 pin connected to the IN2 pin of relay module

// The setup function runs once on reset or power-up
void setup() {
  Serial.begin(9600);

  // initialize digital pin as an output.
  pinMode(PIN_RELAY_1, OUTPUT);
  pinMode(PIN_RELAY_2, OUTPUT);
}

// The loop function repeats indefinitely
void loop() {
  Serial.println("Turned on both relays");
  digitalWrite(PIN_RELAY_1, HIGH);
  digitalWrite(PIN_RELAY_2, HIGH);
  delay(2000);

  Serial.println("Turned off both relays");
  digitalWrite(PIN_RELAY_1, LOW);
  digitalWrite(PIN_RELAY_2, LOW);
  delay(2000);
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

If a relay does the opposite of what your code says, your module is set to low-level trigger, where LOW turns it on and HIGH turns it off. Either move that relay's jumper to high-level trigger or swap HIGH and LOW in your sketch. If the Arduino resets or behaves oddly when the relays click, the two relay coils are likely pulling more current than the board's 5V pin should supply, so move the module to its own 5V adapter and keep the grounds connected together.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-2-channel-relay-module

Top comments (0)