-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Full implementation of AnalogWrite #6544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just to make it absolutely clear what "full implementation of analogWrite() " would mean, acc to Arduino API definitions:
|
I would go further than @dsyleixa and say that analogWrite() is not really implemented until it works like it is specified and the way it works on other platforms. In addition to what @dsyleixa mentioned, there must be a macro digitalPinHasPWM(pin) that exists (as a macro not a function) that returns true if the pin supports analogWrite() The problem in the past is that there was a digitalPinHasPWM(pin) macro that would always return true for all pins but the function analogWrite() didn't even exist at all which meant that user code that went to the effort to check for analogWrite() working would fail to compile due to misimplementation of the digitalPinHasPWM(pin) macro in all the variant header files. This capability is trivial to implement correctly and can be implemented regardless of what/how the analogWrite() is implemented. I would also say that if there are potential failures of analogWrite() then analogWrite() should be changed from a void function to returning an integer status of zero if successful and non zero status like < 0 if there is a failure. |
@bperrybap :
I do understand your intentions and consider it to be a precious feature, but actually dare to disagree though: Having said that I would suggest to discuss that issue with the Arduino development team to create a feature wich still keep cross-platform compatibility for future developments und API function upgrades. Plus to mention the DAC thing about
|
@dsyleixa Perhaps there are some alternatives to a return value that could work and yet still live within the existing APIs One way work around this silent failure issue would be if the macro digitalPinHasPWM(pin) were made smart enough to not only look to see if the pin was capable of pwm but also take into consideration any s/w issues/limitations in the analogWrite() implementation, including any real-time issues. This would resolve the silent failure issue without adding a return value to analogWrite() Although, IMO, I don't care for no pwm pin behavior that Arduino.cc defined. |
@bperrybap all of those "silent fails" will be fixed for 2.1.0 through a new internal module that will manage the different peripherals and their functions, thus providing PWM through more than just LEDC and adding more options for different output modes (servo, etc.) |
@bperrybap so anyone who wants to know just will have to take a look at the specs and manuals: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/ OTOH, you are not forbidden to create a new function |
@dsyleixa Each variant is supposed to provide the appropriate macros/defines to ensure this works. Here is a portable sketch that will print out all the pins that support PWM / analogWrite() for any "duino" board.
Running this sketch on a board should print out the pins that support PWM And since both NUM_DIGITAL_PINS and digitalPinHasPWM() are macros they can even be used by compile time conditionals to control what code is compile in. The problem with the ESP32 core (as of right now with the latest version 1.0.6) is that digitalPinHasPWM() says all pins support PWM but yet it has no analogWrite() function. (I mentioned this in the other analogWrite() issue) And here is example sketch that will blink a built in LED.
IMO, these two sketches are useful to verify the platform core and variant for the board are properly implemented. |
ok, I was missing that, because I never needed it actually - as I always knew all the pins which had pwm or perhaps not. |
That PWM pin detecting sketch works as expected on my ESP32:
The other sketch and the basic Blink sketch from the Arduino IDE 2.0.4 doesn't work for board = "ESP32 Dev Module" because LED_BUILTIN isn't defined. It does work with the Arduino IDE 2.04 with the "esp32 by Espressif Systems Version 2.0.7" and the "DOIT ESP32 DEVKIT V1" board:
|
Adding this to 3.0.0 milestone to evaluate this again. |
by chance I found this Arduino lib: will it work with all ESP32 repo versions? has anyone checked it? BTW, |
Also, perhaps a full implementation of AnalogWrite would help make the standard Servo library work again? Seems like it broke around 3.0. Sorry for the poor bug report but +1 for AnalogWrite PWM :) |
@iseanstevens You can also have a look at the changes I made here: https://github.com/letscontrolit/ESPEasy/tree/mega/lib/ServoESP32 |
Servo library should use RMT on ESP32 chips, not PWM. This issue is more to track being able to use different peripherals (not just LEDC) to output PWM. |
@sivar2311 Servo uses 50Hz PWM with duty between 5% and 10%. Such things are much better handled with RMT than analogWrite. There is nothing preventing you from using analogWrite, it's just not optimal |
@me-no-dev How many RMT channels can you address? |
@TD-er you can use as many as there are TX RMT channels on the chip (it differs from chip to chip). You set it to output continuous for every channel and change as needed. vanilla ESP32 has 8 channels |
Hello, Due to the overwhelming volume of issues currently being addressed, we have decided to close the previously received tickets. If you still require assistance or if the issue persists, please don't hesitate to reopen the ticket. Thanks. |
Current implementation of AnalogWrite is based on LedC Peripheral which has some limitations.
This issue tracks possible full implementation of AnalogWrite.
Related links
The text was updated successfully, but these errors were encountered: