jsolderitsch / ESP32ByExample

Resources for Udemy and Tech Explorations ESP32 By Example Course
MIT License
2 stars 1 forks source link

PWM Examples no longer use ledcSetup and ledcAttachPin in ESP32 lib 3.x #2

Open jomoengineer opened 1 month ago

jomoengineer commented 1 month ago

I tried running the code list under Section5PWM and I get the error listed below. After some searching, I found there has been some changes in the ESP32 Board library in the 3.x versions where ledcSetup and ledcAttachPin have been removed and replaced with just ledcAttach.

Ref: https://docs.espressif.com/projects/arduino-esp32/en/latest/migration_guides/2.x_to_3.0.html

There is a LEDCFade example in the Arduino Examples for the ESP32 that has the changes in it.

This is the error that I seen.

E:\Embedded\ESP32\ESP32_By_Example\ESP32ByExampleSketches\ESP32ByExampleSketches\Section5PWM\pwm_test_eek\pwm_test_eek.ino: In function 'void setup()':
E:\Embedded\ESP32\ESP32_By_Example\ESP32ByExampleSketches\ESP32ByExampleSketches\Section5PWM\pwm_test_eek\pwm_test_eek.ino:25:3: error: 'ledcSetup' was not declared in this scope
   25 |   ledcSetup(upChannel, 4000, 8); // 12 kHz PWM, 8-bit resolution
      |   ^~~~~~~~~
E:\Embedded\ESP32\ESP32_By_Example\ESP32ByExampleSketches\ESP32ByExampleSketches\Section5PWM\pwm_test_eek\pwm_test_eek.ino:27:3: error: 'ledcAttachPin' was not declared in this scope; did you mean 'ledcAttach'?
   27 |   ledcAttachPin(TAKEOFF, upChannel); // assign a led pins to a channel
      |   ^~~~~~~~~~~~~
      |   ledcAttach
jsolderitsch commented 1 month ago

Thanks for the report. I need to go back and review code for the EEK with the 3.0.x ESP32 Core that is out now. Here are two little examples that I need to adapt my code towards:

int led = 12;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright you want the LED to be
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // declare pin 12 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 12:
  analogWrite(led, brightness);

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

And

const byte led_gpio = 12;  // the PWM pin the LED is attached to
int brightness = 0;        // how bright the LED is
int fadeAmount = 5;        // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup() {
  // Initialize Pin for ledc based PWM
  ledcAttach(led_gpio, 4000, 8); // 12 kHz PWM frequency, 8-bit resolution
}

// the loop routine runs over and over again forever:
void loop() {
  ledcWrite(led_gpio, brightness);  // set the brightness of the LED

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness <= 0 || brightness >= 255) {
    fadeAmount = -fadeAmount;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);
}

Good old analogWrite now works.

Channel concerns are no longer front and center.