Skip to content
Tactiverge

School/△ Builder/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

1.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.

  • ◇ Configure a pin as an input or an output and predict its voltage level.
  • ◇ Calculate the series resistor an LED needs on an output pin using Ohm's law.
  • ◇ Explain why driving an output into a shorted button can damage a pin.

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.

2.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.

Loading the interactive…

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

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

4.Predict

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

Guess first. Being wrong here is how the surprise works.

5.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

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.

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

  • An input pin with nothing connected reads 0.
    ✓ It floats and reads noise. Inputs need a pull-up or pull-down to have a defined level.
  • An LED can go straight on a pin; the pin limits the current.
    ✓ Without a series resistor the current can exceed the pin's limit. Always calculate R = (V_pin − V_f) ÷ I.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A 3.3 V output pin drives an LED that drops 2.2 V. What series resistor gives a current of 5 mA? Answer in ohms.

Ω

Practice 3 · a fresh version every time

A 3.3 V output pin drives an LED that drops 2 V. What series resistor gives a current of 10 mA? Answer in ohms.

Ω

Practice 4

Pins GP5, GP4, GP3, GP2 are set to output, input, output, output (1 = output). Written as a 4-bit binary number in that order, what is the direction register in decimal?

Practice 5

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

7.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.

8.Reflect

Concepts: Digital input and output · Voltage, current and Ohm's law