> ## Documentation Index
> Fetch the complete documentation index at: https://learn.pcbcupid.com/llms.txt
> Use this file to discover all available pages before exploring further.

> Wire the G-Mod 2-channel relay module to a Glyph ESP32 development board and switch two independent high-voltage loads from Arduino digital pins.

# 2-Channel Relay

export const ShopHeader = ({title, url}) => <div style={{
  display: 'flex',
  justifyContent: 'flex-end',
  alignItems: 'center',
  width: '100%',
  marginBottom: '1rem',
  gap: '20px'
}}>
    <a href={url} target="_blank" rel="noopener noreferrer" style={{
  display: 'inline-flex',
  alignItems: 'center',
  backgroundColor: '#00b894',
  color: '#fff',
  padding: '8px 18px',
  borderRadius: '8px',
  textDecoration: 'none',
  fontSize: '0.95rem',
  fontWeight: 600,
  whiteSpace: 'nowrap',
  flexShrink: 0
}}>
      Shop now
    </a>
  </div>;

export const ImgGallery = ({images, modelUrl}) => {
  var [active, setActive] = useState(0);
  var [show3D, setShow3D] = useState(false);
  useEffect(function () {
    if (show3D || images.length <= 1) return;
    var t = setTimeout(function () {
      setActive(function (p) {
        return (p + 1) % images.length;
      });
    }, 3500);
    return function () {
      clearTimeout(t);
    };
  });
  var img = images[active];
  return <div style={{
    position: 'relative',
    background: 'linear-gradient(to bottom, #fff 0%, #fff 80%, #f8f8f8 100%)',
    borderRadius: '32px',
    border: '1px solid rgba(0,0,0,0.08)',
    overflow: 'hidden',
    marginTop: '0.75rem',
    marginBottom: '1.5rem'
  }}>
      <div style={{
    position: 'relative',
    height: '450px',
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    background: '#fff',
    padding: '1rem'
  }}>
        {show3D && modelUrl ? <div ref={function (el) {
    if (!el || el._mv) return;
    el._mv = true;
    var s = document.createElement('script');
    s.src = "https://ajax.googleapis.com/ajax/libs/model-viewer/4.2.0/model-viewer.min.js";
    s.type = "module";
    s.onload = function () {
      var mv = document.createElement('model-viewer');
      mv.setAttribute('src', modelUrl);
      mv.setAttribute('camera-controls', '');
      mv.setAttribute('auto-rotate', '');
      mv.style.width = "100%";
      mv.style.height = "100%";
      mv.style.backgroundColor = "#f8f8f8";
      el.innerHTML = "";
      el.appendChild(mv);
    };
    document.head.appendChild(s);
  }} style={{
    width: "100%",
    height: "100%"
  }} /> : <img src={img && img.url} alt={img && img.label} loading="lazy" style={{
    maxWidth: '100%',
    maxHeight: '100%',
    objectFit: 'contain',
    borderRadius: '8px'
  }} />}
      </div>
      <div style={{
    display: 'flex',
    justifyContent: 'center',
    gap: '12px',
    padding: '1rem',
    background: '#eee',
    borderTop: '1px solid rgba(0,0,0,0.08)'
  }}>
        {images.map(function (img, idx) {
    return <button key={img.id || idx} onClick={function () {
      setActive(idx);
      setShow3D(false);
    }} style={{
      padding: '0.6rem 1.5rem',
      borderRadius: '12px',
      border: !show3D && idx === active ? '1px solid #00b894' : '1px solid rgba(0,184,148,0.4)',
      background: !show3D && idx === active ? '#00b894' : '#fff',
      color: !show3D && idx === active ? '#fff' : '#000',
      cursor: 'pointer',
      fontSize: '0.75rem',
      fontWeight: 700,
      textTransform: 'uppercase',
      fontFamily: 'inherit'
    }}>
              {img.label}
            </button>;
  })}
        {modelUrl ? <button onClick={function () {
    setShow3D(true);
  }} style={{
    padding: '0.6rem 1.5rem',
    borderRadius: '12px',
    border: show3D ? '1px solid #00b894' : '1px solid rgba(0,184,148,0.4)',
    background: show3D ? '#00b894' : '#fff',
    color: show3D ? '#fff' : '#000',
    cursor: 'pointer',
    fontSize: '0.75rem',
    fontWeight: 700,
    textTransform: 'uppercase',
    fontFamily: 'inherit'
  }}>
            3D VIEW
          </button> : null}
      </div>
    </div>;
};

<ShopHeader title="2-Channel Relay Module" url="https://shop.pcbcupid.com/product/gm002/" />

<ImgGallery modelUrl="https://raw.githubusercontent.com/pcbcupid/pcbcupid-hardware-libraries/main/pcbcupid_3dmodels/glb/gmod-relay-2ch.glb" images={[{url: "https://files.pcbcupid.com/Documentation/Boards/g-mod/2-ch-relay/relay-2ch-FB-view.avif", label: "ILLUSTRATION VIEW", id: "illustration"}, {url: "https://files.pcbcupid.com/Documentation/Boards/g-mod/2-ch-relay/pcbcupid-relay-2ch-topview.avif", label: "BOARD VIEW", id: "board"}]} />

