Skip to content

214 Relay Driver With Flyback Diode ​

214-relay-driver-with-flyback-diode.mc describes an NMOS relay coil driver.

PWR creates the V5V and GND rails. The relay coil high side connects to V5V, and the low side is named COIL_LOW. Q_RELAY switches COIL_LOW to GND. GPIO_RELAY drives the gate through R_GATE, while R_PULL keeps the gate off by default. D_FLYBACK is placed across the coil with its cathode on V5V and its anode on COIL_LOW, giving coil current a safe path when the MOSFET turns off.

Parse 214-relay-driver-with-flyback-diode.mc:

bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 21-digital-io-recipes/214-relay-driver-with-flyback-diode.mc --lib mcode --pass1 --pass2

Generate HTML for 214-relay-driver-with-flyback-diode.mc:

bash
eval "$(../mcc/scripts/mcc-slot.sh)"
MCC_SYSTEM_ROOT="$(cd .. && pwd)" "$MCC_BIN" --local parse 21-digital-io-recipes/214-relay-driver-with-flyback-diode.mc --lib mcode --viz -o 21-digital-io-recipes/214-relay-driver-with-flyback-diode.html

Source ​

text
// Example: Relay Driver With Flyback Diode
// Goal: Drive a relay coil and add flyback protection.
// Library focus: RELAY, FET.MOSFET.N, DIO, RES, DC.

module main
{
    io COIL_LOW
    io GND
    io GPIO_RELAY
    io V5V

    DC.SRC PWR(5V, 500mA)
    RELAY K_LOAD(1A, 5V)
    FET.MOSFET.N Q_RELAY
    RES R_GATE(100R, 50V)
    RES R_PULL(100000R, 50V)
    DIO D_FLYBACK(0.7V, 100V, 1A)

    PWR.1 -> V5V
    PWR.2 -> GND

    V5V -> K_LOAD.COIL.VCC
    K_LOAD.COIL.GND -> COIL_LOW
    COIL_LOW -> Q_RELAY.DRAIN
    Q_RELAY.SOURCE -> GND

    GPIO_RELAY -> R_GATE -> Q_RELAY.GATE
    Q_RELAY.GATE -> R_PULL -> GND

    COIL_LOW -> D_FLYBACK.A
    D_FLYBACK.K -> V5V
}

MCode Bench is the public face of the MCode stack. Closed capabilities are served through the MCode gateway.