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

ESP32-S2 PWM for a Servo pulse #5050

Closed
StuartsProjects opened this issue Apr 13, 2021 · 5 comments · Fixed by #5452
Closed

ESP32-S2 PWM for a Servo pulse #5050

StuartsProjects opened this issue Apr 13, 2021 · 5 comments · Fixed by #5452
Assignees
Labels
Status: Stale Issue is stale stage (outdated/stuck)

Comments

@StuartsProjects
Copy link

ESP32-S2 PWM settings do not work as expected.

The code bellow when run on an ESP32 WROOM Development board, generates 1ms and 2ms pulses at 50hz, works as intended. It correctly produces a pulse to control a servo that shifts between 1m2 and 2mS.

/*
On ESP32 generates a sweeping pulse of 1mS to 2mS at 50hz
*/

const int servoPin = 17;

int dutyCycle = 0;

const int PWMFreq = 50;                                //50hz
const int PWMChannel = 0;
const int PWMResolution = 12;                       //12 bits 0-4095
 
void setup()
{  
  ledcSetup(PWMChannel, PWMFreq, PWMResolution);
  ledcAttachPin(servoPin, PWMChannel);
  ledcWrite(PWMChannel, dutyCycle);
}

void loop()
{
    ledcWrite(PWMChannel, 200);                     //approx 1ms
    delay(2000);    
  
    ledcWrite(PWMChannel, 400);                     //approx 2mS
    delay(2000);    
}

When the above code is run on a ESP32-S2 Saola board the pulse produced is not correct, it has a period of circa 1500mS and periods of 80mS and 160mS.

If in the code I change PWMFreq from 50 to 4096, I end up with a pulse that is 1ms to 2ms @ 50hz on the ESP32-S2.

@lbernstone
Copy link
Contributor

It seems like something is wrong at the higher resolutions. Can you test at 10 bit and scale your timings to fit?

@StuartsProjects
Copy link
Author

Using this;

const int PWMFreq = 50; //50hz
const int PWMChannel = 0;
const int PWMResolution = 10;

Gives me a pulse period of 250uS.

I need to set PWMFreq = 4096; to get back to a 20mS period.

@Dlloydev
Copy link

Dlloydev commented Apr 18, 2021

In testing an analogWrite library I wrote (using a ESP32S2-Saola-1M) that uses the latest commit here (it wraps the ledc functions) here from 2 days ago, I find that the set frequency is off by a factor of 80. Need to use 80000 to get 1kHz PWM signal.

EDIT: Using a workaround with limited frequency range here in the awLedcSetup and awLedcReadFreq functions.

@stale
Copy link

stale bot commented Jun 26, 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 Jun 26, 2021
@stale
Copy link

stale bot commented Jul 11, 2021

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.

@stale stale bot closed this as completed Jul 11, 2021
@SuGlider SuGlider self-assigned this Jul 23, 2021
me-no-dev pushed a commit that referenced this issue Jul 26, 2021
Dlloydev added a commit to Dlloydev/ESP32-ESP32S2-AnalogWrite that referenced this issue Sep 19, 2021
- This version (2.0.9) removes the existing fix for [#5050](espressif/arduino-esp32#5050) because ESP32 2.0.0-rc2 or newer already includes a [solution](espressif/arduino-esp32@65eafd1).
- Constant strings now use program memory instead of ram by using `Serial.print(F())`function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Stale Issue is stale stage (outdated/stuck)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants