Introduction: How to Control Fan on Raspberry Pi 4 With Transistor S8050 and Monitoring Data on OLED 0.96” Screen

The idea was to design and 3D print the case for Raspberry Pi 4, which will be more open due to better air circulation, and also I wanted to hook up a small 0,96 inch screen for monitoring different data and to have power on button. On the following link you can find STL files for 3D printing.

Firstly, when I put all together I figure out, that when I turned off the Pi fan was still spinning because even when the Pi is turned off, 5V pin is providing current. So, I needed to figure out how to power switch the fan. The best and easiest solution was to control switching with NPN Transistor S8050. And it was not just gave me the option to switch on/off but also to switch fan at any temperature I want.

Step 1: Connecting Components

Connecting fan:

5V pin is connected to the positive pin (+) on the fan. The negative pin (-) of fan is connected on C pin (Collector) on S8050 transistor. Between positive and negative pins on the fan I also added flyback diode 1N4001 for protection.

(A flyback diode is a diode connected across an inductor used to eliminate flyback, which is the sudden voltage spike seen across an inductive load when its supply current is suddenly reduced or interrupted. It is used in circuits in which inductive loads are controlled by switches, and in switching power supplies and inverters.)

Flyback diode is connected that positive (+) side is wired on negative (-) pin of the fan, and negative (-) side of diode is wired on positive (+) pin of the fan.

Then the B pin (Base) on S8050 transistor is connected on GPIO pin 18, and also here was added resistor for protection. The value of used resistor depends on transistor and fan specification. In my case 2785 ohm resistor was needed. You can calculate the value on the following way.

R = (Us - 0.7)hFE / fan current

Us.... source or the trigger voltage to the base resistor. In this case GPIO pin 18 provides 3.3V.

hFE...forward gain of the transistor (you can find it in datasheet of transistor). In my case I used value 150.

Fan current data you can find on the label of th fan. In my case current was 0,14 A.

And for the end E pin (Emitter) of S8050 was connected on the GND GPIO pin of Raspberry Pi.

Then I made the python script which turn on the fan. This script only turn on the fan. (To turn on/off the fan at specific temperature you need to use script from the following link)

sudo nano

add the following text:

#!/usr/bin/env python3
import os

import signal

import sys

import RPi.GPIO as GPIO

pin = 18 # The pin ID, edit here to change it


GPIO.setup(pin, GPIO.OUT)


GPIO.output(pin, True)

Connecting OLED screen:

Here is no rocket science to connect it. You have four pins to wire on.

Vcc ---> 3.3V

GND ---> GND

SDA ---> SDA

SCL ---> SCL

Then you need to enable I2C in raspberry pi over raspi-config and restart the Pi.

sudo raspi-config

Then you need to install following packages:

sudo apt-get install –y python-pil python-smbus ic2-tools

Download and install python library for SSD1306 based OLED screens:

sudo python -m pip install --upgrade pip setuptools wheel

git clone

cd Adafruit_Python_SSD1306

sudo python install

Test if screen is working with one of the following examples which you can find it in Adafruit_Python_SSD1306 folder:

sudo python

To display the temperature on the screen I made python script which you can find on the following link. Download it and put the script and font file (.ttf) in the examples folder.

Connect the power on button switch:

To hook up button switch on the Raspberry Pi, solder one of the button switch pin to the GLOBAL_EN pin and the second pin of the button switch to the GND pin (pin i located between GLOBAL_EN and RUN). So, now you can turn on the Raspberry Pi with short push of the button.

Step 2: Set the Scripts to Run on the Boot Up

Now we need to create services which will run both scripts ( and at boot up.

  • Create new file temp.service at the folder /lib/systemd/system:

sudo nano /lib/systemd/system/temp.service

Add the following text:


Description=My Script Service



ExecStart=/usr/bin/python /home/pi/Adafruit_Python_SSD1306/examples/


  • Create new file fan.service at the folder /lib/systemd/system:

sudo nano /lib/systemd/system/fan.service

Add the following text:


Description=My Script Service [Service]


ExecStart=/usr/bin/python /home/pi/


  • The permission on the unit file needs to be set to 644.

sudo chmod 644 /lib/systemd/system/temp.service

sudo chmod 644 /lib/systemd/system/fan.service

  • Then tell the systemd to start it during the boot sequence:

sudo systemctl daemon-reload

sudo systemctl enable temp.service

sudo systemctl enable fan.service