I want to use a Raspberry Pi 4 to turn my existing dumb doorbell into a smart one that can trigger alerts and other home automations. The doorbell is a simple chime, consisting of an electromagnet in series with a push switch and a 9V PP3 alkaline battery. When you push the button, the magnet pulls the ringer across to hit one chime; when you release it, a spring pulls it back to hit another. To tell if the doorbell has been pushed, I think need to use a GPIO input to detect whether the electromagnet solenoid has been energised. However, I need to avoid the GPIO being burnt out by either the 9V battery, or the spike of reverse voltage caused when the solenoid de-energises.

What is the best way to safely interface the solenoid circuit to the GPIO input?

To be clear, I don't want to control the solenoid, just to detect whether it has been activated. If necessary, I will software-debounce the circuit (i.e. ignore repeated triggers for a period of time after the initial trigger). I also realise that an RPi 4 is probably overkill for the task, but I already have one running other tasks conveniently close to the doorbell. My current best idea is shown below:


simulate this circuit – Schematic created using CircuitLab

The idea is that the diode would take care of the reverse voltage spike from the solenoid, and R1 would limit the current from the battery to 9mA, to avoid burning out the LED in the optocoupler (4N32). R2 and R3 would then give me a nice logic input, while limiting the current to the GPIO if I accidentally misconfigure it as an output.

The electromagnet solenoid does not have an inductance marked on it, but I measure the resistance across it at 6Ω. When the button is pressed, the voltage across the coil is 4.5V, and the current through the circuit is 0.9A.

I did think about using a simple potential divider instead of the optocoupler (as per this answer), but decided that with the solenoid involved, it would be too tricky to get the output at the right level to reliably trigger HIGH on the GPIO, without taking it over 3.3V by accident. When the solenoid is energised, the load pulls down the battery voltage considerably; I thought that a divider on its own would be too sensitive to changes in the battery characteristics over time. I also discounted rewiring the doorbell so that the switch interfaced directly to one GPIO pin, then using another GPIO to control the solenoid through a relay. My wife will kill me if the doorbell doesn't still work normally if I foul up something on the software side, or decide to use the RPi for something else!

