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);

void loop() {
	reading = analogRead(sensorPin);
	int celsius = reading/2;
	lcd.setCursor(0, 0);
	lcd.print("Temperature: ");	
	lcd.print(celsius, DEC);
	if (celsius >35) {
	} else {

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.


Join the conversation!

Error! Please fill all fields.
  • samyeoh3143

    Hey i wanna ask that if i use DHT 11 to change as LM35 the program still can be work?

  • abhinav

    hii..i wanted to know that can i use arduino uno board to make green house controller and run it for continuously 1 there any problem of heating up??

  • Sanna

    I’m trying to get a fan be controlled by a c02 sensor, but having code-troubles as i’m new to it.

    Anyone who can take a look and help me out?

    • Sanna

      Thanks for the input!
      I changed some lines according to your suggestion and now i get the sensor to activate/deactivate the fan and a led light 🙂

      Then i just have a little problem left…
      Maybe you could help me out once again? 😉

      I want the RGB-LED to crossfade between nyances, depending on if
      my value: “coc” is higher or lower than 57.

      The code below works now, sry for swedish comments…a
      But i have no colour code in it, so need some help there

      float coc;
      int senspin = 0; //sensorn kopplas in i analog pin 0
      int ledPin = 13; //Led är kopplad till pin 13, kan också vara fläkt
      int fan1 = 5; //Fan1 är kopplat till pin 5 i arduino

      void setup() {
      // put your setup code here, to run once:

      Serial.begin(9600); //Öppnar serieportkommunikationen med sensorn
      pinMode(ledPin,OUTPUT);//Nu är pin13 en Output


      void loop() {
      // Skriv loopen som vi vill att arduino skall göra med vår Sensor Read:

      coc = analogRead(senspin); // Tar sensorns pin läsning och sätter den = med senspin variabeln
      coc = (5.0*coc*100)/1024.0; //Detta kommer konvertera analog input till digital typ celcius
      Serial.println((byte)coc); // Kommer skriva ut den konverterade värdet

      digitalWrite(ledPin, HIGH); //Aktiverar en lampa eller fläkt




    • P. Marian

      If the rgb led has 4 pins then you should look at it as 3 leds inside one therefore you must connect it to 3 pins on arduino PWM outputs and one to the ground or 5v depending on the LED model, probably to +5V. Then you must provide different voltage level (using analogWrite) to each led obtaining various colors. Better search on google as it is hard for me to test it and post the code now. For example read this article:

    • P. Marian

      and what is happening when you’re running that code? It is better to have an else statement where you set statePin back to LOW otherwise will remain HIGH forever after val >= THRESHOLD occurs even thoug the val will become lower than THRESHOLD. digitalWrite should be outside of the if-else statement and receive the value of statePin.
      See the code below:

      void loop(){
      val = analogRead(sensorPin);
      if(val >= THRESHOLD) {
      statePin != statePin;
      } else {
      statePin = LOW;
      digitalWrite(ledPin, statePin);

  • Robert Fovall

    Worked fine except no back light on LCD.
    Switched LCD #15 to 5V and LCD #16 to ground.
    Now it works fine with back light.

  • Jerry

    Vincent this tutorial application is just what I need, but I lack the electronic skills to do it I will list what I want to do and could you please list the components to use and maybe a schematicI want tocontrol 4 small heaters that will be activated by dew point temperature. It will turn on at 5 degrees below caculated dew point temp and turn off at 5 degrees above dew point temp the further it goes below the dew point temp, the more power will be added. So I need it to calculate dew point, vary the power out put with relation to dewpoint,have a way of setting acontroll point manualy. ALSO is it possible to read in degrees fareheit?? Thanks for you time and I hope you anhelp me, Jerry

  • Maria

    Do you know what changes would have to be made if I were using Arduino Nano and a DC fan (5v)? Thank you very much.

  • Bryant

    What are the values of R1 and R2 in the circuit?

  • Jamie

    i am wondering how to use this with different sensors

  • Ben

    Plz tell me about the connections of circle in the photo:

  • Ben

    Is there anyone figure out the connections of Relay and Fan please I need Help!!!!!