Needed Components with eBay Links
Understanding the LM35 Sensor
The LM35 is a linear temperature sensor, meaning its output voltage is directly proportional to the temperature in degrees Celsius. The output increases by 10 mV for every degree Celsius rise in temperature. For example, at 25°C, the output voltage is 250 mV.
Key Functions
analogRead(pin)
: Reads the value from an analog pin (in this case, from the LM35).Serial.print()
: Sends data to the serial monitor.map()
: Maps an input value from one range to another.
Wiring the Circuit
Connect the LM35 sensor to your Arduino as follows:
- LM35 Pin 1 (VCC) to 5V on the Arduino
- LM35 Pin 2 (Output) to A0 (Analog pin 0) on the Arduino
- LM35 Pin 3 (GND) to GND on the Arduino
Arduino Code
Here’s the code to read the temperature:
// Digital Thermometer with LM35
int sensorPin = A0; // Analog pin connected to LM35
float temperature; // Variable to store the temperature value
void setup() {
Serial.begin(9600); // Initialize serial communication
Serial.println("Digital Thermometer with LM35");
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read the analog input
temperature = (sensorValue * 5.0 * 100.0) / 1024.0; // Convert the value to temperature in Celsius
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000); // Wait for 1 second before reading again
}
Upload & Test
- Connect your Arduino to your computer using a USB cable.
- Open the Arduino IDE and copy the code above into the editor.
- Select your board and the correct port in the IDE.
- Upload the code by clicking the upload button.
- Open the Serial Monitor (Tools > Serial Monitor) and set the baud rate to 9600. You will see the temperature readings in degrees Celsius displayed every second.
Code Breakdown
The LM35 outputs a voltage proportional to the temperature. The Arduino reads this voltage using analogRead()
. The conversion formula translates the analog reading into a temperature value by multiplying the sensor value by 5 (for the 5V reference), 100 (due to the 10 mV/°C conversion factor), and dividing by 1024 (since the Arduino's ADC provides a 10-bit reading).
Conclusion
This simple digital thermometer demonstrates how to read and display analog data from a sensor. By expanding on this, you can build more complex temperature monitoring projects, such as a thermostat or data logger.