Esp32 changing pwm frequency

lucian_v
Posts: 3
Joined: Mon Feb 11, 2019 7:29 pm

Esp32 changing pwm frequency

Postby lucian_v » Wed Jun 05, 2019 6:37 am

Hi,
I am using this code to vary duty cycle of pwm and is working fine

Code: Select all

// the number of the LED pin
const int ledPin = 16;  // 16 corresponds to GPIO16

// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
 
void setup(){
  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);
  
  // attach the channel to the GPIO to be controlled
  ledcAttachPin(ledPin, ledChannel);
}
 
void loop(){
  // increase the LED brightness
  for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){   
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);
    delay(15);
  }

  // decrease the LED brightness
  for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
    // changing the LED brightness with PWM
    ledcWrite(ledChannel, dutyCycle);   
    delay(15);
  }
}
But I want vary frequency and keep duty cycle constant, My issue is that if I use

Code: Select all

ledcWriteTone(channel, frequency);
is not working...I mean if I use with a bigger delay like 1000 is changing frequency but I want do this very fast.
Is there other option to achieve this?
Basically I need change up to 60Hz starting from minimum possible...
Thank you in adavance

ESP_Sprite
Posts: 9051
Joined: Thu Nov 26, 2015 4:08 am

Re: Esp32 changing pwm frequency

Postby ESP_Sprite » Wed Jun 05, 2019 7:20 am

Move: ADF -> ESP-IDF

Who is online

Users browsing this forum: Bing [Bot] and 262 guests