Skip to content

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

Closed
HanaJin opened this issue Nov 17, 2014 · 6 comments
Closed

Inconsistent behaviors of analogWrite and pinMode on Due and Uno #2455

HanaJin opened this issue Nov 17, 2014 · 6 comments
Assignees
Labels
Architecture: SAM Applies only to the SAM microcontrollers (Due) Board: Arduino Due Applies only to the Due Component: Core Related to the code for the standard Arduino API

Comments

@HanaJin
Copy link

HanaJin commented Nov 17, 2014

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.
image

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.

image

Is this behavior expected on Due? Which mode should I set it to for PWM on the Due to make it work?

@ghost
Copy link

ghost commented Nov 20, 2014

Check your electronic circuit.

Uno: They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA.
http://arduino.cc/en/Main/ArduinoBoardUno

Due: They operate at 3.3 volts. Each pin can provide (source) a current of 3 mA or 15 mA.
http://arduino.cc/en/Main/ArduinoBoardDue

@HanaJin
Copy link
Author

HanaJin commented Nov 20, 2014

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.

@facchinm
Copy link
Member

facchinm commented Jun 1, 2015

Hi @HanaJin , sorry for the late response.
Switching analog/digital functions on the same pin on a Due is not straightforward as with a UNO because, at platform level, we need some hacks to achieve maximum performances (as you can see in issue #3064 ). Issue #2198 is also probably related.

Anyway, from your sketch I can't see any pinMode(OUTPUT) in the real code, so how is it supposed to work?

@facchinm facchinm added Component: Core Related to the code for the standard Arduino API Waiting for feedback More information must be provided before we can proceed Board: Arduino Due Applies only to the Due Architecture: SAM Applies only to the SAM microcontrollers (Due) labels Jun 1, 2015
@hanabanana
Copy link

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.
I agree that this is probably related to issue #2198 and #3064. But one thing still odd to me is that, if the analogWrite indeed locks up the resource even after pinMode is called, shouldn't the following analogWrite calls remain functioning properly? As you can see, I am only checking for analog functionality in the sketch before and after the pinMode calls.
It seems to me that the subsequent calls of pinMode actually does something that breaks the analogWrite functionality of the pin.

@ffissore ffissore removed the Waiting for feedback More information must be provided before we can proceed label Jun 17, 2015
@shimizutko
Copy link

/*
 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

@facchinm
Copy link
Member

facchinm commented Jan 7, 2016

Solved by #3524

@facchinm facchinm closed this as completed Jan 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture: SAM Applies only to the SAM microcontrollers (Due) Board: Arduino Due Applies only to the Due Component: Core Related to the code for the standard Arduino API
Projects
None yet
Development

No branches or pull requests

5 participants