>
a basic CMOS output (say a GPIO port on a processor) to switch a relay
This isn't analogue, except in the sense that all "digital" circuits are overdriven analog circuits.
It is more like wiring house lamps.
But there are ways to screw it up.
If you have a
very low power relay, you can drive it directly from some digital chip outputs. CMOS may only be good for 1mA at 4V, or may be good for 16mA at 4.5V: read those datatsheets! There are 5V relays that are made to work directly. (If you remember TTL: in TTL it is better to connect the relay to V+. TTL is poor at pull-up, better at pull-down.)
The general case for small relays is 6V to 24V, 20mA to 100mA. More than a general logic output can deliver. The general solution is any transistor rated for the voltage and current, plus a couple resistors so the logic and transistor play nice together. R1 must limit current: enough to saturate the transistor with full relay current, yet not so much that the logic strains. 1mA will generally be a good number. Assuming the logic pulls up to 4.0V, and the transistor Base voltage is about 0.7V, we have 4V-0.7V= 3.3V across the resistor. 3.3V/1mA= 3.3K ohms. R2 is usually not needed with CMOS and silicon transistors in reasonable temperatures.
If you are whacking big relays, line-power control or valve-tube B+ switching (or I was switching a 3-phase air conditioner), you may need a relay more than about 12V 100mA. Then the old standby TIP120 Power Darlington is cheap handy sturdy interface. Use R1 about 2K. R2 is built into TIP120; if you use another Darlington then use 5K from Base to ground to drain off leakage. TIP120 is rated 60V, so "should be OK" past 48V relay voltage. I like lots of margin: TIP122 is rated 100V and that is what I would use at 48V.
In
all transistor-drive-relay applications, put a diode across the relay coil. Otherwise a 24V relay will spike up over 200V when you turn the transistor off, and xap the transistor. The diode should be rated for the relay voltage and current: 1N914 for 50mA-100mA chores, 1N1004 for most 1A relays, or something larger if your relay is very fat. (Actually the peak diode current is equal to the relay current, and very transient, so you could use a much smaller diode. But diodes like 1N1004 are so cheap, you may as well over-kill.)