GPIO & ADC

Configure pins, read and write digital levels, watch edges, and sample analog inputs.


GPIO is the shortest path from source to a physical result. Start on the built-in LED and boot button, then move to circuit-specific pins.

gpio.output: $led_builtin
gpio.input: $boot_button

to show-button [
  if (gpio.read: $boot_button) = 0 [
    led.on:
  ] else [
    led.off:
  ]
]

show-button:

The common ESP32 boot button is active-low: pressed reads as 0. Other inputs depend on the circuit, pull resistors, and selected pin.

Digital Word Table

WordResultUse
gpio.modenilSet mode 0 for input or 1 for output
gpio.writenilWrite level 0 or 1
pinnilAlias for gpio.write
gpio.readIntRead digital level
gpio.inputnilConfigure input mode
gpio.outputnilConfigure output mode
gpio.highnilWrite level 1
gpio.lownilWrite level 0
gpio.togglenilRead and invert a level

The gpio.* helpers expose logical levels. Board LED helpers additionally use $led_active_level, so led.on: works on both active-high and active-low LEDs.

React To Edges

Register edge handlers inside a word, then call that word once:

to arm-button [
  on $boot_button falling debounce 25 [
    led.toggle:
  ]
]

arm-button:

Use rising, falling, or changes. Cancel every GPIO registration for the pin with cancel $boot_button. See Events for identity, output, and capacity rules.

Analog Input

raw is adc.read: $a0
percent is adc.percent: $a0
above-half? is adc.above?: $a0, 2047
WordResultUse
adc.readIntRead the platform ADC value
adc.above?BoolCompare one reading with a threshold
adc.percentIntMap the converter’s full range to 0–100

Analog reads accept only pins on the chip’s first ADC unit; any other pin fails with bad value. A second unit, where the chip has one, is not exposed when it shares hardware with the radio — reads there fail or return noise whenever the radio is on. Which pins carry analog input depends on your board (on the classic ESP32 DevKit V1, for example, GPIO 32–39) — the board’s $a0 constant always names a safe analog pin.

An unconnected analog pin floats. A stable application circuit needs the appropriate source impedance, reference, filtering, and calibration; the language helper cannot replace those physical details.