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

Alternating between AnalogWrite and DigitalWrite on the same pin behaves unexpectedly. #31

Open
Nilloc opened this issue May 26, 2013 · 1 comment

Comments

@Nilloc
Copy link

Nilloc commented May 26, 2013

The following sketch causes the LED to go fully bright, then dim (analogWrite(50)) then stay dim, never returning to 5 volts.

The sketch does work as expected in the Arduino 1.0.1 IDE.

void setup()
{
  pinMode(11, OUTPUT);
}

void loop()
{
  digitalWrite(11, HIGH);
  delay(500);
  analogWrite(11, 50);
  delay(500);
}

Tested using a Sparkfun Redboard and a 2.7v LED.

@bhagman
Copy link
Member

bhagman commented May 26, 2013

This is an obscure issue since there is typically no reason to use digitalWrite() and analogWrite()/pwmWrite() on the same pin in the same sketch.

The problem comes from the fact that digitalWrite() no longer (compared to previous versions) clears the the PWM mode for the pin.

This was chosen for optimization reasons (i.e. to reduce the digitalWrite() to a single machine instruction if using a constant for the pin number.)

However, as we move forward, it will make more sense to be thorough instead of optimal.

A fix for this problem is to clear the output mode for the given pin in its associated timer at the start of digitalWrite().

This applies to the AVR8Bit core only.

Fix forthcoming.

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

2 participants