Skip to content

analogWrite() not implemented #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vshymanskyy opened this issue Oct 6, 2016 · 148 comments
Closed

analogWrite() not implemented #4

vshymanskyy opened this issue Oct 6, 2016 · 148 comments
Labels
Status: Solved Type: Feature request Feature request for Arduino ESP32

Comments

@vshymanskyy
Copy link

No description provided.

@me-no-dev
Copy link
Member

will be when the driver is ready

@me-no-dev
Copy link
Member

two types of analogWrite are now implemented through SigmaDelta and LEDC.
Headers for the relevant drivers are here and here

@me-no-dev me-no-dev added the Status: To be implemented Selected for Development label Nov 16, 2016
@jandolina
Copy link

jandolina commented Jun 5, 2017

Just found some examples!
AnalogOut

The led stuff worked for me.

@jonnor
Copy link

jonnor commented Jul 24, 2017

Will you provide an Arduino-compatible analogWrite based on one of these functions? Really convenient for compatibility with existing code examples

@Mendes11
Copy link

Is there any news about this? It's labed as 'to be implemented' but what is the priority of this? It would really helpful to have this function so it can be compatible with some libs that use it.

@vseven
Copy link
Contributor

vseven commented Sep 10, 2017

Just tried to move some code over from a ESP8266 to my shiny new ESP32 and had the same issue with analogWrite not being implemented. Will this be fixed any time soon?

@monteslu
Copy link

monteslu commented Dec 5, 2017

any update? Just hit this problem as well

@vseven
Copy link
Contributor

vseven commented Dec 5, 2017

There are work arounds using ledC. I uploaded a example into the GitHub repo:

https://github.com/espressif/arduino-esp32/blob/master/libraries/ESP32/examples/AnalogOut/ledcWrite_RGB/ledcWrite_RGB.ino

You attached a pin to ledc, set it up, then you can call commands with ledc to update the output.

@Sod-Almighty
Copy link

Over two years, and no fix? Seriously?

@stickbreaker
Copy link
Contributor

@Sod-Almighty different hardware. a direct equivalent implementation is impossible. There are other methods to achieve the same functionality, but they are not exactly compatible.

You have to adjust your code to use either SigmaDelta or LEDC. Both work but they are not drop in. You as the programmer have to decide how to handle the differences.

Chuck.

@vseven
Copy link
Contributor

vseven commented Jan 5, 2018

As posted see my example. If you are programming for both say both a ESP8266 and a ESP32 you can use some if then statements to determine which to use. For example a RGB LED strip control program I have attaches the pin to use in the setup:


		#if defined(ARDUINO_ARCH_ESP32)
			ledcAttachPin(m_nPinR, m_nChannelR);
  			ledcSetup(m_nChannelR, 5000, 8);
		#else
			pinMode(m_nPinR, OUTPUT);
		#endif

Then when I need to write to the output:

		#if defined(ARDUINO_ARCH_ESP32)
			ledcWrite(m_nChannelR, subStringR);
		#else
			analogWrite(m_nPinR, subStringR);
		#endif

Although I will disagree with @stickbreaker - nothing is impossible. There has to be a way to add a wrapper for analogWrite that just does the LedC stuff for you but it doesn't look like that's going to happen.

@stickbreaker
Copy link
Contributor

@vseven so,
If I use your defines, I can get a 490hz pwm signal on 4 different pins, each one with a different 8bit duration? (Arduino Uno 3, 9, 10, and 11) and a 980hz PWM signal on 2 different pins (Arduino Uno 5,6) Also with different duration's?

I do not see how the following Arduino Uno commands analogWrite(3,25); analogWrite(5,26); could be directly translated.

To directly translate from analogWrite() to ledcWrite() every interaction of of the Arduino timers would have to be emulated:

TCCR2B = (TCCR2B & 0b11111000) | 6;

