Skip to content
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

analogWrite() not implemented #4

Open
vshymanskyy opened this issue Oct 6, 2016 · 37 comments

Comments

@vshymanskyy
Copy link

commented Oct 6, 2016

No description provided.

@me-no-dev

This comment has been minimized.

Copy link
Member

commented Oct 7, 2016

will be when the driver is ready

@me-no-dev

This comment has been minimized.

Copy link
Member

commented Nov 13, 2016

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

@jandolina

This comment has been minimized.

Copy link

commented Jun 5, 2017

Just found some examples!
AnalogOut

The led stuff worked for me.

@jonnor

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Aug 25, 2017

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Dec 5, 2017

any update? Just hit this problem as well

@vseven

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Jan 4, 2018

Over two years, and no fix? Seriously?

@stickbreaker

This comment has been minimized.

Copy link
Collaborator

commented Jan 4, 2018

@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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Collaborator

commented Jan 5, 2018

@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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Collaborator

commented Jan 5, 2018

@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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Aug 16, 2018

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

@ERROPiX

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented Jan 4, 2019

Bump.

@ctnalves

This comment has been minimized.

Copy link

commented Jan 29, 2019

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

It's working perfectly! Thank you very much!

@Sod-Almighty

This comment has been minimized.

Copy link

commented Jan 30, 2019

I have create a small library that provide the classic analogWrite for ESP32

Excellent. I will have to give it a try.

@iZhangHui

This comment has been minimized.

Copy link

commented Feb 2, 2019

@ERROPiX Do you have a plan to integrate your library codes into esp32/cores/ ?

@alexceltare2

This comment has been minimized.

Copy link

commented Apr 30, 2019

This has gone far too long. Thanks @ERROPiX for providing us with a good wrapper.

@me-no-dev

This comment has been minimized.

Copy link
Member

commented Apr 30, 2019

this will not go into master. It is not the only PWM (or analogWrite) on the ESP32. There are far more reasons, but I think no point going into them. If anything goes in the master that is analogWrite, that would be MWPWM which does not have Arduino hal yet.

@skickar

This comment has been minimized.

Copy link

commented May 21, 2019

It seems like we have time to hear the reasons, because I'm here to say:

Please implement analogWrite so I can write cross-platform code for these microcontrollers.

(updated, 2019 request)

@jandolina

This comment has been minimized.

Copy link

commented May 21, 2019

@atanisoft

This comment has been minimized.

Copy link
Contributor

commented May 21, 2019

@jandolina FastLED uses the RMT to send commands out on the esp32 and not analogWrite to create a PWM signal.

@jandolina

This comment has been minimized.

Copy link

commented May 21, 2019

@exocode

This comment has been minimized.

Copy link

commented May 31, 2019

There are a few implementation proposals within the list of issues.

Why is that not implemented for 3 years? Does nobody use AnalogWrite? As @skickar pointed out: we need that for cross-platform development. :-/

@stickbreaker

This comment has been minimized.

Copy link
Collaborator

commented May 31, 2019

@exocode It seems that you have an interest in it, why don't you implement one of the proposals that will be compatible with analogWrite().

Chuck.

@exocode

This comment has been minimized.

Copy link

commented May 31, 2019

Hi @stickbreaker, I tried that, but without success. I wanna reuse the same codebase for different ESPs (ESP32 and ESP8266). It seems, that in my build process the ifdef statement is not working properly: You can see it here: (with example code) platformio/platformio-core#2597

@exocode

This comment has been minimized.

Copy link

commented Jun 1, 2019

Ok. It seems that the platform I use (platformio) had a problem within it's latest stable release. I installed the development version and itn (#4 (comment)) worked.

The error in my case relied on #ifef which was not compiled properly

@mrayy

This comment has been minimized.

Copy link

commented Jun 22, 2019

This is my solution for compatibility issue with this function. I hope it becomes a builtin function
////////////////////////////////////////////////////////////////////////////////////

int _adc = 0;
int ADC_MAP[48]={
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1
};

#define PWM_FREQ 32000
//change this to set PWM output resolution. 8--> 0~255 values
#define PWM_RESOLUTION 8
//#define USE_SIGMADELTA

void setupPWM(int pin)
{
pinMode(pin, OUTPUT);
#ifdef USE_SIGMADELTA
sigmaDeltaSetup(_adc, PWM_FREQ);
sigmaDeltaAttachPin(pin, _adc);
#else

ledcSetup(_adc, PWM_FREQ, PWM_RESOLUTION);
ledcAttachPin(pin, _adc);
#endif
ADC_MAP[pin] = _adc;

_adc = _adc + 1;
}

void analogWrite(int pin, uint8_t val)
{
if(ADC_MAP[pin]==-1)
setupPWM(pin);
#ifndef USE_SIGMADELTA
ledcWrite(ADC_MAP[pin], val);
#else
sigmaDeltaWrite(ADC_MAP[pin], val);
#endif
}

// the setup routine runs once when you press reset:
void setup() {
}

void loop()
{
analogWrite(9,128);
}

@ERROPiX

This comment has been minimized.

Copy link

commented Jun 22, 2019

@mrayy I think my solution is better, check this repository https://github.com/ERROPiX/ESP32_AnalogWrite

@mrayy

This comment has been minimized.

Copy link

commented Jul 1, 2019

Unless this became internally supported by ESP32, it will be very difficult to manage the channels when using third party libraries.
For example, I recently used ESP32_AnalogWrite library and ESP32_Servo library, which each library was using its own internal ledc channels management, resulting in a conflict that the same ledc channel being allocated by both libraries.

@sdaitzman

This comment has been minimized.

Copy link

commented Sep 4, 2019

Hello! Just expressing continued interest in adding analogWrite.

@extesy

This comment has been minimized.

Copy link

commented Sep 4, 2019

I had a same problem recently: I needed to control a servo and two motors and since the libraries were different (there's no single library to control both) they had their own conflicting implementations of analog write.

@me-no-dev Do you have any better suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.