The HC-SR04 module looks intimidating from the front — two metal cylinders staring at you like robot eyes — but its interface is almost embarrassingly simple. Four pins. Two of them are just power. The other two are one input from the Arduino, one output back.
Why TRIG is OUTPUT and ECHO is INPUT
From the Arduino's perspective, TRIG is something it drives (write HIGH for 10 µs to start a measurement) and ECHO is something it reads (the sensor pulls it HIGH for the duration of the round-trip). So pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); — easy to remember once you've named the pins right.
The sensor handles all the timing internally. The Arduino doesn't generate the 40 kHz burst; it just nudges the sensor to do so. This pattern — the chip giving high-level commands and the sensor doing the dirty work — repeats across almost every digital sensor you'll ever use. The microcontroller is mostly a conductor; the sensors are the orchestra.
Cheap because everything's integrated
What's inside that little board: a 40 kHz oscillator, a transmit driver, two piezoelectric transducers (one to emit, one to listen), an amplifier, a comparator, a small microcontroller that does the pulse-out / listen-for-echo timing. For $2, all of that is hidden behind a four-pin protocol. The whole reason the maker scene works is parts like this.