io

io.output(io, boolean)

Changes the state of a specified IO pin between High and Low.
When this function is called, the IO pin—which is in a high-impedance (Hi-z) state upon startup—begins push-pull output.

Arguments

Argument Type Description
io number The IO pin number to control.
boolean boolean Set to true for High voltage, or false for Low voltage.

The following example demonstrates how to blink two LEDs connected to IO1 and IO2 alternately.

local tick = 0
local flag = false

-- Log message to indicate startup
os.log(" - Lua PowerOn");

-- Retain the state of IO1 and IO2 even during sleep or disconnection
io.retain(1, true);
io.retain(2, true);

-- Loop executed when the device is offline
function on_offline_loop()
  loop()
end

-- Loop executed when the device is online
function on_online_loop()
  loop()
end

-- Main logic to toggle IO states every 300ms
function loop()
  -- Check if 300ms has passed since the last toggle
  if tick + 300 < os.getTick() then
    tick = os.getTick()
    
    -- Output High/Low to pins 1 and 2 alternately
    io.output(1, flag);
    io.output(2, not flag);
    
    -- Toggle the flag state
    flag = not flag
  end
end

io.input(io)

Reads the High/Low state of a specified IO pin.

By calling the input function after an output operation, you can set the IO pin to a Hi-z (High Impedance) state.

Functionality

  • Read Logic Levels: Detects whether the voltage on a pin is High or Low.
  • State Control: Transitions the pin to a High Impedance state when used following an output command.

Code Example

The following example demonstrates how to read the input from IO2 and output that state directly to IO1.

-- Loop execution while the device is offline
function on_offline_loop()
  loop()
end

-- Loop execution while the device is online
function on_online_loop()
  loop()
end

-- Core logic to sync IO1 with IO2
function loop()
  -- Read the state of IO2 and output it to IO1
  io.output(1, io.input(2));
end

-- Initialization
os.log(" - Lua Start");

-- Maintain the state of the IO pins
io.retain(1, true);
io.retain(2, true);

Parameters

  • io.input(pin_number): Returns the current state (true/false or 1/0) of the specified pin.
  • io.output(pin_number, value): Sets the specified pin to the given value.

Notes

Using io.input is an effective way to reset a pin from an active output state back to a neutral, high-impedance monitoring state.