-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Inconsistent behaviors of analogWrite and pinMode on Due and Uno #2455
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
Check your electronic circuit. Uno: They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA. Due: They operate at 3.3 volts. Each pin can provide (source) a current of 3 mA or 15 mA. |
Thanks, but I don't see clearly this as a possible cause of the behaviors. In most cases, I can get correct PWM output on Due. One more observation, if I do not call analogWrite once before manipulating the pin mode as above, I get consistent behaviors on the two boards. |
Hi @HanaJin , sorry for the late response. Anyway, from your sketch I can't see any |
Hi @facchinm, thanks for your response. Sorry that I took the screenshot of the wrong sketch. There was a line between line 17 and 18 that is 'pinMode(3, OUTPUT);' for both sketches. |
/*
Date: July.16,'15
Arduino Sketch bug check: The first PWM ON duty is unstable bug TEST.
This is for "Arduino ATMega328 16MHz" as Arduino Pro mini., UNO and others
How To Test:
1) Compile & upload code to Arduino board.
2) Use oscilloscope at D3 port. Y:2V/div, X:1mS/div, +trigger.
Tool chain: Arduino softwares
Command Line: None
Release 0.1
*/
//-------------------------------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
const int INL_C_PWM = 3; // PWM pin D3
const int LED = 13;
//-----------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------
void setup()
{
pinMode(INL_C_PWM, OUTPUT);
digitalWrite(INL_C_PWM, 0); // init OFF
pinMode(LED, OUTPUT);
}
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void loop()
{
digitalWrite(LED, 0);
analogWrite(INL_C_PWM, 0); // analogWrite values from 0 to 255
// digitalWrite(INL_C_PWM, 0); // port OFF
delay (100); // any delay time for retrigger
digitalWrite(LED, 1);
// The first PWM ON duty is unstable Sketch bug is here
analogWrite(INL_C_PWM, 1); // analogWrite values from 0 to 255
delay (6); // any delay time
analogWrite(INL_C_PWM, 200); // analogWrite values from 0 to 255
delay (3); // any delay time
}
// End Of File |
Solved by #3524 |
I connected an RC circuit between D3 and A1 pins. Basically, D3 is connected to a 100K resistor, then to a 1uF capacitor. The voltage drop on the capacitor is measured on pin A1.
With the code below, I get reasonable readings on Uno R3.

However, using a Due and the same code, after changing the pinMode to INPUT and then OUTPUT manually, analogWrite stopped working as shown below. You can rule out the malfunctioning of analogRead as I had confirmed that using a DMM.
Is this behavior expected on Due? Which mode should I set it to for PWM on the Due to make it work?
The text was updated successfully, but these errors were encountered: