A Python script monitors CPU temperature and controls the fan using on-off control with temperature hysteresis.
By default, the fan is directly connected to the GPIO – this implies its constant operation. Despite the relative quiet operation of the fan, its continuous operation is not an effective use of an active cooling system. At the same time, the constant operation of a fan can just be annoying. Also, if Raspberry Pi is turned off, the fan will still work if the power is connected.
This article will show how, using simple and not complicated manipulations, turn an existing cooling system into a smart one, which will only be turned on when the processor really needs it. The fan would be turned on only when there is heavy usage, thus reducing fan power consumption and noise. Also extending fan life by keeping it off when not needed.
What You Will Learn
- How to implement a Python script to control a fan based on the current temperature of Raspberry CPU using On-Off control with temperature hysteresis.
- How to transport data from your RaspberryPi to Things Speak Cloud.
Items for Getting Started
The components you will be required for this project are as follows
- Raspberry Pi 4 Computer Model B 4GB
- NPN transistor S8050
- 330ohms resistor
- Armor Aluminum Metal Case with Dual Fans for Raspberry Pi
- Jumper cables
Building the circuit
The circuit is pretty simple. The power to the fan is cut off using NPN transistor. In this configuration, the transistor is acting as a low-side switch. Resistor is only required to limit the current through GPIO. The Raspberry Pi’s GPIO has a maximum current output of 16mA. I used 330 ohms which gives us a base current of about (5-0.7)/330 = 13mA. I selected a NPN transistor S8050, so switching a 400mA load from both fans is no problem.
Perform connections as shown in the following schematic diagram above.
Log CPU temperature with ThingSpeak
ThingSpeak is a platform for projects based on the Internet of Things concept. This platform allows you to build applications based on data collected from sensors. The main features of ThingSpeak include: real-time data collection, data processing and visualization. ThingSpeak API not only allows you to send, store and access data, but also provides various statistical methods for processing them.
ThingSpeak can integrate popular devices and services such as:
- Raspberry pi
- ioBridge / RealTime.io
- Electric imp
- Mobile and Web applications
- Social networks
- Data Analysis in MATLAB
Before we start, you need an account at ThingSpeak.
- Go to the following link and sign up to ThingSpeak.
- After your account activation, sign in.
- Go to Channels -> My Channels
Low cost PCB at PCBWay - only $5 for 10 PCBs and FREE first order for new members
PCB Assembly service starts from $30 with Free shipping all around world + Free stencil
Extra 15% off for flex and rigid-flex PCB