TactivergeLite

Lite / Textbooks / Electrical and Embedded Systems / Chapter 6

6. Pins, inputs and sensors

Version 1 · Technically checked (automated checks only) · 5 source lessons · Full interactive version

Introduction

A digital pin reads high or low, but an unconnected input floats and reads nonsense, so a pull-up resistor gives a button a resting voltage. Mechanical contacts bounce, so one press must be debounced into one count. Analogue sensors go through the ADC, which turns a voltage into a whole number of steps; a datasheet line or a two-point calibration then turns those counts into degrees, metres or newtons.

Figure 6.1. Voltage in, stepped code out (described in words)

A 10-bit ADC with a 5 V reference has 1024 codes, 4.88 mV per step, shown as a staircase beside the ideal straight line; code 512 means 2.5 V. A 10 kΩ pull-up to 3.3 V draws 0.33 mA when the button is pressed.

Explanation

Built from 5 School lessons. Each part is the lesson's own explanation.

6.1 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?

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.

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.

Common mix-ups

Open the Lite lesson: Pins: high, low and in between

6.2 Pull-ups: giving a button a resting voltage

A button only connects two wires. So why does every microcontroller button also need a resistor?

The pull-up idea

A pull-up resistor connects the input to the supply. With the button open, almost no current flows (the pin barely draws any), so there is almost no voltage across the resistor and the pin sits at the supply: reads 1. Pressing the button connects the pin straight to 0 V: reads 0. The resistor stops that press from being a short circuit.

It is a voltage divider

Treat the pull-up as R1 and the button as R2 in a divider: Vout = Vin × R2 ÷ (R1 + R2). Open button: R2 is enormous, so Vout ≈ Vin. Pressed: R2 ≈ 0, so Vout ≈ 0 V. A pull-down is the mirror image: resistor to 0 V, button to the supply, so pressed = 1.

Strong or weak?

While pressed, the pull-up carries I = V ÷ R. A 10 kΩ pull-up at 3.3 V wastes 0.33 mA; a 1 kΩ one wastes 3.3 mA, which matters on a battery. But a very weak pull-up (say 1 MΩ) is easily disturbed by noise and long wires. Typical choices sit between about 4.7 kΩ and 47 kΩ, and many chips have a built-in pull-up you can switch on.

Common mix-ups

Open the Lite lesson: Pull-ups: giving a button a resting voltage

6.3 Debouncing: one press, one count

You press a button once. Your program counts seven presses. Is the code wrong, or is the button lying?

Why buttons bounce

Inside a push button, a springy metal contact slams onto another. It bounces apart and back a few times, like a dropped ball, before staying shut. For a millisecond or more the pin flickers between 0 and 1. Humans never notice; a loop reading thousands of times a second sees every flicker.

A counting debouncer

Keep the accepted state. Each reading, if the pin disagrees with it, count how many readings in a row have disagreed. Only when the count reaches N do you accept the new state. In Python-style code:

if raw == last: run += 1
else: run = 1
last = raw
if raw != state and run >= N:
    state = raw

The price is delay

Waiting for N readings every p ms means a press is accepted about N × p ms after the bouncing stops. 4 readings every 0.5 ms adds about 2 ms: fine. 6 readings every 5 ms adds 30 ms, which starts to feel sluggish and can miss very short presses entirely. Engineers pick values from how long their switches bounce.

Common mix-ups

Open the Lite lesson: Debouncing: one press, one count

6.4 Reading the datasheet line

A temperature sensor says 750 mV. The datasheet has one line of maths. What is the temperature?

Offset and slope

A linear sensor's output is output = offset + slope × quantity. The offset is the output when the quantity is zero; the slope is how much the output changes per unit. A TMP36-style sensor has an offset of 500 mV (so it can show temperatures below 0 °C) and a slope of 10 mV per °C. An LM35-style sensor has 0 mV offset and 10 mV per °C.

Undo it in firmware

Firmware reads the output and needs the quantity, so it rearranges: quantity = (output − offset) ÷ slope. Subtract first, then divide. Doing it in the wrong order is one of the most common sensor bugs.

def temp_c(mv):
    return (mv - 500) / 10

Units are your safety net

Keep units on every number: (750 mV − 500 mV) ÷ (10 mV/°C) = 25 °C. The mV cancel, leaving °C. If the units come out as mV² or °C/mV, the formula is upside down.

