Skip to content

Pin not "writable" after tone() #5306

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

Closed
fabianoriccardi opened this issue Nov 4, 2018 · 4 comments
Closed

Pin not "writable" after tone() #5306

fabianoriccardi opened this issue Nov 4, 2018 · 4 comments

Comments

@fabianoriccardi
Copy link

fabianoriccardi commented Nov 4, 2018

Basic Infos

  • [ X] This issue complies with the issue POLICY doc.
  • [ X] I have read the documentation at readthedocs and the issue is not addressed there.
  • [X ] I have tested that the issue is present in current master branch (aka latest git).
  • [ X] I have searched the issue tracker for a similar issue.
  • [X ] If there is a stack dump, I have decoded it.
  • [ X] I have filled out all fields below.

Platform

  • Hardware: ESP-01
  • Core Version: 8ae0746
  • Development Env: Arduino IDE
  • Operating System: Windows

Settings in IDE

  • Module: Wemos D1 mini v3.0.0
  • Flash Mode: dio
  • Flash Size: 4MB
  • lwip Variant: v2 Lower Memory
  • Reset Method: ?
  • Flash Frequency: 40Mhz
  • CPU Frequency: 80Mhz
  • Upload Using: SERIAL
  • Upload Speed: 921600

Problem Description

I'm using tone(..) function to play some melodies on a passive buzzer (this is my breakout: https://robotdyn.com/buzzer-module.html). After the melody, I should standby the buzzer to save energy, so I have to write 1 on the digital pin. I observed that after the tone function is impossible to command that pin, it stays low. The problem is not hardware, since this behaviour is observable even without a buzzer...

MCVE Sketch

#include <Arduino.h>

void setup() {
  pinMode(D6,OUTPUT);
  digitalWrite(D6, HIGH);  // the pin really goes HIGH
  delay(5000);

  tone(D6,1000,500);
  delay(1500);
  noTone(D6);   // This makes no difference, I was thinking it is necessary to "release" the pin...
  
  pinMode(D6,OUTPUT);
  digitalWrite(D6, HIGH);  // It stays low
}

void loop() {

}
@earlephilhower earlephilhower self-assigned this Nov 4, 2018
@liebman
Copy link
Contributor

liebman commented Nov 4, 2018

I can confirm this. I modified the sketch to add a second pin to trigger a scope. The pin actually does go high but is then driven low less than 1ms later, I've seen 100us to 750us. I also tried adding a delay after noTone() to see if it was some delayed interrupt/timer but that made no difference.

#include <Arduino.h>

void setup() {
    pinMode(D5, OUTPUT);
    digitalWrite(D5, LOW); // Second scope channel used as trigger

    pinMode(D6,OUTPUT);
    digitalWrite(D6, HIGH);  // the pin really goes HIGH
    delay(5000);

    tone(D6,1000,500);
    delay(1500);
    noTone(D6);   // This makes no difference, I was thinking it is necessary to "release" the pin...

    delay(1500);
    
    pinMode(D6,OUTPUT);
    digitalWrite(D5, HIGH);  // trigger scope
    digitalWrite(D6, HIGH);  // pulses high and is changed low again after 750us
}

void loop() {

}

earlephilhower added a commit to earlephilhower/Arduino that referenced this issue Nov 4, 2018
The ISR could end up writing a 0 to a GPIO that had previously been
stopped, effectively overwriting user writes to those pins.  Fix to
only actually disable and write when a pin was enabled and times out.

Fixes esp8266#5306
@earlephilhower
Copy link
Collaborator

Thanks for the clear MCVE. The ISR was disabling the pin and writing 0 every time it was called, even if it was already disabled. Check the PR if you can, but I've verified it on my own logic analyzer.

@fabianoriccardi
Copy link
Author

Yeah the fix works for me, thanks!

I'm new with this procedure (pull request, complex versioning and so on...) but I would ask if this fix will be included in version 2.5.

devyte pushed a commit that referenced this issue Nov 5, 2018
The ISR could end up writing a 0 to a GPIO that had previously been
stopped, effectively overwriting user writes to those pins.  Fix to
only actually disable and write when a pin was enabled and times out.

Fixes #5306
@devyte
Copy link
Collaborator

devyte commented Nov 5, 2018

PR is merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants