├── LICENSE ├── README.md ├── examples └── ledFade │ └── ledFade.ino ├── keywords.txt ├── library.json ├── library.properties └── src ├── analogWrite.cpp └── analogWrite.h /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 ERROPiX 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | I'm looking for active maintainers for this project as I no longer have the time to support it. Please get in touch if you're interested 2 | --- 3 | 4 | # About 5 | 6 | This library provides an analogWrite function polyfill for ESP32 Arduino framework by wrapping the [ledc](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c) library. 7 | 8 | Licensed under the MIT license. 9 | 10 | # Usage 11 | The library will do all the timer setup and channel selection work behind the scene so you don't have to worry about that. 12 | 13 | ## Fade Example 14 | ```cpp 15 | #include 16 | #include 17 | 18 | int pin = LED_BUILTIN; 19 | int brightness = 0; 20 | int brightStep = 1; 21 | 22 | void setup() 23 | { 24 | } 25 | 26 | void loop() 27 | { 28 | brightness += brightStep; 29 | analogWrite(pin, brightness); 30 | 31 | if (brightness == 0 || brightness == 255) 32 | { 33 | brightStep = -brightStep; 34 | } 35 | 36 | delay(10); 37 | } 38 | ``` 39 | 40 | ## Default Value Range 41 | Call the `analogWrite` function like in standard arduino framework: 42 | ```cpp 43 | analogWrite(LED_BUILTIN, 255); // value range 0-255 so 255 = 100% 44 | ``` 45 | 46 | ## Custom Value Range 47 | You can also set the maximum value as third parameter: 48 | ```cpp 49 | analogWrite(LED_BUILTIN, 255, 1023); // value range 0-1023 so 255 = 25% 50 | ``` 51 | 52 | ## Timer Resolution 53 | The default timer resolution is set to 13 bits in all the 16 channels, if you want to change that, use the `analogWriteResolution` function: 54 | ```cpp 55 | analogWriteResolution(10); // set resolution to 10 bits for all pins 56 | analogWriteResolution(LED_BUILTIN, 10); // set resolution to 10 bits for LED pin 57 | ``` 58 | 59 | ## PWM Frequency 60 | The default PWM frequency is set to 5000 Hz in all the 16 channels, if you want to change that, use the `analogWriteFrequency` function: 61 | ```cpp 62 | analogWriteFrequency(10000); // set frequency to 10 KHz for all pins 63 | analogWriteFrequency(LED_BUILTIN, 10000); // set frequency to 10 KHz for LED pin 64 | ``` 65 | 66 | Please note that both timer resolution and PWM frequency should be calculated to get the expected results, if frequency is not set correctly, the output PWM signal wont be as expected, read more about [Supported Range of Frequency and Duty Resolution](https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/ledc.html#ledc-api-supported-range-frequency-duty-resolution) in the official Espressif documentation website. 67 | -------------------------------------------------------------------------------- /examples/ledFade/ledFade.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int brightStep = 1; 5 | int brightness = 0; 6 | 7 | void setup() { 8 | // Set resolution for a specific pin 9 | analogWriteResolution(LED_BUILTIN, 12); 10 | } 11 | 12 | void loop() { 13 | brightness += brightStep; 14 | if ( brightness == 0 || brightness == 255 ) { 15 | brightStep = -brightStep; 16 | } 17 | 18 | analogWrite(LED_BUILTIN, brightness); 19 | 20 | delay(10); 21 | } -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Datatypes 3 | ####################################### 4 | 5 | analog_write_channel_t KEYWORD1 6 | 7 | ####################################### 8 | # Methods and Functions 9 | ####################################### 10 | 11 | analogWrite KEYWORD2 12 | analogWriteChannel KEYWORD2 13 | analogWriteFrequency KEYWORD2 14 | analogWriteResolution KEYWORD2 15 | 16 | ####################################### 17 | # Constants 18 | ####################################### 19 | 20 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ESP32 AnalogWrite", 3 | "keywords": "esp32, analogWrite, LEDC", 4 | "description": "Provides an analogWrite polyfill for ESP32 using the LEDC functions", 5 | "license": "MIT", 6 | "version": "0.3", 7 | "frameworks": "arduino", 8 | "platforms": "espressif32", 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/ERROPiX/ESP32_AnalogWrite.git" 12 | }, 13 | "authors": [ 14 | { 15 | "name": "Abdelouahed ERROUAGUY", 16 | "email": "errouaguy.pro@gmail.com" 17 | } 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=ESP32 AnalogWrite 2 | version=0.3 3 | author=Abdelouahed ERROUAGUY 4 | maintainer=Abdelouahed ERROUAGUY 5 | sentence=ESP32 Polyfill for analogWrite functions 6 | paragraph=Provides an analogWrite polyfill for ESP32 using the LEDC functions 7 | category=Signal Input/Output 8 | url=https://github.com/ERROPiX/ESP32_AnalogWrite 9 | architectures=esp32 10 | includes=analogWrite.h 11 | -------------------------------------------------------------------------------- /src/analogWrite.cpp: -------------------------------------------------------------------------------- 1 | #include "analogWrite.h" 2 | 3 | analog_write_channel_t _analog_write_channels[16] = { 4 | {-1, 5000, 13}, 5 | {-1, 5000, 13}, 6 | {-1, 5000, 13}, 7 | {-1, 5000, 13}, 8 | {-1, 5000, 13}, 9 | {-1, 5000, 13}, 10 | {-1, 5000, 13}, 11 | {-1, 5000, 13}, 12 | {-1, 5000, 13}, 13 | {-1, 5000, 13}, 14 | {-1, 5000, 13}, 15 | {-1, 5000, 13}, 16 | {-1, 5000, 13}, 17 | {-1, 5000, 13}, 18 | {-1, 5000, 13}, 19 | {-1, 5000, 13}}; 20 | 21 | int analogWriteChannel(uint8_t pin) 22 | { 23 | int channel = -1; 24 | 25 | // Check if pin already attached to a channel 26 | for (uint8_t i = 0; i < 16; i++) 27 | { 28 | if (_analog_write_channels[i].pin == pin) 29 | { 30 | channel = i; 31 | break; 32 | } 33 | } 34 | 35 | // If not, attach it to a free channel 36 | if (channel == -1) 37 | { 38 | for (uint8_t i = 0; i < 16; i++) 39 | { 40 | if (_analog_write_channels[i].pin == -1) 41 | { 42 | _analog_write_channels[i].pin = pin; 43 | channel = i; 44 | // ledcSetup and ledcAttachedPin are removed from Arduino 3.0 and newer - instead, letdcAttach is being used. 45 | // ledcSetup(channel, _analog_write_channels[i].frequency, _analog_write_channels[i].resolution); 46 | // ledcAttachPin(pin, channel); 47 | ledcAttach(pin, _analog_write_channels[i].frequency, _analog_write_channels[i].resolution); 48 | break; 49 | } 50 | } 51 | } 52 | 53 | return channel; 54 | } 55 | 56 | void analogWriteFrequency(double frequency) 57 | { 58 | for (uint8_t i = 0; i < 16; i++) 59 | { 60 | _analog_write_channels[i].frequency = frequency; 61 | } 62 | } 63 | 64 | void analogWriteFrequency(uint8_t pin, double frequency) 65 | { 66 | int channel = analogWriteChannel(pin); 67 | 68 | // Make sure the pin was attached to a channel, if not do nothing 69 | if (channel != -1 && channel < 16) 70 | { 71 | _analog_write_channels[channel].frequency = frequency; 72 | } 73 | } 74 | 75 | void analogWriteResolution(uint8_t resolution) 76 | { 77 | for (uint8_t i = 0; i < 16; i++) 78 | { 79 | _analog_write_channels[i].resolution = resolution; 80 | } 81 | } 82 | 83 | void analogWriteResolution(uint8_t pin, uint8_t resolution) 84 | { 85 | int channel = analogWriteChannel(pin); 86 | 87 | // Make sure the pin was attached to a channel, if not do nothing 88 | if (channel != -1 && channel < 16) 89 | { 90 | _analog_write_channels[channel].resolution = resolution; 91 | } 92 | } 93 | 94 | void analogWrite(uint8_t pin, uint32_t value, uint32_t valueMax) 95 | { 96 | int channel = analogWriteChannel(pin); 97 | 98 | // Make sure the pin was attached to a channel, if not do nothing 99 | if (channel != -1 && channel < 16) 100 | { 101 | uint8_t resolution = _analog_write_channels[channel].resolution; 102 | uint32_t levels = pow(2, resolution); 103 | uint32_t duty = ((levels - 1) / valueMax) * min(value, valueMax); 104 | 105 | // write duty to LEDC 106 | ledcWrite(channel, duty); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/analogWrite.h: -------------------------------------------------------------------------------- 1 | #ifndef _ESP32_ANALOG_WRITE_ 2 | #define _ESP32_ANALOG_WRITE_ 3 | 4 | #include 5 | 6 | typedef struct analog_write_channel 7 | { 8 | int8_t pin; 9 | double frequency; 10 | uint8_t resolution; 11 | } analog_write_channel_t; 12 | 13 | int analogWriteChannel(uint8_t pin); 14 | 15 | void analogWriteFrequency(double frequency); 16 | void analogWriteFrequency(uint8_t pin, double frequency); 17 | 18 | void analogWriteResolution(uint8_t resolution); 19 | void analogWriteResolution(uint8_t pin, uint8_t resolution); 20 | 21 | void analogWrite(uint8_t pin, uint32_t value, uint32_t valueMax = 255); 22 | 23 | #endif --------------------------------------------------------------------------------