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

Butterfly analogWrite and pinMode conflicts #48

Open
jacky4566 opened this issue Sep 17, 2019 · 8 comments
Open

Butterfly analogWrite and pinMode conflicts #48

jacky4566 opened this issue Sep 17, 2019 · 8 comments

Comments

@jacky4566
Copy link

jacky4566 commented Sep 17, 2019

There seems to be a problem with calling pinMode multiple times and using analogWrite.
The following code will fail for me:

static const int MOSFET = 9;

void setup() {
  //ON
  pinMode(MOSFET, OUTPUT);
  analogWrite(MOSFET, 255);
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  delay(3000);
  //ON
  pinMode(MOSFET, OUTPUT);//This pinMode causes problems
  analogWrite(MOSFET, 255);
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  pinMode(MOSFET, INPUT);
}

void loop() {
  delay(1);
}

Quite a few of the Arduino universe libraries use this format so its a bit frustrating to change them all.

@GrumpyOldPizza
Copy link
Owner

What is the problem exactly ? Hang ? Wrong value ? Spike ?

@jacky4566
Copy link
Author

jacky4566 commented Sep 17, 2019

Ah sorry. The code continues but no output voltage is observed.
The first "ON" executes correctly 3.3v for 1s but the second does nothing. 0 volts.

@kriswiner
Copy link

kriswiner commented Sep 17, 2019 via email

@jacky4566
Copy link
Author

jacky4566 commented Sep 17, 2019

Yes I understand its not necessary but many Arduino libraries do this. This behavior is also very unexpected and could easily confuse new users.
I found this while using an older Motor Driver Library

Its an easy fix to remove the pinMode() calls but still not great practice since pinMode() should be repeatable under Arduino.

This also kills Charlieplexing LEDs where you need tri-state logic to change between INPUT and OUTPUT rapidly.

@kriswiner
Copy link

kriswiner commented Sep 17, 2019 via email

@jacky4566
Copy link
Author

jacky4566 commented Sep 17, 2019

Changing the mode also does not work, see my example below. Repeatedly calling the same mode may not be good practice no, but it happens. Even in Adafruit's libraries

I feel this should be a concern for you guys since it's common enough in the Arduino enviroment.

static const int MOSFET = 9;

void setup() {
  //ON
  pinMode(MOSFET, OUTPUT);
  analogWrite(MOSFET, 255);
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  delay(3000);
  //ON
  pinMode(MOSFET, INPUT);
  pinMode(MOSFET, OUTPUT);
  analogWrite(MOSFET, 255); //No output produced. 0v
  delay(1000);
  //OFF
  analogWrite(MOSFET, 0);
  pinMode(MOSFET, INPUT);
}

void loop() {
  delay(1);
}

@kriswiner
Copy link

kriswiner commented Sep 17, 2019 via email

@jacky4566
Copy link
Author

Kris the code is just a simplified example of how to cause the issue.
A delay of 50ms also did not fix the problem.

Your free to do as you wish but I believe that repeated calls to "pinMode( , OUTPUT);" should not cause a failure of the pin to conduct.

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

No branches or pull requests

3 participants