GPIO
On this page
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.
See it
A program sets a 3.3 V pin as an input. What voltage does the chip put on that 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
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
- 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.
- Find the current
I = V ÷ R = 1.3 V ÷ 330 Ω = 0.0039 A, which is 3.9 mA.
- 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 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
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?
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?
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.
- 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.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.