• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 Pulse-Width Modulation (PWM) code not compiling

Q&A Forum › Category: ESP32 › ESP32 Pulse-Width Modulation (PWM) code not compiling
0 Vote Up Vote Down
Andrew Vine asked 11 months ago

I am working my way through the “Learn ESP32 with Arduino IDE” course. All units have compiled without problem up to Module 2, Unit 3 (“ESP32 Pulse-Width Modulation (PWM)”. In particular the WiFi Scan test runs without problems.

Compile is giving me the following error message “C:\Data\Random Nerd ESP32 Course\My Sketches\av_analog_dimming_1\av_analog_dimming_1.ino: In function ‘void setup()’:
C:\Data\Random Nerd ESP32 Course\My Sketches\av_analog_dimming_1\av_analog_dimming_1.ino:14:3: error: ‘ledcSetup’ was not declared in this scope
14 | ledcSetup(ledChannel, freq, resolution);
| ^~~~~~~~~
C:\Data\Random Nerd ESP32 Course\My Sketches\av_analog_dimming_1\av_analog_dimming_1.ino:17:3: error: ‘ledcAttachPin’ was not declared in this scope; did you mean ‘ledcAttach’?
17 | ledcAttachPin(ledPin, ledChannel);
| ^~~~~~~~~~~~~
| ledcAttach

exit status 1

Compilation error: ‘ledcSetup’ was not declared in this scope.

The same error message is given for the second code example (three leds).

I have copied the code from the course without alteration.

My set up is as follows :-
Arduino IDE 2.3.2
Running as “Board ESP32 Dev Module” on Port (Com 11)
Device manager shows “Silicon Labs CP210x USB to UART Bridge (Com 11)
Board “ESP-WROOM-32”
USB/UART chip “SI CP2102”

4 Answers
1 Vote Up Vote Down
Sara Santos Staff answered 11 months ago

Hi.
 
There was a recent update of the ESP32 boards add-on in Arduino IDE.
 
The new version, version 3, introduced some breaking changes. So, there are many codes that used to work that don’t work at the moment.
 
Meanwhile, we recommend you downgrade your boards installation to guarantee compatibility with our current projects.
 
Go to Tools > Boards > Boards Manager, search for ESP32. Downgrade to version 2.0.17.
The code should compile without problems.
Let me know if you can solve the issue.
 
If you prefer to use the newest version, you need to make some changes to the code.
You can now use analogWrite() to output a PWM signal. Pass as argument the GPIO, and the duty cycle.

void analogWrite(uint8_t pin, int value);

If using the ledcWrite() function, it now accepts only two arguments (the GPIO and the duty cycle).

bool ledcWrite(uint8_t pin, uint32_t duty);

You no longer need to assign a PWM channel. It will be automatically attributed. If you want to assign a specific PWM channel, use ledcAttachChannel().

bool ledcAttachChannel(uint8_t pin, uint32_t freq, uint8_t resolution, int8_t channel);

 
 
More info about this here: https://randomnerdtutorials.com/esp32-migrating-version-2-to-3-arduino/
 
Thanks for your patience.
Regards,
Sara

0 Vote Up Vote Down
Kent McNaughton answered 11 months ago

Thank you Sara. I had the same problem with compiling. ‘ledc…’
I was perplexed to find that even the examples wouldn’t compile. I hope there was a good reason for the change that would seem to break old code under maintenance.
Thanks also for giving us the new prototypes, and the method of going back to the earlier version.

0 Vote Up Vote Down
Andrew Vine answered 11 months ago

Sara,

Many thanks. The version 2.0.17 route worked well.

0 Vote Up Vote Down
Sara Santos Staff answered 11 months ago

Great.
I’ll mark this issue as resolved. If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.