Go Down

Topic: [SOLVED]Toggling LED on Arduino Uno with a switch.(IDE used atmel studio) (Read 211 times) previous topic - next topic

gahlot

Hey, so i am using an Original Arduino Uno Rev. 3 Board to toggle the inbuilt LED using a switch. If i connect the switch on any PORTB pin, and set that particular pin to high. Then high voltage level on that particular pin is 2.0 V as measured by my multimeter. Due to this the inbuilt LED always stays on.   

    /*n can be any 0 to 4 pin of PORTB*/
    #define F_CPU 16000000UL
    #include <avr/io.h>
    #include <util/delay.h>
    #define LED_ON PORTB = (1<<PB5)
    #define LED_OFF PORTB = (0<<PB5)
    #define LED_Blink PINB = (1<<PB5)
   
   
    int main(void)
    {
        DDRB = (1<<PB5);
       DDRB = (1<<PBn);
       
        while (1)
        {
          PORTB = (1<<PBn);
          if(!(PINB & (1<<PBn)))/*Reading pin state,If pin is low turn LED on*/
          {
             LED_ON;
          }
          else
          {
             LED_OFF;
          }
        }
    }

Now if i take the same code written above and just use PORTD to toggle the LED with switch. Everything works fine, which means If i connect the switch on any PORTD pin, and set that particular pin to high. Then high voltage level on that particular pin is 4.95 V as measured by my multimeter. Due to this the inbuilt LED can be toggled.

    /*n can be any 0 to 7 pin of PORTD*/
        #define F_CPU 16000000UL
        #include <avr/io.h>
        #include <util/delay.h>
        #define LED_ON PORTB = (1<<PB5)
        #define LED_OFF PORTB = (0<<PB5)
        #define LED_Blink PINB = (1<<PB5)
        int main(void)
        {
            DDRB = (1<<PB5);
           DDRD = (1<<PDn);
           
            while (1)
            {
              PORTD = (1<<PDn);
              if(!(PIND & (1<<PDn)))/*Reading pin state,If pin is low turn LED on*/
              {
                 LED_ON;
              }
              else
              {
                 LED_OFF;
              }
            }
        }

The board is fine as well, I made all PORTB & PORTD pins high and check the voltage level on those pins and it came out to be 4.95V only. Below is the code i used to do it.

   
            #define F_CPU 16000000UL
            #include <avr/io.h>
            #include <util/delay.h>
           
            int main(void)
            {
                DDRB = 0b00111111;
               DDRD = 0b11111111;
               
                while (1)
                {
                  PORTB = 0b00111111;
                   PORTD = 0b11111111;
                }
            }

If anyone can provide answer to why the inbuilt LED cannot be toggled using switch on PORTB, will be helpful!  Also why am i getting the PORTB pins HIGH voltage level to 2.0V instead f 4.95V when i am trying to toggle an LED with a switch will be a big help as well.

PaulRB

Please read the forum guide in the sticky post, then modify your post above and correct it.

Go Up