Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 55 ยท Meet the Computer

Input and Output Devices

Learn how keyboards, mice, touchscreens, sensors, screens and printers connect to a computer, and follow a key press from the switch to a character on screen.

A processor can only work on numbers that are already inside the machine. Input devices turn the outside world (a finger press, a sound wave, a light level) into electrical signals and then into data. Output devices do the reverse: they turn data into something your senses can use, such as light, sound or ink on paper. Some devices do both, like a touchscreen that shows a picture and also senses where you tap.

In this lesson you will survey the common devices, see how they connect through ports and drivers, and then follow a single key press all the way from a switch under a key to a letter on the screen. It continues from Types of Computers, and the transformation of "something physical" into numbers is explored more in Bits and Bytes.

Common input devices

  • Keyboard: each key closes a tiny electric switch. A small controller notices which one and reports a key code.
  • Mouse and touchpad: an optical sensor (a tiny camera) or the pad's electrodes detect movement and report "moved 3 units left, 1 unit up" many times per second.
  • Touchscreen: a grid of electrodes senses the change in electric field when a finger comes near, and the controller calculates the position.
  • Microphone: turns air pressure waves into a varying voltage, which is then measured many thousands of times per second to get numbers.
  • Camera and scanner: a sensor measures the light falling on millions of small cells and records each as a number.
  • Sensors: accelerometers, gyroscopes, GPS receivers, temperature and fingerprint sensors. Phones and cars contain many.
  • Game controllers and other devices: buttons and sticks whose states are reported as numbers.

All of these are analog-to-digital converters at heart: they measure something that varies smoothly (or is pressed or not) and report numbers.

Common output devices

  • Display: a grid of pixels, each lit with a color the computer specifies. More in Images, Sound and Video as Data.
  • Speakers and headphones: a stream of numbers is converted to a varying voltage that moves a diaphragm and makes sound.
  • Printers: inkjet printers spray tiny drops, laser printers fuse toner powder with heat, 3D printers deposit melted plastic layer by layer.
  • Haptics: the vibration motor in your phone.
  • Actuators: motors, valves and lights controlled by embedded computers.

Some devices are both: a touchscreen, a network card (sends and receives data), a storage drive (written to and read from).

Connecting devices: ports, cables and wireless

A device needs a path to the processor. That path has a physical side (a connector or a radio) and an agreed language (a protocol).

Connection Typical use
USB (Type-A, Type-C) Keyboards, mice, drives, chargers, phones
HDMI / DisplayPort Monitors and TVs
Audio jack Headphones and microphones (analog)
Ethernet Wired network
Bluetooth Short-range wireless: headsets, keyboards, controllers
Wi-Fi Wireless network

USB is the most widespread. It is a standard that defines the connector, the power it provides and the way a device describes itself to the computer. When you plug in a device, it announces what it is (a keyboard, a storage drive) and the computer picks the correct software for it. That software is a driver, as introduced in Hardware and Software. Common device types use drivers that ship with the operating system, which is why most keyboards work the moment you plug them in.

Connector shape and speed are separate things. A USB-C plug can carry slow USB 2.0 signals or very fast ones, and may or may not carry video. When buying a cable, look at what it supports, not just the plug.

How a key press becomes a character

Let us follow one key press. It is a good example of layers working together.

sequenceDiagram
  participant K as Keyboard controller
  participant D as Driver
  participant O as Operating system
  participant A as Application
  K->>D: key code for the A key pressed
  D->>O: key event with scan code
  O->>O: apply layout and Shift state
  O->>A: character event "A" (or "a")
  A->>A: add to text, request redraw
  A->>O: draw the glyph on screen

The same physical key can produce different characters depending on layout and modifier keys, so translation happens in software.

  1. Switch closes. The keyboard's controller scans its grid of keys many times a second and notices a change.
  2. Scan code sent. It sends a small number meaning "this physical key went down". The code names the position of the key, not the letter on it.
  3. Driver and operating system translate. The system knows the keyboard layout (QWERTY, AZERTY, and so on) and which modifier keys (Shift, Caps Lock) are active. Together they turn the position into a character.
  4. Application receives an event. The text editor is told "the character A was typed" and inserts it into the document.
  5. Output. The application asks the operating system to draw the letter's shape (a glyph) at the right place, and the display shows it.

This is why you can switch your layout in the settings without buying a new keyboard: the keys still send the same positions, and only the software table changes.

JS A key event pipeline

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The same physical presses produce "Hi1" on one layout and "Ih1" on the swapped one. The scan codes shown are illustrative values, not a faithful copy of any real keyboard standard, and real systems use richer events (repeat, key release, dead keys, input methods for Chinese or Japanese). The principle is the real one: positions first, characters later.

Polling and interrupts

How does the computer notice that you pressed a key? There are two basic approaches.

  • Polling: the processor asks the device again and again, "anything new?". This is simple but wastes effort when nothing happens.
  • Interrupts: the device sends a signal that makes the processor pause, handle the event and return to what it was doing. Keyboards and network cards mostly work this way.

You can picture the difference with a doorbell versus checking the door every five seconds.

JS Polling versus interrupts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Polling made 100 checks to find three events; the interrupt style only worked when needed. (Real polling is still used where it makes sense, for example when events arrive constantly.)

Choosing and caring for devices

  • Look at resolution and refresh rate for a display, DPI (dots per inch) for mice and printers, and sampling rate for microphones.
  • Latency matters for games and music: wireless devices add a tiny delay.
  • Accessibility devices (screen readers, switches, eye trackers) are also input and output devices, which shows how flexible the model is.

Reading data automatically: OCR, MICR, barcodes and QR codes

Typing everything in by hand is slow and error-prone, so many input devices read data directly from the world. Five technologies appear everywhere.

Technology What it reads How Typical use
Barcode reader A pattern of parallel bars (1D code) A laser or camera measures the widths of bars and spaces Shop checkouts, library books, parcels
QR code reader A square matrix of dots (2D code) A camera finds three corner markers and decodes the grid Links, payments, tickets, boarding passes
OCR (optical character recognition) Printed or handwritten text in an image Software compares the shapes in a scanned page to known letters Digitizing books, reading number plates, scanning forms
OMR (optical mark recognition) Filled-in bubbles on a sheet Detects dark marks at known positions Exam answer sheets, surveys, lottery slips
MICR (magnetic ink character recognition) Special characters printed in magnetic ink Magnetizes the ink and detects its signal; very hard to forge or smudge Account and cheque numbers on bank cheques

Barcodes

A 1D barcode such as the EAN-13 on a product stores a number, usually 13 digits. The scanner does not know the price: it reads the number, and the shop's database returns the product and price. The last digit is a check digit that lets the scanner detect a misread. For EAN-13, multiply the first 12 digits alternately by 1 and 3, add them up, and the check digit makes the total a multiple of 10.

Python Compute and verify an EAN-13 check digit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A single wrong digit is always caught; the same check also catches most swapped neighbours. This idea of adding redundancy to detect errors is used everywhere in computing (see Compression and File Formats and Bits and Bytes).

QR codes

A QR code (quick response code) stores far more than a barcode, up to about 3,000 bytes, because it uses a two-dimensional grid. The three large squares in the corners tell the reader the orientation, so you can scan it at any angle; the rest is data plus error-correcting codes (Reed-Solomon) that let a code still scan when part of it is dirty or hidden by a logo. Its content is usually text: a web address, a Wi-Fi password, a payment request, a contact card. Because the content is invisible to you until you scan, be careful with QR codes in public places: a sticker placed over a real code can send you to a fake site (see Malware and Safe Habits). Check the address your phone shows before you open it.

OCR and OMR

OCR turns a picture of text into real, editable text. The software cleans up the image, finds lines and characters, and compares each to a trained model. It is much better on clean print than on handwriting, and results should be proofread. Phones use OCR to translate signs and to copy text from photos. OMR is simpler: it does not read characters, only detects whether a mark exists at each expected position, which is why exam sheets with bubbles can be scored in seconds.

MICR

At the bottom of a bank cheque are numbers printed in a special font with magnetic ink. A MICR reader magnetizes the ink and detects the signal, which is reliable even if the cheque is stamped, folded or written over, and hard to photocopy convincingly. Banks therefore read the account and routing numbers automatically and sort millions of cheques per day. Barcodes and OCR rely on light and can be fooled by dirt or ink; MICR relies on magnetism.

Choosing

Use barcodes for numbers that identify items, QR codes for links and bigger payloads, OCR to digitize existing documents, OMR for forms with tick boxes, and MICR where security and reliability matter most. Other devices read RFID/NFC tags by radio (no line of sight needed, as with contactless cards and shop tags). Together they save time and reduce typing errors, which ties back to the input devices you already know.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which device is both an input and an output device?

    A touchscreen shows pictures (output) and senses touches (input).
  2. 2. What does a keyboard actually send when you press a key?

    The keyboard reports which key moved. The operating system uses the layout and modifier state to decide which character it means.
  3. 3. What is a driver?

    A driver is software that knows how to communicate with a particular device.
  4. 4. What is the advantage of interrupts compared with polling?

    With interrupts the device signals the processor only when it has something to report.

Summary

  • Input devices turn physical events into data; output devices turn data into light, sound, ink or motion.
  • Many devices, such as touchscreens and storage drives, do both.
  • Ports and wireless standards connect devices, and drivers let the operating system speak each device's language.
  • A key press is a position code that the operating system turns into a character using the layout and modifier keys.
  • Interrupts let devices notify the processor; polling checks repeatedly.

Next you will see how all this information is represented inside the machine in Bits and Bytes.