analogWrite on ESP32

Posted on
  • Hello,

    I just tried to perform analogWrite on a Widora air and it looks like it's not supported. Or is it something I am doing wrong?

    >digitalWrite(D26, true) // LED lights up
    >analogWrite(D26, 1) // LED dims


  • Sat 2018.11.24

    Hello Ronald,­bal_analogWrite

    What is it you are attempting to do? Please post link to tutorial/example etc so we may follow along.

    L3 >analogWrite(D26, 1) // LED dims

    Are you asking why an LED dims when controlling with an analog/PWM voltage?

    What does the circuit look like? Link or schematic image please

    Did you set pinMode() ?

  • I just try to control the brightness of an LED, similar to this:­g, just basic stuff. I did not set the pinMode, because analogWrite will set it to 'output'.

    Oh... with LED dims I mean it turns black. And, even more surprising, after that it does not light up anymore with digitalWrite(D26, true)...

  • Ronald,
    With that link then, are we talking Arduino 'C' or Espruino Javascript?

    'even more surprising, after that it does not light up anymore with'

    There is a current limiter of at least 220 ohm?

    Test the LED out of circuit with the limiter in series.

    As this is a non-authentic Espruino board, have you verified the pin you are on supports PWM or DAC ?

  • No worries, no LED are being killed here. :-)

    I just trying to dim an LED in the console of the the Espruino Web IDE. For what I understand GPIO26 supports DAC.­idf/en/latest/api-reference/peripherals/­dac.html, correct?

    I just did the same on GPIO4 (a PWM output) of an ESP8266 which works as expected.

  • 'no LED are being killed here'

    Thank goodness, wouldn't want to 'tee off' ASPCA - PITA


    Does cross check with:­eb-ide

    As you have a 'Widora air' board, what does that manufacturer data sheet say?

    You may need their schematic to see if their GPIO DAC/PWM pin goes to which header pin.

    Also, analogWrite() has a third argument, see link #2 above. Have you attempted using that?

  • This is the board:­f

    It exposes all the ESP32 pins, and also GPIO26.

    Did you see analogWrite working on any ESP32? What would you recommend for the third argument?

  • 'Did you see analogWrite working on any ESP32?'

    No, I have no practical experience with analogWrite() on the ESP32

    Suggests keeping above 50Hz to avoid flicker to 'fool our senses' though.

    Third argument was just a thought as:

    doesn't specifically indicate 'Widora air' unless one has verified the 'Xtensa' chip for the ESP32 category

    Re-read 'Digital Read / Write Example' and 'Problem'­eb-ide

    "PWM on most pins. True DAC on GPIO25 and GPIO26"

  • Thanks for helping me think this trough and for pointing me to some remarks in the documentation. I will dive into this.

    In the mean time can who knows how to get DAC or PWM working on an ESP32 please elaborate a bit?

  • Sun 2018.11.25

    'how to get DAC or PWM working on an ESP32'

    Google is your friend. Type your question and add the 'site:' qualifier like this:

    At Google: how to get DAC or PWM working on an ESP32

    There are more than ten supporting articles with and many others without the 'site:' qualifier

  • Yeah... I know how google works.

  • Hi Rovale,

    analogWrite, should work on the ESP32, depending on what version you are using. I've used it a fair amount. I know in recent versions of Espruino (for various reasons -- changes in the Espressif SDK specifically), there have been a few features that are currently regressed (such as WS2812 support), on the ESP32. But I did not notice anything about the analogWrite specifically.

    That said, unless I am mistaken, the analogWrite is a software feature -- not a hardware one -- so its timing is not super precise (as opposed to say, if it were interrupt driven). So its possible that setting the value after the PIN parameter causes the LED to dim, due to timing issues, though I have not specifically tried to drive an LED using this feature on the ESP32.

    I did try using analogWrite on the ESP8266 about a year or so ago, and it usually led to LEDs having a slight dimming. Specifically I was trying to control an RGB LED (not a WS2812, but one LED with three differently colored diodes, fed by three output pins), and there were issues with color consistency and dimness which I attributed to the software implementation.

    My suggestion would be to use digitalWrite for off and fully on, and then analogWrite for dimming or brightening the LED in between. Unless you need a luminosity profile that is extremely precise, it should be good enough for most projects I would imagine.

    More info here:

  • Hello Patreon,

    Thanks for your response. The analogWrite appears to work on a non DAC pin, like GPIO5. On a DAC pin, like GPIO26 it will only turn off the LED. It's a bit confusing.

    Maybe it will work in a next version, I am OK with this outcome.

  • It looks like ESP32 has actual DAC hardware on gpio 25/26:­idf/en/latest/api-reference/peripherals/­dac.html

    By default analogWrite will use software PWM, but on pins 25/26 it tries to use the DAC.

    It's possible there is some problem with Espruino's DAC implementation - maybe the pin output mode isn't being set correctly?

  • Thanks for your reply. I just added an issue for this:­ues/1574

  • Thr 2018.11.29

    Seeking clarification from someone with ESP32 experience please.

    I have minimal knowledge of the ESP32 and have no practical experience using it. As a total noob, just trying to understand the event unfolding process of this thread here.

    Correct me if I mis-understand the following please, as I'm adding to my knowledge base:

    My understanding is the ESP32 is a 'category' representing many manufactures of board derivations. Specifically the Tensilica Xtensa LX6 microprocessor.


    There are several individuals, whose input is used to create a port of Espruino for the ESP32 and a few of the many board possibilities are supported. Their efforts are to be commended for the resultant work that Espruino on ESP32 is what it is today. Thank you.

    Rovale was able to use analogWrite() successfully on a separate Xtensa board,

    ref #5 'on an ESP8266 which works as expected'

    ref #1 The stated issue is, with an 'Widora air'

    which doesn't appear to be a known working (tested on) board, unless one forms the opinion that any Xtensa board has to work as it is listed as an ESP32

    Wikipedia also indicates 'Compact ESP32 development board'

    This implies to me a limited instruction set or limited hardware feature list.

    From Issue 1574

    'It seems DAC does not work on the espruino_2v00_esp32'

    As that code snippet worked on another type of ESP32 using Espruino 2v0, isn't the above statement from the filed issue, pointing the finger at the wrong culprit, e.g. isn't this a hardware issue on a non-tested, non-supported board instead?

    Shouldn't it state something along the line of that the issue is with a specific board manufacturer, rather than an issue with the Espruino source?

  • ESP8266 and ESP32 are totally different chips and ports of Espruino despite using the same Architecture. Much like nRF52 and STM32.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

analogWrite on ESP32

Posted by Avatar for Rovale @Rovale