How to turn on/off GPIO LED based on state

Solution 2

Instead of doing gcode in a loop, its possible to use controller_fan to toggle a GPIO pin based on an event.
controller_fan is enabled when a heater/stepper motor is engaged.

Here I have a LED strip attached to pin 35 of a raspberry pi. This turns on the LED any time a motor moves, and turns off the LED 30 seconds after idle_timeout is reached (600 seconds after last motor movement by default)

[controller_fan led_bar]
pin: rpi:gpio19

Note: If you wish to have the LED be controllable from the GUI, I’ve added an output_pin and also a duplicate_pin_override to prevent errors.

# Define the LED as a toggle in the GUI
[output_pin LED]
pin: rpi:gpio19

[controller_fan led_bar]
pin: rpi:gpio19

[duplicate_pin_override]
pins: rpi:gpio19

Note: While this does work exactly how I want. it has the following downsides

  1. LED shows up twice in the outputs pane
  2. If the controller_fan turns on/off the LED, the state is not updated in the outputs section of the gui