cleaver
Posts: 3
Joined: Tue Oct 02, 2012 7:40 pm

Heartbeat from GPIO to safely drive relay

Tue May 28, 2013 9:57 pm

I'm currently designing a circuit controlled by GPIO to switch my central heating on/off via a relay (using a suitable transistor). I think I've got to grips with it, but I want to add a failsafe.

If the program controlling the circuit crashes or the Pi hangs, I understand that any GPIO pins will remain in the state they were set at. So if my heating is on (GPIO pin high) the heating will stay on in the event of a crash.

I want the circuit to go low and the relay to deactivate if something goes wrong. I've looked into the built-in Broadcom watchdog which will reset the Pi. That sounds great, but then I have to rely on the GPIO pins I'm using being in a low state at boot -- apparently that's bad practice and could change in a future firmware or kernel.

So I thought that a hearbeat coming out of the GPIO pin would be more appropriate to drive the circuit. The circuit would remain high only if a heartbeat pulse was received in the last x seconds.

This is where I'm stuck. What component should I drive from the GPIO pin with a heartbeat? I thought maybe a 555 timer in monostable mode acting as an active high trigger? Am I on the right lines? But then what happens if the heartbeat pin gets stuck high?!

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5618
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: Heartbeat from GPIO to safely drive relay

Tue May 28, 2013 10:21 pm

cleaver wrote: ...
So I thought that a hearbeat coming out of the GPIO pin would be more appropriate to drive the circuit. The circuit would remain high only if a heartbeat pulse was received in the last x seconds.

This is where I'm stuck. What component should I drive from the GPIO pin with a heartbeat? I thought maybe a 555 timer in monostable mode acting as an active high trigger? Am I on the right lines? But then what happens if the heartbeat pin gets stuck high?!
As I've mainly used 555's in astable mode (ie. as an oscillator) I'm not sure** if it's "re-triggerable" ie. re-generates a pulse if it's trigger pin is still in active state (I guess we both could check the specs. :) ). If it is all you'd need is a non-re-triggerable device (eg. CMOS 4047 or a 74xx123 {121?}) to buffer (and perhaps) stretch the GPIO pulse to the 555.
Trev.
**It's probably >10 years since I last used one.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Heartbeat from GPIO to safely drive relay

Tue May 28, 2013 11:17 pm

You could try a small npn transistor. Connect it across a 0.1 uF cap that has a 100 k resistor pullup to 12V. Each time the npn turns on it will discharge the cap. If it stops turning on the cap will charge to +12V. Connect Source of a p-ch MOSFET to 12V with the gate to the cap. Connect the Drain of the MOSFET to your relay. The relay has power if the npn is switching and keeps the cap discharged. Make it switch by connecting the Base to GPIO pin thru another cap 0.010uF in series with 1k resistor. Add a diode from emitter to base. Program GPIO to give a 100Hz frequency to make the npn switch.
1N4148, 2N3904, BSS84

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Heartbeat from GPIO to safely drive relay

Wed May 29, 2013 3:06 am

Here is a simulation schematic that shows the circuit and a couple of waveforms. The top waveform shows the voltage applied to the relay (RLOAD in the schematic). The bottom waveform shows the voltage across the 0.1uF capacitor. When the npn transistor stops switching the relay turns off. V1 represents the GPIO pin.
Attachments
failsafe.jpg
LTSpice simulation
failsafe.jpg (40.01 KiB) Viewed 2670 times

sprinkmeier
Posts: 410
Joined: Mon Feb 04, 2013 10:48 am
Contact: Website

Re: Heartbeat from GPIO to safely drive relay

Wed May 29, 2013 4:22 am

I used a VOX circuit for a HAM radio.
(not this one, but one like it:
http://www.rainbowkits.com/kits/vox-1p.html
)
Instead of a microphone input I used a digital output pin.
Rather than setting the pin I had to toggle it in order to fool the VOX into thinking there was a microphone signal, causing it to trigger the relay.
If the PC were ever to crash, no matter what state the pin was in, the VOX would shut off.

Pretty much exactly what Tage suggested, only lazier as I just had to buy/assemble a kit.

cleaver
Posts: 3
Joined: Tue Oct 02, 2012 7:40 pm

Re: Heartbeat from GPIO to safely drive relay

Wed May 29, 2013 12:57 pm

Thanks ever so much Tage for taking the time to produce an LTSpice simulation.

Does the the 0.1uF capacitor discharge because 3.3V is less than 12V? So when the pulse is high, the capacitor discharges? Does V2 need to be 12V or can it be 5V? Or will the difference between 3.3V and 5V cause the capacitor to discharge more slowly? I guess I can try it for myself in LTSpice (I've been meaning to learn how to use it anyway).

On another note, can the GPIO pin be set to pulse at a particular frequency? If so, does the GPIO pin still oscillate when the Pi or controlling program has crashed and thus the relay continues to be high?

Or do I have to pulse it myself in code? If so, how do I protect from the pulse getting "stuck" on i.e. high?

EDIT: I've since learnt that C1 before the transistor prevents an always high signal from pulsing the transistor and thus guards against a stuck GPIO pin. Also, the voltage difference between doesn't matter as C2 is discharged to ground, not via the 3.3V portion of the circuit! But the question still stands on whether the GPIO pin will continue pulsing in the event of a lock/crash.

User avatar
Tage
Posts: 287
Joined: Fri May 24, 2013 2:29 am
Location: St Thomas, Ontario Canada

Re: Heartbeat from GPIO to safely drive relay

Thu May 30, 2013 12:55 am

I think it is highly unlikely that the GPIO pin continues to pulse after a crash. Have a look at what Joan posted in robotics forum, she has created some very useful functions for controlling servos, outputting PWM signals, reading signals on the pins, etc. I tried it out when I needed to control several servos from my Pi (I am building a remotely controlled camera using the camera module, and need a way to move the camera up-down, right-left, etc.) and found Joans work just incredible useful. Joan may be able to answer your question if there is a risk that the GPIO will continue to pulse after the main program crashes.
I think it is a good idea for anyone tinkering with electronics to learn to use LTSpice. it will answer most questions and is a great learning tool. I use it almost every day myself to check out circuit ideas an verify calculations. And it is totally free!

Return to “Interfacing (DSI, CSI, I2C, etc.)”