I need help turning fan on based on temperature

I’m using the code below that I found on a youtube video to take a temperature reading and display it on the oled screen and it works perfectly. I just need to add some code to it to turn on a relay when ever the temperature gets above a set point and to also turn off the relay when the temperature drops to a certain set point. Can someone please provide the code needed to do this and add it to the code below. Thank you very much.

/*********************************************************************
  This is an example for reading the temperature out of a
  DS18B20 sensor and displaying the result on an SSD1306
  128x64 OLED display over the I2C bus.
  Temperature is displayed in degrees C and F.
  Gadget Reboot
*********************************************************************/

#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_SSD1306.h>

#define ONE_WIRE_BUS 2                // DS18B20 data wire is connected to input 2
#define OLED_RESET 4                  // Adafruit needs this but we don't use for I2C



DeviceAddress thermometerAddress;     // custom array type to hold 64 bit device address

Adafruit_SSD1306 display(OLED_RESET); // create a display instance
OneWire oneWire(ONE_WIRE_BUS);        // create a oneWire instance to communicate with temperature IC
DallasTemperature tempSensor(&oneWire);  // pass the oneWire reference to Dallas Temperature


void setup()   {

  Serial.begin(9600);

  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C address of the display
  display.clearDisplay();                     // clear the display buffer
  display.display();                          // update display

  Serial.println("DS18B20 Temperature IC Test");
  Serial.println("Locating devices...");
  tempSensor.begin();                         // initialize the temp sensor

  if (!tempSensor.getAddress(thermometerAddress, 0))
    Serial.println("Unable to find Device.");
  else {
    Serial.print("Device 0 Address: ");
    printAddress(thermometerAddress);
    Serial.println();
  }

  tempSensor.setResolution(thermometerAddress, 11);      // set the temperature resolution (9-12)
}


void loop() {

  tempSensor.requestTemperatures();                      // request temperature sample from sensor on the one wire bus
  displayTemp(tempSensor.getTempC(thermometerAddress));  // show temperature on OLED display

  delay(500);                                            // update readings every half a second
}

void displayTemp(float temperatureReading) {             // temperature comes in as a float with 2 decimal places

  // set up OLED text size and print the temperature data
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Temp:");

   //show temperature °C
  display.print(temperatureReading, 1);  // rounded to 1 decimal place
  display.print((char)247);              // degree symbol
  display.println("C");
  Serial.print(temperatureReading);      // serial debug output
  Serial.print("°");
  Serial.print("C  ");

  // show temperature °F
  display.print(DallasTemperature::toFahrenheit(temperatureReading), 1); // rounded to 1 decimal place
  display.print((char)247);                                              // degree symbol
  display.println("F");
  Serial.print(DallasTemperature::toFahrenheit(temperatureReading));     // serial debug output
  Serial.print("°");
  Serial.println("F");

  display.display();                    // update the OLED display with all the new text
}


// print device address from the address array
void printAddress(DeviceAddress deviceAddress)
{
  for (uint8_t i = 0; i < 8; i++)
  {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

What kind of fan? How is it powered? What pin do you use to control it? How are you planning to actually control the power to the fan?

Those questions have to be answered log before thinking of "code".

Paul

Well I just need the arduino to power a cheap chinese relay. The relay will be used to power the fan. I would like to use digital pin 4 for the relay.

Link to the cheap relay i will be using.

The fan will just be a small dc motor that will be powered by a battery but Im planning on changing that to a bigger dc motor in the future.

Please edit your post to add code tags, as described in "How to use this forum".

Uther2017:
Well I just need the arduino to power a cheap chinese relay. The relay will be used to power the fan. I would like to use digital pin 4 for the relay.

Link to the cheap relay i will be using.

The fan will just be a small dc motor that will be powered by a battery but Im planning on changing that to a bigger dc motor in the future.

That should work fine.It has a driver transistor already on the board. Do you see on the picture of the back of the board there is a note that a "low" on the control line will turn the relay on?

Do you have the relay board in hand?

Paul

Paul_KD7HB:
That should work fine.It has a driver transistor already on the board. Do you see on the picture of the back of the board there is a note that a "low" on the control line will turn the relay on?

Do you have the relay board in hand?

Paul

Yeah I already have the relay board on hand. The back of the board on mine just says HW-307 and shows the wiring diagram for the relay.