Derivation: Rearranging a sensor line
  1. Datasheet form: V = V₀ + k × T, with V₀ the output at 0 °C and k the millivolts per °C.

  2. Subtract V₀ from both sides: V − V₀ = k × T.

  3. Divide both sides by k: T = (V − V₀) ÷ k.

  4. Example (TMP36-style, V₀ = 500 mV, k = 10 mV/°C): V = 620 mV gives T = (620 − 500) ÷ 10 = 12 °C.

  5. Check: 500 + 10 × 12 = 620 mV. ✓

Common mix-ups

Open the Lite lesson: Reading the datasheet line

6.5 ADC counts, quantisation and noise

A 10-bit ADC says 512. A 12-bit one says 2048. Are they measuring the same voltage?

Counts and steps

An ADC compares the input with a reference voltage and reports a whole number, a count. More bits give more counts and finer steps: 8 bits = 256 steps, 10 bits = 1,024, 12 bits = 4,096. With a 5 V reference, an 8-bit step is 5 ÷ 256 ≈ 19.5 mV; a 12-bit step is about 1.22 mV. Some datasheets divide by 2ⁿ − 1 instead; the difference is under one step, but check your chip.

Quantisation is rounding

The ADC cannot report 310.3; it reports 310. That rounding is quantisation: the staircase in the signal lab. Its error is always smaller than one step, and it is the same for every reading, so you can state it in advance: '±1 step, which is 0.32 °C for this sensor'.

When noise beats bits

Real signals carry noise. If the noise wiggles by 10 mV and your step is 0.8 mV, the last few bits just record the noise: extra resolution buys nothing. Then it is better to average several readings or filter the signal. Choose bits so the step is a little smaller than the noise and much smaller than the changes you care about.

