Skip to content
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

ledcWrite doesn't change duty cycle if duty cycle was changed less than one period ago and if using HS LEDC (channels 0-7) #5306

Closed
joshua-8 opened this issue Jun 18, 2021 · 9 comments
Assignees
Labels
Area: ESP-IDF related ESP-IDF related issues Area: Peripherals API Relates to peripheral's APIs.
Milestone

Comments

@joshua-8
Copy link

I tried to search for an issue about this didn't find anything, but if this is a known issue, I apologize.

Hardware:

Board: ESP32 Dev Module (WROOM)
Core Installation version: 1.0.6
IDE name: Arduino IDE 1.8.13
Flash Frequency: 80MHz
PSRAM enabled: Disabled
Upload Speed: 921600
Computer OS: Windows 10
no additional hardware needed.

Description:

I found this issue when using LEDC to control a servo. Sometimes, seemingly randomly, the servo wouldn't go to the position I was telling it to.
ledcWrite was not always changing the duty cycle of the signal.

ledcWrite doesn't change the duty cycle if ledcWrite is used soon after it was just used (with a shorter delay than the period of the PWM wave) and if using an HS LEDC channel(0-7) (channels 8-15 work perfectly, with the duty cycle always changing)

I was able to make a sketch that could consistently reproduce the issue for me.

Sketch:

const int outputPin = 25; //pin to attach ledc to (ch1 on scope)
const int triggerPin = 27; //pin used to trigger oscilloscope at point of interest (ch2 on scope)

const int freq = 500;
const int ledChannel = 6; //0-7 have issue, 8-15 work fine
const int resolution = 10;

void setup() {
  pinMode(triggerPin, OUTPUT);
  digitalWrite(triggerPin, LOW);

  ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(outputPin, ledChannel);

  ledcWrite(ledChannel, 800); //start PWM output

  delayMicroseconds(2500); //wait until partway through first pulse

//  delay(2); // uncomment this line to wait until second pulse and the issue goes away

  digitalWrite(triggerPin, HIGH); //trigger oscilloscope
  ledcWrite(ledChannel, 200); //attempt to change duty cycle
}

void loop() {
}

This image shows that the PWM output keeps its duty cycle of 800/1023 instead of changing to 200/1023 as set later.

issue

Adding a delay to allow the first pulse to go by makes the duty cycle change on the next pulse. This is what I would expect to always happen

line19uncommented

The issue doesn't just appear when the "first pulse" is the first pulse since the program started. ledcWrite was used three times here, the first duty cycle runs for a while, then a second duty cycle is successfully set, and where the top blue line goes up, a third frequency was set, but doesn't appear.

three

Debug Messages:

none

Solutions:

Unfortunately, I don't know enough about ledc and esp32s to be able to fix this issue.

I did find a workaround that's good enough for my servo project: if I continuously use ledcWrite from within a loop, if one call of ledcWrite doesn't work, the next one (or how ever many it takes to wait for a new cycle) will.

I know I could just use channels 8-15 since they don't have the issue, but I have a project that uses 9 pwm signals

I was hoping that I could call ledcWrite only when I need to change the duty cycle, to optimize the speed of my program.

As a related note, ledcRead returns the value that was most recently set even if the duty cycle didn't change to the new value, so I can't use that to detect if ledcWrite needs to be used.

If anyone has an idea for a better workaround, or knows why this issue happens, I would be very interested in hearing it.

I would be happy to help with any testing, or provide more information if needed.

Thank you, esp32 community, for all your work!

@VojtechBartoska VojtechBartoska self-assigned this Jul 14, 2021
@stale
Copy link

stale bot commented Sep 19, 2021

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: Stale Issue is stale stage (outdated/stuck) label Sep 19, 2021
@VojtechBartoska VojtechBartoska removed their assignment Oct 6, 2021
@stale
Copy link

stale bot commented Oct 6, 2021

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

@stale stale bot removed the Status: Stale Issue is stale stage (outdated/stuck) label Oct 6, 2021
@oycq
Copy link

oycq commented Dec 1, 2021

same Problem

@P-R-O-C-H-Y
Copy link
Member

Hi @joshua-8 and @oycq
Issue is currently being fixed on ESP-IDF side. Once the fix is merged and updated in Arduino I will let you know :)
Thanks for reporting the issue.

@P-R-O-C-H-Y P-R-O-C-H-Y added Area: ESP-IDF related ESP-IDF related issues Status: In Progress Issue is in progress and removed Status: Test needed Issue needs testing labels Jan 17, 2022
@VojtechBartoska
Copy link
Contributor

Relates to ESP-IDF issues: 6710 and 7288.

@VojtechBartoska
Copy link
Contributor

Hello @joshua-8, this problem was fixed in ESP-IDF side, changes will take effect in upcoming Arduino core release 2.0.3.

@VojtechBartoska VojtechBartoska moved this from In Progress to In Review in Arduino ESP32 Core Project Roadmap Mar 23, 2022
@P-R-O-C-H-Y
Copy link
Member

Hi @joshua-8, can you test your issue with the 2.0.3-RC1 release? So we can close this ;)

@VojtechBartoska VojtechBartoska added the Resolution: Awaiting response Waiting for response of author label Mar 31, 2022
@joshua-8
Copy link
Author

With 2.0.3-RC1, the issue is fixed!

I tested with the code I posted when I opened this issue, and the duty cycle now changes:
the frequency changes now

Thank you to everyone who worked on fixing this problem!

@VojtechBartoska
Copy link
Contributor

@joshua-8 Perfect, thank you for testing and contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: ESP-IDF related ESP-IDF related issues Area: Peripherals API Relates to peripheral's APIs.
Projects
Development

No branches or pull requests

4 participants