Go Down

Topic: digitalWrite bug in Arduino Mega 2560 (Read 775 times) previous topic - next topic

Porlune

May 28, 2013, 07:04 am Last Edit: May 28, 2013, 09:20 am by Porlune Reason: 1
On my newly purchased Arduino Mega, using pinMode(11, OUTPUT); then digitalWrite(11,LOW); will set pin 11 to a HIGH value. (and vice versa).

Not supposed to do this right?

CrossRoads

setPin(11, OUTPUT);
Why are you using that instead of
pinMode (11, OUTPUT);
?
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

CrossRoads

I've not seen setMode before, only pinMode.  Try that.

http://arduino.cc/en/Reference/HomePage
&
http://arduino.cc/en/Reference/PinMode
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Porlune

Bug in my Arduino MEGA 2560:
Code: [Select]

void setup() {               
  // initialize the digital pin as an output.
  Serial.begin(9600);
  Serial.println(PORTB);    //prints 0
  pinMode(led, OUTPUT);     
  bitClear(PORTB, 5);      //turns ON led connected to pin 11
  Serial.println(PORTB);   //prints 0
}
void loop() {

}

This must be a bug.. A pin should not drive an LED when it is cleared.

Carlmikael


Grumpy_Mike

#5
May 28, 2013, 10:53 am Last Edit: May 28, 2013, 01:58 pm by Grumpy_Mike Reason: 1
Quote
A pin should not drive an LED when it is cleared.

It will if it is wired for current sinking rather than current sourcing
Anyway why are you using direct port manipulation to turn on an LED? What is wrong with the normal digitalWrite?
Are you counting the bits from zero not one?

Porlune

would you mind explaining more about what you mean by: "It will if it is wired for current sinking rather than current sourcing". Would be greatly appreciated! thank you!! :D

SurferTim

One lead of the LED is connected to D11. Where is the other end of the LED/resistor connected? Is it connected to 5v (current sink) or ground (current source)?

If the anode of the LED/resistor is connected to 5v and cathode to D11, it will light when D11 is LOW.
If the cathode of the LED/resistor is connected to ground and anode to D11, it will light when D11 is HIGH.

James C4S

#8
May 28, 2013, 02:34 pm Last Edit: May 28, 2013, 02:35 pm by James C4S Reason: 1
This is not a bug because no body else has ever reported this issue.

It is more likely the behavior of the pin is different from what you expect.  So let's look at your code.


Code: [Select]

void setup() {              
 // initialize the digital pin as an output.
 Serial.begin(9600);
 Serial.println(PORTB);    //prints 0
 pinMode(led, OUTPUT);    
 bitClear(PORTB, 5);      //turns ON led connected to pin 11
 Serial.println(PORTB);   //prints 0
}



If the cathode of the LED is connected to the pin (through a current limited resistor) and the anode is connected 5V, this could turn on the LED.

This is, of course, assuming that "led" is set to 11.  That part is missing in your code.  Which is troubling.  You're describing a behavior that nobody else experiences but you have yet to:  a) explain exactly how your hardware is connected and b) post proper code.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Porlune

Thank you James C4S! That explains a lot :)

Go Up