ad space

How to Build a PID-Controlled Temperature System with an Arduino, MOSFET, and 12V Fan

Creating an Arduino-based PID-controlled temperature system can add precision and automation to many temperature-sensitive projects. By using a MOSFET instead of a relay to control a 12V fan, we gain smoother control over fan speed, allowing us to maintain a consistent temperature. This guide will walk you through setting up this system, explaining how a MOSFET, such as the IRF540N, can be an effective component in Arduino PID controller projects.

Components You’ll Need

  • Arduino (any model with PWM output)
  • LM35 Temperature Sensor
  • 12V DC Fan
  • IRF540N MOSFET
  • 10kΩ resistor (for gate pull-down)
  • 220Ω resistor (optional, for gate protection)
  • 12V power supply for the fan
  • Jumper wires
  • Breadboard or similar setup

Why Use a MOSFET Over a Relay?

Using a MOSFET like the IRF540N instead of a relay allows us to take advantage of PWM (Pulse Width Modulation) control, which enables us to adjust the fan speed smoothly based on temperature changes. A relay, on the other hand, would only allow us to turn the fan on or off, lacking the proportional control a MOSFET provides. For a detailed overview on IRF540N MOSFET usage in Arduino projects, refer to this IRF540N ultimate guide for Arduino users.

Wiring Diagram

  1. LM35 Sensor: Connect the LM35 as follows:

    • VCC to 5V on the Arduino.
    • Output to A0 on the Arduino.
    • GND to GND on the Arduino.
  2. MOSFET Connection:

    • Connect the Drain of the MOSFET to the fan’s negative terminal.
    • Connect the Source to GND.
    • Connect the Gate to Arduino Pin 5 through a 10kΩ pull-down resistor to ground and a 220Ω resistor from the Arduino pin for current limiting.
    • Connect the positive terminal of the fan to the 12V power supply.
  3. Power Supply: The 12V fan requires its own 12V DC power source. 

 The circuit diagram for PID controller based on Arduino.

How to Build a PID-Controlled Temperature System with an Arduino, MOSFET, and 12V Fan

Why Use a 10kΩ Gate Pull-Down Resistor?

The pull-down resistor ensures that the gate of the MOSFET stays at a defined low (0V) when the Arduino pin is not actively driving the gate. Without it, the gate may "float" and cause the MOSFET to turn on or partially conduct, leading to unpredictable behavior of the fan or load.

Placement of the Pull-Down Resistor

  • Connect one end of the 10kΩ resistor to the gate of the MOSFET.
  • Connect the other end to the source (which should be connected to ground).

This resistor will keep the gate at ground potential (0V) when there’s no signal from the Arduino, ensuring that the MOSFET remains off.

Additional Recommendations

  • If the Arduino pin drives the gate at a high frequency, you can also consider a 220Ω resistor in series between the Arduino pin and the MOSFET gate to limit inrush current and protect the pin.
  • Ensure that your ground connections (Arduino ground and MOSFET source ground) are solid to avoid ground loops or floating grounds.

For further insights into using MOSFETs and other components for motor control, check out this guide on TIP31C as a motor controller.

Arduino Code for PID Control

In this code, the PID library calculates the required fan speed based on temperature. Instead of toggling on and off, the fan speed is adjusted with PWM signals.

#include <PID_v1.h>

const int sensorPin = A0; // LM35 sensor connected to analog pin A0
const int fanPin = 5; // Fan control pin connected to MOSFET gate

double setpoint = 25; // Desired temperature in Celsius
double input, output; // Variables for temperature and PID output
double Kp = 2, Ki = 5, Kd = 1; // PID tuning parameters

PID myPID(&input, &output, &setpoint, Kp, Ki, Kd, DIRECT);

void setup() {
pinMode(fanPin, OUTPUT);
Serial.begin(9600);
myPID.SetMode(AUTOMATIC);
}

void loop() {
int sensorValue = analogRead(sensorPin);
double millivolts = (sensorValue / 1024.0) * 5000;
input = millivolts / 10;

myPID.Compute();
analogWrite(fanPin, output); // PWM control for smooth fan speed

Serial.print("Temperature: ");
Serial.print(input);
Serial.print(" C, Fan Speed: ");
Serial.println(output);

delay(1000);
}

Code Explanation

  • Reading Temperature: The LM35 provides a voltage proportional to the temperature, read via an analog pin on the Arduino.
  • PID Calculation: Using the PID library, we calculate the optimal fan speed based on the difference between the setpoint and current temperature.
  • PWM Fan Control: The analogWrite() function sends a PWM signal to the MOSFET gate, controlling the fan speed to maintain the set temperature.

If you’re new to PID controllers on Arduino, this tutorial on using Arduino as a PID controller provides a helpful overview.

Fine-Tuning the PID Parameters

The constants Kp, Ki, and Kd in the code control the proportional, integral, and derivative actions of the PID controller. Start by adjusting Kp to improve responsiveness, then slowly increase Ki and Kd to reduce overshoot and steady-state error. For additional guidance on PID tuning, you might find this article on a similar PID temperature controller setup useful.

Video Demonstration

Watch the following video to learn how the control system works with arduino as PID controller to automatically control a dc motor by detecting temperature with LM35 sensor.

Applications and Benefits of the MOSFET-Based PID Temperature Controller

This MOSFET-controlled PID setup allows for smooth, energy-efficient fan control, making it suitable for a range of applications:

  • Greenhouse Temperature Control: Maintain optimal conditions for plants by regulating airflow.
  • Electronics Enclosure Cooling: Avoid overheating in DIY electronics enclosures.
  • PC Cooling Fan Controller: Automatically control fan speed based on CPU temperature or other sensors.

If you’re interested in controlling DC motors with minimal components, check out this tutorial on the simplest DC motor controller setup.


Using a MOSFET instead of a relay enhances the flexibility and precision of your Arduino temperature controller, allowing you to use PID to control fan speed smoothly. This approach improves efficiency and reduces noise, making it a great choice for DIY home automation projects.

Post a Comment

Previous Post Next Post