|
| 1 | +################## |
| 2 | +LED Control (LEDC) |
| 3 | +################## |
| 4 | + |
| 5 | +About |
| 6 | +----- |
| 7 | +The LED control (LEDC) peripheral is primarly designed to control the intensity of LEDs, |
| 8 | +although it can also be used to generate PWM signals for other purposes. |
| 9 | + |
| 10 | +ESP32 SoCs has from 6 to 16 channels (variates on socs, see table below) which can generate independent waveforms, that can be used for example to drive RGB LED devices. |
| 11 | + |
| 12 | +========= ======================= |
| 13 | +ESP32 SoC Number of LEDC channels |
| 14 | +========= ======================= |
| 15 | +ESP32 16 |
| 16 | +ESP32-S2 8 |
| 17 | +ESP32-C3 6 |
| 18 | +ESP32-S3 8 |
| 19 | +========= ======================= |
| 20 | + |
| 21 | +Arduino-ESP32 LEDC API |
| 22 | +---------------------- |
| 23 | + |
| 24 | +ledcSetup |
| 25 | +********* |
| 26 | + |
| 27 | +This function is used to setup the LEDC channel frequency and resolution. |
| 28 | + |
| 29 | +.. code-block:: arduino |
| 30 | +
|
| 31 | + double ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits); |
| 32 | +
|
| 33 | +* ``channel`` select LEDC channel to config. |
| 34 | +* ``freq`` select frequency of pwm. |
| 35 | +* ``resolution_bits`` select resolution for ledc channel. |
| 36 | + |
| 37 | + * range is 1-14 bits (1-20 bits for ESP32). |
| 38 | + |
| 39 | +This function will return ``frequency`` configured for LEDC channel. |
| 40 | +If ``0`` is returned, error occurs and ledc channel was not configured. |
| 41 | + |
| 42 | +ledcWrite |
| 43 | +********* |
| 44 | + |
| 45 | +This function is used to set duty for the LEDC channel. |
| 46 | + |
| 47 | +.. code-block:: arduino |
| 48 | +
|
| 49 | + void ledcWrite(uint8_t chan, uint32_t duty); |
| 50 | +
|
| 51 | +* ``chan`` select the LEDC channel for writing duty. |
| 52 | +* ``duty`` select duty to be set for selected channel. |
| 53 | + |
| 54 | +ledcRead |
| 55 | +******** |
| 56 | + |
| 57 | +This function is used to get configured duty for the LEDC channel. |
| 58 | + |
| 59 | +.. code-block:: arduino |
| 60 | +
|
| 61 | + uint32_t ledcRead(uint8_t chan); |
| 62 | +
|
| 63 | +* ``chan`` select LEDC channel to read the configured duty. |
| 64 | + |
| 65 | +This function will return ``duty`` set for selected LEDC channel. |
| 66 | + |
| 67 | +ledcReadFreq |
| 68 | +************ |
| 69 | + |
| 70 | +This function is used to get configured frequency for the LEDC channel. |
| 71 | + |
| 72 | +.. code-block:: arduino |
| 73 | +
|
| 74 | + double ledcReadFreq(uint8_t chan); |
| 75 | +
|
| 76 | +* ``chan`` select the LEDC channel to read the configured frequency. |
| 77 | + |
| 78 | +This function will return ``frequency`` configured for selected LEDC channel. |
| 79 | + |
| 80 | +ledcWriteTone |
| 81 | +************* |
| 82 | + |
| 83 | +This function is used to setup the LEDC channel to 50 % PWM tone on selected frequency. |
| 84 | + |
| 85 | +.. code-block:: arduino |
| 86 | +
|
| 87 | + double ledcWriteTone(uint8_t chan, double freq); |
| 88 | +
|
| 89 | +* ``chan`` select LEDC channel. |
| 90 | +* ``freq`` select frequency of pwm signal. |
| 91 | + |
| 92 | +This function will return ``frequency`` set for channel. |
| 93 | +If ``0`` is returned, error occurs and ledc cahnnel was not configured. |
| 94 | + |
| 95 | +ledcWriteNote |
| 96 | +************* |
| 97 | + |
| 98 | +This function is used to setup the LEDC channel to specific note. |
| 99 | + |
| 100 | +.. code-block:: arduino |
| 101 | +
|
| 102 | + double ledcWriteNote(uint8_t chan, note_t note, uint8_t octave); |
| 103 | +
|
| 104 | +* ``chan`` select LEDC channel. |
| 105 | +* ``note`` select note to be set. |
| 106 | + |
| 107 | +======= ======= ======= ======= ======= ====== |
| 108 | +NOTE_C NOTE_Cs NOTE_D NOTE_Eb NOTE_E NOTE_F |
| 109 | +NOTE_Fs NOTE_G NOTE_Gs NOTE_A NOTE_Bb NOTE_B |
| 110 | +======= ======= ======= ======= ======= ====== |
| 111 | + |
| 112 | +* ``octave`` select octave for note. |
| 113 | + |
| 114 | +This function will return ``frequency`` configured for the LEDC channel according to note and octave inputs. |
| 115 | +If ``0`` is returned, error occurs and the LEDC channel was not configured. |
| 116 | + |
| 117 | +ledcAttachPin |
| 118 | +************* |
| 119 | + |
| 120 | +This function is used to attach the pin to the LEDC channel. |
| 121 | + |
| 122 | +.. code-block:: arduino |
| 123 | +
|
| 124 | + void ledcAttachPin(uint8_t pin, uint8_t chan); |
| 125 | +
|
| 126 | +* ``pin`` select GPIO pin. |
| 127 | +* ``chan`` select LEDC channel. |
| 128 | + |
| 129 | +ledcDetachPin |
| 130 | +************* |
| 131 | + |
| 132 | +This function is used to detach the pin from LEDC. |
| 133 | + |
| 134 | +.. code-block:: arduino |
| 135 | +
|
| 136 | + void ledcDetachPin(uint8_t pin); |
| 137 | +
|
| 138 | +* ``pin`` select GPIO pin. |
| 139 | + |
| 140 | +ledcChangeFrequency |
| 141 | +******************* |
| 142 | + |
| 143 | +This function is used to set frequency for the LEDC channel. |
| 144 | + |
| 145 | +.. code-block:: arduino |
| 146 | +
|
| 147 | + double ledcChangeFrequency(uint8_t chan, double freq, uint8_t bit_num); |
| 148 | +
|
| 149 | +* ``channel`` select LEDC channel. |
| 150 | +* ``freq`` select frequency of pwm. |
| 151 | +* ``bit_num`` select resolution for LEDC channel. |
| 152 | + |
| 153 | + * range is 1-14 bits (1-20 bits for ESP32). |
| 154 | + |
| 155 | +This function will return ``frequency`` configured for the LEDC channel. |
| 156 | +If ``0`` is returned, error occurs and the LEDC channel frequency was not set. |
| 157 | + |
| 158 | +analogWrite |
| 159 | +*********** |
| 160 | + |
| 161 | +This function is used to write an analog value (PWM wave) on the pin. |
| 162 | +It is compatible with Arduinos analogWrite function. |
| 163 | + |
| 164 | +.. code-block:: arduino |
| 165 | +
|
| 166 | + void analogWrite(uint8_t pin, int value); |
| 167 | +
|
| 168 | +* ``pin`` select the GPIO pin. |
| 169 | +* ``value`` select the duty cycle of pwm. |
| 170 | + * range is from 0 (always off) to 255 (always on). |
| 171 | + |
| 172 | +Example Applications |
| 173 | +******************** |
| 174 | + |
| 175 | +LEDC software fade example: |
| 176 | + |
| 177 | +.. literalinclude:: ../../../libraries/ESP32/examples/AnalogOut/LEDCSoftwareFade/LEDCSoftwareFade.ino |
| 178 | + :language: arduino |
| 179 | + |
| 180 | +LEDC Write RGB example: |
| 181 | + |
| 182 | +.. literalinclude:: ../../../libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino |
| 183 | + :language: arduino |
0 commit comments