I have only used analog Write for one motor controller; a brushed 8 Amp gear motor. I had to adjust the PWM freq way down before the motor could operate. So I reconfigured the base PWM to 122hz with this TCCR2B value. This allowed me to use analogWrite(3,x) to successfully operate this motor. But, I cannot expect this same command to set the ESP32's pin 3 PWM freq to 122hz.

I believe that making a partial translation would be more of a hindrance than benefit.

I know that the ESP32 is different, so to successfully set a PWM freq to 122hz I must Learn how to configure the ESP32 to achieve this goal.

Chuck.

@vseven
Copy link
Contributor

vseven commented Jan 5, 2018

Although I made the ledC example sketch for the ESP32 it was more of a learning thing then anything. I've never programmed a Uno controller, I've never actually used the analogWrite command for that matter. I've only used the ESP32 and then I modified my code to also support ESP8266 chips (using analogWrite) so I don't know the detailed ins and outs. I just know that for a ESP32 you have to add in the ledC PWM "channel" and tie it to your output. You can then modify the channel for your frequency and resolution.

Like I said I'm just using it for relatively simple analog RGB LED control and my code works for both the ESP32 and 8266 without having to modify anything. Check out the sketch above. It might help (or not).

@stickbreaker
Copy link
Contributor

@vseven Like you have experienced, the ESP32 hardware is not equivalent to the AtMega328p. So to effectively use it you have to Learn the differences and exploit them. Your example is a good reference of how to achieve a similar result. My harping that the programmer must discover/learn the ESP32 hardware to maximize his effectiveness is my hardware background showing through.

Thank you for your assistance. I have not needed analogWrite() yet on the ESP32 so I am ignorant on the work-a-rounds.

Chuck.

Lauszus added a commit to felis/USB_Host_Shield_2.0 that referenced this issue Jan 14, 2018
@K1ngjulien
Copy link

K1ngjulien commented May 21, 2018

I have just had an issue with the LiquidCrystal which wouldn't compile because it was missing the analogWrite function.

I added to #define analogWrite ledcWrite the top of the library to replace it with the ledc version and it works without issues now.

Might be a quick fix for anyone wanting to use existing arduino code on their esp32

Note: This was using PlatformIO. In the arduino IDE it just worked without any special defines

@capedra
Copy link

capedra commented Jul 20, 2018

Wow, it looks like this is the oldest opened issue on espressif/arduino-esp32.
analogWrite() is really important. How and when may we solve this issue?

@hznupeter
Copy link
Contributor

it's a very important function ,is there anyone can rerewrite the lib?

@erropix
Copy link

erropix commented Oct 28, 2018

Hi @me-no-dev

I have create a small library that provide the classic analogWrite for ESP32, please test it and give me your feedback

Repository https://github.com/ERROPiX/ESP32_AnalogWrite

@ricardoboss
Copy link

Bump.

@Dlloydev
Copy link

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
Copy link
Member

analogWrite PR Link: #5861

@Harvie
Copy link

Harvie commented Nov 9, 2021

@pedrominatel
image

@VojtechBartoska
Copy link
Contributor

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.

Repository owner moved this from Todo to Done in Arduino ESP32 Core Project Roadmap Apr 6, 2022
@Harvie
Copy link

Harvie commented Apr 6, 2022

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
Copy link
Contributor

@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
Copy link

dsyleixa commented Apr 7, 2022

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
Copy link
Contributor

VojtechBartoska commented Apr 7, 2022

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

@dsyleixa
Copy link

dsyleixa commented Apr 7, 2022

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

@bperrybap
Copy link
Contributor

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

@ifrew ifrew mentioned this issue Jun 5, 2022
1 task
@m5it
Copy link

m5it commented Sep 26, 2022

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
Copy link
Member

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
Copy link

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
Copy link

Harvie commented Sep 15, 2024

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

@dsyleixa
Copy link

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.))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Solved Type: Feature request Feature request for Arduino ESP32
Projects
Development

No branches or pull requests