Does the optocoupler circuit look like it will work (without destroying the GPIO)? Are there any potential pitfalls I have missed? Is there a more sensible approach to detecting the doorbell press?

  • Is there a reason for isolating the ground for the doorbell circuit from the RPi ground? If not, this can be done more simply and reliably. If it does need to be isolated, you still need some changes for this to work properly.
    – Seamus
    Commented Mar 30, 2021 at 20:16
  • No specific need to isolate the ground, so long as no unpleasant voltages from the solenoid are going to foul up the RPi circuitry. Would be interested to learn how to improve this design too, as well as any better ideas.
    – JRI
    Commented Mar 30, 2021 at 21:30
  • A couple of questions: 1. Are you certain (as in taken it apart and looked at it) that your doorbell wiring is as you've shown it in the schematic? 2. Are you aware that the steady-state voltage across L1 in your schematic is zero volts, and the implications that has for the life of your 9V battery? What I'm suggesting is that what you've shown in your schematic is not a complete picture. Please take another look at it & let us know what you find.
    – Seamus
    Commented Apr 2, 2021 at 7:33
  • 1) No, no extra components (other than the diode, which I've just added). 2) Yes, my multimeter reads 0V across L1 with the switch open. When the button is pushed and held, it is 4.5V across L1. L1 has no marked inductance, but I measure a DC resistance of about 6 ohm. A fresh 9V battery measures about 9.5V, the one in the bell now measures 8.2V open circuit. My multimeter isn't fast enough to measure the voltage spikes from a momentary press or release.
    – JRI
    Commented Apr 2, 2021 at 18:18
  • So, you can see the solenoid... it's not in a box with "other components"?
    – Seamus
    Commented Apr 2, 2021 at 18:50

2 Answers 2


You asked, "How should I interface a solenoid to a RPi GPIO input?", and solicited some feedback on the schematic in your question:

Does the optocoupler circuit look like it will work (without destroying the GPIO)? Are there any potential pitfalls I have missed? Is there a more sensible approach to detecting the doorbell press?

General Answer:

Your schematic omits some details that may be relevant, but I think your design is a reasonable approach to the objective you have outlined. To add some substance to that answer, and hopefully elevate it above opinion, I have done a wee bit of analysis - summarized below:

Results of a Simulation:

I used LTspice to simulate the operation of your circuit, and then began making changes to address some of the issues I saw. Here's the current state of the schematic:

LTspice Simulation Schematic

A few notes on the schematic:

  1. The odd-looking contraption above the 9V battery labeled V1 and DbSw is simply your doorbell switch as modeled in LTspice.
  2. I assigned a value of 50uH for the inductance of the solenoid L1 based on these figures.
  3. The value of R3 was taken from the measurement you reported in your comments.
  4. The duration of this simulation is 1 second. The doorbell is pressed 100msec after the simulation starts, it is held down for 250msec, and then released.

Following are plots generated from the schematic above:

  • Vdb: doorbell voltage; ref the node in the schematic above
  • I(L1): current through the inductor/solenoid labeled L1 in schematic
  • V(gpio): voltage at opto output to be fed to a RPi GPIO; ref node gpio schematic

Full 1 sec simulation run

The next pair of plots show a zoomed-in view of the timeline near where the boorbell button is pressed, and when it is released:

zoomed-in plots at button events

Perhaps the most noteworthy result in this collection is the I(L1) plot showing a 1.3 Amp current drain on your 9V battery! You didn't say what type of 9 volt battery you were using - I assumed a 9V alkaline battery, and estimated its source impedance to be 1𝛀 (in retrospect, this should have been higher). It's virtually certain the actual current will be less than that shown here - models allow many things we'll never see in the "real world" :) Nevertheless, the simulation result does flag an issue, and so has served a useful purpose.

Some REFERENCES on 9V batteries for your reading pleasure: 1, 2, 3

Recommendations & Alternatives from the Simulation:

  1. The 1N4001 diode in your schematic is not an effective snubber diode - it switches too slowly. The 1N5819 is a Schottky diode - Schottky diodes are often used as suppression diodes as they offer far better performance.

  2. A comment: As an alternative to the opto-coupler, you could use a 3.3V Zener diode. An earlier version of the simulation model/schematic showed good results with a 1/4 watt, 3.3 V zener, but galvanic isolation from a doorbell circuit is "a good thing".

  3. Contact bounce was not considered here, but probably should be. You may want to ask that as another question. One consideration to ponder in your "smart doorbell" project is this: "Are you trying to count visitors, or are you trying to count button presses?

  4. As previously noted: This is not a reasonable application for a 9V alkaline battery - or any battery IMHO.

Hope this is useful to you.

Two comments to the OP:

Your comments helped; you should consider adding them to your question.

I made an edit to the schematic in your question reflecting what I think your comments meant. You should "undo" that edit if you believe I'm wrong.

  • 1
    Thanks for your time helping me with this. My first take-away point is to use a schottky diode, which seems to be doing the trick of neutralising the inductive spikes. But thanks also for pointing me in the direction of LTSpice. I've grabbed a copy and can now model my own circuits. Using 3Ω for the battery internal resistance and modelling the coil with a 6Ω series resistance (rather than a separate resistor) gives me results that match what I can measure, giving me confidence to build the circuit - which now seems to work fine.
    – JRI
    Commented Apr 8, 2021 at 13:17

This is an EE Question, not Pi specific.

What you propose should work (depending on current transfer ratio of opto-isolator - which should be adequate).

I can see only one potential issue - the snubber diode may impact on the ding on release.
If so, you could protect the opto-isolator by connecting the diode across the opto-isolator input (D2 in your diagram). This will bypass any reverse voltage and not damp the solenoid.

  • How do you figure that the snubber diode will impact the ding on release... it's movement is controlled by a spring?
    – Seamus
    Commented Apr 1, 2021 at 21:00
  • Worth checking, but the movement is indeed spring loaded. I've tried adding the diode on its own, with no ill effects.
    – JRI
    Commented Apr 2, 2021 at 17:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.