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.


io.drive(io, mode)

Selects the output drive method of a pin.

Available for OS7.1.0 and later

Arguments

Argument Type Description
io number The IO pin number to control.
mode string The drive method (see below).
Mode Description
"3v" Push-pull output at 3V. This is the default.
"5v" Push-pull output at 5V.
"open-drain" Open-drain output.

Note: "5v" is available only on obniz Board. On the ESP32 family, IO operates at 3V.

io.drive(1, "open-drain");  -- Drive IO1 as open-drain
io.output(1, true);

io.pull(io, mode)

Selects the pull resistor applied to a pin.

Available for OS7.1.0 and later

Arguments

Argument Type Description
io number The IO pin number to control.
mode string The pull setting (see below).
Mode Description
null No pull resistor (floating). This is the default.
"5v" Pull up to 5V.
"3v" Pull up to 3V.
"0v" Pull down to 0V.

Note: "5v" is available only on obniz Board. On the ESP32 family, IO operates at 3V.

io.pull(2, "3v");       -- Pull IO2 up to 3V
local val = io.input(2);

io.pull(2, nil);        -- Back to floating (no pull resistor)