1. Blog>
  2. PWM Cooling-FAN Control and Over Temperature Protection using LM35 and ATTiny13

PWM Cooling-FAN Control and Over Temperature Protection using LM35 and ATTiny13

by: May 30,2022 10310 Views 0 Comments Posted in Technology


By Hesam Moshiri, Anson Bao


Proper thermal dissipation is an essential rule for nowadays electronics. The best operating temperature for the electronic components is 25 degrees (standard room temperature). Thermal dissipation in some commercial devices is not done properly which affects the lifetime and performance of the devices. So, embedding a compact automatic cooling Fan controller board would be useful. Also, it can be used to protect your own designed circuits and their power components, such as regulators, Mosfets, power transistors … etc.

Previously, I had introduced a circuit to control the cooling fans, however, my intention was not to use any microcontroller and keep it as simple as possible. So, the device was a simple ON/OFF switch for the FAN, depending on the defined temperature threshold. This time, I decided to design a complete and more professional circuit to control the majority of the standard FANs (25KHz PWM) using an LM35 temperature sensor and an ATTiny13 microcontroller.

I used SMD components and the PCB board is compact. It can control one or several standard 3-wires or 4-wires FANs, connected in parallel, such as CPU Fans. Moreover, the target device/component can be protected against over-temperature using a Relay. The user is also notified by visual/acoustic warnings (a flashing LED and a Buzzer).

To design the schematic and PCB, I used Altium Designer 22 and the SamacSys component libraries (Altium plugin). To get high-quality fabricated PCB boards, you can send the Gerbers to PCBWay and purchase original components using the componentsearchengine.com. I initially tested the circuit on a breadboard. I used the Siglent SDM3045X multimeter to accurately examine the voltages and the Siglent SDS1104X-E oscilloscope to examine the shape, duty cycle, and frequency of the PWM pulse.

Download the Gerbers or order 10Pcs high-quality boards, for just 5USD

To order a fully assembled PCB board (Plus FREE shipping). Please contact: anson@pcbway.com


  • Supply Voltage: 12VDC (See text)
  • Load Over-temperature Protection: Yes (above 60 degrees)
  • PWM Frequency: 25KHz
  • FAN Voltage: 12VDC
  • Maximum Load Voltage/Current: 250V-10A (AC/DC)
  • Active Temperature Thresholds: 25C to 60C
  • Over-temperature Warning: Yes (Flashing LED and Buzzer)

Circuit Analysis

Figure 1 shows the schematic diagram of the PWM cooling FAN control device. The heart of the circuit is an ATTiny13 microcontroller [1]. It reads the temperature values and decides what to do with the FAN, Relay, and the Buzzer.


Figure 1

Schematic diagram of the PWM cooling Fan controller device


According to the ATTiny13 datasheet: “The ATtiny13 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. By executing powerful instructions in a single clock cycle, the ATtiny13 achieves throughputs approaching 1 MIPS per MHz allowing the system designer to optimize power consumption versus processing speed.”

I have configured the clock source of the IC1 for 9.6MHz, internal. It is enough for our applications without using an external clock source, such as a crystal. R1 is a pull-up resistor for the Reset pin to prevent the MCU to Reset unwantedly. C2 and C3 are decoupling capacitors to reduce the noise of the +5V supply.

Power Supply

The main component of the power supply is REG1, which is a 78L05 regulator [2]. I selected the SO-8 package for this regulator. P5 is a two-pins male XH connector that supplies the controlling board and FAN(s). The current of the supply voltage (12V) is dependent on the number of connected FANs, otherwise, the current consumption of the +5V supply rail is very low. R7 and C7 build a low-pass RC filter at the input to reduce the input noise as much as possible, however, the voltage drop on the RC filter is not significant to affect the operation of the regulator. D3 is an 0805 green LED that shows the proper supply provision and R8 limits the current to D3. C5 and C7 are used to reduce the output voltage noise.


