{"id":67,"date":"2025-03-02T08:07:40","date_gmt":"2025-03-02T08:07:40","guid":{"rendered":"https:\/\/samerli.com\/en\/lessons\/?p=67"},"modified":"2025-05-05T15:18:54","modified_gmt":"2025-05-05T15:18:54","slug":"lesson-5-using-an-lm35-temperature-sensor-with-arduino-to-blink-an-led-on-high-temperature","status":"publish","type":"post","link":"https:\/\/samerli.com\/en\/lessons\/?p=67","title":{"rendered":"Lesson 5: Using LM35 Temperature Sensor with Arduino"},"content":{"rendered":"\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<div class=\"wp-block-aioseo-table-of-contents\"><ul><li><a href=\"#aioseo-1-needed-components\">1. Needed Components<\/a><\/li><li><a href=\"#aioseo-2-connecting-your-circuit\">2. Connecting Your Circuit<\/a><ul><li><a href=\"#aioseo-set-up-the-breadboard\">Set Up the Breadboard:<\/a><\/li><li><a href=\"#aioseo-schematics-and-pictures\">Schematics and Pictures<\/a><\/li><\/ul><\/li><li><a href=\"#aioseo-3-how-the-lm35-sensor-works\">3. How the LM35 Sensor Works<\/a><ul><li><a href=\"#aioseo-lm35-package-types\">LM35 Package Types:<\/a><\/li><li><a href=\"#aioseo-where-to-get-the-lm35\">Where to Get the LM35:<\/a><\/li><\/ul><\/li><li><a href=\"#aioseo-4-the-code-and-how-it-works\">4. The Code and How It Works<\/a><ul><li><a href=\"#aioseo-how-this-code-works\">How This Code Works:<\/a><\/li><\/ul><\/li><li><a href=\"#aioseo-5-how-to-go-further-in-your-temperature-controlled-led-circuit\">5. How to Go Further in Your Temperature-Controlled LED Circuit<\/a><\/li><\/ul><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-1-needed-components\">1. Needed Components<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Arduino Board<\/strong> (Arduino Uno or another type)<\/li>\n\n\n\n<li><strong>LM35 Temperature Sensor<\/strong><\/li>\n\n\n\n<li><strong>LED<\/strong> (any color)<\/li>\n\n\n\n<li><strong>Resistor<\/strong> (330 ohm)<\/li>\n\n\n\n<li><strong>Breadboard<\/strong><\/li>\n\n\n\n<li><strong>Jumper Wires<\/strong><\/li>\n\n\n\n<li><strong>Power Supply<\/strong> (USB cable or battery pack)<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-2-connecting-your-circuit\">2. Connecting Your Circuit<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-set-up-the-breadboard\">Set Up the Breadboard:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>LM35 Sensor Connections<\/strong>:<\/li>\n\n\n\n<li><\/li>\n\n\n\n<li><strong>LED Connections:<\/strong>\n<ul class=\"wp-block-list\">\n<li>Connect the anode (longer leg) of the LED to <strong>digital pin 13<\/strong> on the Arduino.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Connect the cathode (shorter leg) of the LED to a 330-ohm resistor, and connect the other end of the resistor to the <strong>GND<\/strong> on the Arduino.<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Connect <strong>VCC<\/strong> pin of the LM35 to the <strong>5V<\/strong> pin on the Arduino<\/li>\n\n\n\n<li><strong>GND<\/strong> pin of the LM35 &#8211;&gt; <strong>GND<\/strong> pin on the Arduino.<\/li>\n\n\n\n<li><strong>Vout<\/strong> pin of the LM35 &#8211;&gt; <strong>analog pin A0<\/strong> on the Arduino.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-schematics-and-pictures\">Schematics and Pictures<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>MAKE SURE TO CONNECT THE LM35 SENSOR&#8217;S PINS PROPERLY. <\/strong>Otherwise LM35 will heat and may get damaged as shown below. <\/li>\n\n\n\n<li>Put the sensor&#8217;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)<br><img loading=\"lazy\" decoding=\"async\" width=\"250\" height=\"150\" class=\"wp-image-108\" style=\"width: 250px;\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/Bottom-view-LM35.png\" alt=\"Bottom view of LM35 Sensor\"><\/li>\n\n\n\n<li>Circuit Diagram<br><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"631\" class=\"wp-image-107\" style=\"width: 800px;\" src=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/lm35-CIRCUIT.png\" alt=\"Circuit showing arduino with a breadboard connected with a resistor and LED, as well as LM 35\" srcset=\"https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/lm35-CIRCUIT.png 682w, https:\/\/samerli.com\/en\/lessons\/..\/..\/wp-uploads\/2025\/03\/lm35-CIRCUIT-300x237.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-3-how-the-lm35-sensor-works\">3. How the LM35 Sensor Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The LM35 is a precision<em><strong> analog <\/strong><\/em>temperature sensor that provides an output voltage linearly proportional to the Celsius temperature. Here\u2019s how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The LM35 outputs <strong>10 mV per degree Celsius<\/strong>. For example:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>At 25\u00b0C, the output voltage is <strong>250 mV<\/strong>.<\/li>\n\n\n\n<li>At 30\u00b0C, the output voltage is <strong>300 mV<\/strong>.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The output voltage is read by the Arduino\u2019s analog input pin (A0).<\/li>\n\n\n\n<li>The Arduino converts the analog voltage to a digital value using the <strong>analogRead()<\/strong> function.<\/li>\n\n\n\n<li>The digital value is converted to temperature using the formula:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Temperature (\u00b0C) = (Analog Value \u00d7 5.0) \/ 1024.0 \u00d7 100<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-lm35-package-types\">LM35 Package Types:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>TO-92<\/strong>: The most common package, looks like a small transistor. Easy to use on a breadboard.<\/li>\n\n\n\n<li><strong>TO-220<\/strong>: Larger package, often used for higher power applications.<\/li>\n\n\n\n<li><strong>SOIC<\/strong>: Surface-mount package, used in compact designs.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-where-to-get-the-lm35\">Where to Get the LM35:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can purchase the LM35 from electronics component stores like:<\/li>\n\n\n\n<li><strong>Online retailers<\/strong><\/li>\n\n\n\n<li><strong>Local electronics shops<\/strong>: Check for availability in your area.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-4-the-code-and-how-it-works\">4. The Code and How It Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the Arduino code to blink an LED when the temperature exceeds a certain threshold:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Define pin numbers\nconst int lm35Pin = A0;  \/\/ LM35 connected to analog pin A0\nconst int ledPin = 13;   \/\/ LED connected to digital pin 13\n\n\/\/ Define temperature threshold\nconst float tempThreshold = 30.0; \/\/ Blink LED if temperature &gt; 30\u00b0C\n\nvoid setup() {\n  \/\/ Set pin modes\n  pinMode(lm35Pin, INPUT);\n  pinMode(ledPin, OUTPUT);\n\n  \/\/ Initialize serial communication\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \/\/ Read the analog value from the LM35\n  int analogValue = analogRead(lm35Pin);\n\n  \/\/ Convert the analog value to temperature in Celsius\n  float temperature = (analogValue * 5.0) \/ 1024.0 * 100;\n\n  \/\/ Print temperature to Serial Monitor\n  Serial.print(\"Temperature: \");\n  Serial.print(temperature);\n  Serial.println(\" \u00b0C\");\n\n  \/\/ Control LED based on temperature\n  if (temperature &gt; tempThreshold) {\n    digitalWrite(ledPin, HIGH); \/\/ Turn on LED\n    delay(500);                 \/\/ Blink delay\n    digitalWrite(ledPin, LOW);  \/\/ Turn off LED\n    delay(500);                 \/\/ Blink delay\n  } else {\n    digitalWrite(ledPin, LOW);  \/\/ Turn off LED\n  }\n\n  \/\/ Add a small delay for stability\n  delay(100);\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"aioseo-how-this-code-works\">How This Code Works:<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Variable Declaration<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>lm35Pin<\/code> is set to <code>A0<\/code> for the LM35 sensor.<\/li>\n\n\n\n<li><code>ledPin<\/code> is set to <code>13<\/code> for the LED.<\/li>\n\n\n\n<li><code>tempThreshold<\/code> is set to <code>30.0<\/code> (the temperature threshold in Celsius).<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Setup<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>pinMode()<\/code> function sets the <code>lm35Pin<\/code> as an input and the <code>ledPin<\/code> as an output.<\/li>\n\n\n\n<li>Serial communication is initialized for debugging.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Loop<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>analogRead()<\/code> function reads the analog value from the LM35 sensor.<\/li>\n\n\n\n<li>The analog value is converted to temperature using the formula.<\/li>\n\n\n\n<li>The temperature is printed to the Serial Monitor.<\/li>\n\n\n\n<li>The <strong>if condition<\/strong> checks if the temperature is greater than the threshold (<code>tempThreshold<\/code>):\n<ul class=\"wp-block-list\">\n<li>If <strong>true<\/strong>, the LED blinks by turning on and off with a 500 ms delay.<\/li>\n\n\n\n<li>If <strong>false<\/strong>, the LED remains off.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Delay<\/strong>:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A small delay is added for stability and to avoid rapid readings.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aioseo-5-how-to-go-further-in-your-temperature-controlled-led-circuit\">5. How to Go Further in Your Temperature-Controlled LED Circuit<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Adjust Temperature Threshold<\/strong>: Change the <code>tempThreshold<\/code> value in the code to trigger the LED at a different temperature.<\/li>\n\n\n\n<li><strong>Add a Buzzer<\/strong>: Include a buzzer to sound an alarm when the temperature exceeds the threshold.<\/li>\n\n\n\n<li><strong>Multiple LEDs<\/strong>: Use multiple LEDs to indicate different temperature ranges (e.g., Green for normal, Yellow for warning, Red for high).<\/li>\n\n\n\n<li><strong>LCD Display<\/strong>: Add an LCD or OLED display to show the temperature in real-time.<\/li>\n\n\n\n<li><strong>Cooling Fan<\/strong>: Integrate a small DC motor or fan to activate when the temperature is too high.<\/li>\n\n\n\n<li><strong>Data Logging<\/strong>: Use an SD card module to log temperature data over time.<\/li>\n\n\n\n<li><strong>Wireless Communication<\/strong>: Add a Bluetooth or Wi-Fi module to send temperature data to a smartphone or computer.<\/li>\n\n\n\n<li><strong>Advanced Logic<\/strong>: Implement hysteresis to prevent rapid toggling of the LED near the threshold.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">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!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[21,19,20],"class_list":["post-67","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","tag-analog-sensors","tag-lm35","tag-temperature-sensor"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/67","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=67"}],"version-history":[{"count":10,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions"}],"predecessor-version":[{"id":182,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/posts\/67\/revisions\/182"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=\/wp\/v2\/media\/107"}],"wp:attachment":[{"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=67"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=67"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/samerli.com\/en\/lessons\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=67"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}