NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Basic Electronics » Digital Input Question

May 26, 2011
by frankdwest
frankdwest's Avatar

Is it possible to configure an I/O port pin so that an external +5V will be a HIGH? The examples I have seen so far configure the pins so that an external ground is a HIGH and an open circuit is LOW. This is confusing to me, I don't think I understand this.

Thanks in advance

May 27, 2011
by Rick_S
Rick_S's Avatar

I think you may have gotten your high and low terminology a bit mixed up. 5v is high level no matter what. What you are seeing is not ground being HIGH but ground being used as what is known as a low level or "sink" type input.

Most of the examples given use a common practice of pulling an input low by connecting it to ground to detect a button press. I believe the main reason this is done especially with the AVR micro-controllers is due to the fact they have built in pull-up resistors. This allows the end user to connect an input button with no extra circuitry.

Now, if you want to detect an input when it makes a low to high transition, you can definately do that. You simply have to connect a resistor from the input pin to ground that will "pull-down" the input. Then connect your normally open (N/O) switch to 5v. This way when the switch is open, the input is low and when you press the switch, the input goes high.

If you do this, you must change the code that sets up the pin you are using for an input. Mainly you DO NOT want to enable the internal pull-ups.

So if you wanted to setup Port C pin 1 as an input, the common method would be this:

DDRC &= ~(1<<PC1); // set PC1 as input
PORTC |= (1<<PC1); // turn on internal pull up resistor for PC1
// Read button press 
// a2 = 0 when pressed
// a2 = 1 when not pressed
a2 = (PINC & (1<<PC1)) >> PC1;

If you wanted to use an external pull-down resistor and read a high input you would turn off the internal pull-up and then what you read would be reveresed.

DDRC &= ~(1<<PC1); // set PC1 as input
PORTC &= ~(1<<PC1); // turn off internal pull up resistor for PC1
// Read button press 
// a2 = 1 when pressed
// a2 = 0 when not pressed
a2 = (PINC & (1<<PC1)) >> PC1;

Hope that helps,

Rick

May 27, 2011
by frankdwest
frankdwest's Avatar

Thank you! I knew I was confused on some points on this.

May 28, 2011
by frankdwest
frankdwest's Avatar

I breadboarded this and tried the code and it worked perfectly.

Post a Reply

Please log in to post a reply.

Did you know that SPDT stands for "Single Pole, Double Throw"? Learn more...