The components of this section are R5, R6, P4, and D2. D2 is a 0805 red LED that flashes when over-temperature happens. R5 limits the D2 current. P4 is a 2-pins XH male connector that is used to connect a 5V buzzer. R6 limits the current to the buzzer.


The components of this section are Q1, D1, R4, C4, K1, and P3. K1 is a 12V-10A Relay that is used to switch OFF the load when over-temperature happens. It is normally closed (NC) which means the load is ON. D1 protects the Q1 against reverse currents of the Relay’s inductor and C4 damps the current spikes. Q1 is a 2N7002 [3] Mosfet that is used to switch the Relay. R4 is used to pull-down the gate pin of Q1 to avoid unwanted triggering.

LM35 Temperature Sensor

P1 is a 3-pins male XH connector that is used to connect the LM35 sensor to the board. You should mound the LM35 on the heatsink using thermal glue and use a short wire to connect the sensor to the board. C1 is a decoupling capacitor that is used to reduce noise.

AVR ISP Programmer

ISP is a 5-pins male header that is used to program the microcontroller onboard. You can use whatever programmer you like, such as the cheap USBasp programmer or similar.


P2 is a 3-pins XH male connector that is used to connect the Fan(s) to the board. Q2 is used to transfer the PWM pulses to the control pin of the Fan. R2 is a 10K pull-up resistor that shifts the 5V PWM signal level to 12V. R3 is a pull-down resistor to avoid unwanted triggering of the gate pin of Q3. Figure 2 shows a typical Intel CPU Fan which could be one of your options to cool down the components, using this controller board.


Figure 2

An Intel CPU Fan which is cheap enough to be used for cooling

PCB Layout

Figure 3 shows the PCB layout of the design. It is a two layers PCB board and except for the Relay and connectors, other components are SMD. The smallest package size is 0805, you shouldn’t have any problem with soldering the component, although you can order it fully assembled.

Figure 3

PCB layout of the PWM Cooling FAN control circuit 

When I decided to design the schematic and PCB for this project, I realized that I don’t have the component libraries of Q1[4], Q2, REG1[5], and IC1[6] in my component libraries storage. So as usual, I selected IPC-rated SamacSys component libraries and installed the missing libraries (schematic symbol, PCB footprint, 3D model) using the free SamacSys tools and services. There are two ways to import the libraries: You can visit the componentsearchengine.com and download and import the libraries manually, or you can use the SamacSys CAD plugins and automatically import/install the libraries into the design environment. Figure 4 shows all supported electronic designing CAD software [7]. As it is clear, all famous players are supported. I use Altium Designer, so I installed the missing libraries using the SamacSys Altium plugin (Figure 5) [8]. Figure 6 shows a 3D view of the PCB board and assembly drawings.


Figure 4

All supported electronic designing CAD software by the SamacSys plugins


Figure 5

Selected component libraries in the SamacSys Altium plugin


Figure 6

A 3D view and two assembly drawings of the PCB board


The Code of the Microcontroller

I use the Arduino IDE to write and compile the code of the microcontroller. I installed the MicroCore [9] in the library manager to be able to compile the code for the ATTiny13. You can consider the code below:

Code Here

#include <util/delay.h>    

// Clock at 9.6MHz
#define F_CPU 9600000

const int PWMPin = 1;  
analog_pin_t PotPin = A3;
const unsigned char relayPin = 0, buzzerPin = 4;
unsigned int rawTemp = 0, out = 0;
unsigned char counter = 0;

void setup()
  pinMode(PWMPin, OUTPUT);
  pinMode(relayPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  digitalWrite(relayPin, 0);
  digitalWrite(buzzerPin, 0);
  // Phase Correct PWM Mode, no Prescaler
  // PWM on Pin 1(PB1), Pin 0(PB0) disabled
  // 9.6MHz / 192 / 2 = 25Khz
  TCCR0A = _BV(COM0B1) | _BV(WGM00);
  TCCR0B = _BV(WGM02) | _BV(CS00);
  // Set TOP and initialize duty cycle to zero(0)
  OCR0B = 192;    // duty cycle for Pin 1(PB1)

void loop()
  rawTemp = analogRead(PotPin) + rawTemp;
  counter ++;
  if (counter == 15) {
    rawTemp = rawTemp / 15;
    if (rawTemp < 232) {
      OCR0B = 192;
    } else {
      out = map(rawTemp, 232, 558, 192, 0);
      OCR0B = out;
    if (rawTemp > 560)
    counter = 0;
    rawTemp = 0;

void emergency_OFF() {
  while (1) {
    digitalWrite(relayPin, 1);
    digitalWrite(buzzerPin, 1);
    digitalWrite(buzzerPin, 0);

I have defined the ADC reference voltage to be 1.1V-Internal. It means the maximum value of the ADC is 1023 for 1100mV of input voltage. The output voltage of the LM35 temperature sensor for 25 degrees is 250mV and 600mV for 60 degrees. So, it nicely fits within the range of ADC input, up to 110 degrees, without any hardware modifications.

To change the thresholds, you should modify the out = map(rawTemp, 232, 558, 192, 0), for instance, to increase the temperature upper threshold from 60 degrees to 70 degrees.

To install the MicroCore, you should insert this URL in the Additional Boards Manager URLs, in the preferences section of the Arduino IDE:


Figure 7 shows this part of the Arduino IDE.


Figure 7

Additional Boards Manager URLs, Arduino IDE Preferences Section

Then you should go to the Tools menu and Boards Manager and install the MicroCore. Then you will see the installed Board as it is shown in Figure 8.


Figure 8

Installed MicroCore library to support the ATTiny13 MCU

To generate the HEX file and program the MCU, you should go to the Sketch menu and press the Export Compiled Binary. Figure 9 shows a picture of this process.


Figure 9

HEX file generation in the Arduino IDE

Then just connect your programmer to the ISP header of the PCB board and program the MCU. Fuse bits should be set on the 9.6MHz internal clock, with no clock division.


As it is clear from the YouTube video, I tested the circuit on the breadboard before I go to design the schematic and PCB. So, you can be sure that everything works perfectly. Figure 10 shows the PWM signal of the FAN control pin. I used the Siglent SDS1104X-E oscilloscope to capture the signal [10].


Figure 10

25KHz PWM signal to the FAN (Siglent SDS1104X-E)

Bill of Materials

Figure 11 shows the bill of materials and part numbers for this project.


Figure 11

Bill of materials for the PWM Cooling-FAN control circuit


[1]: ATTiny13 datasheet: https://componentsearchengine.com/Datasheets/1/ATtiny13-20SSU.pdf

[2]: 78L05 datasheet: https://www.st.com/resource/en/datasheet/l78l.pdf

[3]: 2N7002 datasheet: https://datasheet.datasheetarchive.com/originals/distributors/Datasheets-26/DSA-502170.pdf

[4]: 2N7002 schematic symbol, PCB footprint, 3D model: https://componentsearchengine.com/part-view/2N7002/Nexperia

[5]: L78L05 schematic symbol, PCB footprint, 3D model: https://componentsearchengine.com/part-view/L78L05ABD13TR/STMicroelectronics

[6]: ATTiny13 schematic symbol, PCB footprint, 3D model: https://componentsearchengine.com/part-view/ATTINY13-20SSU/Microchip

[7]: Electronic designing CAD software plugins: https://www.samacsys.com/library-loader-help

[8]: Altium Designer plugin: https://www.samacsys.com/altium-designer-library-instructions

[9]: MicroCore board manager: https://github.com/MCUdude/MicroCore#analog-pins

[10]: Siglent SDS1104X-E oscilloscope: https://siglentna.com/product/sds1104x-e-100-mhz/

Join us
Wanna be a dedicated PCBWay writer? We definately look forward to having you with us.
  • Comments(0)
You can only upload 1 files in total. Each file cannot exceed 2MB. Supports JPG, JPEG, GIF, PNG, BMP
0 / 10000
    Back to top