Advertisement
arduino lcd lm35

Temperature Controlled Relay with Arduino – Tutorial #6

temperature controlled fan

In this project, we are going to build something very simple project, a temperature controlled relay that is used to turn on a dc fan. You can actually change the DC Fan to other electrical devices such as light or servo motor. We are going to make an automatic fan that will be ON when the temperature rises above certain threshold temperature and OFF when it is below.

Just follow the steps below and you are ready to get yourself one Temperature-Controlled Automatic Fan!

Things that you need

  • Arduino UNO
  • LM35 Temperature Sensor(Celsius) – datasheet
  • Relay Module ( May refer to my previous post on how to make a relay module)
  • A 9V battery (To supply to the coil of relay and the fan)
  • A DC fan (9V/12V)
  • Some jumper wires
  • 16 x 2 LCD

Okay, now let’s get started.

Step 1

Connect the hardware according to the schematic below:

LCD & LM 35

arduino lcd lm35

Relay Module

arduino relay module

DC Fan

arduino dc fan

Done with the hardware part? Awesome!

Part 2: Software

For the software part, I program it using arduino IDE v1.01.

#include <LiquidCrystal.h>
int reading = 0;
int sensorPin = A0;
int relay =7;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
	// set up the LCD's number of columns and rows: 
	lcd.begin(16, 2);
	pinMode(relay,OUTPUT);
}

void loop() {
	reading = analogRead(sensorPin);
	int celsius = reading/2;
	lcd.setCursor(0, 0);
	lcd.print("Temperature: ");	
	lcd.setCursor(0,1);
	lcd.print(celsius, DEC);
	lcd.print((char)223);
	lcd.print("C");
	if (celsius >35) {
		digitalWrite(7,HIGH);
	} else {
		digitalWrite(7,LOW);
	}
	delay(500);
	lcd.clear();
}

Some brief explanation about the coding:
1. reading = analogRead(sensorPin);
– Read analog pin from LM35 temperature sensor input pin and store at reading.
2. int celsius = reading/2;
– LM35 are design to give the exact value of temperature in Celsius. To get the celsius
value, the ADC value are required to divided by 2 .

So, load this code into your arduino UNO and you are ready to go!

Demo video

Problem Encountered & Solutions

Problem 1

The GND of the 9V battery is not connected to the GND of the arduino. So, this cause the fan unable to move even if the temperature exceed the threshold temperature.

Solution 1

Connect the GND of the battery to the GND of arduino – common GND. If you are using a 5V relay instead of 12V relay, you only have to connect the arduino GND. I am using a 12V relay. So the coil voltage required would be higher.

Problem 2

When done uploading the code into my arduino UNO, the LCD display nothing.

Solution 2

After some troubleshoot work, I realised it is due to the contrast(pin 3 on LCD). You have to adjust the potentiometer connected to pin 3 of LCD until the LCD would display what you want it to display.

Hope this simple guides would lead you to make your own success Temperature-Controlled Automatic Fan too.

If you have any doubt, do leave your comments or contact me.

Hope you enjoy. Happy reading !

Thank you.

52 Comments

Join the conversation!

Error! Please fill all fields.
  • Ed

    wil this work with LCD 20×4 Display
    Have you please sketch for 20×4 Display

  • Thomas

    hi Vincent
    Great script iv changed it a bit to work with the LCD Shield.
    it gave som other problems like the sensor pin and output pin , but all that fixed it works great

    i use output 1 and 3
    and sensor A1

    oh and i added a fan more

    and there is a problem when it hits 31 it starts the fan and then it cools the sensor to 30 then the fan stops but then it get to 31 again…… get the point… i can’t see a workaround for this… so if any one has a idea please……..

    but here is what i changed the script to: (all credit to

    #include
    int reading = 0;
    int sensorPin = A1;
    int relay = 3;
    int relay2 = 1;
    //int celsius;
    // initialize the library with the numbers of the interface pins
    LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

    void setup() {
    // set up the LCD’s number of columns and rows:
    lcd.begin(16, 2);

    pinMode(relay,OUTPUT);
    pinMode(relay2,OUTPUT);
    //digitalWrite(3,LOW);
    //digitalWrite(1,LOW);
    }

    void loop() {
    reading = analogRead(sensorPin);
    int celsius = reading/2;
    lcd.setCursor(0, 0);
    lcd.print(“Temperature:”);
    lcd.setCursor(0,1);
    lcd.print(celsius, DEC);
    lcd.print((char)223);
    lcd.print(“C”);
    if (celsius >40)
    {
    digitalWrite(3,HIGH);
    digitalWrite(1,HIGH);
    lcd.print(” Two fans ON”);
    }
    else if (celsius >30)
    {
    digitalWrite(3,HIGH);
    digitalWrite(1,LOW);
    lcd.print(” Fan ON”);
    }

    else

    {
    digitalWrite(3,LOW);
    digitalWrite(1,LOW);
    lcd.print(” Fan OFF”);
    }

    delay(500);
    lcd.clear();
    }

  • ants

    hey id like help with this to control a still to make booze it needs to stay the same temp and use water to control it as well as the heater think you could help ? email me and il draw up what it needs looks like

  • Alex

    Hi,
    I need to controll the temperature with 22 sensors, and have no idea how to.
    Is there some board where i can connect them all and read one by one, because there’s not enought pins for all XD.
    If you know of commercial things that can do it it’s ok for me to.
    Thanks in advance.

  • Pritam

    Hi I want to know what relay have you used in your circuit. Is it available in the market?What are its specifications?

  • LUCKY

    I found this tutorial for Temperature Controlled Relay with Arduino very nice.
    I made an egg incubator and want to control by a controller.
    In this regard, i need you favor to add two more parameters (humidity & motor control) with your existing Arduino temperature controller.
    I mean, by putting 2 or 3-relays to control temperature, humidity and egg turning motor and which can display on LCD.
    Please make a tutorial on it with its circuit connection and programming etc.

    Thanks in advance

  • ahmad mifdhal

    Hi there..i have done this project through these tutorial..

    The problem is, i would like to delay this program.

    I would like to know how to set a delay interval between 5 second after relay is switched on..

    I hope you can guide me the source code that have to add in the previous coding that you shared..

    thank you..

  • SCHEMATICS NEED TO BE IMPROVED

    GOOD ,
    COULD YOU PLEASE UPLOAD A BETTER SCHEMATICS FOR A BETTER UNDERSTANDING FOR ALL GEEKS . THE IDE STATES THE HIGH STATE OF THE PIN 7 , THE SCHEMATICS YOU HAVE DRAWN HARDLY SHOWS ANY CONNECTION THERE .
    MOREOVER YOUR JP1 STATES VCC FOR PIN 1 , PIN2 TO MCU …HERE PLEASE CLEAR WHERE ITS CONNECTING TO AS OF I UNDERSATND IT SHOULD GO TO PIN 7 OF ARDUINO AT A HIGH STATE TO TURN ON THE RELAY CIRCUIT .
    A COMPLETE SCHEMATICS NEED BE REDONE I PRESUME TO MAKE IT MORE ILLUSTARTIVE .OTHERWISE ALL LOOKS FINE BRO .PEOPLE ARE DOUBTFUL ON THE VALUES OF R1 AND R2 …AS I SAID AN BETTER SCHEMATICS WILL BE SELF EXPLANATORY .NICE WORK

  • Roelsky

    how did you connect the relay to the Arduino?

  • Naveen

    Hi Marian, Nice video ; just wanted to know will the arduino circuit not fry up because of the back emf of the dc fan.
    or will the relay shield take care of that.

Looking for the latest from TI?