Arduino Temperature Sensor Project Using LM35

Good morning, friends!
In this session, we will learn and discuss an Arduino project focused on building a temperature sensor. Before starting your Arduino learning journey, it is recommended that you first prepare the required components to support your learning activities in this series.

If you are ready to learn, let’s begin this exciting discussion. Hopefully, this tutorial will help you learn effectively and boost your motivation to explore Arduino projects.

Introduction to the LM35 Sensor

The LM35 is a temperature sensor capable of detecting ambient temperature in its surrounding environment.

In this Arduino project, we will measure the ambient temperature using the LM35 sensor.

Required Components

Prepare the following components:

  • 1× Arduino board
  • 1× Breadboard
  • 1× LM35 temperature sensor
  • 2× Green LEDs
  • 2× Yellow LEDs
  • 2× Red LEDs
  • 6× 220-ohm resistors
  • 11× Jumper wires

Connect the components as shown in the diagram below.

 

Wiring Notes:

  • Connect the GND and 5V pins from the Arduino to the breadboard.
  • Connect the left pin of the LM35 to 5V, the right pin to GND, and the middle pin to A0 on the Arduino.
  • Connect:

    Green LEDs to digital pins 2 and 3
  • Yellow LEDs to digital pins 4 and 5
  • Red LEDs to digital pins 6 and 7
  • Connect each LED’s negative (cathode) pin to GND through a 220-ohm resistor.

LM35 Temperature Sensor Code

To read temperature data from the LM35 sensor, you can use the following Arduino sketch:

byte lm35 = A0;
int value;

void setup() {
  Serial.begin(9600);
}

void loop() {
  value = analogRead(lm35);
  value = value * 0.488;
  Serial.println(value);
  delay(500);
}

Code Explanation

  • Serial.begin(9600);
        Enables serial communication between Arduino and the computer.
  • analogRead(lm35);
        Reads the analog value from the LM35 sensor.
  • value = value * 0.488;
        Converts the sensor value to degrees Celsius.
  • Serial.println(value);
        Displays the temperature value in the Serial Monitor.

Creating a Temperature Indicator Using LEDs

First, observe the temperature values displayed in the Serial Monitor.
You can open the Serial Monitor by pressing CTRL + SHIFT + M.

sourch image: https://kelasrobot.com

Note:
A value such as 27 indicates the temperature in degrees Celsius. Try touching the LM35 sensor—its value will increase because it detects heat from your body.

Adding LED Logic

To control LEDs based on temperature, add conditional logic (if statements) as shown below:

if (temperature == 28) {
  // command code
}

Complete Arduino Sketch

int LM35 = A0;           // LM35 connected to analog pin A0
int valueLM35 = 0;       // Variable to store LM35 readings
int LED1 = 2;            // LED connected to pin 2
int LED2 = 3;            // LED connected to pin 3
int LED3 = 4;            // LED connected to pin 4

void setup() {
  Serial.begin(9600);        // Serial communication
  pinMode(LED1, OUTPUT);    // Set LED pins as output
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
}

void loop() {
  valueLM35 = analogRead(LM35);
  valueLM35 = valueLM35 * 0.488;   // Convert to Celsius
  Serial.println(valueLM35);
  delay(500);

  if (valueLM35 == 28) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
  }
  else if (valueLM35 == 29) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
  }
  else if (valueLM35 == 30) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
  }
  else {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
  }
}

Additional Notes

  • The temperature values used in the code should be adjusted based on your local environment, as ambient temperatures may vary.

  • if (valueLM35 == 28)
    Executes the code block when the temperature equals 28°C.

  • else if
    Executes when the previous condition is not met.

  • else
    Executes when none of the conditions are true.

Creative Challenge

There are still three unused LEDs in the circuit.

Try creating a program with the following logic:

  • If LM35 == 28, all green LEDs turn ON

  • If LM35 == 29, all yellow LEDs turn ON

  • If LM35 == 30, all red LEDs turn ON

Posting Komentar untuk "Arduino Temperature Sensor Project Using LM35"