arduino-STM32L4 icon indicating copy to clipboard operation
arduino-STM32L4 copied to clipboard

Butterfly analogWrite and pinMode conflicts

Open jacky4566 opened this issue 5 years ago • 8 comments

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.

jacky4566 avatar Sep 17 '19 16:09 jacky4566

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

GrumpyOldPizza avatar Sep 17 '19 16:09 GrumpyOldPizza

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.

jacky4566 avatar Sep 17 '19 16:09 jacky4566

Why are you calling this a second time? Not necessary.

On Tue, Sep 17, 2019 at 9:16 AM jacky4566 [email protected] wrote:

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.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/48?email_source=notifications&email_token=ABTDLKTY2ZCXD2NSTUSEUMTQKD7EDA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HL43DYQ, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTDLKVBLXITQJQIFFHMBM3QKD7EDANCNFSM4IXSJSXA .

kriswiner avatar Sep 17 '19 17:09 kriswiner

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.

jacky4566 avatar Sep 17 '19 17:09 jacky4566

Changing the mode should work fine, repeatedly calling the same mode is no good practice.

On Tue, Sep 17, 2019 at 10:12 AM jacky4566 [email protected] wrote:

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 https://www.arduinolibraries.info/libraries/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 to change between INPUT and OUTPUT rapidly.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/48?email_source=notifications&email_token=ABTDLKUQHFE5PD3CXPCYRLDQKEFXXA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65HP2Y#issuecomment-532314091, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTDLKS7EZHVW2AOGM75Q2TQKEFXXANCNFSM4IXSJSXA .

kriswiner avatar Sep 17 '19 17:09 kriswiner

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);
}

jacky4566 avatar Sep 17 '19 18:09 jacky4566

Comment out these two lines and it should work:

pinMode(MOSFET, INPUT); pinMode(MOSFET, OUTPUT);

Why are you toggling the mode for no reason here?

If you insist on doing so, at least put a delay between the calls.

On Tue, Sep 17, 2019 at 11:14 AM jacky4566 [email protected] wrote:

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 http://Adafruit-Motor-Shield-library

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); }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/GrumpyOldPizza/arduino-STM32L4/issues/48?email_source=notifications&email_token=ABTDLKX2CN3YWKLPYTVI5BTQKEM7FA5CNFSM4IXSJSXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD65NQEI#issuecomment-532338705, or mute the thread https://github.com/notifications/unsubscribe-auth/ABTDLKX2MDO72GVVM5YDEFDQKEM7FANCNFSM4IXSJSXA .

kriswiner avatar Sep 17 '19 18:09 kriswiner

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.

jacky4566 avatar Sep 17 '19 18:09 jacky4566