Arduino UNO R4 - LM35 Temperature Sensor
This tutorial instructs you on how to use Arduino Uno R4 with LM35 temperature sensor. In detail, we will learn:
- How to connect the Arduino Uno R4 to the LM35 temperature sensor.
- How to program the Arduino Uno R4 to read the temperature from the LM35 sensor.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .
Additionally, some of these links are for products from our own brand, DIYables .
Overview of LM35 Temperature Sensor
Pinout
The LM35 temperature sensor has three pins.
- GND pin: connect to GND (0V)
- VCC pin: connect to VCC (5V)
- OUT pin: this is the signal pin. It outputs a voltage related to the temperature. Connect it to an analog pin on the Arduino UNO R4.
How It Works
The LM35 gives a voltage that increases with temperature. Each degree Centigrade makes the output go up by 10 millivolts (mV). To find the temperature, divide the voltage by 10.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
See The best way to supply power to the Arduino Uno R4 and other components.
How To Program For LM35 Temperature Sensor
- Use the analogRead() function to get the ADC value from the temperature sensor.
int adcVal = analogRead(PIN_LM35);
- Change the ADC value into voltage measured in millivolts.
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
- Change the voltage into the Celsius temperature.
float tempC = milliVolt / 10;
- Change Celsius to Fahrenheit if needed.
float tempF = tempC * 9 / 5 + 32;
Arduino UNO R4 Code
/*
* This Arduino UNO R4 code was developed by newbiely.com
*
* This Arduino UNO R4 code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-lm35-temperature-sensor
*/
#define ADC_VREF_mV 5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35 A0
void setup() {
Serial.begin(9600);
}
void loop() {
// get the ADC value from the temperature sensor
int adcVal = analogRead(PIN_LM35);
// convert the ADC value to voltage in millivolt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
// convert the voltage to the temperature in Celsius
float tempC = milliVolt / 10;
// convert the Celsius to Fahrenheit
float tempF = tempC * 9 / 5 + 32;
// print the temperature in the Serial Monitor:
Serial.print("Temperature: ");
Serial.print(tempC); // print the temperature in Celsius
Serial.print("°C");
Serial.print(" = "); // separator between Celsius and Fahrenheit
Serial.print(tempF); // print the temperature in Fahrenheit
Serial.println("°F");
delay(1000);
}
Detailed Instructions
Follow these instructions step by step:
- If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
- Connect Arduino Uno R4 to the LM35 sensor according to the provided diagram.
- Connect the Arduino Uno R4 board to your computer using a USB cable.
- Launch the Arduino IDE on your computer.
- Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
- Copy the code above and open it in Arduino IDE.
- Click the Upload button in Arduino IDE to transfer the code to Arduino UNO R4.
- Hold the sensor in your hand.
- Check the results on the Serial Monitor.
COM6
Temperature: 26.31°C = 79.36°F
Temperature: 26.44°C = 79.59°F
Temperature: 26.50°C = 79.70°F
Temperature: 26.56°C = 79.81°F
Temperature: 27.06°C = 80.71°F
Temperature: 27.75°C = 81.95°F
Temperature: 28.37°C = 83.07°F
Temperature: 29.00°C = 84.20°F
Temperature: 29.56°C = 85.21°F
Temperature: 30.00°C = 86.00°F
Temperature: 30.31°C = 86.56°F
Temperature: 30.62°C = 87.12°F
Temperature: 30.87°C = 87.57°F
Autoscroll
Clear output
9600 baud
Newline