Circuit showing arduino with a breadboard connected with a resistor and LED, as well as LM 35

Lesson 5: Using LM35 Temperature Sensor with Arduino


Using LM35 with Arduino allows you to measure the temperature through an analog sensor, and use it to react accordingly. This lesson covers some of the basics you will need while using LM35 analog temperature sensor.


1. Needed Components

  • Arduino Board (Arduino Uno or another type)
  • LM35 Temperature Sensor
  • LED (any color)
  • Resistor (330 ohm)
  • Breadboard
  • Jumper Wires
  • Power Supply (USB cable or battery pack)

2. Connecting Your Circuit

Set Up the Breadboard:

  1. LM35 Sensor Connections:
  2. LED Connections:
    • Connect the anode (longer leg) of the LED to digital pin 13 on the Arduino.
  3. Connect the cathode (shorter leg) of the LED to a 330-ohm resistor, and connect the other end of the resistor to the GND on the Arduino.
  • Connect VCC pin of the LM35 to the 5V pin on the Arduino
  • GND pin of the LM35 –> GND pin on the Arduino.
  • Vout pin of the LM35 –> analog pin A0 on the Arduino.

Schematics and Pictures

  • MAKE SURE TO CONNECT THE LM35 SENSOR’S PINS PROPERLY. Otherwise LM35 will heat and may get damaged as shown below.
  • Put the sensor’s legs towards your face and its flat part towards the roof, then follow the below diagran to identify the pins (for TO-92 package)
    Bottom view of LM35 Sensor
  • Circuit Diagram
    Circuit showing arduino with a breadboard connected with a resistor and LED, as well as LM 35

3. How the LM35 Sensor Works

The LM35 is a precision analog temperature sensor that provides an output voltage linearly proportional to the Celsius temperature. Here’s how it works:

  1. The LM35 outputs 10 mV per degree Celsius. For example:
  • At 25°C, the output voltage is 250 mV.
  • At 30°C, the output voltage is 300 mV.
  1. The output voltage is read by the Arduino’s analog input pin (A0).
  2. The Arduino converts the analog voltage to a digital value using the analogRead() function.
  3. The digital value is converted to temperature using the formula:
   Temperature (°C) = (Analog Value × 5.0) / 1024.0 × 100

LM35 Package Types:

  • TO-92: The most common package, looks like a small transistor. Easy to use on a breadboard.
  • TO-220: Larger package, often used for higher power applications.
  • SOIC: Surface-mount package, used in compact designs.

Where to Get the LM35:

  • You can purchase the LM35 from electronics component stores like:
  • Online retailers
  • Local electronics shops: Check for availability in your area.

4. The Code and How It Works

Here’s the Arduino code to blink an LED when the temperature exceeds a certain threshold:

// Define pin numbers
const int lm35Pin = A0;  // LM35 connected to analog pin A0
const int ledPin = 13;   // LED connected to digital pin 13

// Define temperature threshold
const float tempThreshold = 30.0; // Blink LED if temperature > 30°C

void setup() {
  // Set pin modes
  pinMode(lm35Pin, INPUT);
  pinMode(ledPin, OUTPUT);

  // Initialize serial communication
  Serial.begin(9600);
}

void loop() {
  // Read the analog value from the LM35
  int analogValue = analogRead(lm35Pin);

  // Convert the analog value to temperature in Celsius
  float temperature = (analogValue * 5.0) / 1024.0 * 100;

  // Print temperature to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  // Control LED based on temperature
  if (temperature > tempThreshold) {
    digitalWrite(ledPin, HIGH); // Turn on LED
    delay(500);                 // Blink delay
    digitalWrite(ledPin, LOW);  // Turn off LED
    delay(500);                 // Blink delay
  } else {
    digitalWrite(ledPin, LOW);  // Turn off LED
  }

  // Add a small delay for stability
  delay(100);
}

How This Code Works:

  1. Variable Declaration:
  • lm35Pin is set to A0 for the LM35 sensor.
  • ledPin is set to 13 for the LED.
  • tempThreshold is set to 30.0 (the temperature threshold in Celsius).
  1. Setup:
  • The pinMode() function sets the lm35Pin as an input and the ledPin as an output.
  • Serial communication is initialized for debugging.
  1. Loop:
  • The analogRead() function reads the analog value from the LM35 sensor.
  • The analog value is converted to temperature using the formula.
  • The temperature is printed to the Serial Monitor.
  • The if condition checks if the temperature is greater than the threshold (tempThreshold):
    • If true, the LED blinks by turning on and off with a 500 ms delay.
    • If false, the LED remains off.
  1. Delay:
  • A small delay is added for stability and to avoid rapid readings.

5. How to Go Further in Your Temperature-Controlled LED Circuit

  1. Adjust Temperature Threshold: Change the tempThreshold value in the code to trigger the LED at a different temperature.
  2. Add a Buzzer: Include a buzzer to sound an alarm when the temperature exceeds the threshold.
  3. Multiple LEDs: Use multiple LEDs to indicate different temperature ranges (e.g., Green for normal, Yellow for warning, Red for high).
  4. LCD Display: Add an LCD or OLED display to show the temperature in real-time.
  5. Cooling Fan: Integrate a small DC motor or fan to activate when the temperature is too high.
  6. Data Logging: Use an SD card module to log temperature data over time.
  7. Wireless Communication: Add a Bluetooth or Wi-Fi module to send temperature data to a smartphone or computer.
  8. Advanced Logic: Implement hysteresis to prevent rapid toggling of the LED near the threshold.

This project demonstrates how to use the LM35 temperature sensor to create a temperature-controlled LED system. Experiment with the code and hardware to explore more advanced applications!


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *