Why are the pins getting inputs...when not connected???

Discussion in 'Embedded Systems and Microcontrollers' started by nepdeep, Feb 15, 2013.

  1. nepdeep

    Thread Starter Member

    Sep 14, 2011
    139
    0
    I want to begin with simple example problem....I created a led blink interface with u-controller and led..i wrote a code like this

    Code ( (Unknown Language)):
    1.  
    2.  while (1)
    3.     {
    4.        
    5.              // when port B pin 3 is high light led else turn it off             PORTA =0b10101010;
    6.             if(PINB&(1<<PB3))
    7.             {
    8.             PORTA =0b1000000;
    9.             }            
    10.             else
    11.             {
    12.             PORTA =0b00000000;
    13.             _delay_ms(200);    
    14.                         }
    15.            
    16.        
    17.            
    18.     }
    19.  
    problem is that wehen i connecte nothing to the pin....no push button ...no vcc no gnd....the output pin...or the led is flickering...the led should turn on if pin input is 5v and off when none right....do i need to ground the pin to turn it off otherwise...like pulldown to ground or something by default......please i need some experienced tips...thanks in advance

    PS:
    chip-ATMEGA32
     
    Last edited by a moderator: Feb 15, 2013
  2. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    Are you sure the analog is turned off for those pins on that port?
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Yes, a resistor is the normal thing to use, 10k would be fine.
    It's possible that there is an internal pullup resistor that can be turned on, check the datasheet.
    Unconnected input pins tend to oscillate, which can cause higher than expected power use or possibly even reset the microcontroller. Unused pins should be set to outputs, or as inputs and grounded.
     
    nepdeep likes this.
  4. nepdeep

    Thread Starter Member

    Sep 14, 2011
    139
    0
    didnot understand...analog turned off for those pins
     
  5. nepdeep

    Thread Starter Member

    Sep 14, 2011
    139
    0
    another question....i tried to read the read analog pins.....even if i dont connect anything ....like no 5 v or 3v3 or gnd...the analog pins are reading values like 916..etc

    full resolution is 1024 -- 5V
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    read this.

    CMOS inputs will fluctuate randomly when floating....
     
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Those work excellent for initial seeding of a pseudo random number generator, so it's not always bad. If you seed a PRNG with an actual hard-coded number, say for a game, the exact same pattern will repeat every time due to the same seed being used. Pulling a seed from a floating ADC makes a PRNG closer to a true RNG (closer, not the same).

    If you aren't sure what is going on, the phenomenon you are seeing should be avoided if possible (Reasons given by Markd77 above). Setting to outputs low, or inputs tied to GND through 10k is best practice.
     
    nepdeep likes this.
Loading...