0
\$\begingroup\$

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() {
         ReadSwitch();
              }


   void ReadSwitch()
   {
    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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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? \$\endgroup\$ – PeterJ Mar 19 '14 at 4:49
  • \$\begingroup\$ 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) :) \$\endgroup\$ – bitsmack Mar 19 '14 at 6:21
  • \$\begingroup\$ 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! \$\endgroup\$ – bitsmack Mar 19 '14 at 6:34
  • \$\begingroup\$ 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. \$\endgroup\$ – jippie Mar 19 '14 at 6:44
  • \$\begingroup\$ 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). \$\endgroup\$ – Lundin Mar 19 '14 at 10:16
1
\$\begingroup\$

After reading the comments and researching "pull up registers" some more, I have come to understand that my mental image of "pull up" registers was wrong. Fundamentally, when the pull up configuration is used, the switch "press" causes a 0 on the pin. When the switch is open it causes a 1 on the pin.I thought it was the other way around, hence the confusion.

Thanks for all the help and the hints.

\$\endgroup\$
  • \$\begingroup\$ To clarify, OP's code is fine, they just mistook the input to be inverted. The switch is pulled up, active low. \$\endgroup\$ – Passerby Mar 20 '14 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.