How to modify existing circuit to additionally control it from outside by raspberry?
The purpose of the 4017 IC in this circuit is to "remember" whether the relay should be on or off. If you control the relay using software in a microcontroller (an Arduino, Raspberry Pi, etc), then you can simply store that state in a variable in the program instead. The 4017 IC is used here as a one-bit toggle-able memory and can be replaced by the microcontroller.

That way, you also don't need to "send the light status" to the microcontroller, since the microcontroller is the device that gets to decide the on/off state, and thus already "knows" the state.

The only parts of the circuit you need to keep are:


The relay, of course.
The snubber diode connected in parallel to the relay (important).
The C1 decoupling capacitor (C1).
The transistor and its base resistor (R2).
Optional: The indicator LED and its current limiting resistor (R1).


You connect an output pin from the microcontroller to the base resistor, replacing pin 2 of the 4017 IC.

The 4017 IC, D2, C2, C3, R3 and R4 can then be removed.

For information about how to choose the transistor and the base resistor, I recommend Ben Krasnow's excellent video tutorial: https://www.youtube.com/watch?v=8DMZSxS-xVc

If you want to keep the button

You can also keep the button. In that case I would recommend connecting it to an input of the microcontroller. You also need to debounce it, either in software (very easy) or using extra hardware components. If you debounce it is software you would typically connect the button between the microcontroller input and the positive microcontroller power supply voltage (3.3 V or 5 V, not 12 V!), and also connect a pull-down resistor of about 10k Ohms between the input and ground.

What pressing the button does (eg. toggle the relay) is then up to the software.