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

Thread Starter

nepdeep

Joined Sep 14, 2011
140
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

Rich (BB code):
 while (1) 
    {
        
             // when port B pin 3 is high light led else turn it off             PORTA =0b10101010;
            if(PINB&(1<<PB3))
            {
            PORTA =0b1000000;
            }            
            else
            {
            PORTA =0b00000000;
            _delay_ms(200);    
                        }
            
       
            
    }
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:

Markd77

Joined Sep 7, 2009
2,806
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.
 

Thread Starter

nepdeep

Joined Sep 14, 2011
140
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
 

thatoneguy

Joined Feb 19, 2009
6,359
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
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.
 
Top