Greenhouse Fan control with DS18B20 sensor - code help

Hi all,

This is my first project, please be gentle.

Aim: To control a greenhouse fan to be ON at ambient temperature (Ta) >= 25degC, and OFF at Ta <= 24.9 degC.

Materials: Arduino Uno, DS18B20 sensor, resistor, Mofset, 12V/380mA DC fan.

So far, I have had success poaching and modifying other code. In short, it works. BUT I think the code is messy and I am missing something in the mapping of the sensor value to control the fan speed.

Specifically, my serial fanspeed reports are >100%, and the full mA for the fan is not reached.

I can make the fan turn ON at sensor temp >25degC, and off below that, but I think my 'span' for the fan speed is wrong...

Any help would be appreciated.

Sincere thanks

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

#define D1 3 // fan assigning the to arduino pin
#define ONE_WIRE_BUS 7 // thermometer

int fanPin = 3;

// 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() {

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

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


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

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

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

  // Map (change) the sensor reading of <=24 to >=25 to a value between 0 and 255
  int fanSpeedPercent = map(sensorTemp, 24, 25, 0, 255);

  controlFanSpeed (fanSpeedPercent); // Update fan speed

first step:

read how to use this forum

come back, on the bottom right of your first post is the more/modify button

fix the code by adding "code tags" as explained in step 7 of the link

Have I done this correctly now?

This does not do what you want. The map() function is integer only.

int fanSpeedPercent = map(sensorTemp, 24, 25, 0, 255);

For several reasons the above approach won't work at all. Get your project working as a simple on/off controller before attempting finer (such as PID) control.


Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

What are the specs on the fan you are driving?

Thanks.. Tom.. :slight_smile:

I am sure your fan speed is not linear to the range 0-255. For example it might not even spin at all until you get to 200. You could find that starting number and try using that instead of 0. Then turn it off if less than 24.

Without knowing the actual response curve of your fan, I would probably manually find about 5 good speeds (if you can) from just moving, to full speed and write down those numbers. Use nested if statements, or a simple switch/case to set the fan speed with PWM based on temp range or build an index into a small array. If your temp (T) is >=24 and <25 you can build an index using something like speed = ((T*5)-120) to get a number from 0-4. So 24deg would be index 0 (just moving), 24.2 would go to index 1 (moving a little faster), and eventually 24.8 would return an index of 4 (full speed).

Good luck...

Thanks! I'll give that a go.

Would prefer the on/off too, but still finding code for that.


PLEASE read POST #4.

Tom.... :slight_smile: