Arduino water solar heater pump control
the transistor is there because the relay operates at 12V and probably wasnt over 100mA of current to operate. the arduino can't provide a signal of that magnitude so the transistor boots its output capability..

The diode is there because the relay contains an electromagnet, and an electromagnet is a type of inductor, and when you try to turn an inductor off it generates a high voltage, which could easily be hundereds of volts. but the BC547 transistor is can only withstand a few tens of volts Vce when the transistor turns off the energy in the inductor forces the switched terminal more positive until it gets to 0.6V above the 12V supply and then the iN4001 starts to conduct the current and he stored energy is burned off as heat in the electromagnet's resistance and in the diode's voltage drop. This is called a freewheeling diode or catch diode

All this complexity could be avoided by using a solid-state relay as they can be triggered directly by the arduino outputs but they can be quite expensive.