Reference
Pin modes
Every way a GPIO can be declared, and the methods each mode gives you.
A pin is declared in the hardware block with a name, a mode and a GPIO number. The mode decides which methods the pin has, and the compiler checks that the GPIO can actually do it on your chip: an input-only pin can’t be an Output, a pin without an ADC can’t be Analog.
hardware {
pin led: Output = 2
pin button: PullUpInput = 23
pin knob: Analog = 34
pin buzzer: PWM = 18
}
every 50ms {
if button.is_pressed() {
led.on()
buzzer.set_pwm(knob.read() / 16)
} else {
led.off()
buzzer.set_pwm(0)
}
}Leave the mode out (pin led = 2) and the compiler works it out from how the pin is used.
Output
LEDs, relays, anything you switch on and off.
-
on()
Drives the pin high.
-
off()
Drives the pin low.
-
toggle()
Flips the pin: high becomes low, low becomes high.
-
write(level: int)
Sets the level:
1for high,0for low. -
is_on() -> bool
Whether the pin is currently high.
-
blink(interval: Duration)
Turns the pin on, waits
interval, and turns it back off. -
blink_once(duration: Duration)
Turns the pin on for
duration, then off.
Input
A digital signal driven by something else. The pin floats when nothing drives it.
-
read() -> int
The pin’s level:
1or0. The pin floats when nothing drives it.
PullUpInput
A button or switch wired to ground. The internal pull-up holds the pin high until it’s pressed.
-
read() -> int
The pin’s level:
1when open,0when connected to ground. -
is_pressed() -> bool
Whether a button wired to ground is held down (the pin reads
0).
PullDownInput
A button or switch wired to 3.3V. The internal pull-down holds the pin low until it’s pressed.
-
read() -> int
The pin’s level:
0when open,1when connected to 3.3V. -
is_pressed() -> bool
Whether a button wired to 3.3V is held down (the pin reads
1).
Analog
A voltage from 0 to 3.3V: potentiometers, light-dependent resistors, analog sensors.
-
read() -> int
The voltage on the pin as a 12-bit value,
0(0V) to4095(3.3V).
PWM
Dimming an LED, the speed of a motor, or a tone on a buzzer.
-
write(value: int)
Sets the duty cycle,
0(off) to255(fully on). PWM runs at about 1 kHz. -
set_pwm(value: int)
The same as
write. -
tone(frequency: int)
Plays a square wave at
frequencyHz untilno_tone().0is silence. -
tone_ms(frequency: int, duration: int)
Plays
frequencyHz fordurationmilliseconds, then stops. -
no_tone()
Stops any tone.
-
click()
A short 2 kHz tick, for button feedback.
-
beep_success()
Two rising beeps.
-
beep_error()
Three low buzzes.
-
beep_warning()
A two-tone alert.
-
beep_startup()
An ascending three-note chime.
-
beep_shutdown()
A descending two-note chime.
-
beep_notify()
A gentle double beep.
-
beep_alarm()
An urgent siren pattern.
-
beep_keypad(digit: int)
A keypad tone for a digit,
0to9.
Servo
A hobby servo: 50 Hz pulses mapped to an angle.
-
move(angle: int)
Moves to an angle at once,
0to180degrees (or the maximum you set). -
move_us(microseconds: int)
Sets the pulse width in microseconds, bypassing the angle mapping.
-
angle() -> float
The angle last written.
-
ease_to(angle: int, duration: Duration)
Moves smoothly to
angleoverduration. Blocks until it arrives. -
ease_to_with(angle: int, duration: Duration, easing: int)
Like
ease_to, with an easing curve:0linear,1–3quadratic in, out, in-out,4–6cubic,7–9sine,10bounce,11back,12elastic. -
sweep(from: int, to: int, duration: Duration)
Sweeps from one angle to another over
duration. Blocks for the duration. -
sweep_ease(from: int, to: int, duration: Duration, easing: int)
Like
sweep, with an easing curve (the same numbers asease_to_with). -
detach()
Stops the signal. The servo goes limp and no longer holds its position.
-
set_max_angle(max_angle: int)
Sets the servo’s full range,
180by default (270for 270° servos).
DAC
A true analog voltage out. On the ESP32 only GPIO 25 and 26 have one.
-
write(value: int)
Sets the output voltage as a value from
0(0V) to255(3.3V). -
voltage(volts: float)
Sets the output voltage directly,
0.0to3.3. -
read() -> int
The value last written,
0to255.
Touch
A capacitive touch pad: a wire, foil or PCB pad. ESP32 GPIO 0, 2, 4, 12–15, 27, 32 and 33.
-
read() -> int
The raw touch reading. Lower means touched: typically 50–100 untouched, 5–20 touched.
-
is_touched() -> bool
Whether the reading is below the threshold.
-
set_threshold(threshold: int)
Sets the reading below which
is_touched()is true. -
release()
Stops touch sensing on the pin.