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 is not compliant with standard Arduino API #2895

Closed
Makuna opened this issue Jan 23, 2017 · 8 comments · Fixed by #7456
Closed

analogWrite is not compliant with standard Arduino API #2895

Makuna opened this issue Jan 23, 2017 · 8 comments · Fixed by #7456
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Milestone

Comments

@Makuna
Copy link
Collaborator

Makuna commented Jan 23, 2017

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

@mAPBhlJ
Copy link

mAPBhlJ commented Jan 24, 2017

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

@devyte
Copy link
Collaborator

devyte commented Sep 9, 2017

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

@igrr
Copy link
Member

igrr commented Sep 9, 2017

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.

@Makuna
Copy link
Collaborator Author

Makuna commented Sep 9, 2017

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

@igrr
Copy link
Member

igrr commented Sep 10, 2017

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

@devyte
Copy link
Collaborator

devyte commented Oct 4, 2017

@Makuna will you be providing the requested info?

@devyte devyte added the waiting for feedback Waiting on additional info. If it's not received, the issue may be closed. label Oct 4, 2017
@Makuna
Copy link
Collaborator Author

Makuna commented Oct 4, 2017

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.

@majenkotech
Copy link
Contributor

majenkotech commented Nov 11, 2018

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.

@earlephilhower earlephilhower added this to the 3.0.0 milestone Jan 16, 2019
earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Jul 14, 2020
Matching standard Arduino cores, make the default analogWrite() take
values from 0...255.  Users can always use the analogWriteRange() call
to change to a different setup.

Fixes esp8266#2895
earlephilhower added a commit that referenced this issue Jul 29, 2020
Matching standard Arduino cores, make the default analogWrite() take
values from 0...255.  Users can always use the analogWriteRange() call
to change to a different setup.

Add a `analogWriteResolution` which takes a number of bits and sets
the range from 0...(1<<bits)-1, part of the standard Arduino API.

Remove the PWMRANGE define.  It's non-standard and not generally valid
(i.e. it's fixed at 1024 of 256, but the real range varies depending on
what you last set).

Also add note about the change and how to fix pre 3.0 applications.

Fixes #2895
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for feedback Waiting on additional info. If it's not received, the issue may be closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants