Q: What is the purpose of the diode next to relay on the schematic?
The transistor controls the relay by switching ON/OFF current through the coil.
The inductance law says that a change of current flowing through the inductor generates
voltage across the inductor, and the faster the current change the greater the resulting voltage will be.
The drawing below illustrates this.
When relay if turned off, negative current change will generate voltage of reverse
polarity across the inductance of the relay.
This volatge can two or three times exceed the supply voltage and may damage the transistor,
so the doide creates a safe pass for the current and removes the voltage spike on the inductor.