Derivation: ADC resolution and conversions
  1. An n-bit ADC has 2ⁿ possible counts, from 0 to 2ⁿ − 1. 10 bits → 1,024 counts (0–1,023).

  2. It shares the reference voltage between them, so one step (1 LSB) is step = Vref ÷ 2ⁿ. 3.3 V, 10 bits: 3.3 ÷ 1,024 ≈ 3.22 mV.

  3. Count to volts: V = count × Vref ÷ 2ⁿ. Count 310 → 310 × 3.3 ÷ 1,024 ≈ 0.999 V.

  4. Volts to count: count = floor(V ÷ Vref × 2ⁿ). 1.00 V → floor(310.3) = 310. Every voltage in the same step gives the same count.

  5. So the stored value can be up to one step below the truth (this lesson's ADC rounds down): that is quantisation error.

  6. In sensor units: with a 10 mV/°C sensor, one 3.22 mV step is 0.322 °C. Each extra bit halves the step.

Common mix-ups

Open the Lite lesson: ADC counts, quantisation and noise

Worked examples

Pins: high, low and in between: example 1

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

Solution

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.

Pull-ups: giving a button a resting voltage: example 1

A 5 V system uses a 4.7 kΩ pull-up. How much current flows while the button is held, and how much power is wasted?

Solution

I = 5 V ÷ 4.7 kΩ ≈ 1.06 mA. P = V × I = 5 × 0.00106 ≈ 5.3 mW. Tiny, but on a coin cell it adds up if the button is held for hours.

Debouncing: one press, one count: example 1

A switch bounces for up to 8 ms. Your loop reads every 2 ms. How many identical readings should the debouncer require?

Solution

8 ms ÷ 2 ms = 4 readings could all fall inside the bounce, so require more than 4, say 5 (10 ms of steady signal). The press is accepted about 10 ms after the bouncing ends.

Reading the datasheet line: example 1

An MCP9700A-style sensor gives 400 mV at 0 °C and 19.5 mV per °C. It reads 790 mV. Temperature?

Solution

T = (790 − 400) ÷ 19.5 = 390 ÷ 19.5 = 20 °C. Check: 400 + 19.5 × 20 = 790 mV. ✓

ADC counts, quantisation and noise: example 1

A TMP36-style sensor (500 mV + 10 mV/°C) feeds an 8-bit ADC with a 5 V reference. It reads 38. What temperature is that, and how finely can this system measure?

Solution

V = 38 × 5 ÷ 256 ≈ 0.742 V = 742 mV. T = (742 − 500) ÷ 10 ≈ 24.2 °C. Step = 5 ÷ 256 ≈ 19.5 mV ≈ 1.95 °C: far too coarse for a greenhouse. A 10- or 12-bit ADC, or a 3.3 V reference, would do better.

Interactive figures, in words

Figure 6.2 from Pins: high, low and in between

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.

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.

Figure 6.3 from Debouncing: one press, one count

Start with 1 identical reading needed (no debouncing) and count the 'presses'. Raise the readings needed until you get exactly one press. Then try a 5 ms reading interval: how few readings can you get away with, and how late is the press accepted?

The debounce diagram shows a raw button signal over 40 ms: released (1) until 5 ms, chattering between 0 and 1 until 11 ms, pressed (0) until 30 ms, chattering again until 33 ms, then released. You choose the time between readings and how many identical readings are needed. With 1 reading needed, the chatter counts as several presses. With enough readings, exactly one press is counted, accepted a few milliseconds after the bouncing ends.

Figure 6.4 from Reading the datasheet line

Focus on stages 1 and 2. Move the temperature and watch the sensor output change. Write down the millivolts at 0 °C, 10 °C and 20 °C. What is the pattern?

The pipeline diagram starts with a temperature slider. Stage 2 shows the sensor output in millivolts using 500 mV + 10 mV per °C: 500 mV at 0 °C, 600 mV at 10 °C, 700 mV at 20 °C. The output rises by the same amount for every degree, so it is a straight line.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. A pin is set as a plain input with nothing connected. What does it read?
    Answer it in “Pins: high, low and in between”
  2. Why is it dangerous to set a button's pin as an output and write HIGH?
    Answer it in “Pins: high, low and in between”
  3. With a pull-up and a button to 0 V, which line of code means 'button pressed'?
    Answer it in “Pull-ups: giving a button a resting voltage”
  4. A button connects a pin to 0 V. The pin has a 4.7 kΩ pull-up resistor to 5 V. While the button is held, how much current flows through the pull-up, in milliamps?
    Answer it in “Pull-ups: giving a button a resting voltage”
  5. Without debouncing, a lift-call button sometimes registers three presses. Why?
    Answer it in “Debouncing: one press, one count”
  6. A temperature sensor outputs 400 mV at 0 °C and rises by 19.5 mV for every 1 °C. It reads 205 mV. What temperature is it measuring, in °C?
    Answer it in “Reading the datasheet line”
  7. What voltage does one step (1 LSB) of a 10-bit ADC with a 3.3 V reference represent, in millivolts?
    Answer it in “ADC counts, quantisation and noise”
  8. A 8-bit ADC with a 5 V reference reads 53. Using volts = count × Vref ÷ 2^n, what voltage is that, in volts?
    Answer it in “ADC counts, quantisation and noise”

Engineering applications

Keyboards

Every key on a keyboard bounces for a few milliseconds when pressed. Firmware samples the key repeatedly and only accepts a change once it has been stable, so one press types one letter.

Reading a temperature

A temperature sensor's datasheet gives a voltage per degree and an offset. Firmware converts ADC counts to volts, then volts to degrees, and engineers check the result against a reference thermometer at two points.

Watch (optional)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

Glossary

Pull-up and pull-down resistors
A pull-up resistor connects an input pin to the supply so it reads high when nothing else drives it; a pull-down connects it to ground so it reads low. Field Guide
ADC resolution
An n-bit analogue-to-digital converter splits its reference voltage into 2^n steps; one step, the least significant bit, is the smallest change it can detect. Field Guide
Reading a datasheet
A datasheet is the manufacturer's technical description of a part: pinout, electrical characteristics, absolute maximum ratings, timing and typical application circuits. Field Guide
Calibration
Calibration compares an instrument's readings with known references and builds a correction, often an offset and a gain, so that future readings are accurate. Field Guide
Voltage divider
Two resistors in series across a supply produce, at their junction, a fraction of the supply voltage set by the ratio of the resistors. Field Guide

Project: A calibrated thermometer

Design the input side of a thermometer: a thermistor divider into a 10-bit ADC. Choose the resistor, estimate the counts at 0 °C and 40 °C, and describe a two-point calibration that turns counts into degrees. State the temperature resolution you would get.

Deliverables

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

References

← 5. Inside a microcontroller7. PWM, state machines and actuators →