Wiring guide

How to wire a relay module to an Arduino Mega

To wire a relay module to an Arduino Mega, connect VCC to 5V, GND to GND and IN to pin 2. The Arduino Mega drives IN at 5 V and the module’s own transistor carries the coil current, so the pin only supplies a few milliamps. The sketch below uses no library beyond what the board includes. It switches the relay on for a second and off for a second, printing a line each time.

  • 5 V logic
  • Library: none
  • 3 wires
Arduino Mega · relay modulelive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

Relay module to Arduino Mega wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

Relay module pinArduino Mega pinWire colorWhy
VCC5VredThe coil is a 5 V coil; it takes about 70 mA, more than any pin can give.
GNDGNDblueCommon ground.
INpin 2amberA logic signal only: the module’s transistor switches the coil.
COM, NO, NCThe loadn/aThe switched side: COM is the arm, NO is open until the relay pulls in, NC is closed until then.
Voltage

Power and logic levels for relay module and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The relay module takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

The Arduino Mega drives IN at 5 V and the module’s own transistor carries the coil current, so the pin only supplies a few milliamps. The simulated relay triggers on HIGH; many blue modules trigger on LOW, set by the part’s trigger property.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: none, the sketch drives IN with digitalWrite(). This is the sketch the circuit above runs.

Relay on Arduino Mega pin 2
const int RELAY_PIN = 2;   // IN, pin 2

void setup() {
  Serial.begin(9600);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);      // released at start
}

void loop() {
  digitalWrite(RELAY_PIN, HIGH);     // pull in: COM to NO
  Serial.println("Relay on");
  delay(1000);
  digitalWrite(RELAY_PIN, LOW);      // release: COM back to NC
  Serial.println("Relay off");
  delay(1000);
}
Output

What you should see

The relay clicks closed and open. The serial monitor, at 9600 baud, prints Relay on when the coil is energized and Relay off when it is released.

The switched side, COM, NO and NC, is isolated from the board: wire the load there, never to the board’s pins.

The serial monitor prints this, recorded from the circuit above running for two and a half seconds:

Relay on
Relay off
Relay on
Pitfalls

Common mistakes wiring a relay module to an Arduino Mega

Active low modules

Many cheap relay boards switch when IN goes LOW, so the sketch seems inverted and the relay is on at boot. Check the board, and in the editor set the relay’s trigger property to low to match.

Driving a coil straight from a pin

A bare relay coil wants 70 mA or more and kicks back a voltage spike when it releases. On a module the transistor and the flyback diode are already there; a bare relay needs both, and never a direct pin.

Switching mains without care

The contacts are rated for mains, but the board and the screw terminals are not a safe enclosure. Learn on a 12 V lamp or a motor, and leave mains to a properly rated, enclosed module.

Build your own relay module project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.