Attaching a CPU fan to a RPi running Ubuntu Core

When I purchased my Raspberry Pi4 I kind of expected it to operate under similar conditions as all the former Pi’s I owned …

So I created an Ubuntu Core image for it (you can find info about this at Support for Raspberry Pi 4 on the snapcraft forum)

Runnig lxd on this image off a USB3.1 SSD to build snap packages (it is faster than the Ubuntu Launchpad builders that are used for, so a pretty good device for local development), I quickly noticed the device throttles a lot once it gets a little warmer, so I decided I need a fan.

I ordered this particular set  at amazon, dug up a circuit to be able to run the fan at 5V without putting too much load on the GPIO managing the fan state … luckily my “old parts box” still had a spare BC547 transistor and an 1k resistor that I could use, so I created the following addon board:




Finished addon board (with pic how it gets attached)

So now I had an addon board that can cool the CPU, but the fan indeed needs some controlling software, this is easily done via some small shell script by echoing 0 or 1 into /sys/class/gpio/gpio14/value … this script can be found on my github account as

Since we run Ubuntu Core we indeed want to run the whole thing as a snap package, so lets quickly create a snapcraft.yaml file for it:

name: pi-fancontrol
base: core18
version: '0.1'
summary: Control a raspberry pi fan attached to GPIO 14
description: |
  Control a fan attached to a GPIO via NPN transistor
  (defaults to GPIO 14 (pin 8))

grade: stable
confinement: strict
  - build-on: armhf
    run-on: armhf
  - build-on: arm64
    run-on: arm64

    daemon: simple
      - gpio
      - hardware-observe

    plugin: nil
    source: .
    override-build: |

The image is based on core18, so we add a base: core18 entry. It is very specific to the Raspberry Pi, so we also add an architectures: block that makes it only build and run on arm images. Now we need a very simple apps: entry that spawns the script as a daemon, allows it to access the info about temperature via the hardware-observe interface and also allows it to write to the gpio interface we connect the snap to, to echo the 0/1 values into the sysfs node for the GPIO. A simple fancontrol part that just copies the script into the snap package, and off we go !

The whole code for the pi-fancontrol snap can be found on github and there is indeed a ready made snap for you to use in the snap store at

You can easily install it with:

snap install pi-fancontrol
snap connect pi-fancontrol:gpio pi4-devel:bcm-gpio-14
snap connect pi-fancontrol:hardware-observe

… and your fan should start to fire up every time your CPU temperature goes above 50 degrees….

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s