Wire relays and switches
The station control computer switches relays, antenna switches and keying inputs through its control lines. This page is about the hardware between a line and what it switches: what a line can drive by itself, what needs a driver and a relay, where a line stands before the station drives it, and two examples, an antenna switch built from relays and an amplifier keyed with the transmitter.
You add the lines and the devices that use them in Web Radio Control (see Other devices and control lines and Antenna switches and relays). Switches you buy or build for the antennas are on Remote antenna switches.
The control lines of the station
A control line is an on and off line that the station switches or reads. Every device that switches something names the lines it uses: the antennas of an antenna switch, the relays of another device, the lines a radio switches while it transmits, and the keying of a radio without CAT. You choose them in the list of lines, by their kind:
| Kind | What it is | Its level and what it can drive |
|---|---|---|
| GPIO pins | The pins of the computer itself, on a Raspberry Pi its 40-pin header, found by the station. The list names each by its header pin, such as GPIO17 · header pin 11 · gpiochip0 line 17. | 3.3 V logic, a few milliamperes. Not 5 V tolerant. |
| Serial port pins | The DTR and RTS lines of a serial port: a USB serial adapter, such as FTDI, CH340 or CP210x, or an RS-232 port of the computer. | On an RS-232 port, RS-232 levels: positive while on, negative while off. On an adapter with logic-level (TTL) pins, 3.3 V or 5 V logic, usually low while on. A few milliamperes either way. |
| Sound card pins | The pins of a USB sound card with a C-Media CM108 or CM119 chip, as many radio interfaces have. GPIO1 to GPIO4 are switched, and the four button pins VOLUME_UP, VOLUME_DOWN, PLAYBACK_MUTE and RECORD_MUTE are read. A new sound card line uses GPIO3 unless you choose another. | The logic level of the chip, with little current. An interface built on such a card usually keys the radio through a transistor of its own. |
| Network relays | A relay, lamp or button on a board on the network, switched and read over HTTP (see Set up a network relay). | The relay contacts of the board, at their rating. |
| Inverted lines | Another line the other way round (see Inverted lines). | As the line it inverts. |
What a line can switch by itself
A GPIO pin of a Raspberry Pi gives a few milliamperes at 3.3 V. That is enough for:
- an LED, through a resistor of its own;
- the LED of an optocoupler, through its resistor;
- the base of a transistor through a resistor, or the gate of a MOSFET;
- the input of a driver chip, or of a relay module made for 3.3 V logic.
It can’t drive the coil of a relay, a motor or a lamp. It must never meet more than 3.3 V: a 5 V or 12 V signal on a pin damages it, and can damage the computer.
Never connect a pin straight to the PTT of a radio or the keying input of an amplifier. Those inputs are usually pulled up to 5 V, 12 V or more inside the equipment, and keyed by connecting them to ground. Key them through an optocoupler, a transistor or a relay, as below.
The DTR and RTS of a serial port and the pins of a sound card are no stronger. Treat them as a GPIO pin, at their own levels.
Reading a line, such as a door contact, a limit switch or a button: the station reads an input without the pull-up or pull-down of the pin itself, so give the contact a resistor of its own. Put 10 kΩ from the pin to 3.3 V, and the contact between the pin and ground. The line then reads off while the contact is closed. An inverted line turns that round. A signal at a higher voltage, such as that of a 12 V lamp, reaches a pin through an optocoupler or a voltage divider, never directly.
Ground. Whatever a line drives shares the ground of the computer, unless an optocoupler or a relay separates the two sides. On the header of a Raspberry Pi, the ground pins are 6, 9, 14, 20, 25, 30, 34 and 39.
Between a line and a relay
A transistor or a MOSFET
A small NPN transistor, its base driven from the pin through a resistor, or an N-channel MOSFET, its gate driven from the pin, switches the coil of a relay to ground.
- Put a resistor of 10 kΩ to 100 kΩ from the base or the gate to ground, so that the transistor stays off while the pin isn’t driven.
- Put a diode across the coil, its cathode (the striped end) to the positive end of the coil. When the coil is switched off, its stored energy otherwise makes a voltage spike that destroys the transistor.
The few milliamperes of a pin keep a small transistor fully on for a small relay only. For coils that draw more, use a driver chip, below, or a MOSFET made for logic levels. Its datasheet gives its on-resistance at a gate voltage of 2.5 V or so. Many common MOSFETs need 10 V on the gate to switch fully.
A driver chip: ULN2003 and ULN2803
The ULN2003, with seven channels, and the ULN2803, with eight, are Darlington transistor arrays made for driving relays. Each input takes a logic line, and its output switches a coil to ground, up to 50 V and 500 mA a channel by their datasheets, less when several channels carry current at once. The coil diodes are built in.
- Connect the common pin, COM, to the positive supply of the relays.
- Connect the ground of the chip to the supply of the relays and to the ground of the computer.
An input left open is off.
Relay modules
Ready-made relay boards carry the transistor, the diode and an LED for each relay, and often an optocoupler. Before you buy one, check:
- Its input level. A board made for 5 V logic may not switch, or not switch off, from a 3.3 V pin. Choose one sold for 3.3 V, or drive its inputs through a transistor or a driver chip.
- Active low or active high. On many boards a relay is energized while its input is low. Such a relay is energized whenever the pin is low, while the computer starts too (see Before the station drives a line). It needs an inverted line, so that on means energized.
- The isolation of the optocoupler. It isolates the relays from the computer only when the relay side has its own supply, often on a jumper marked JD-VCC. Remove the jumper and power the relays separately.
Optocouplers
An optocoupler separates the computer from the equipment it keys. Its LED, on the side of the pin through a resistor, switches a transistor on the other side, with nothing in common between them. It suits keying inputs with a low voltage and little current, such as the PTT of a radio or the keying input of a modern amplifier. Mind the polarity of its output transistor, its collector to the input and its emitter to the ground of the equipment, and its voltage and current ratings. For anything larger, or of unknown polarity, a small relay keys it with complete isolation.
The relay
Choose a relay for what it switches:
- Its coil: its voltage, from a supply of its own, and its current, which the driver must carry.
- Its contacts, rated for the voltage and current of the load, and for direct current where they switch DC. The DC rating of a relay is often much lower than its AC rating, which is printed largest. The power supply of a radio or an amplifier draws a large current as it is switched on.
- For radio frequencies, a relay rated for RF at the frequency and power: a coaxial relay, or one made for the purpose (see Remote antenna switches).
Power the relays from their own supply, not from the 3.3 V pins of the computer. The 5 V pins on the header of a Raspberry Pi take what its own power supply can spare, which a few relays may exceed.
Before the station drives a line
The station drives a line only once it runs and a device that uses the line has started. Until then, a line stands where the hardware leaves it: from the moment the computer is switched on, after a power cut, during a reboot and while Web Radio Control starts, a minute or more in all.
- The GPIO pins of a Raspberry Pi start as inputs with the default pull of the board. On a Raspberry Pi 1 to 4, GPIO0 to GPIO8 are pulled up and read high, and GPIO9 to GPIO27 are pulled down and read low. GPIO2 and GPIO3 also have resistors to 3.3 V on the board, so they are always high while not driven.
- The DTR and RTS of a serial port. Linux raises both as it opens a port. The station clears them as soon as it has opened the port, but a relay or a keying input on them may see a short pulse each time.
- A network relay comes up as its board is set to after its own power cut, whatever the station does. Set that in the settings of the board itself.
Once it runs, the station drives each line as its device says:
- an antenna switch comes up on its default antenna, every line off, or on the antenna last chosen, as its At start says;
- each output of another device comes up off, on or as it was, as its own At start says;
- the lines a radio switches while it transmits are off until it transmits.
So wire every line so that the relay at rest, not energized, is the safe state: the default antenna or every antenna grounded, the amplifier not keyed, the power supply of a radio off. Also wire it so that the level of the line at power-on keeps the relay at rest. Use an active-high driver on a pin pulled low at power-on, with a resistor of its own from its input to ground that holds it there whatever the board does.
Then test it. Reboot the station from Settings › Station › Restart & shutdown while you watch the relays, and switch the power of the computer off and on.
Pins to use on a Raspberry Pi
| GPIO | Header pins | Use |
|---|---|---|
| GPIO17, GPIO27, GPIO22, GPIO23, GPIO24, GPIO25 | 11, 13, 15, 16, 18, 22 | Outputs and inputs. Low at power-on on a Raspberry Pi 1 to 4. |
| GPIO12, GPIO13, GPIO16, GPIO26 | 32, 33, 36, 37 | The same. |
| GPIO4, GPIO5, GPIO6 | 7, 29, 31 | Outputs and inputs. High at power-on on a Raspberry Pi 1 to 4, so suited to inputs and to active-low relay boards. |
| GPIO2, GPIO3 | 3, 5 | Avoid. The I²C bus, which the image turns on, with resistors to 3.3 V on the board. |
| GPIO7 to GPIO11 | 26, 24, 21, 19, 23 | Avoid. The SPI bus, which the image turns on. The system holds its chip-select pins, which the list marks used by the system. |
| GPIO14, GPIO15 | 8, 10 | Avoid. The serial port (UART). |
| GPIO0, GPIO1 | 27, 28 | Avoid. Kept for the identity memory of a HAT. |
| GPIO18 to GPIO21 | 12, 35, 38, 40 | Avoid where an audio HAT uses them. |
The 3.3 V of the header is on pins 1 and 17, its 5 V on pins 2 and 4.
Inverted lines
An inverted line is another line the other way round: on while the line it names is off. Use one where a relay is wired the other way round, such as an active-low relay board or a normally closed contact used as the switched one, so that the devices that use it say on for energized.
Add it in Settings › Station › Other devices & control lines: choose Add a control line beside Your own control lines, and Inverted line as its kind, with the line it inverts (see Add a control line). Then choose the inverted line in the devices, not the line under it.
An inverted line changes what the station drives, not what the hardware does before the station drives it. An active-low board on a pin that is low at power-on still energizes its relay until the station starts.
Break before make
Choosing an antenna turns off the lines the new antenna doesn’t use before it turns on those it uses, so the lines of the station never select two antennas at once. The station doesn’t wait between the two. The times of the relays decide whether one has opened before the next closes, and a relay with a diode across its coil opens more slowly than one without. A switch built as a tree of changeover relays, as in the example below, never connects two antennas, whatever the timing, since each relay chooses one path of two.
Changing the antenna while a transmitter sends through the switch, hot switching, arcs the relay contacts. It also leaves the transmitter, and above all an amplifier in operate, with an open or a wrong load. The station refuses it: no one changes the antenna while a radio related to the switch transmits, on whichever of its antenna ports, and a change waits until the radio stops (see Never while transmitting). Keep the switch related to its radio, on the Related devices tab of the radio, so that the station knows which radio transmits through it. The radio then also refuses to transmit by the PTT and keyer of the station while the switch has no antenna selected. A switch related with an Antenna number applies only while the radio is on that antenna port. One related without a number, as Add a device relates it, applies on every port of the radio that no other switch is related on.
The station can’t stop a transmission it doesn’t key, such as one from the front panel or the microphone of the radio while someone is at the station. Change antennas only while the radios on the switch receive.
Example: an antenna switch for four antennas
Three changeover (SPDT) relays in a tree select one of four antennas, and connect antenna 1 while every relay is at rest:
┌─ NC ── K2 (line B) ──┬─ NC ── Antenna 1
Radio ── K1 (line A) ───┤ └─ NO ── Antenna 2
└─ NO ── K3 (line C) ──┬─ NC ── Antenna 3
└─ NO ── Antenna 4| Antenna | Line A (K1) | Line B (K2) | Line C (K3) | In the antenna switch |
|---|---|---|---|---|
| 1 | off | off | off | No line. The default antenna, connected at rest. |
| 2 | off | on | off | Line B |
| 3 | on | off | off | Line A |
| 4 | on | off | on | Lines A and C |
The parts: three relays rated for RF at your frequencies and power, with 12 V coils; a ULN2803 driver; a 12 V supply for the coils; and three GPIO pins, GPIO17, GPIO27 and GPIO22, on header pins 11, 13 and 15, which are low at power-on on a Raspberry Pi 1 to 4.
The wiring: each pin to an input of the driver; each output of the driver to one end of the coil of a relay; the other ends of the coils and COM of the driver to +12 V; and the ground of the driver to the negative of the supply and to a ground pin of the header, pin 9 or 14. With nothing driven, every relay rests and antenna 1 is connected.
Add the switch
In Settings › Station › Antenna switches & relays, choose Add a device and an antenna switch (see An antenna switch).
Name the antennas and their lines
- Antenna 1, with no control line, as the default antenna.
- Antenna 2, with GPIO27 on.
- Antenna 3, with GPIO17 on.
- Antenna 4, with GPIO17 and GPIO22 on.
Every line an antenna doesn’t name is turned off when it is chosen. Set At start to the default antenna, unless the station should come back to the last antenna chosen.
Test the switch
Read the lines reads the lines, switching nothing. Switch to each antenna then switches to each antenna in turn. Listen to the relays, and check the path of each antenna with an ohmmeter or an antenna analyzer before you connect a transmitter (see Test the switch).
Relate it to the radio
On the Related devices tab of the radio, choose Add a related device and the switch.
A switch whose antennas are grounded at rest is built otherwise: a relay for each antenna, grounding it until it is chosen. It has no default antenna (see Grounded at rest).
Example: key an amplifier with the transmitter
The keying input of an amplifier switches it from bypass to amplifying. Its relays must have changed over before the RF of the radio reaches them, and stay changed over until the RF has stopped. The radio can key the amplifier itself, from its own SEND (TX) output. Or the station can, by a line it switches while the radio transmits.
First, measure the keying input with the amplifier on: the voltage across it, and the current through it when it is connected to ground. Many amplifiers carry 12 V or less on it, at some milliamperes. Some draw the full current of their own relays through it, and some older amplifiers carry over 100 V on it. Key a low-voltage, low-current input through an optocoupler, and anything else through a small relay whose contacts are rated for it.
The wiring: GPIO23, on header pin 16, through a driver to a small relay, or through an optocoupler. Its contacts connect the keying input of the amplifier to its ground.
Choose the line
On the Connection tab of the radio, in its Keying section, under Switched while transmitting, choose + Add a line… and GPIO23. Choose Save, then Apply and restart. A new radio has the same section on its Keying step (see Add a device).
Set the sequencer
On the Operating tab of the radio, under Sequencer, set Lines on before the radio keys longer than the changeover time of the amplifier, as its manual gives it. Set Lines off after the radio unkeys long enough for the output of the radio to have stopped (see The sequencer).
Test it
Transmit into the amplifier on standby first, then in operate at low drive. Watch its relays and the reflected power.
For every transmission the station keys, the line goes on, the wait before passes, then the radio keys. At the end, the radio unkeys, the wait after passes, then the line goes off. That covers the PTT button, an application through WRC Link, a test of a device, and a text or a key from the Morse keyer of the station. If the radio fails to key, the station puts the line off again.
Transmissions the station doesn’t key don’t switch the line. These are a text sent by the keyer of the radio itself, WinKeyer emulation on the keyer of the radio, CW from an application through a Hamlib link in WRC Link, VOX, and the microphone and front panel of the radio. Most amplifiers then stay in bypass while the radio transmits through them. Where any of these is used, or where every transmission must be covered, key the amplifier from the SEND output of the radio instead. Set the transmit delay of the radio, which many radios have in their menus, for the amplifier.
Other lines switched with the transmitter
Switched while transmitting switches any line with the transmitter, on before the radio keys and off after it, by the same sequencer. For example:
- a receive preamplifier, or a separate receive antenna, switched out while transmitting;
- a relay that switches the polarization of a satellite antenna, one sense to receive and the other to transmit. Use an inverted line where the rest position of the relay is the transmitting one.
Test the wiring
- Read the line with its own test, Test the line, before any device uses it. It reads the line and switches nothing (see The tests).
- Switch it with only an LED or a meter on the output of the driver before you connect the relay, and switch the relay before you connect the equipment.
- Reboot and switch off. Watch every relay while the station reboots, and while the computer is switched off and on again, as in Before the station drives a line.
- The test of the device, such as the test of an antenna switch or the keying test of a radio, switches it through the station, and keeps your answer with the result.