Skip to content
You Start with one site
Amounts in Start with one site
Microcontrollers · Concept

GPIO

One idea Beginner 5 min read
Found this useful?

A light switch on a wall tells the lamp what to do, and a doorbell button tells the house that somebody is there. One sends a decision out and the other brings a fact in. A GPIO pin (general purpose input and output) is a pin on a microcontroller that can do either job, and the program chooses which.

Output, write 1A switch inside the chip connects the pin to the supply.The pin is at 3.3 V
Output, write 0A second switch connects the pin to ground.The pin is at 0 V
InputBoth switches are open. The pin only listens.The outside sets the voltage

See it

Predict first

A program sets a 3.3 V pin as an input. What voltage does the chip put on that pin?

See it: what the chip does behind one pin

Mode of the pin
Voltage on the pin
Current through the pin

As an output, the chip closes one of two switches, and the pin is pulled firmly to 3.3 V or to 0 V. As an input, both switches are open: the pin drives nothing, and the reader reports what the outside circuit puts on it.

The idea

A program sets the direction of each pin by writing one or two bits in a register of the chip. On an ATmega328P, for example, one bit in the register DDRx makes the pin an output or an input.

As an output, the pin is driven by two switches inside the chip. Write 1 and the upper switch connects the pin to the supply. Write 0 and the lower switch connects it to ground.

As an input, both switches are open. The pin draws almost no current, and the chip reports a 1 when the voltage is near the supply and a 0 when it is near ground. A voltage in the middle has no dependable reading.

After a reset every pin is an input. Nothing connected to a pin is driven until the program has set that pin as an output.

One worked number

The question

A 3.3 V output pin of an STM32F103 should give about 8 mA at most in normal work (25 mA is its absolute maximum, not a working value). It drives a red LED, which keeps 2 V across itself, through a 330 Ω resistor. What current does the pin give, and is it within the limit?

Show the worked solution, 3 steps
  1. Find the voltage across the resistor

    The pin is at 3.3 V and the LED takes 2 V, so the resistor has 3.3 V − 2 V = 1.3 V across it.

  2. Find the current

    I = V ÷ R = 1.3 V ÷ 330 Ω = 0.0039 A, which is 3.9 mA.

  3. Compare with the limit

    3.9 mA is below 8 mA, so the pin can drive this LED directly. The figure opens on this case.

The mistake to avoid

A pin is not a power supply. The switches inside the chip are very small. They can light an LED, and they cannot run a motor, a relay coil or a strip of lamps. A load that needs more than the limit of the pin (about 8 mA in normal work on an STM32F103 and 20 mA on an ATmega328P, given in the data sheet) will overheat those switches. Let the pin control a transistor, and let the transistor carry the current of the load.

A second trap: an input with nothing connected to it has no known voltage and reads 0 or 1 at random. That is the problem a pull-up resistor solves.

Check yourself

Question 1

A 5 V pin with a limit of 20 mA drives a red LED (2 V across it) through a 220 Ω resistor. What current flows?

Question 2

A relay coil needs 70 mA. The pin should give 8 mA at most, and 25 mA is its absolute maximum. What is the right way to switch the relay?

Question 3

By mistake, two output pins are wired together. One program writes 1 to the first and 0 to the second. What happens?

Where you meet it

  • Float switches and door contactsA level switch in a water tank or a contact on a cold room door is read by a pin set as an input.
  • Pumps and compressorsA pin set as an output switches a transistor, the transistor switches a relay, and the relay starts the motor.
  • Water and energy metersMany meters give one pulse for each litre or each unit of energy. An input pin counts the pulses.
Key points
  • The program chooses the direction of each pin: output or input.
  • An output connects the pin to the supply or to ground through a small switch inside the chip.
  • An input drives nothing. It reads the voltage that the outside circuit gives it.
  • A pin carries a signal, not power. For more than a few milliamperes, use a transistor.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.