I have started studying GPIO. Please refer to the image attached. When Button is pressed, Vout = 0. When Button is released,Vout = Vin. My question: Using GPIO, software can set the values for port. If I write value of port as 1, does that means Vout = 1, irrespective of Button is pressed or not.? If Y write value of port as 0, does that means Vout = 0, irrespective of Button is pressed or not.? In the figure below Port is logic gate.

enter image description here


A GPIO is configured as either input or output under software control. Your diagram shows the input part (buffer) and some external components (switch, resistor). For this circuit to work, the GPIO should be configured as input.

When you configure the GPIO as output, you can indeed write a bit to it that sets the pin level: high for 1 and low for 0. Making the pin high this way and connecting (and closing) a switch to ground results in a short circuit: that is a bad idea. Don't do that.

If you raly want to know what happens in such a case: most likely the switch has a much lower resistance than the GPIO driver transistor, so the pin voltage will be (almost) low. A current will be drawn (rthrough the siwtch and the output resistor) that will be well above the maximum rating for the pin. Officially,m that results in undefined behaviour, which means that anything can happen.

| improve this answer | |
  • \$\begingroup\$ +1. Worth adding also that on many MCUs, writing a value to the port when it is configured as an input enables or disables the pull-up resistor. \$\endgroup\$ – Tom Carpenter Feb 19 '17 at 16:07
  • \$\begingroup\$ I wouldn't call that undefined: letting out the magic smoke is usually well defined :P \$\endgroup\$ – Vladimir Cravero Feb 19 '17 at 16:12
  • \$\begingroup\$ @Vladimir the trouble with undefined is that it is realy undefined: there is no guarantee that any smoke will come out. \$\endgroup\$ – Wouter van Ooijen Feb 19 '17 at 16:38
  • \$\begingroup\$ @Tom that is very much chip dependent, on most the chips I know pull-ups are enabled/disabled by a different register, sometimes 'in bulk' with one bit controlling the pullups of all pjns of a port. \$\endgroup\$ – Wouter van Ooijen Feb 19 '17 at 16:39

Have a look at this:

Typical output port of a microcontroller (Courtesy of Microchip - link)

Sorry for the messed up fonts.

When you set a port as output, WR_TRIS is zero so that the P and N mosfets are driven by whatever value you write to WR_PORT.

If you set the value to logic 0, the N is on, it pulls down the output sinking current from your external resistor, whatever happens to your button.

If you set it to 1, the P is on, and if you press the button you are theoretically shorting Vdd to Vss, which is a Bad Thing\$^\text{(C)}\$.

The output transistor can withstand a maximum current before blowing up, but it is usually protected. If, say, the output port can source/sink a maximum of 20mA, if the MOS are protected, i.e. the current is limited to 20mA, you will have 20mA flowing in the P, through the button, to Vss. I expect the button resistance to be very low, in the 100 m\$\Omega\$ range, so the output voltage would be low, i.e. the button wins.

If the transistor is not protected, it will probably blow up. This depends on your power supply though: if it is current limited to a safe value, nothing happens and the button still wins, otherwise you blow the micro port.

To sum up: do not set as output a port that should always be an input.

| improve this answer | |

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.