Pages: [1]   Go Down
Author Topic: digitalWrite bug in Arduino Mega 2560  (Read 523 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: May 28, 2013, 02:20:35 am by Porlune » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23759
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 439
Posts: 23759
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://arduino.cc/en/Reference/HomePage
&
http://arduino.cc/en/Reference/PinMode
Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Bug in my Arduino MEGA 2560:
Code:
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.
Logged

Stockholm Sweden
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How did you connect the led?
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 509
Posts: 31469
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: May 28, 2013, 06:58:40 am by Grumpy_Mike » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5380
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 64
Posts: 6055
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: May 28, 2013, 07:35:50 am by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you James C4S! That explains a lot smiley
Logged

Pages: [1]   Go Up
Jump to: