espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.55k stars 7.39k forks source link

analogWrite() not implemented #4

Closed vshymanskyy closed 2 years ago

AlanGreyjoy commented 4 years ago

omg. I have to unsub from this now... but what dsyleixa just said is 100% true.

Harvie commented 4 years ago

People who use the Arduino IDE for either boards are 99% beginners and hobby users and mostly do not even understand neither real C++ coding nor RTOS coding

That's what i was saying. Then they don't need all the advanced optimizations, because they will not get 100% performance with that attitude anyways... You will not get that with Arduino UNO / atmegas either. analogWrite() does not have to be 100% perfect.

chegewara commented 4 years ago

It is very long issue, but i see some confusion here about signals. Analog signal is when you can control peak to peak, and with PWM or LEDC we cant do that. It still will be digital signal. esp32 has only 2 analog out pins and i believe only those pins can be used for analogWrite. To prove my point try to connect LEDS or PWM driven "analog" signal to scope or to analog IN pin on esp32 and try to get readings. What will be the result?

chegewara commented 4 years ago

Why has that issue topic been closed? It is not resolved yet and thus has to be kept open UNTIL it will be solved! So please reopen this issue!

Where do you see it has been closed? Screenshot from 2020-09-30 11-44-17

dsyleixa commented 4 years ago

analogWrite() generally works on Arduino DIGITAL pins, where a pwm signal is generated, and the argument is an integer value for the pwm value. In this respect the Arduino API function name is misleading, it better had been named "pwmWrite" instead, but it wasn't, so it is as it is. Additionally, there is kind of overloaded functionality for Arduinos which provide also analog voltage output on DAC pins (e.g., like the Arduino Due): on those DAC pins analogWrite creates a "real" analog voltage level from a passed integer value within a defined range.

ghost commented 3 years ago

Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

voltmetru:2:46: fatal error: ESP32 WiFi.h: No such file or directory

compilation terminated.

exit status 1

ESP32 WiFi.h: No such file or directory

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

HOW TO FIX THIS NEVER SOLVED?? I HAVE TRIED TO MY BEST

chegewara commented 3 years ago

Arduino: 1.8.13 (Windows Store 1.8.42.0) (Windows 10), Board: "ESP32 Dev Module, Disabled, Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS), 240MHz (WiFi/BT), QIO, 80MHz, 4MB (32Mb), 921600, None"

voltmetru:2:46: fatal error: ESP32 WiFi.h: No such file or directory

compilation terminated.

exit status 1

ESP32 WiFi.h: No such file or directory

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

HOW TO FIX THIS NEVER SOLVED?? I HAVE TRIED TO MY BEST

Maybe this is stupid question of mine, but how it is related to analogWrite?

alexceltare2 commented 3 years ago

Roses are red Flowers are mixed I've got a notification But analogWrite's not fixed

Maximilian4711 commented 3 years ago

It is very long issue, but i see some confusion here about signals. Analog signal is when you can control peak to peak, and with PWM or LEDC we cant do that. It still will be digital signal. esp32 has only 2 analog out pins and i believe only those pins can be used for analogWrite. To prove my point try to connect LEDS or PWM driven "analog" signal to scope or to analog IN pin on esp32 and try to get readings. What will be the result?

There is no ressource problem according to true analog outputs G25 and G26. No timers or PWM is needed. So why is there no TrueAnalogWrite function for G25 and G26 implemented yet?

dsyleixa commented 3 years ago

There is no ressource problem according to true analog outputs G25 and G26. No timers or PWM is needed. So why is there no TrueAnalogWrite function for G25 and G26 implemented yet?

the Arduino API function analogWrite() is mainly designed for writing pwm to digital pins (e.g., motor control by L298 H-bridges), and just subordinatly also for true analog voltages to DAC pins (which almost all of the Arduino boards actually not even are providing). So when we are talking here about analogWrite() then it's actually meant for applying pwm to digital pins, and that's what's still missing for ESP32 boards: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/ https://www.arduino.cc/en/Tutorial/Foundations/PWM

Maximilian4711 commented 3 years ago

Thanks to bitluni (https://bitluni.net/oscilloscope-as-display) it shows that true analog write for ESP32 G25/26 is already there :)

dsyleixa commented 3 years ago

Thanks to bitluni (https://bitluni.net/oscilloscope-as-display) it shows that true analog write for ESP32 G25/26 is already there :)

AFAIK it uses functions dac_output_enable() and dac_output_voltage(), but this topic is mainly about PWM-write!!

jumper-d-1981 commented 3 years ago

I was recommended to get a ESP32 because of the ease of OTA, and after spending 20 bucks and waiting almost a month for it to arrive I can not get it to analogwrite to save my life. I see where someone made a library but it is for the onboard led from what I can see...has anyone got a clear walkthough on how to get this or some similar functionality added to these things?

I am stuck at this point like so many others...any ideas?

bperrybap commented 3 years ago

I am stuck at this point like so many others...any ideas?

Switch to the esp8266 as it is not an issue on that platform. I've also run into a few other issues on the ESP32 that are not issues on the esp8266.

vseven commented 3 years ago

I am stuck at this point like so many others...any ideas?

I posted on this thread a couple years ago as I was also having problems but there are plenty of work arounds. https://github.com/espressif/arduino-esp32/issues/4#issuecomment-349358312

If the code is relatively simple it shouldn't be hard to change to adapt to the ESP32. Personally I can't see going back to a "old" 8266 at this point.

lbernstone commented 3 years ago

If you search for "analogWrite" in the library manager, you will see the ESP32Servo library. Install it, #include <analogWrite.h>, and analogWrite should work as per Arduino spec.

jumper-d-1981 commented 3 years ago

Well I did discover the ESP32 library called ESP32 AnalogWrite. I include this particular one which does allow my code to compile without errors but it still won't write HIGH to those three pins 9,10,11 though they are called something else on this board's little tiny writing...does that matter? It compiles and still allows the web interface to work(trying OTA) but the light will not change...

bperrybap commented 3 years ago

Personally I can't see going back to a "old" 8266 at this point.

And yet, I can't see moving to the "new" ESP32 platform that has issues that won't run code due to various issues that are not issues on the esp8266 nor any other Arduino platform.

IMO, at this point in time, for hobby/personal projects, I would not use the ESP32 over the esp8266 unless there was some driving h/w need to have something critical for the project that the ESP8266 doesn't have like bluetooth. This is just my opinion, but it is based on several s/w issues(not just this analogWrite() issue) that I've run into and had to wrestle with and work around. There were several issues I had to deal with to get an LCD library that I have that works on pretty much every Arduino platform to work on the ESP32 platform. Sure, there are often issues in 3rd party platforms where the platform code is incomplete or implemented something incorrectly that has to be worked around until the platform code is fixed, but so far, the ESP32 platform has had the most issues that I've had to work around and some like the issue with analogWrite() cannot be worked around in just my library code so I had to disable backlight dimming in my LCD library when using the ESP32 platform.

This issue in particular is quite sad, as analogWrite() is an Arduino core library function that should have come with the ESP32 platform when it originally released or fixed and supported in platform code LONG ago.

a working analogWrite() comes with the esp8266, but yet it doesn't come with the "new" ESP32.

Sod-Almighty commented 3 years ago

Yeah, and look when this issue was opened. And what issue number it is. Pathetic.

jumper-d-1981 commented 3 years ago

I am relentless....for now. I have completed reloaded the absolute bare min. code. Just trying to get it to turn on my LED break out board with the RGB LED. I am using what would be called pin 9,10 and 11 on the regular Arduino boards...and I was told that the mapping of pins is the same...but all I manage is pin 10 being on by default no matter what code I throw at it. So in this case my blue light is on but even just kicking the other pins to HIGH won't turn it on like in other controllers. I call it JUNK...but I am not done just yet.

jumper-d-1981 commented 3 years ago

122 lines. That is apparently what it takes to get this thing working. I found another LEDC library that actually does work...though the pins are not uniformly sequential like on the UNO that I am use to...

So after working out which hole to use...it is successfully turning on and off each color.

I am going to tweak a little but the resolution on this model looks a lot worse than that of even the cheapest UNO or NANO. I am increasing and decreasing the stregnth of light by a very small increment at a time but it still looks like it is vibrating or shaking...

Well, I will not buy this again.

What is your opinion on a micro controller that supports bt and wifi but actually works without issues? I have two projects in mind. A remotely deployable camera with sd recording and wifi interface...and a robot creature for my kid to make...thought this would work but if everything is this difficult on this one, it will likely be tossed or recycled.

atanisoft commented 3 years ago

@jumper-d-1981 pins 6-11 can not be used as they are connected to the on-board flash. If you use them you will likely either corrupt the flash or crash the esp32

vseven commented 3 years ago

I'm going to leave one more comment before I unsubscribe to notifications on this very very old issue. Mainly cuz all I hear is people complaining anymore. There are ways around not having a built in analogwrite that are pretty easy as already pointed out. When I started with the esp32 I knew nothing about the platform, nothing about the 8266, nothing really about programming for them, and I was able to figure it out for my projects.

I now have multiple esp32s doing LED work. One is connected to SmartThings and controlling multiple programmable LEDs using an interface for fast LED. Another is controlling good old analog RGB LEDs using the LEDC stuff. Both work great. It's also real nice to be able to turn on Bluetooth logging, throw a Bluetooth serial program on my phone, and watch the log output using that instead of plugging a computer into the controller when needed. I haven't gotten to point of actually sending commands through Bluetooth but I know it's possible and from what I've done just in logging I don't I think it would be that difficult. 8266 can't touch it in this regard

I have "back ported" things ive written on the esp32 back to the esp8266 and maybe it's because I went all in on the esp32 but for me it seems easier. The first couple ESP 32 hardware revisions were meh but the newer chip revisions and software have been rock solid for me. I also have the need for multiple analog inputs converting temperature sensors that output voltage that are extremely accurate and the ESP32 excels at this over the io limited 8266.

I'm not going to go backwards at this point.

Harvie commented 3 years ago

image

jumper-d-1981 commented 3 years ago

I came here for the help...imastayforthememes

jumper-d-1981 commented 3 years ago

I got it. Thanks to the person who told me about the flash pins not being able to do pwm without issues...that solved this issue. I got it over on three pins that are not paired with that and it just works. the resolution and stepping is smooth now too.

sorry to bug you all, much appreciated...

Harvie commented 3 years ago

image

Sod-Almighty commented 3 years ago

Perhaps if the issue wasn't so "very very old" - and had actually been fixed - people wouldn't be complaining so much.

I'm aware of the difficulty in selecting a "canonical" implementation, but way I see it, even an empty analogWrite() stub would be better than the current situation - to wit, no analog code written for Arduino will even compile on this core.

But instead, you say "this issue is very old" and "all I see here is complaining". Well? This issue has existed for FIVE YEARS and we are no further toward a solution. "Complaining" is what happens when you report a bug and then nothing is done for five years. What did you expect?

Sod-Almighty commented 3 years ago

Yes, let's just use this bug page as a dumping ground for juvenile memes. THAT'LL get our point across, I'm sure.

I'm unsubscribing from this train wreck as well. Clearly the devs don't GIVE A SLOPPY SHIT (go on, report me, see if I care) about fixing things. And now the memes (literally "dumb things posted repeatedly in the hope that they will, through repetition, become witty") have arrived. Goodbye.

Dlloydev commented 3 years ago

I should have a new analogWrite library ready in a few days. This should co-operate fine with sidgmadelta and pwm or any code that has already accessed a GPIO pin.

Board PWM Pins DAC Pins PWM Frequency PWM Resolution
ESP32 2, 4, 5, 12-19, 21-23, 27, 32, 33 DAC1, DAC2 5000 Hz default 13-bit default, 8-bit DAC
ESP32S2 1- 14, 21, 33-42 DAC1, DAC2 5000 Hz default 13-bit default, 8-bit DAC

The AnalogWrite library has been added. arduino-library-badge GitHub link ... https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite

EDIT: Now with phase control!

3-phase PWM example:

  analogWrite(4, 341, 100, 10, 0);
  analogWrite(5, 341, 100, 10, 341);
  analogWrite(12, 341, 100, 10, 682);

image

VojtechBartoska commented 3 years ago

We are tracking this issue and considering it in 2.0.0 but it will cause a lot of changes in APIs.

bperrybap commented 3 years ago

We are tracking this issue and considering it in 2.0.0 but it will cause a lot of changes in APIs.

I assuming you are meaning mostly internal APIs that are not exposed to user sketches, since the official Arduino API and associated detection macros for analogWrite() are quite clear and have not changed in well over 10 years.

i.e. you can create a new APIs or function overloads to support a true analog function, but you must make sure that the goofy analogWrite() as defined by arduino.cc which does pwm works as they defined it.

bperrybap commented 3 years ago

What is most frustrating on this issue, is that as I indicated a while back there are macros that define whether or not analogWrite() works on a pin, and the core didn't even bother to implement them so that code could continue to compile & link and even compile time detect that analogWrite() does not work and deal with it accordingly.

Dlloydev commented 3 years ago

Something to consider is making the hpoint value more accessible as this can be used to provide PWM phase shift control. Might come in handy for discrete RGB LED color mixing, and generating unique signal patterns, quadtrature signals, etc. I realize that multiple channels are not synchronized (that's a job for MCPWM) but at lower frequencies a few µs here or there becomes less important depending on the application.

pedrominatel commented 2 years ago

analogWrite PR Link: https://github.com/espressif/arduino-esp32/pull/5861

Harvie commented 2 years ago

@pedrominatel image

VojtechBartoska commented 2 years ago

Hello all contributors of this request,

AnalogWrite was already implemented based on LedC with PR #5861.

This implementation has some limitation so we created a new issue #6544 and add it to our Roadmap.

If you are interested you can follow newly created issue and I'm closing this one.

Thanks for your patience and help.

Harvie commented 2 years ago

Thank you very much!

This is actually all i've needed. At least some basic functionality. Because i only use arduino API for some simple things and preliminary hardware tests. For anything serious i prefer to use espressif API directly anyway....

VojtechBartoska commented 2 years ago

@Harvie We are glad that this basic functionality works for you. If you are interested, feel free to comment newly created issue for full implementation.

dsyleixa commented 2 years ago

does analogWrite() now finally truly work like the Arduino API implementation both a) for digital output pins to generate pwm signals? b) for analog DAC pins to generate an analog DC voltage?

analogWrite() Description

Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite()) on the same pin.

  • In addition to PWM capabilities on the pins noted above, the MKR, Nano 33 IoT, and Zero boards have true analog output when using analogWrite() on the DAC0 (A0) pin. ** In addition to PWM capabilities on the pins noted above, the Due has true analog output when using analogWrite() on pins DAC0 and DAC1.

