Closed
Description
Board
ESP32 Dev Module
Device Description
Random Devkit
Hardware Configuration
Any GPIO than can do PWM
Version
v2.0.4
IDE Name
Arduino IDE
Operating System
Whatever
Flash frequency
Whatever
PSRAM enabled
no
Upload speed
Whatever
Description
The following sequence of function calls
analogWrite(pin, 128);
ledcDetachPin(pin);
analogWrite(pin,128);
does only give PWM once and not after the second call to analogWrite.
Problem: Internal conditional on pin_to_channel array prevents second ledcAttachPin.
Solution:
$ diff -u .arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/esp32-hal-ledc.c{.orig,}
--- .arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/esp32-hal-ledc.c.orig 2022-07-06 11:47:49.000000000 +0200
+++ .arduino15/packages/esp32/hardware/esp32/2.0.4/cores/esp32/esp32-hal-ledc.c 2022-08-10 00:18:47.778755484 +0200
@@ -222,6 +222,8 @@
pin_to_channel[pin] = cnt_channel--;
ledcAttachPin(pin, cnt_channel);
ledcSetup(cnt_channel, 1000, 8);
+ } else {
+ ledcAttachPin(pin, cnt_channel);
}
ledcWrite(pin_to_channel[pin] - 1, value);
}
Apply inline patch and it works.
Regards,
Harald.
Sketch
See above, you can reproduce
Debug Message
See above, you can reproduce
Other Steps to Reproduce
No response
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Metadata
Metadata
Assignees
Type
Projects
Status
Done
Activity
habazut commentedon Aug 18, 2022
Ok, I see no reaction (1 week).
Harald.
P-R-O-C-H-Y commentedon Aug 18, 2022
@habazut Its on my list.
habazut commentedon Aug 30, 2022
I found one more bug in that corner. The first time you do
analogWrite()
to the pin it doesledcAttachPin(pin,cnt_channel);
but
cnt_channel
is 16 and channels are numbered 0 to 15.I recommend this patch instead which does put the values 0 to 15 into the pin array.
Then it would be nice to have an API to get the channel/pin relationship so that it's possible to change the frequency on a pin. Or do a
analogWriteFrequency(pin, frequency)
function.Greetings,
Harald.
P-R-O-C-H-Y commentedon Aug 30, 2022
Hi @habazut, I will take a look on the bugs and about the new API you mentioned, official Arduino don't have any of this, so why don't you just use our LEDC API without analogWrite?
habazut commentedon Sep 21, 2022
I use analogWrite() because this is supposed to work on AVR, ESP32, SAMD, STM32 without
#ifdef
-hell.I think he analogWriteFrequency was available in one of the other non-AVR versions of the Arduino API.
Regards,
Harald.
P-R-O-C-H-Y commentedon Oct 12, 2022
Hi @habazut, there is no bug in the analogWrite API.
I have added quick debug information to
analogWrite()
and call it 16 times and all channels from 15 to 0 are set.Check output message:
About the
analogWriteFrequency
I will create this function, that will change frequency for all analogWrite channels used.8 remaining items