Hardware watchdog for Raspberry Pi Zero
This is an additional attempt to make Raspberry Pi more reliable (the fist
was to make RPI file system read-only). I have noticed that RPI sometimes does not boot after it was properly
powered down. Sometimes it can freeze during the normal operation. The solution for those situations is a
watchdog timer. The idea is that the watchdog would reset the device unless the device pings it on a regular
basis.
RPI does have a built-in watchdog, but as far as I could understand, it is a
part of the OS. But what if the OS did not boot? You would end up with a frozen machine.
That is the reason why I tried to find some hardware-based watchdog
implementations for the RPI. To be more precise, I wanted to manage RPI Zero, since Zeros are used to gather
sensor data all around my flat.
There are several hardware implementations based on the 555 timer IC. It is a
very versatile IC and one setup that I have found useful for the watchdog implementation is shown in the
picture below:
The RUN pin on the Zero needs to be connected to the ground in order to reset the device.
The watchdog is pinged via GPIO pin (in this particular example, it is GPIO
pin 21). To do so, it is sufficient to periodically ping the watchdog via GPIO pin 21. Here is the Python
code which pings the watchdog:
import RPi.GPIO as GPIO
import time
import threading
import os
import sys
# Port which shorts the capacitor in the watchdog(GPIO 21)
# pin 40
PORT_OFF = 21
# GPIO pin enumeration
GPIO.setmode(GPIO.BCM)
GPIO.setup(PORT_OFF, GPIO.OUT)
#short the capacitor
GPIO.output(PORT_OFF, 0)
time.sleep(0.250)
# disconnect
GPIO.cleanup()
RPI pings the watchdog by shorting the 100uF capacitor to the ground. It is
done by setting the logical 0 to the GPIO 21. The capacitor is shorted to ground via 200 Ohm resistor, and
that just restarts the 555. If the capacitor is not shorted in time, the 555 would send the logical 0 to the
RUN pin on the RPI and that would reset the device.
The period for the reset is approx. 2 minutes, and that was set using the 470K
resistor and 100uF capacitor. Increase one of them, and you will get the longer period.
I have set the Python script to ping the watchdog at the boot time, and then
to be called each minute. The first ping occurs just 15 seconds after the boot.
It is important to set the RPI file system to read-only. If not done so, the
reset would probably corrupt the SD card data. I have found a good script for setting up the RPI file system to read-only
Comments
Comments powered by Disqus