digitalWrite bug in Arduino Mega 2560

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?

setPin(11, OUTPUT);
Why are you using that instead of
pinMode (11, OUTPUT);
?

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

&

Bug in my Arduino MEGA 2560:

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.

How did you connect the led?

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?

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!! :smiley:

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.

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.

Porlune:

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.

Thank you James C4S! That explains a lot :slight_smile: