Closed vshymanskyy closed 2 years ago
Will you provide an Arduino-compatible analogWrite
based on one of these functions? Really convenient for compatibility with existing code examples
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.
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?
any update? Just hit this problem as well
There are work arounds using ledC. I uploaded a example into the GitHub repo:
You attached a pin to ledc, set it up, then you can call commands with ledc to update the output.
Over two years, and no fix? Seriously?
@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.
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.
@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.
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).
@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.
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
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?
it's a very important function ,is there anyone can rerewrite the lib?
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
Bump.
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!
I have create a small library that provide the classic analogWrite for ESP32
Excellent. I will have to give it a try.
@ERROPiX Do you have a plan to integrate your library codes into esp32/cores/ ?
This has gone far too long. Thanks @ERROPiX for providing us with a good wrapper.
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.
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)
FastLed is awesome for everything. Analog write is nice for anyone transitioning from the arduino world to esp32. Analog write would make a lot more examples carry over. But for all it’s worth. There are plenty of libraries that take car of PWM.
On Mon, May 20, 2019 at 7:35 PM Skickar notifications@github.com wrote:
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)
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/espressif/arduino-esp32/issues/4?email_source=notifications&email_token=AFAW7LWSPLUTAJK5ULJTXGLPWNNWPA5CNFSM4CSCDMFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV2SNYY#issuecomment-494216931, or mute the thread https://github.com/notifications/unsubscribe-auth/AFAW7LXETU4L467EEUV3YZ3PWNNWPANCNFSM4CSCDMFA .
@jandolina FastLED uses the RMT to send commands out on the esp32 and not analogWrite to create a PWM signal.
Damn, my bad
On Mon, May 20, 2019 at 8:33 PM Mike Dunston notifications@github.com wrote:
@jandolina https://github.com/jandolina FastLED uses the RMT to send commands out on the esp32 and not analogWrite to create a PWM signal.
— You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub https://github.com/espressif/arduino-esp32/issues/4?email_source=notifications&email_token=AFAW7LQ3CHZO47TSXQQMQXDPWNUP5A5CNFSM4CSCDMFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODV2UY2A#issuecomment-494226536, or mute the thread https://github.com/notifications/unsubscribe-auth/AFAW7LRT2GJKIM5N7VZUI53PWNUP5ANCNFSM4CSCDMFA .
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. :-/
@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.
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) https://github.com/platformio/platformio-core/issues/2597
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 (https://github.com/espressif/arduino-esp32/issues/4#issuecomment-433720705) worked.
The error in my case relied on #ifef which was not compiled properly
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 };
//change this to set PWM output resolution. 8--> 0~255 values
//#define USE_SIGMADELTA
void setupPWM(int pin) { pinMode(pin, OUTPUT);
sigmaDeltaSetup(_adc, PWM_FREQ); sigmaDeltaAttachPin(pin, _adc);
ledcSetup(_adc, PWM_FREQ, PWM_RESOLUTION); ledcAttachPin(pin, _adc);
ADC_MAP[pin] = _adc;
_adc = _adc + 1; }
void analogWrite(int pin, uint8_t val) { if(ADC_MAP[pin]==-1) setupPWM(pin);
ledcWrite(ADC_MAP[pin], val);
sigmaDeltaWrite(ADC_MAP[pin], val);
}
// the setup routine runs once when you press reset: void setup() { }
void loop() { analogWrite(9,128); }
@mrayy I think my solution is better, check this repository https://github.com/ERROPiX/ESP32_AnalogWrite
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.
Hello! Just expressing continued interest in adding analogWrite.
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?
Can you please add this? It should be really easy to do. It's a major inconvenience for lots of people.
Whoever downvoted Harvie's comment should just go implement AnalogWrite.
Whoever downvoted Harvie's comment should just go implement AnalogWrite.
@ERROPiX already did a brilliant job.
analogWrite() is actually a crucial Arduino command (like e.g. analogRead() and digitalWrite() ) and thus should be available for ESP by default.
alternatively and to delimit from "analogWrite DAC values" it might be helpful to implement both a function softPwmWrite() for pwm pins (see: wiringPi for Raspberry Pi) additionally to analogWrite() for DAC pins.
@ERROPiX already did a brilliant job.
So what is the problem with merging it upstream?
I ran into the same problem as @extesy and @mrayy did. This is such a core Arduino feature I was more than surprised to find it not there when I first went to use it.
@ERROPiX already did a brilliant job.
So what is the problem with merging it upstream?
According to @me-no-dev , only MWPWM can be used for analogWrite, however I am about to object that and say: Implement it with whatever wrapper possible and worry about perfecting it later.
@igrr @kedars @mahavirj @me-no-dev @projectgus @Spritetm Can you PLEASE push this bit harder? :-)
Oh wow 😲, didn't realise this was missing! I might look into some other boards then 😕...
The ESP32 is an amazing platform. I have been a big fan of this hardware for years. The analogWirte no native is not a dealbreaker. There are many PWM libraries to help you get you where you need to be. Yes it would be cool to have this function, but the platform offers so much that it is a non issue.
Start here: https://randomnerdtutorials.com/esp32-pwm-arduino-ide/
@wouterds @jandolina I have created a good wrapper library to work with analogWrite https://github.com/ERROPiX/ESP32_AnalogWrite you can install it on both Arduino IDE and PlatformIO
If you love the esp32 so much, why don't you go write an analog write love poem to it so everyone else can enjoy it too.
On Sun, Jan 12, 2020, 6:50 PM Joe Andolina notifications@github.com wrote:
The ESP32 is an amazing platform. I have been a big fan of this hardware for years. The analogWirte no native is not a dealbreaker. There are many PWM libraries to help you get you where you need to be. Yes it would be cool to have this function, but the platform offers so much that it is a non issue.
Start here: https://randomnerdtutorials.com/esp32-pwm-arduino-ide/
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/espressif/arduino-esp32/issues/4?email_source=notifications&email_token=AJTC7HIO7OI5QBMTMXYMDCLQ5PJHRA5CNFSM4CSCDMFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIXMNMQ#issuecomment-573490866, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJTC7HIHJMSHAM4RV4RJPH3Q5PJHRANCNFSM4CSCDMFA .
Power is red Radio is blue But I can’t dim my LED So AnalogWrite, I miss you
will be when the driver is ready