-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[ESP8266 - core 2.5.0]: BUGS in analogWrite functions #5957
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
Comments
Correct. PWMRANGE is not some variable that can be changed, it's a macro defined to the capability of the underlying hardware. Therefore, analogWriteRange(PWMRANGE) sets the range to the max that the hw allows, while e.g.: analogWriteRange(400) sets the range to a lower value. A Note: I thought Arduino had the same interface, but I just checked, and it doesn't. Instead, it has analogWriteResolution(bits). I guess this could be implemented in terms of our current analogWriteRange(), i.e. along the lines of:
Our pwm is software-based, there is no pwm hw on the ESP. |
analogWrite(PIN, 0) in
So from my own experience and the code examination it looks like it is fine, too. |
Hi, thanks for your answer. So alnalogWrite(PIN, 0) should be the same as digitalWrite(LOW), right? |
Correct.
Yes.
Per @earlephilhower, it is the same as the three lines of code he said. Closing as user error for the first part and can't reproduce for the second. Also, your code is incomplete, and I see at least one bug in your i calculation, so also user error for the second part. |
I will check again with the new infomation in mind. |
Ok I checked the behaviour again.
The part1 The part2 |
And if I set |
I would say we should have a look for a better implementation. Because the reaching of the Endpoint HIGH and LOW depends on the PWMRANGE and the frequency. |
@hasenradball In the code referenced above for analogWrite(), I understand that high and low are the times the pin should be high and low within a pwm period. For val=1023: for val=0: For analogFreq=1000, analogScale=100, I see the following: For val=100: for val=0: In all cases above I think low==0 and high==0 is in fact being hit. Beware your logic:. In the code in the first post: I suggest thinking your code through carefully. |
Hi, I adapted the code today so that I reached in a loop 1023 and 0.
Your calculations looks right, but the Oszi show the difference. |
Ok the code of calculating high and low looks fine. Hypothesis: |
Our sw PWM is implemented as a specific case of the waveform generator. It is this which allows it to work at the same time as e.g.: the Servo class, or the buzzer. |
Thanks for your reply, |
@hasenradball Can you give a 2-3 line MCVE snippet with your specific analog frequency and values being written that fails? Some simple straight-line code without loops or calculations that doesn't do what you think it should? Something like:
That way we isolate any core issue from the calling app, and it's trivial to reproduce by a 3rd party. |
@earlephilhower Hi thanks for your reply. |
@earlephilhower Hi, ist it possible to send you a small video from the iphone to show you the issue I mean? The following issues I see on the scope:
BUT: setting without fading it seems to be fine. |
@earlephilhower Hi I think we can close the topic. The issue was a triggering issue on the scope. |
----------------------------- Delete above -----------------------------
Basic Infos
Platform
Settings in IDE
Problem Description
If you use the function analogWrite(PIN, PWM) the you will find the following issues:
1.) the change of PWMRANGE via analogWriteRange(new_range) is not possible!
-> if you set 1000 or 255 for example there ist no change -> only 1023.
2.) if you write analogWrite(PIN, 0) the voltage is not zero -> an LED will not go off
-> if you write digitalWrite(PIN, LOW) the LED goes off.
-> for my understanding digitalWrite(PIN, LOW) and analogWrite(PIN, 0) should have the same effect on the Pin!
-> checked via Oszi
MCVE Sketch
#include <Arduino.h>
// #include <ESP8266WiFi.h>
#define LED 2
#define LED2 4
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
//=======================================================================
// Power on setup
//=======================================================================
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
analogWriteRange(1023);
delay(100);
Serial.println();
Serial.println(PWMRANGE);
}
//=======================================================================
// Main Program Loop
//=======================================================================
void loop() {
delay(5000);
for(int i = 0; i < PWMRANGE; i = i + PWMRANGE/10) {
analogWrite(LED, constrain(i, 0, PWMRANGE));
analogWrite(LED2, constrain(PWMRANGE - i, 0, PWMRANGE));
Serial.print("PWM: ");
Serial.print(i);
Serial.print(" \n\n");
delay(2000);
}
digitalWrite(LED2, HIGH);
delay(10000);
digitalWrite(LED2, LOW);
delay(10000);
//Continuous Fading
Serial.println("Fadding Started");
while(1)
{
// set the brightness of pin 9:
analogWrite(LED, brightness);
analogWrite(LED2, PWMRANGE - brightness);
}
}
//=======================================================================
Debug Messages
The text was updated successfully, but these errors were encountered: