A relay lets the Nano switch a separate circuit. This guide covers the low-voltage side: coil power from 5V, the trigger on D7, and making sure the relay starts off.
The test load is a 12 V lamp on COM/NO. That proves the wiring without touching mains. If you later switch a 230 V device, keep it within the relay’s rating and use a contactor for pumps and motors. An electrician should do that part.
Wiring
| Arduino Nano | 1-channel relay module (5 V coil) | Wire | Notes |
|---|---|---|---|
| — | COM | — | To 12 V DC supply (+) for a test lamp (not drawn) |
| — | NO | — | To 12 V lamp (+); lamp (−) to supply (−) (not drawn) |
| 5V | DC+ | red | One relay coil draws about 70 mA — fine from USB. |
| GND | DC− | black | |
| D7 | IN | yellow |
5 V coil part, 5 V board. The coil runs on 5 V, but the IN pin only drives an optocoupler LED. Pick a module rated for a 3.3 V trigger when using ESP32 or Pico — some 5 V-only modules never fully release from a 3.3 V HIGH.
Board quirks
- The Nano’s pins are 5 V, so a standard 5 V relay module switches cleanly — no “3.3 V trigger” module needed.
- Most blue modules are active-LOW. The sketch writes HIGH to D7 before
pinMode(OUTPUT), so the relay never clicks on during the bootloader’s first second. - Avoid D13 for relays: the bootloader blinks it at every reset.
- This bench test switches a 12 V lamp. A relay module may only switch a small 230 V load within its contact rating (a lamp, not a motor); pumps and motors go through a contactor. Any 230 V wiring is for a licensed electrician, with an MCB, RCCB and earth.
Code
// 1-channel 5 V relay module on an Arduino Nano: toggles every 5 s, or type on / off.
// DC+ → 5V, DC− → GND, IN → D7. Most blue modules are active-LOW (IN LOW = relay on).
// Low-voltage loads only on this bench test; 230 V wiring is for an electrician.
const uint8_t RELAY_PIN = 7;
const bool ACTIVE_LOW = true; // set false for a "high-level trigger" module
bool state = false;
unsigned long lastSwitch = 0;
void relay(bool on) {
state = on;
digitalWrite(RELAY_PIN, (on != ACTIVE_LOW) ? HIGH : LOW);
Serial.println(on ? F("Relay ON") : F("Relay OFF"));
lastSwitch = millis();
}
void setup() {
// Set the level before switching to OUTPUT so the relay doesn't click at power-up.
digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println(F("Type on / off, or wait for the 5 s cycle"));
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd == "on") relay(true);
else if (cmd == "off") relay(false);
}
if (millis() - lastSwitch >= 5000) relay(!state);
}// 1-channel 5 V relay module on an Arduino Nano: toggles every 5 s, or type on / off.
// DC+ → 5V, DC− → GND, IN → D7. Most blue modules are active-LOW (IN LOW = relay on).
// Low-voltage loads only on this bench test; 230 V wiring is for an electrician.
const uint8_t RELAY_PIN = 7;
const bool ACTIVE_LOW = true; // set false for a "high-level trigger" module
bool state = false;
unsigned long lastSwitch = 0;
void relay(bool on) {
state = on;
digitalWrite(RELAY_PIN, (on != ACTIVE_LOW) ? HIGH : LOW);
Serial.println(on ? F("Relay ON") : F("Relay OFF"));
lastSwitch = millis();
}
void setup() {
// Set the level before switching to OUTPUT so the relay doesn't click at power-up.
digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println(F("Type on / off, or wait for the 5 s cycle"));
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd == "on") relay(true);
else if (cmd == "off") relay(false);
}
if (millis() - lastSwitch >= 5000) relay(!state);
}Expected serial output
Type on / off, or wait for the 5 s cycle
Relay ON
Relay OFFType on / off, or wait for the 5 s cycle Relay ON Relay OFF
Common errors
Relay clicks but the lamp doesn’t switch
Load wired to NC instead of NO, or COM not in the circuit. Full fix.
Relay is ON when it should be OFF
Your module is high-level trigger. Set ACTIVE_LOW = false.
Nano resets when the relay switches
Coil back-EMF or an inductive load. Use a module with an opto-isolator and flyback diode, and keep load wiring away from the Nano.