# LED glows on low port is set to 0?

I have an Arduino UNO board with a ATMEGA328-PU and am trying to control the LED on output 13 that is on the processor pin B5.

I have a switch connected on the Arduino port 7, which is on the processor pin D8. This is the program I am running on the Arduino IDE like this:

    #include <util/delay.h>

void setup() {

DDRB = DDRB | 0xff;
DDRD = DDRD & 0x7f;
PORTD=PORTD | 0x80;
}

void loop() {
}

{
PORTB= PORTB | 0xff;
_delay_ms(500);
if(PIND & 0x80)
{
PORTB=0x00;
}
else
{
PORTB= 0xff;
}
_delay_ms(500);

}


My question is: when I set the PORTB to all 0's it switches on the LED, but when I set it to all 1, it switches off the LED. How is that possible? Should not the LED switch off when the PORTB is set to 0? Is this normal or something is broken somewhere?

For a bit of background: I have been programming.NET for many years but have never written programs for the microprocessors directly. So I am really new, if this question is inappropriate for this forum, kindly re-direct me to the correct place.

• Looking at the Uno schematic I take it you're using the yellow LED that comes on the board? Low should be off, what happens if you set PORTB to 0 in your loop and comment out the code that reads the switch? – PeterJ Mar 19 '14 at 4:49
• Is it possible that the code isn't actually asserting the pins as you intended? You could do a simple check with a voltmeter, to see if PINB5 is doing what you want. (Oops, sorry, I'm typing more as we speak) :) – bitsmack Mar 19 '14 at 6:21
• Actually, I've got nothing else. I was going to explain the need for pull-up resistors, but I see you already know, since you are using the internal pull-ups! – bitsmack Mar 19 '14 at 6:34
• Reduce your sketch to the bare minimum and see if it still happens. Do you have an original Arduino board, if not, what make (and is the LED actually wired as on the original one, wire up your own external LED with a series resistor and to ground)? I tested your sketch and it seems to work like you intend. – jippie Mar 19 '14 at 6:44
• According to this schematic (which was drawn by some really sloppy muppet), PB5 has a yellow led which will get lit whenever that pin is driven high (1). – Lundin Mar 19 '14 at 10:16