Can I be the only one having this problem, I suppose it’s possible it’s my computer. But I have been trying to get the ledcwrite function on my Mac to work properly for a while. I was hoping this book would shed some light on it. I got to that point in the book to run an LED dimming sketch. When I tried the sketch, I came up with a long time problem I’ve had. I encounter an error while compiling the sketch. What is the strangest is that it works with an Arduino Nano ESP32 but not a ESP32 WROOM. Through a lot of digging, I did get the sketch to work with an ESP32 WROOM by changing the sketch slightly. This is what I found. There is no ledcSetup function in the ESP32 WROOM sketch that compiles correctly. I can get the sketch to work correctly, but it would be nice if they compiled the same way. Did Espressif change the compiler?
This works with an Arduino nano ESP32:
// 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);
}
}
And this works with an ESP32 WROOM:
#define ledPin 22 // 16 corresponds to GPIO16
// setting PWM properties
#define freq 5000
#define ledChannel 0
#define resolution 8
void setup(){
// attach the channel to the GPIO to be controlled
ledcAttach(ledPin, freq, resolution);
// configure LED PWM functionalitites
//ledcSetup(ledChannel, freq, resolution);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(5);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle–){
// changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(5);
}
}
Hi.
Can you send me a link to the specific board you’re using that doesn’t work? When you’re using that board, what board model do you select in Tools > Board?
What changes did you make to the sketch to make it work with that board?
Regards,
Sara
The micro I’m using is the ESP32 DEVKITV1, though I’ve used other models. The board I selected when compiling is DOIT ESP32 DEVKIT V1. The error I get is: Compilation error: ‘ledcSetup’ was not declared in this scope. The sketch is the ‘dimming an LED’ from GitHub
https://github.com/RuiSantosdotme/ESP32-Course/blob/master/code/LED_PWM_Example_1/LED_PWM_Example_1.ino
So what I did to make it work;
ESP32 DEVKIT V1, sketch works
ledcAttach(ledPin, freq, resolution);
ledcWrite(ledPin, dutyCycle);
Arduino Nano ESP32, sketch works
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(ledPin, ledChannel);
ledcWrite(ledChannel, dutyCycle);
The Arduino nano ESP32 works with the example sketch.
The Arduino nano ESP32 of course uses a different compiler, which led me to believe that Espressif had changed their compiler
The complete error message I get when compiling the ESP32 DEVKIT V1 with the example sketch:
/private/var/folders/p6/qpn69lvx3d3gq1lrf3601dtr0000gn/T/.arduinoIDE-unsaved2024122-8930-1726fl7.yc7o/LEDCSoftwareFade/LEDCSoftwareFade.ino: In function ‘void setup()’:
/private/var/folders/p6/qpn69lvx3d3gq1lrf3601dtr0000gn/T/.arduinoIDE-unsaved2024122-8930-1726fl7.yc7o/LEDCSoftwareFade/LEDCSoftwareFade.ino:40:3: error: ‘ledcSetup’ was not declared in this scope
40 | ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_12_BIT);
| ^~~~~~~~~
/private/var/folders/p6/qpn69lvx3d3gq1lrf3601dtr0000gn/T/.arduinoIDE-unsaved2024122-8930-1726fl7.yc7o/LEDCSoftwareFade/LEDCSoftwareFade.ino:41:3: error: ‘ledcAttachPin’ was not declared in this scope; did you mean ‘ledcAttach’?
41 | ledcAttachPin(LED_PIN, LEDC_CHANNEL_0);
| ^~~~~~~~~~~~~
| ledcAttach
exit status 1
Compilation error: ‘ledcSetup’ was not declared in this scope
This is really strange, because no channel was declared for the ESP32 DEVKIT1, but the sketch does work. I don’t know why it works. Try as I may, I haven’t been able to find out what’s going on.
Hi.
What ESP32 boards’ version are you using? Go to tools > Board > Boards Manager > ESP32 and check the version you’re using.
I’ve searched for a while and saw that there were breaking changes from version 2.x to 3.x
Specifically, they removed the ledcSetup function. See it here.
Now, you should use a function ledcAttach
()
This function accepts as arguments the pin, frequency and resolution in this order
ledcAttach(pin, freq, resolution)
What’s weird is that I don’t even have the option to update to ESP32 version 3.x on my IDE. That’s why I haven’t noticed anything strange.
Let me know your thoughts.
Regards,
Sara
Sorry it took so long to get back. Did you ever wish there were more hours in the day?
Anyway, I did try a few more things. Looking in the Espressif docs it has: “This function is used to setup LEDC pin with given frequency and resolution. LEDC channel will be selected automatically.” That’s new. So I tried the LED fade sketch that is provided, that worked. Then I set the sketch to do a two LED fade to see if it would automatically assign the channels. That worked also. But the docs still have the ledcAttachhannel function. I tried that, and I got an error when I compiled the sketch.
Just to make sure, I checked my Espressif board address in the Arduino IDE, that was correct. All very strange. Something has changed and I would have to think it’s by Espressif.
Around the time I started having problems, I had updated the OS on my Mac. I found out that update had removed Python from the OS. I had to use the terminal app, that I’m not very familiar with, to try and reinstall Python. I wasn’t absolutely sure I did it correctly.
What it comes down to is, I can make the PWM function work.
And the ESP32 boards version I’m using is 3.0.0-a
Thanks for the feedback, it’s been an interesting go around, and it may not be over yet.
Joe
Hi.
Can you tell me which link are you using in File > Preferences > Additional URLs??
I think you may be using one different than mine.
Regards,
Sara
Well, I went right past that part in the book. Making the assumption that I had the correct URL already. You know how that goes. After everything I had gone through, I was starting to suspect it was the compiler. The Arduino nano ESP32 wasn’t having the same problem, because it was using a different compiler.
It all started when I updated the OS on my Mac. For some reason Apple didn’t want to include Python and I had a hard time trying to figure out how to install Python in the new OS. Somewhere along the way, I deleted the Arduino IDE and reinstalled it. I don’t know where I got the new json file from, but it obviously wasn’t the same one. I haven’t changed it yet to the one in the book. It’s getting late. I’ll do it tomorrow. I’m thinking it will work better.
Anyway, this is what I had in my additional boards manager URL:
https://espressif.github.io/arduino-esp32/package_esp32_index.json
Thanks so much for the help,
Joe
I wish I could say everything works fine now, but it doesn’t. I changed the URL to the one in the book, but I still have the same problems. Maybe I screwed something up on my Mac when I tried to reinstall Python. I have no idea what Python does except it doesn’t work without it. Sketches like FastLED and ESP32 Servo-sketches come up with the same errors, but if I use the Arduino nano ESP32 board I get no errors when it compiles. Does changing the URL change the way the sketch gets compiled? Or do I have to do something like uninstall the Arduino IDE and reinstall it?
On a sidenote, I do have two ESP32s running WLED without a problem. I assume that uses a form of PWM also.
So it seems I’m the only one that has this problem. I just don’t know where the problem is. The obvious workaround is to invest more in the Arduino nano ESP32s. Though there are some limitations to those and they cost considerably more.
Joe
Hi.
What version of the ESP32 boards do you have installed?
If you go to tools > Board > Boards Manager, and search for ESP32, what is the version that you have? Double-check that you have the option from Espressif and not the other one. Can you double-check that?
Regards,
Sara
I have two ESP32 boards managers. The Arduino ESP32 boards and esp32 by Espressif Systems version 3.0.0-alpha2. Would it make a difference to go to an earlier version?
Yes.
Use version 2.0.14, which is the latest stable version and compatible with our projects at the moment.
Regards,
Sara
Thank you for your help!
What can I say. If I had just followed the instructions in the book, I would have saved myself a lot of frustration. Thank you for pointing out the obvious when it shouldn’t have been necessary. Yes, the Dimming an LED sketch works the way it’s supposed to now.
It’s a very humbling experience to have somebody point out the obvious to you – you are not following the instructions you were given. I will do so through the rest of the book.
Joe