Skip to content

analogWrite is not compliant with standard Arduino API #2895

Closed

Description

@Makuna

The standard Arduino API analogWrite() takes a value from 0-254. The Esp Arduino version defaults to a 10 bit value. This makes code incompatible between all other Hardware and leads to confusion on the part of users.

analogWrite() should be modified to default to 8bit and the standard analogWriteResolution() should be implemented to set the resolution to 10bit (and/or what ever else it can support).

Activity

  1. mAPBhlJ commented on Jan 24, 2017

    @mAPBhlJ

    #295

    Use the defined PWMRANGE.

    You could then go along and calibrate it accordingly: https://www.arduino.cc/en/Tutorial/Calibration

    (
    I somewhat agree that it's not 100% compliant, or should be documented better.

    I once thought it was my mosfets not working as good on 3.3V since it did work with an Arduino.
    But I had to scale the PWM to work with the ESP8266's range and now it works like a treat.
    )

  2. devyte commented on Sep 9, 2017

    @devyte
    Collaborator

    @igrr is this something that we want to do? I really don't like defaulting to 8bits and having to change to 10bits with a function call, it would break who know what out there, but the request has a point about compatibility with Arduino.

  3. igrr commented on Sep 9, 2017

    @igrr
    Member

    Maybe put this under "major breaking changes for new major release" category (i.e. if/when we bump the version to 3.0). I agree that this should be made Arduino compatible, at some point.

  4. Makuna commented on Sep 9, 2017

    @Makuna
    CollaboratorAuthor

    @devyte It currently is breaking stuff all over the place. Several libraries don't work out of the box with esp8266 due to this (internally they use analog write).

  5. igrr commented on Sep 10, 2017

    @igrr
    Member

    @Makuna can you mention some of these libraries? As in, it would probably take ~15min per library to send a PR to take PWMRANGE into account. That would be a reasonable short term solution, I think.

  6. devyte commented on Oct 4, 2017

    @devyte
    Collaborator

    @Makuna will you be providing the requested info?

  7. added
    waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.
    on Oct 4, 2017
  8. Makuna commented on Oct 4, 2017

    @Makuna
    CollaboratorAuthor

    The library issues came up in a maker class I was teaching where students applied specifics from their projects. We discovered the issues with several, but I don't have the links to them sorry. They were not Esp8266 libraries, they were for standard Arduino projects sort of thing that we were porting to esp8266.

    I just want to make sure its clear, the PWMRANGE is not an Arduino standard, and currently the AnalogWrite work is not Arduino compliant as it sits.

    If its not a priority to fix, then at least leave this issue open so others can get a good search hit as how they can work around it.

  9. majenkotech commented on Nov 11, 2018

    @majenkotech
    Contributor

    It is confusing for an Arduino user that expects analogWrite() to be 8 bit.

    • Here is one example where they are confused that analogWrite(pin, 255); doesn't act the same as digitalWrite(pin, HIGH);.

    On chipKIT we default to 8 bit and give analogWriteResolution() to change it to higher resolutions. We also provide the (non-standard, though very useful) analogWriteFrequency() function to change the fundamental frequency of the PWM - great for motor driving at 25kHz.

  10. added this to the 3.0.0 milestone on Jan 16, 2019
  11. added a commit that references this issue on Jul 14, 2020
    13f5dfb
  12. added a commit that references this issue on Jul 29, 2020
    a679869
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions