for better isolation and because of sparking at the contacts, it is suggested using a separate supply with optoisolation like an image attached, but in my design, I wave only one power supply and I have a limitation in adding another power supply, how can I isolated the power of relay from the power of micro with considering that I have only one power supply?
2 Answers
If you've got the same ground on both sides the optocoupler adds very little. the magnitude of signal that can flow back theough the base resistor if you'd connected R1 straght to Q2 is very small.
I'm using a raspberry pi to control some 12V relays with just a resistor (330 ohms) and a transistor (BC547) and a diode parallel with the relay coil.
With the diode there that will stop sparking (unless you mean sparking in the relay contacts) and to keep that out is a lot more work.
If sparking of the relay contacts is the problem you need a snubber.
For a separate power supply use a second regulator to power the relay, or connect before the regulator that is used for the MCU
As Jasen said, You can use a snubber circuit at relay contact and can turn on a transistor directly with MCU GPIO pin. By which you can compromise the separate power supply need of relay.