TactivergeLite

Lite / Building Machines That Respond / Week 20

Pins: high, low and in between

One tiny metal leg on a chip can light an LED, read a button or wreck itself. What decides which?

About 30 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Every button press and every LED in an embedded project passes through a GPIO (general-purpose input/output) pin. Getting the pin mode and the resistor right is the difference between a project that works for years and a chip that dies on day one.

Safety: Simulation only. On real hardware, never connect a pin directly to a supply or to ground, and stay within the datasheet's current limits.

Predict

A 3.3 V microcontroller sets a pin as an output and writes HIGH. Roughly what voltage appears on the pin?

  1. 0 V
  2. About 3.3 V
  3. 5 V
  4. It depends on the program's mood
Guess first, then reveal

An output pin connects to the chip's supply (HIGH, about 3.3 V here) or to 0 V (LOW). It cannot make 5 V: a pin can never go above the chip's own supply.

Discover

Light the LED on GP3, then lower its resistor until the panel warns you. Next set GP2 to Output, write HIGH and hold its button. Finally set GP5 to plain Input and press 'Read all pins again' a few times.

What the interactive shows (text version)

The pin panel lists four pins. Each can be set to Input, Input with pull-up or Output. Outputs write HIGH (3.3 V) or LOW (0 V); GP3 lights an LED whose current is (3.3 − 2.0) ÷ R. Buttons on GP2 and GP4 pull their pin to 0 V when held. A plain input with nothing connected reads unpredictable values. A HIGH output with its button held is reported as a short circuit. Two registers show the pin directions and the input readings as 4-bit numbers.

You can also open the interactive on the full site when you have a faster connection.

Understand

Output: the pin pushes

As an output, a pin is connected inside the chip either to the supply (HIGH, logic 1) or to 0 V (LOW, logic 0). It can supply a small current, often around 10–20 mA depending on the chip. That lights an LED but cannot run a motor.

Input: the pin listens

As an input, a pin draws almost no current. It reports 1 if its voltage is near the supply and 0 if near 0 V. If nothing connects it to either, it is floating and reads noise: the pin panel shows the same pin giving different answers on each read. Next lesson fixes that with a pull-up resistor.

Registers: pins as bits

The chip stores pin settings as bits in registers. If GP5…GP2 have directions 0, 1, 1, 0 (1 = output), the direction register holds 0110 = 6. Reading all inputs at once gives another 4-bit number. That is why binary from Level 1 matters: firmware sets pins by writing numbers.

Never fight an output

If an output drives HIGH while a button connects that same pin to 0 V, the pin is shorted: a large current flows and the pin can be damaged. Buttons belong on inputs, and every LED needs a series resistor.

Explain it another way

Intuitive

An output pin is a tap you turn fully on or fully off. An input pin is a pressure gauge that only says 'high pressure' or 'low pressure'. A floating input is a gauge connected to nothing, guessing.

Technical

GPIO pins have direction, output and input registers. Logic thresholds are typically around 30% and 70% of the supply; voltages between are undefined. Datasheets give the maximum current per pin and per chip, and absolute maximum voltages that must never be exceeded.

Mathematical

R = (V_pin − V_f) ÷ I. Register value = Σ bit_k × 2^k, so bits b3 b2 b1 b0 = 0110 give 0×8 + 1×4 + 1×2 + 0×1 = 6.

Applied

Microwave keypads, car-door switches and the status LED on a router all connect to GPIO pins configured exactly like the panel: inputs with resistors for switches, outputs with series resistors for lights.

Derivation: Sizing an LED resistor
  1. The pin gives V_pin (3.3 V). The LED takes a nearly fixed drop V_f (about 2.0 V for a red LED).

  2. The resistor gets whatever is left: V_R = V_pin − V_f = 3.3 − 2.0 = 1.3 V.

  3. Ohm's law, rearranged: R = V_R ÷ I. For I = 10 mA = 0.010 A: R = 1.3 ÷ 0.010 = 130 Ω.

  4. Round up to the next standard value (150 Ω) so the current is a little less, never more.

  5. Check: I = 1.3 ÷ 150 ≈ 8.7 mA, comfortably under a typical pin limit.

Worked example 1

A 5 V pin drives a green LED (V_f = 2.2 V) at 15 mA. What resistor?

V_R = 5 − 2.2 = 2.8 V. R = 2.8 ÷ 0.015 ≈ 187 Ω. Choose the next standard value up, 200 Ω, which gives 14 mA.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. A pin is set as a plain input with nothing connected. What does it read?

Ω

Ω

Practice 5. Why is it dangerous to set a button's pin as an output and write HIGH?

Apply it

Design the pins for a two-button, three-LED game controller on a 3.3 V chip: list each pin's mode, the resistor for each LED at 8 mA (V_f = 2.0 V), and the direction register as a binary number. Check your design in the pin panel.

Reflect

Which mistake from today could you most easily make on a real board, and how would you check for it before powering up?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Set up once, loop foreverPull-ups: giving a button a resting voltage →