I think we all agree that every computer is better with blinking lights. In an effort to overcome my fear of electronics, I have recently acquired an Arduino and one of my first projects was a CPU load indicator for my computer:

It was of course inspired by the blinkenlights on BeBoxes and the way it works is very simple. I got a 10 segment LED bar graph and two six-pin 330 ohm resistor networks (they're just five resistors in a single package with one leg of each resistor connected to the sixth common pin). I connected the positive legs of the LEDs to ten digital pins on the Arduino and I connected the negative legs to ground through the resistors. Here's what it looked like during testing:

Then I needed a way to tell the Arduino which LEDs to turn on. An obvious choice was to communicate over USB (which will also provide power). Here's the sketch that's running on the Arduino:
const int FIRST_PIN = 3;

void setup() {
  for (int i=0; i<10; i++) {
    pinMode(FIRST_PIN + i, OUTPUT);

void loop() {
  if (!Serial.available()) {
  int cpu_load = Serial.read();
  int n = (cpu_load+5)/10;
  for (int i=0; i<10; i++) {
    digitalWrite(FIRST_PIN + i, i<n ? HIGH : LOW);
And here's the Python program that's running on the computer:
import serial
import psutil

ser = serial.Serial('/dev/ttyACM0')
while True:
    cpu_load = psutil.cpu_percent(interval=0.1)
As you can see it sends a byte with the CPU load value (0-100) ten times per second. I only tested it under Linux, but the psutil module should also work on Windows and OS X. It can also give you per-CPU core percentages, but that would require more LEDs and some other way to drive them when we run out of I/O pins on the Arduino.

No comments:

Post a Comment