Simple Relay Circuit
A relay is a simply electromechanical switch. It has a magnetic coil that causes a small switch to close when current is applied. Relays are commonly used in automotive solutions. They can usually handle high currents and voltages on their load side while only taking a few milliamps to switch. Because it is a mechanical switch there are a limited number of cycles it can close due to electrical arcing and mechanical plating limitations.
Most relays require 100mA. This is easy when you are a car operating off a 12V battery with a low internal resistance capable of supply hundreds of Amps. For smaller micro controllers it is more difficult and we will need a driver circuit.
There are a few options but a BJT transistor is a common solution. The transistor allows the relay to be turned on and off using only a few milliamps since most micro controllers cannot supply more than 10mA per IO pin. In the circuit below a 100 Ohm resistor at the base of the resistor biases the transistor and sets the base current. Diode D1 is used to handle the reverse voltage generated when the relay is turned off. When the relay is turned off the magnetic field collapses creating a reverse voltage called the inductive kickback or back EMF. This could damage Q1 if the diode were not there to clamp that voltage.