Create: Computing AgesBrass Docs
Devices

Relay

The electromechanical relay: a coil at its back moves a contact. A computer reads the coil and the output, and switches it between normally open and normally closed.

The Relay is the electromechanical relay of the telegraph lines and of the first relay computers: a coil moves a contact. Redstone at its back energises the coil, and while the contact is closed, the front gives redstone. On its own it is a redstone part, like a repeater, and needs no computer. A computer connected to it reads its coil and its output, and switches its contact between normally open and normally closed, as a right-click does.

Brass
local relay = peripheral.find("relay")
print("coil: " .. tostring(relay.coil()) .. ", front: " .. relay.output())
Methods
relay.coil()Whether the coil is energised: true while redstone arrives at the back of the relay.
relay.output()The power the front of the relay gives now: 0 while the contact is open; while it is closed, the strongest signal on the sides, or 15 when the sides receive nothing.
relay.normally_closed()Which way the contact works. false, normally open (a new relay): it conducts while the coil is energised. true, normally closed: it conducts while the coil is not.
relay.set_normally_closed(closed)Makes the relay normally closed (true) or normally open (false), as a right-click does: it clicks and its front follows the new mode. The relay keeps that mode after the program ends.

Setting up

Placing it. The relay is a flat block that stands on a solid block: it drops when the block under it goes. Its front faces you when you place it, and a Create Wrench turns it afterwards.

Its sides.

  • The back is the coil: any redstone signal there energises it. The contact moves 2 ticks later (a tenth of a second), with a click.
  • The left and right sides feed the contact: the stronger of their two signals is what goes through.
  • The front gives the output while the contact is closed: the strongest signal of the sides, or a full 15 when no signal arrives on the sides. It powers the block in front of it, like a repeater.

Normally open or normally closed. A right-click switches the way the contact works:

at the backnormally opennormally closed
no signalopen: the front gives 0closed: the front gives the sides, or 15
a signalclosed: the front gives the sides, or 15open: the front gives 0

A new relay is normally open. Alone, it passes a signal on (normally open) or inverts it (normally closed); a signal on a side sets the strength that goes through.

Reaching it from a computer. Put it against a face of the computer and name it by that face, or, from the Minicomputer on, against a Data Cable of the computer's network: it is then named like relay@120,64,-35. peripheral.find("relay") takes the first one and peripheral.list("relay") gives the names of all of them. Every computer can drive it, the Tube Computer included. See Peripherals and Every device.

Redstone far from the computer

A relay on the Data Cable is a redstone input and output for the computer, anywhere on its network, without a Redstone Link. Its coil tells whether a lever or a pressure plate behind it is on (relay.coil()). With nothing at its back, relay.set_normally_closed() turns its front on (true: 15) or off (false: 0).

When the relay is gone (broken, replaced), every method stops the program with relay removed; when its chunk is not loaded, with peripheral is not loaded.

Reading

#

relay.coil()

→ boolean

Whether the coil is energised: true while redstone arrives at the back of the relay.

Returns
boolean
true while the coil is energised

It follows the contact, so it changes 2 ticks after the signal at the back, with the click you hear. A pulse shorter than that may never move it.

A visitor counter: a pressure plate in the doorway is wired to the back of a relay, and the computer counts each time the coil closes.

Brass
local relay = peripheral.find("relay")
local visitors = 0
local was_on = relay.coil()
while true do
  local on = relay.coil()
  if on and not was_on then
    visitors = visitors + 1
    print(visitors .. " visitors so far")
  end
  was_on = on
  sleep(0.1)
end

See also relay.output()

#

relay.output()

→ number

The power the front of the relay gives now: 0 while the contact is open; while it is closed, the strongest signal on the sides, or 15 when the sides receive nothing.

Returns
number
the redstone power the front gives, from 0 to 15

The contact follows the sides at once: only the coil waits 2 ticks.

Brass
local relay = peripheral.find("relay")
local power = relay.output()
if power == 0 then
  print("contact open: nothing goes out the front")
else
  print("contact closed: " .. power .. " goes through")
end

See also relay.coil() relay.normally_closed()

#

relay.normally_closed()

→ boolean

Which way the contact works. false, normally open (a new relay): it conducts while the coil is energised. true, normally closed: it conducts while the coil is not.

Returns
boolean
true for a normally closed relay, false for a normally open one
Brass
local relay = peripheral.find("relay")
if relay.normally_closed() then
  print("normally closed: the front is on while the back is off")
else
  print("normally open: the front is on while the back is on")
end

See also relay.set_normally_closed()

Switching

#

relay.set_normally_closed(closed)

Makes the relay normally closed (true) or normally open (false), as a right-click does: it clicks and its front follows the new mode. The relay keeps that mode after the program ends.

Parameters
closed boolean
true for normally closed, false for normally open

Asking for the mode it already has does nothing, not even a click. Anything other than true or false stops the program: relay.set_normally_closed(1) gives bad argument #1 to 'set_normally_closed' (boolean expected, got number).

The change wakes the blocks around the relay, so it is applied at the end of the program's tick, once, with the last value asked: a loop that switches the relay many times in a tick costs the world a single update. For the same reason, normally_closed() and output() still read the old state in the same tick; sleep(0.05) waits a tick.

Brass
local relay = peripheral.find("relay")
relay.set_normally_closed(true)
print(relay.normally_closed())   -- still the old mode
sleep(0.05)
print(relay.normally_closed())   -- true

A warning lamp at the far end of the factory: a relay on the cable, nothing at its back, a Redstone Lamp in front of it. The computer makes it blink.

Brass
local relay = peripheral.wrap("relay@120,64,-35")
while true do
  relay.set_normally_closed(true)    -- the lamp lights (15)
  sleep(1)
  relay.set_normally_closed(false)   -- the lamp goes out
  sleep(1)
end

See also relay.normally_closed() relay.output()

Example: a relay board

A board that shows every relay of the cable, like the panel of an old telephone exchange: its name, its mode (NO for normally open, NC for normally closed), its coil and the power of its front.

Brass
while true do
  term.clear()
  term.set_cursor(1, 1)
  for _, name in ipairs(peripheral.list("relay")) do
    local relay = peripheral.wrap(name)
    local mode = "NO"
    if relay.normally_closed() then
      mode = "NC"
    end
    local coil = "off"
    if relay.coil() then
      coil = "on"
    end
    print(string.format("%-22s %s coil %-3s front %2d", name, mode, coil, relay.output()))
  end
  sleep(1)
end

A relay against the computer shows up by its face (left...), one on the cable by its network name.