/* 4-Channel Relay Module โ€” Combined Sample Sketch Tishvi Docs ยท guide.tishvi.com Wiring (Arduino UNO R3): Relay VCC -> Arduino 5V Relay GND -> Arduino GND Relay IN1 -> Arduino D2 (controls K1) Relay IN2 -> Arduino D3 (controls K2) Relay IN3 -> Arduino D4 (controls K3) Relay IN4 -> Arduino D5 (controls K4) JD-VCC jumper: leave installed for this sketch (single 5V supply powers both the logic side and the relay coils). Remove it only if you're powering the coils from a separate 5V supply โ€” see the guide's "Understanding the JD-VCC Jumper" section. Output wiring (per channel, screw terminals): COM -> one side of your load's power source NO -> the load, for "on when triggered" behaviour (most common) NC -> the load, for "on until triggered" fail-safe behaviour SAFETY: This module can switch up to 250V AC / 10A. Disconnect mains power before wiring or rewiring the output side. Test with a low-voltage DC load (e.g. a 12V lamp on its own supply) before working with mains. Trigger polarity: Most boards built with this layout (opto-isolator + transistor driver, D1-D4 status LEDs) are LOW-level triggered โ€” LOW on an IN pin turns that relay ON. If your relays behave the opposite way, set RELAY_ACTIVE_LOW to false below. What it does: - On startup, runs a quick cycle test: K1 -> K2 -> K3 -> K4, one at a time, so you can confirm wiring and polarity. - After that, listens on Serial Monitor for '1', '2', '3', or '4' to toggle that channel individually. Baud rate: 9600. No external libraries needed. */ const int relayPins[4] = {2, 3, 4, 5}; // IN1..IN4 bool relayState[4] = {false, false, false, false}; // Flip to false if your board's relays trigger backwards from what you expect. const bool RELAY_ACTIVE_LOW = true; void relayWrite(int pin, bool on) { bool level = RELAY_ACTIVE_LOW ? !on : on; digitalWrite(pin, level ? HIGH : LOW); } void setup() { Serial.begin(9600); // Set every relay OFF immediately, before anything else runs, so a // briefly-floating pin during boot can't flash a relay on. for (int i = 0; i < 4; i++) { pinMode(relayPins[i], OUTPUT); relayWrite(relayPins[i], false); } Serial.println("4-Channel Relay Module ready."); Serial.println("Running cycle test: K1 -> K2 -> K3 -> K4..."); for (int i = 0; i < 4; i++) { Serial.print("K"); Serial.print(i + 1); Serial.println(" ON"); relayWrite(relayPins[i], true); delay(1000); relayWrite(relayPins[i], false); delay(300); } Serial.println("Cycle test done. Send 1, 2, 3, or 4 to toggle a relay."); } void loop() { if (Serial.available()) { char c = Serial.read(); if (c >= '1' && c <= '4') { int idx = c - '1'; relayState[idx] = !relayState[idx]; relayWrite(relayPins[idx], relayState[idx]); Serial.print("K"); Serial.print(idx + 1); Serial.println(relayState[idx] ? " -> ON" : " -> OFF"); } } }