Arduino LM35 Temperature Sensor for Beginners: Control Fan with Relay

Arduino LM35 Temperature Sensor for Beginners: Control Fan with Relay

Objective: Use LM35 Temperature Sensor with Arduino

This guide will help you use the LM35 temperature sensor with Arduino to control a fan through a relay module. It is designed for beginners, providing step-by-step instructions, code explanations, and troubleshooting tips.

Project Goals for LM35 Temperature Sensor with Arduino

  • Understand how to interface the LM35 temperature sensor with Arduino.
  • Use Arduino to read temperature data from the LM35 sensor.
  • Control a fan using a relay module based on the temperature reading.

Required Components for LM35 Temperature Sensor Project

Here’s a list of components needed to build this project:

ComponentDescriptionLink
Arduino UnoMain microcontrollerAmazon Link
LM35 Temperature SensorMeasures temperature in °CAmazon Link
Relay ModuleControls the fan based on temperatureCheck Availability
Fan (or any AC/DC load)Operates based on relay controlCheck Availability
Jumper WiresConnects components to ArduinoAmazon Link
BreadboardFor prototyping connectionsAmazon Link

Understanding the LM35 Temperature Sensor

The LM35 temperature sensor is an analog sensor that provides a voltage output proportional to the temperature in °C. It outputs 10 mV per °C, making it easy to interface with Arduino for temperature measurement.

LM35 Pinout Overview

PinDescription
VCCConnects to 5V on Arduino
OutputConnects to an analog input pin (A0)
GNDConnects to GND on Arduino

Circuit Connection for LM35 and Relay Module

Connect the LM35 and relay module to the Arduino as described below:

ComponentArduino PinDetails
LM35 VCC5VPower the LM35 sensor
LM35 OutputA0Connect to analog input A0
LM35 GNDGNDGround connection
Relay VCC5VPower the relay module
Relay INPin 2Controls the relay (fan)
Relay GNDGNDGround connection

How the Circuit Works

The Arduino reads the temperature from the LM35 sensor. If the temperature exceeds a set threshold (e.g., 40°C), it activates the relay, turning the fan on. If the temperature drops below the threshold, the relay is deactivated, turning the fan off.

Arduino Code for Controlling Fan with LM35

This code reads temperature from the LM35 sensor and controls the fan through a relay. Copy and upload the code to your Arduino.


const int lm35_pin = A0;   // LM35 output pin
const int relay_pin = 2;   // Relay control pin

void setup() {
  Serial.begin(9600);
  pinMode(relay_pin, OUTPUT);  // Set relay pin as output
  digitalWrite(relay_pin, LOW); // Turn off relay (fan)
}

void loop() {
  int temp_adc_val;
  float temp_val;

  temp_adc_val = analogRead(lm35_pin);  // Read temperature
  temp_val = (temp_adc_val * 4.88); // Convert ADC value to voltage
  temp_val = (temp_val / 10);       // LM35 outputs 10mV/°C

  Serial.print("Temperature = ");
  Serial.print(temp_val);
  Serial.println(" °C");

  if (temp_val < 40) {
    Serial.println("Fan OFF");
    digitalWrite(relay_pin, LOW);   // Turn off relay (fan)
  } else {
    Serial.println("Fan ON");
    digitalWrite(relay_pin, HIGH);  // Turn on relay (fan)
  }
  delay(1000); // 1-second delay for stability
}

Steps to Upload Arduino Code

  • Connect your Arduino to the computer using a USB cable.
  • Open the Arduino IDE, paste the code into a new sketch.
  • Select the correct board (e.g., Arduino Uno) and port from the "Tools" menu.
  • Click the "Upload" button to transfer the code to the Arduino.

Check Output for LM35 Temperature Sensor

  • Open the Serial Monitor (Ctrl + Shift + M) after uploading the code.
  • Observe the temperature readings displayed in the Serial Monitor.
  • Check if the relay turns on when the temperature exceeds 40°C and turns off when it drops below 40°C.

Troubleshooting Tips for LM35 Temperature Sensor

  • No Temperature Readings: Check the wiring and ensure the LM35 is connected correctly.
  • Incorrect Temperature Values: Verify that the LM35 is receiving 5V power and is properly grounded.
  • Relay Not Switching: Check the relay wiring and ensure the control pin is connected to the right Arduino pin.

Suggestions for Beginners

Start by testing the LM35 sensor alone to see if it provides accurate temperature readings on the Serial Monitor. Once you’re confident with the readings, add the relay control to complete the project.

Recommended Book for Learning Arduino

Arduino Programming for Absolute Beginners - This book offers easy-to-understand instructions and projects, making it ideal for beginners who want to learn Arduino programming.

For more Arduino tutorials, visit MechatronicsLab.net, where you’ll find resources on Arduino, ESP8266, ESP32, and Raspberry Pi.

No comments

Theme images by Dizzo. Powered by Blogger.