Skip to content

GPIO: Trigger, Simulate & Write

Download ezUI File

Overview

This example demonstrates how to:

  • Select a GPIO device to listen for triggers or send commands to.
  • React to incoming triggers by taking a specific Page on-air.
  • Simulate trigger events for testing.
  • Write Boolean values to GPIO outputs.

GPIOtriggerSimulateWrite

When to Use

  • Linking external GPIO inputs (e.g., physical buttons, sensors) to Studio actions.
  • Sending control signals to GPIO outputs (e.g., relays, tally lights).
  • Quickly emulating hardware signals during development or troubleshooting.

Requirements

  1. At least one GPIO device is configured in Studio → Settings → GPIO.
  2. Know the device index (0, 1, 2, …). If left empty, Gpio.device() will use the first available device.
  3. Understand your hardware channel numbering (which input/output corresponds to which number).

UI Elements

Device - Input Number

The index of the GPIO device to be listened or triggered. If left empty, the first available GPIO device will be used.

On Trigger:

  • Trigger - Input Number: the GPIO input channel to monitor for triggers.
  • Page - Input Number: page number to take on-air when the trigger is received.
  • Simulate - Button: triggers the specified input channel programmatically for testing.

Write:

  • Input Number (labeled writeInput in this UI): the GPIO output channel to write to.
  • Switch Control: sets the Boolean value (True/False).
  • Write - Button: sends the selected Boolean value to the specified output channel.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Get the GPIO device by its configured name or index from the "device" control
const GpioDevice = Gpio.device(device.value);

// Event handler for GPIO "trigger" events — fires when the device receives an input signal
GpioDevice.on('trigger', e => {
  console.log('Gpio on trigger', e);

  // Read the page number from the "triggerPage" control
  const raw = triggerPage.value;

  // Validate that the value is a finite number
  if (!Number.isFinite(Number(raw))) {
    console.warn('[TakeIn] Invalid page number:', raw);
    return;
  }

  // Convert to an integer and format as a 4-digit string (e.g., "0005")
  const page = String(Math.trunc(Number(raw))).padStart(4, '0'); 

  try {
    // Execute a "Take In" command for the specified page in the active program profile
    Api.profile.program.takeIn(page);
  } catch (err) {
    console.error('[TakeIn] Api.profile.program.takeIn failed:', err);
  }
});

// Event handler for the "Simulate" button click — simulates a GPIO input trigger
simulate.onClick = () => {
  const input = Number(num_GpioInput.value); // Input channel number
  if (!Number.isFinite(input)) {
    console.warn('[GPIO] Invalid input channel for simulate');
    return;
  }

  console.log(`SIMULATE INPUT ON GPIO INPUT ${input}`);
  // Trigger a simulated signal on the specified input channel
  GpioDevice.simulate(input);
};

// Event handler for the "Write" button click — sends a value to a GPIO output channel
simulate1.onClick = () => {
  const output = Number(num_GpioInput1.value); // Output channel number
  const value  = Boolean(writeValue.value);    // Boolean value to set (true/false)
  if (!Number.isFinite(output)) {
    console.warn('[GPIO] Invalid output channel for write');
    return;
  }

  console.log(`[GPIO] WRITE to output ${output}: ${value}`);
  // Write the specified value to the selected GPIO output channel
  GpioDevice.write(output, value);
};