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:
Component | Description | Link |
---|---|---|
Arduino Uno | Main microcontroller | Amazon Link |
LM35 Temperature Sensor | Measures temperature in °C | Amazon Link |
Relay Module | Controls the fan based on temperature | Check Availability |
Fan (or any AC/DC load) | Operates based on relay control | Check Availability |
Jumper Wires | Connects components to Arduino | Amazon Link |
Breadboard | For prototyping connections | Amazon 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
Pin | Description |
---|---|
VCC | Connects to 5V on Arduino |
Output | Connects to an analog input pin (A0) |
GND | Connects to GND on Arduino |
Circuit Connection for LM35 and Relay Module
Connect the LM35 and relay module to the Arduino as described below:
Component | Arduino Pin | Details |
---|---|---|
LM35 VCC | 5V | Power the LM35 sensor |
LM35 Output | A0 | Connect to analog input A0 |
LM35 GND | GND | Ground connection |
Relay VCC | 5V | Power the relay module |
Relay IN | Pin 2 | Controls the relay (fan) |
Relay GND | GND | Ground 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