|
1 | 1 | ####
|
2 | 2 | GPIO
|
3 | 3 | ####
|
| 4 | + |
| 5 | +About |
| 6 | +----- |
| 7 | + |
| 8 | +One of the most used and versatile peripheral in a microcontroller is the GPIO. The GPIO is commonly used to write and read the pin state. |
| 9 | + |
| 10 | +GPIO stands to General Purpose Input Output, and is responsible to control or read the state of a specific pin in the digital world. For example, this peripheral is widely used to create the LED blinking or to read a simple button. |
| 11 | + |
| 12 | +.. note:: There are some GPIOs with special restrictions, and not all GPIOs are accessible through the developemnt board. For more information about it, see the corresponding board pin layout information. |
| 13 | + |
| 14 | +GPIOs Modes |
| 15 | +*********** |
| 16 | + |
| 17 | +There are two different modes in the GPIO configuration: |
| 18 | + |
| 19 | +- **Input Mode** |
| 20 | + |
| 21 | +In this mode, the GPIO will receive the digital state from a specific device. This device could be a button or a switch. |
| 22 | + |
| 23 | +- **Output Mode** |
| 24 | + |
| 25 | +For the output mode, the GPIO will change the GPIO digital state to a specific device. You can drive an LED for example. |
| 26 | + |
| 27 | +GPIO API |
| 28 | +-------- |
| 29 | + |
| 30 | +Here is the common functions used for the GPIO peripheral. |
| 31 | + |
| 32 | +pinMode |
| 33 | +******* |
| 34 | + |
| 35 | +The ``pinMode`` function is used to define the GPIO operation mode for a specific pin. |
| 36 | + |
| 37 | +.. code-block:: arduino |
| 38 | +
|
| 39 | + void pinMode(uint8_t pin, uint8_t mode); |
| 40 | + |
| 41 | +* ``pin`` defines the GPIO pin number. |
| 42 | +* ``mode`` sets operation mode. |
| 43 | + |
| 44 | +The following modes are supported for the basic `input` and `output`: |
| 45 | + |
| 46 | +* **INPUT** sets the GPIO as input without pullup or pulldown (high impedance). |
| 47 | +* **OUTPUT** sets the GPIO as output/read mode. |
| 48 | +* **INPUT_PULLDOWN** sets the GPIO as input with the internal pulldown. |
| 49 | +* **INPUT_PULLUP** sets the GPIO as input with the internal pullup. |
| 50 | + |
| 51 | +Internal Pullup and Pulldown |
| 52 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 53 | + |
| 54 | +The ESP32 SoC families supports the internal pullup and pulldown throught a 45kR resistor, that can be enabled when configuring the GPIO mode as ``INPUT`` mode. |
| 55 | +If the pullup or pulldown mode is not defined, the pin will stay in the high impedance mode. |
| 56 | + |
| 57 | +digitalWrite |
| 58 | +************* |
| 59 | + |
| 60 | +The function ``digitalWrite`` sets the state of the selected GPIO to ``HIGH`` or ``LOW``. This function is only used if the ``pinMode`` was configured as ``OUTPUT``. |
| 61 | + |
| 62 | +.. code-block:: arduino |
| 63 | +
|
| 64 | + void digitalWrite(uint8_t pin, uint8_t val); |
| 65 | +
|
| 66 | +* ``pin`` defines the GPIO pin number. |
| 67 | +* ``val`` set the output digital state to ``HIGH`` or ``LOW``. |
| 68 | + |
| 69 | +digitalRead |
| 70 | +*********** |
| 71 | + |
| 72 | +To read the state of a given pin configured as ``INPUT``, the function ``digitalRead`` is used. |
| 73 | + |
| 74 | +.. code-block:: arduino |
| 75 | +
|
| 76 | + int digitalRead(uint8_t pin); |
| 77 | +
|
| 78 | +* ``pin`` select GPIO |
| 79 | + |
| 80 | +This function will return the logical state of the selected pin as ``HIGH`` or ``LOW``. |
| 81 | + |
| 82 | +Interrupts |
| 83 | +---------- |
| 84 | + |
| 85 | +The GPIO peripheral on the ESP32 supports interruptions. |
| 86 | + |
| 87 | +attachInterrupt |
| 88 | +*************** |
| 89 | + |
| 90 | +The function ``attachInterruptArg`` is used to attach the interrupt to the defined pin. |
| 91 | + |
| 92 | +.. code-block:: arduino |
| 93 | +
|
| 94 | + attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode); |
| 95 | +
|
| 96 | +* ``pin`` defines the GPIO pin number. |
| 97 | +* ``handler`` set the handler function. |
| 98 | +* ``mode`` set the interrupt mode. |
| 99 | + |
| 100 | +Here are the supported interrupt modes: |
| 101 | + |
| 102 | +* **DISABLED** |
| 103 | +* **RISING** |
| 104 | +* **FALLING** |
| 105 | +* **CHANGE** |
| 106 | +* **ONLOW** |
| 107 | +* **ONHIGH** |
| 108 | +* **ONLOW_WE** |
| 109 | +* **ONHIGH_WE** |
| 110 | + |
| 111 | +attachInterruptArg |
| 112 | +****************** |
| 113 | + |
| 114 | +The function ``attachInterruptArg`` is used to attach the interrupt to the defined pin using arguments. |
| 115 | + |
| 116 | +.. code-block:: arduino |
| 117 | +
|
| 118 | + attachInterruptArg(uint8_t pin, voidFuncPtrArg handler, void * arg, int mode); |
| 119 | +
|
| 120 | +* ``pin`` defines the GPIO pin number. |
| 121 | +* ``handler`` set the handler function. |
| 122 | +* ``arg`` pointer to the interrupt arguments. |
| 123 | +* ``mode`` set the interrupt mode. |
| 124 | + |
| 125 | +detachInterrupt |
| 126 | +*************** |
| 127 | + |
| 128 | +To detach the interruption from a specific pin, use the ``detachInterrupt`` function giving the GPIO to be detached. |
| 129 | + |
| 130 | +.. code-block:: arduino |
| 131 | +
|
| 132 | + detachInterrupt(uint8_t pin); |
| 133 | +
|
| 134 | +* ``pin`` defines the GPIO pin number. |
| 135 | + |
| 136 | +.. _gpio_example_code: |
| 137 | + |
| 138 | +Example Code |
| 139 | +------------ |
| 140 | + |
| 141 | +GPIO Input and Output Modes |
| 142 | +*************************** |
| 143 | + |
| 144 | +.. code-block:: arduino |
| 145 | +
|
| 146 | + #define LED 12 |
| 147 | + #define BUTTON 2 |
| 148 | +
|
| 149 | + uint8_t stateLED = 0; |
| 150 | +
|
| 151 | + void setup() { |
| 152 | + pinMode(LED, OUTPUT); |
| 153 | + pinMode(BUTTON,INPUT_PULLUP); |
| 154 | + } |
| 155 | +
|
| 156 | + void loop() { |
| 157 | +
|
| 158 | + if(!digitalRead(BUTTON)){ |
| 159 | + stateLED = stateLED^1; |
| 160 | + digitalWrite(LED,stateLED); |
| 161 | + } |
| 162 | + } |
| 163 | +
|
| 164 | +GPIO Interrupt |
| 165 | +************** |
| 166 | + |
| 167 | +.. literalinclude:: ../../../libraries/ESP32/examples/GPIO/GPIOInterrupt/GPIOInterrupt.ino |
| 168 | + :language: arduino |
| 169 | + |
| 170 | +.. _datasheet: https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf |
0 commit comments