ESP8266 Fan Speed Control with DS18B20 Temperature Sensor

temperature-sensor-fan-nodemcu.jpg

This simple project controls the speed of a fan based on the reading from a temperature sensor.

The video below shows the fan speed changing when the temperature sensor is moved from cold water to hot water. You can hear the fan speed increase and see the readings changing on the Arduino IDE serial monitor.


For this project you need any ESP8266 development board (a NodeMCU in this case), a DS18B20 temperature sensor, a mosfet module (eBay, AliExpress), a two wire computer fan and a 12v power supply or batteries.

Project Components

The 12 volts that the fan requires cannot be directly connected to the pins of the ESP8266. The ESP8266 is used to control the mosfet module which bridges the power between the fan and the power supply. The speed of the fan is controlled using PWM (Pulse Width Modulation) by switching the power on and off rapidly.

The wiring looks like this. Note the use of the resistor.

ESP8266 DS18B20 Fan Wiring

Wiring diagram for the ESP8266, DS18B20 and 12v fan

ESP8266 DS18B20 Breadboard

Breadboard layout for the ESP8266, DS18B20 and 12v fan

If you don’t have the OneWire and DallasTemperature libraries installed you’ll need to install them via the library manager. In the IDE menu Sketch > Include Library > Manage Libraries… Search for OneWire and install it:

OneWire Installation

Then search for Dallas and install DallasTemperature:

DallasTemperature Install

The code is fairly simple. There’s one function that reads the temperature of the sensor. The reading is then passed to another function that controls the speed of the fan. The line map(….) takes the value from the DS18B20 and converts it into a number between 10 and 100. More info on the map function here

// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>

#define D1 5 // fan assigning the ESP8266 pin to arduino pin
#define ONE_WIRE_BUS 12 // thermometer

int fanPin = 5;
int dutyCycle = 0;

// Setup a oneWire instance to communicate with OneWire device
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup() {
  Serial.begin(115200);

  pinMode(fanPin, OUTPUT); // sets the pins as outputs:

  sensors.begin(); // Start up the library

  analogWriteRange(100); // to have a range 1 - 100 for the fan
  analogWriteFreq(10000);
}

float readSensorTemp() {
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.print("Temperature: ");  
  Serial.println(sensors.getTempCByIndex(0));
  return sensors.getTempCByIndex(0);
}

void controlFanSpeed (int fanSpeedPercent) {
  Serial.print("Fan Speed: ");
  Serial.print(fanSpeedPercent);
  Serial.println("%");  
  analogWrite(fanPin, fanSpeedPercent); // set the fan speed
}

void loop() {
  float sensorTemp = readSensorTemp(); // Request sensor value

  // Map (change) the sensor reading of <=5 to >=60 to a value between 10 and 100
  int fanSpeedPercent = map(sensorTemp, 5, 60, 10, 100);

  controlFanSpeed (fanSpeedPercent); // Update fan speed
  delay(1000);
}

 

Note the IRF520 on the mosfet module board isn’t really the correct component for switching high currents with the low voltage signal (gate) voltage supplied by the ESP but it works for this experiment.

Leave a Reply

Your email address will not be published. Required fields are marked *

scroll to top