ext-gpio · PHP API
Functions and constants
Four global functions and two constants. None throws for a bad pin number: pass a GPIO the chip has.
All names are global. Arguments are strict int: passing a non-numeric string throws \TypeError, as for any
PHP function.
Constants
| Constant | Value | Meaning |
|---|---|---|
GPIO_INPUT |
0 |
Input mode for gpio_mode(). |
GPIO_OUTPUT |
1 |
Output mode for gpio_mode(); the pin stays readable. |
gpio_mode()
gpio_mode(int $pin, int $mode): void
Public method. Returns void.
Parameters
-
$pinintrequired The chip's GPIO number.
-
$modeintrequired GPIO_INPUT(0) orGPIO_OUTPUT. Any value other than 0 means output.
Resets the pin to its default state, then sets the direction. On ESP32 the reset selects the GPIO function,
disconnects any peripheral from the pin and enables the internal pull-up; output mode is input-and-output, so
gpio_read() returns what the pin is driving. gpio_mode() does not set the output level: call gpio_write()
right after it to start from a known state.
gpio_write()
gpio_write(int $pin, int $level): void
Public method. Returns void.
Parameters
-
$pinintrequired An output pin.
-
$levelintrequired 0 for low; any other value for high.
Sets the output level. On a pin configured as an input it has no visible effect.
gpio_read()
gpio_read(int $pin): int
Public method. Returns int.
The current level of the pin, 0 or 1. Works on inputs and on outputs (it reads back the driven level).
gpio_available()
gpio_available(): bool
Public method. Returns bool.
Always true. When ext-gpio is not in the firmware the function does not exist, so test for the extension
with function_exists('gpio_available') or extension_loaded('gpio').
Example
<?php
gpio_mode(2, GPIO_OUTPUT);
gpio_write(2, 1);
echo 'GPIO 2 reads ', gpio_read(2), "\n"; // 1: an output reads back its level
gpio_mode(4, GPIO_INPUT);
echo 'GPIO 4 reads ', gpio_read(4), "\n"; // 1 when nothing pulls it low (pull-up)
Related
- Blink an LED Recipe
- Read a button Recipe
- esp32-gpio-hal HAL