You do not need to call pinMode() to set the pin as an output before calling analogWrite(). The analogWrite function has nothing to do with the analog pins or the analogRead function.

ref.: https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

VojtechBartoska commented 2 years ago

@dsyleixa as mentioned above, full implementation is tracked in newly created issue #6544

dsyleixa commented 2 years ago

thanks, but then I don't see what has been implemented actually and what not, tbh...

bperrybap commented 2 years ago

I would say that this issue should not be closed until users can at least run the included Fade.ino sketch that comes with the IDE. The Fade sketch still fails to compile for me with the latest released ES32 platform core, so I would say that this still an issue. The Fade sketch can be found here: Examples->01.Basics->Fade

m5it commented 2 years ago

Hola. I have updated Arduino IDE from 1.8.xx to 2.0.0 and analogWrite on ESP32 dont not work anymore. before was.. :x

me-no-dev commented 2 years ago

Hola. I have updated Arduino IDE from 1.8.xx to 2.0.0 and analogWrite on ESP32 dont not work anymore. before was.. :x

The IDE does not control those features. Which core version did you install?

dsyleixa commented 1 month ago

by chance I found this Arduino lib: https://github.com/ERROPiX/ESP32_AnalogWrite

will it work with all ESP32 repo versions? has anyone checked it?

Harvie commented 1 month ago

@dsyleixa have you tried the thing merged 3 years ago? https://github.com/espressif/arduino-esp32/pull/5861/files

dsyleixa commented 1 month ago

I do not know that - where is the example code? which board version? (finally the Basics Fade.ino example does not compile at all, I always work with core 1.06 because of major code incompatibilities since 2.0 ff.))