A **2-channel relay** for controlling two independent high-power loads simultaneously using low-power control signals from microcontrollers like Arduino, [GLYPH](https://shop.pcbcupid.com/product-category/development-boards/), or even Raspberry Pi. Here's how it works:

### Components

* Two Relays: Each relay can independently control a high-power (Both AC / DC) device.
* Control Pins: There are two input pins (IN1 and IN2) for controlling each relay.
* Power Supply: A single power source is used for both relays **5V**
* Protection : Has 2 Optocoupler to electrical isolation between the control and relay circuits for both channels.

### Pin Configuration

* VCC: Power supply for the module (5V).
* GND: Ground connection.
* IN1: Control signal for Relay 1.
* IN2: Control signal for Relay 2.
* COM1, NO1: Relay 1 switching terminals.
* COM2, NO2: Relay 2 switching terminals.

### Relay Contacts

* COM (Common): The input terminal.
* NO (Normally Open): Open by default, closes when the relay is activated.

### Working

* Each relay channel works independently, following the same principles as a single-channel relay:
  * When Control Signal is LOW (0V):

    The associated transistor remains off.
    The corresponding relay coil is de-energized.
    The relay's Normally Open (NO) contact stays open, and the Normally Closed (NC) contact remains closed.

  * When Control Signal is HIGH (3.3V/5V):

    The associated transistor turns on, allowing current to flow through the relay coil.
    The coil energizes, creating a magnetic field that pulls the armature.
    The relay's NO contact closes, completing the circuit for the connected load, while the NC contact opens.

  * Operation Sequence for Two Relays

    * IN1 HIGH: Activates Relay 1 to control Load 1.
    * IN2 HIGH: Activates Relay 2 to control Load 2.
    * Both IN1 and IN2 HIGH: Activates both relays simultaneously to control both loads.
    * Both IN1 and IN2 LOW: Deactivates both relays, disconnecting both loads.

### Applications

* Home Automation: Controlling lights and fans independently.
* Industrial Automation: Managing two separate devices or motors.
* DIY Projects: Operating appliances remotely with microcontroller-based projects.

This guide will help you interface a 2 Channel Relay assuming you are using [GLYPH-C3](https://learn.pcbcupid.com/boards/glyph-c3/overview)(but any [GLYPH development board](https://shop.pcbcupid.com/product-category/development-boards/) from the ESP32 Series should work)

## Step 1: Hardware Required

1. Glyph Board
2. GMOD 2 Channel Relay Module

## Step 2: Circuit Diagram

![pcbcupid\_2chanrel](https://files.pcbcupid.com/Documentation/Boards/modules/2_channel_relay/2chanrelay_circuit.avif)

## Step 3: Code Setup

1. **Open Arduino IDE.**
2. **Copy and paste the following code into the Arduino IDE:**

```cpp theme={null}
// Pin definitions for 2-Channel Relay Module
const int RELAY_1 = 3;  // GPIO2 for first relay
const int RELAY_2 = 2;  // GPIO3 for second relay

void setup() 

{
  // Initialize serial communication
  Serial.begin(115200);
  
  // Configure relay pins as outputs
  pinMode(RELAY_1, OUTPUT);
  pinMode(RELAY_2, OUTPUT);
  
  // Initialize Both Relay Channels to OFF state (relays are typically Active LOW- Means they are OFF when a HIGH Signal is Applied and vice-versa)
  digitalWrite(RELAY_1, HIGH);
  digitalWrite(RELAY_2, HIGH);
  
  Serial.println("Relay Module Initialized"); //Print that Relay Module is Initialized
}

void loop() 
{
  // Example control sequence
  
  // Turn ON Relay 1
  digitalWrite(RELAY_1, LOW);
  Serial.println("Turning on Relay 1");
  delay(2000);
  
  // Turn OFF Relay 1
  digitalWrite(RELAY_1, HIGH);
  Serial.println("Turning off Relay 1");
  delay(1000);
  
  // Turn ON Relay 2
  digitalWrite(RELAY_2, LOW);
  Serial.println("Turning on Relay 2");
  delay(2000);
  
  // Turn OFF Relay 2
  digitalWrite(RELAY_2, HIGH);
  Serial.println("Turning off Relay 2");
  delay(1000);
}

// Helper functions for relay control
void turnOnRelay(int relayPin) 
{
  digitalWrite(relayPin, LOW);
}

void turnOffRelay(int relayPin) 
{
  digitalWrite(relayPin, HIGH);
}

void toggleRelay(int relayPin) 
{
  digitalWrite(relayPin, !digitalRead(relayPin));
}

```

## Step 4: Upload the Code

1. **Connect the Board**

* Connect your GLYPH board to your computer

2. **Select the Board and Port**

   Do the following settings in your Arduino IDE,

   * `Tools > Board > esp32 > Pcbcupid GLYPH C3`

<Warning>
  For the `Pcbcupid GLYPH C3 ` to appear under `Tools > Board > esp32`, the esp32 board version installed in the Arduino IDE should be greater than or equal to 3.1.0.
</Warning>

* `Tools > Port` and select the port connected to your GLYPH.
* `Tools > USB CDC on Boot >` ***Enabled***

<Warning>
  If `USB CDC on BOOT` is not enabled, you won't be seeing any serial data on Arduino IDE.
</Warning>

3. **Upload the Code**

* Click the upload button (➡️ icon) or use the shortcut `CTRL + U` in Arduino IDE to upload the code to the board.

## Step 5: Observe the Output

On Serial Monitor, you should see the output like this:

![pcbcupid\_2channelrelay](https://files.pcbcupid.com/Documentation/Boards/modules/2_channel_relay/PCBCUPID_double_channel_relay_serialmon.avif)

Along with this you should see the relay ticking and corresponding LED blink.
