Raspberry Pi Zero Load Average Monitor

3 minute read

In a previous post, I reviewed the status display I built for a Raspberry Pi server running in my workshop. Recently, I had a similar need for a Raspberry Pi Zero W server I was building for the office. In this case, I wanted to know at a glance, if the Pi Zero was operating, and an estimate of the current load level. Also, I wanted the form factor to be as small as possible, near zero (pun intended). During my research for this project, I discovered this tutorial on the Pi Hut site for controlling LEDs tied to a GPIO pin. As I had scavanged many low-profile LEDs from dead CD drives over the years, this seemed like a perfect solution. After some trial and error, I found a suitable LED and location on the Pi Zero. By trimming the leads of the LED appropriately, and using some heat shrink tubing to insulate the resistor, I was able to solder it neatly in place against the Pi Zero board (well, I burned the heat shrink tubing a little, but ignore that).

Pi Zero Heartbeat LED

Also, in keeping with the near zero form factor requirement, I used a Zero Stem USB shim to allow the board to be plugged directly into a USB power adapter, avoiding the need for any cables or enclosures. In future, I could add a simple enclosure to protect the mainboard itself, but as this will be in an out of the way location, I didn’t bother.

Once I had the hardware done and tested, I put together the Python code to make the Load Monitor LED actually work. I had originally thought it would be a neat idea to mimic a heartbeat, where the LED “heartbeat” would increase with the load average of the device. I even went through the trouble of researching the average “resting heart rate” (p.s. about 60 bpm). My plan was for the LED to blink at the speed of a resting heartbeat when the load was close to zero, and increase as load increased. Unfortunately, after some testing, I discovered this had the interesting side effect of also driving the load average up. As the load average increased, the script looped faster, causing the script to use more cycles, until it became deadlocked at 100% CPU load.

I could have inserted a governing mechanism to limit the LED blink rate and avoid the deadlock, but I wanted to avoid the script impacting load average whatsoever. In the end, I built the script to increase the number of flashes as load average increased. In the code shown below, it is set to blink twice per second if the load average is 0 to 0.30, blink three times if the load is 0.30 to 0.70, and four times if greater than 0.70. This does not impact the CPU load in any meaningful way, but allows me to quickly see if the Pi is running (e.g. if the Pi has crashed, the LED stops blinking completely), and what is the current load average. Also, the script can easily be edited to add more granularity to the reporting, or change the load levels used.

#!/usr/bin/python
import RPi.GPIO as GPIO
import os, time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)

while True:
	loadavg=os.getloadavg()[0]
	if (loadavg < 0.3): beat = 2
	elif (loadavg > 0.7): beat = 4 
	else: beat = 3
	repeat = 0
	time.sleep(1)
	while repeat < beat:
		GPIO.output(18,GPIO.HIGH)
		time.sleep(0.15)
		GPIO.output(18,GPIO.LOW)
		time.sleep(0.15)
		repeat = repeat + 1

Lastly, to ensure the script runs at boot time, I added it to the /etc/rc.local file on the Pi Zero.

# Start LED heartbeat script
/home/pi/bin/heartbeat.py &


For your viewing pleasure, I have included a short video of my Pi Zero Heartbeat LED in action. Please forgive the poor quality, as the lighting wasn’t the best, but you get the idea. This video shows the LED “heartbeat” increasing as load level rises from 0.00 to 0.98 while I run a Jekyll rebuild of this site. I have it plugged into an APC UPS in my office using a USB power adapter, allowing it to sit flush with the UPS. When at my workstation, I can glance over and see the LED easily. As always, it’s not the prettiest solution, but does the job, and I like it.


I hope you found this post useful, or at least interesting. If you would like to contact me or provide feedback, please use the Contact page, or message me on Twitter. Thanks!