This Temperature Monitor with Serial Monitor Using Arduino project aims to monitor temperature readings from a sensor (e.g., an LM35 or similar) and display them on the Serial Monitor using Arduino. You’ll learn how to work with global and local variables, manage float data types for temperature values, and utilize analog input to capture the sensor readings.
Fundamental Programming Concepts
- Global Variables: Used to store configuration settings, such as sensor pin assignments.
- Local Variables: Used to store sensor readings within functions.
- Float Data Type: Used for storing temperature values, which often involve decimals.
- Analog Input: Reads data from the temperature sensor through the Arduino’s analog pins.
- Serial Communication: Outputs the temperature readings to the Serial Monitor for display.
Requirement Components
To complete this project, you’ll need the following:
- Arduino Uno Board
- Temperature Sensor (LM35 or similar)
- Breadboard
- Jumper Wires
- USB Cable (to connect Arduino to your computer)
Circuit Diagram
Circuit Connection
Component | Arduino Pin |
Temperature Sensor (Vcc) | 5V |
Temperature Sensor (GND) | GND |
Temperature Sensor (Output) | A0 |
How to Connect the Circuit
- Insert the temperature sensor into the breadboard.
- Connect the Vcc pin of the sensor to the 5V pin on the Arduino.
- Connect the GND pin of the sensor to the GND pin on the Arduino.
- Connect the Output pin of the sensor to the A0 analog input pin on the Arduino.
Explanation of Circuit
- The temperature sensor (e.g., LM35) generates an analog voltage based on the current temperature.
- The Arduino reads this analog signal using the analogRead() function and converts it into a temperature value.
- The Serial Monitor is used to display the temperature readings in Celsius.
Programming Section
Arduino Syntax
Topic Name | Syntax | Explanation |
Analog Read | analogRead(pin) | Reads the analog input value (0-1023) from the specified pin. |
Serial Begin | Serial.begin(baudrate) | Initializes serial communication between Arduino and Serial Monitor. |
Serial Print | Serial.print(data) | Prints data to the Serial Monitor. |
Float Data Type | float variableName | Declares a variable to store decimal (floating-point) values. |
Arduino Code:
Here is the code to monitor temperature and display it on the Serial Monitor:
// Global variable for the sensor pin
const int sensorPin = A0;
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
}
void loop() {
int sensorValue = analogRead(sensorPin); // Read the analog value from the sensor
float voltage = sensorValue * (5.0 / 1023.0); // Convert the analog reading to voltage
float temperatureC = voltage * 100; // Convert voltage to Celsius (LM35 provides 10mV per degree Celsius)
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C"); // Print temperature with Celsius unit
delay(1000); // Wait 1 second before taking another reading
}
Code Explanation
- Global Variable (sensorPin): Declares the pin connected to the temperature sensor.
- analogRead(): Reads the analog signal from the sensor, giving a value between 0 and 1023.
- Voltage Conversion: The sensor value is converted to voltage using the formula sensorValue * (5.0 / 1023.0).
- Temperature Conversion: The voltage is converted to temperature in Celsius based on the LM35 sensor’s 10mV per degree characteristic.
- Serial.print(): Sends the temperature reading to the Serial Monitor for display.
Steps to Upload Code:
- Connect the Arduino to your computer using a USB cable.
- Open the Arduino IDE and select the correct Board and Port.
- Copy and paste the provided code into a new sketch.
- Click the Upload button to transfer the code to the Arduino.
- Open the Serial Monitor from the Tools menu in the Arduino IDE to view temperature readings.
Check Output
Once the code is uploaded and the Serial Monitor is open, you should see the temperature readings printed in Celsius every second. If no values are displayed, double-check the sensor connections and the Serial Monitor settings.
Troubleshooting Tips
- No output in Serial Monitor? Ensure that the baud rate in the Serial Monitor matches the baud rate defined in the code (Serial.begin(9600);).
- Inaccurate temperature readings? Check the sensor connections or try recalibrating the sensor by adjusting the voltage-to-temperature conversion formula.
- Serial Monitor not opening? Ensure you have selected the correct COM port and that your Arduino board is properly connected.
Further Exploration
- Display Fahrenheit: Modify the code to display temperature in Fahrenheit. Use the formula:
temperatureF = (temperatureC * 9.0 / 5.0) + 32; - Add an Alert System: Set a temperature threshold and add an LED to indicate when the temperature exceeds the threshold. For example, if the temperature goes above 30°C, turn on an LED.
- Data Logging: Save the temperature readings over time by sending the data to a file on your computer using the Serial Monitor.
Note
This project demonstrates how to use analog inputs, global and local variables, and serial communication to monitor real-world data like temperature. Understanding these core concepts will help you in more advanced projects involving sensors and displays.
FAQ
Q1: How does the LM35 temperature sensor work?
The LM35 sensor provides a linear voltage output directly proportional to temperature, where 10mV equals 1°C.
Q2: Why do we use a float data type for temperature?
Since temperature values can include decimals (e.g., 24.5°C), the float data type is used to store the temperature as a precise value.
Q3: How can I display temperature in Fahrenheit?
You can convert Celsius to Fahrenheit using the formula:
temperatureF = (temperatureC * 9.0 / 5.0) + 32;
Q4: What is the purpose of delay() in the code?
The delay(1000); function pauses the program for 1 second between readings, preventing the Serial Monitor from being flooded with data.