Temperature Monitor with Serial Monitor Using Arduino

Temperature Monitor with Serial Monitor Using Arduino

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

  1. Insert the temperature sensor into the breadboard.
  2. Connect the Vcc pin of the sensor to the 5V pin on the Arduino.
  3. Connect the GND pin of the sensor to the GND pin on the Arduino.
  4. 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:

  1. Connect the Arduino to your computer using a USB cable.
  2. Open the Arduino IDE and select the correct Board and Port.
  3. Copy and paste the provided code into a new sketch.
  4. Click the Upload button to transfer the code to the Arduino.
  5. 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.

LED Brightness Controller Using Arduino

LED Brightness Controller Using Arduino

The objective of this LED Brightness Controller Using Arduino project is to control the brightness of an LED using a potentiometer and an Arduino. By adjusting the potentiometer, you will vary the voltage being read by the Arduino, which in turn changes the brightness of the LED using PWM (Pulse Width Modulation). Through this project, you will learn how to:

  • Use variables to store sensor readings.
  • Implement analogRead() to read input values from the potentiometer.
  • Use analogWrite() to output PWM signals to control the LED brightness.

Fundamental Programming Concepts

  1. Variables: Store data such as the potentiometer’s analog readings and the LED’s brightness level.
  2. PWM (Pulse Width Modulation): PWM is a technique where the digital pin alternates rapidly between HIGH and LOW to create the illusion of analog output. This is key to adjusting the LED’s brightness.
  3. Data Types: Variables such as int (integer) are used to hold numerical values like sensor readings and brightness levels.
  4. Analog Input (analogRead): Reads analog values (0 to 1023) from the potentiometer, representing voltages between 0V and 5V.
  5. Analog Output (analogWrite): Outputs a PWM signal between 0 and 255 to control the brightness of the LED.

Requirement Components

To complete this project, you will need:

  • Arduino Uno Board: The microcontroller that runs the code and controls the components.
  • LED: A standard light-emitting diode to demonstrate brightness control.
  • Potentiometer (10kΩ): A variable resistor that provides adjustable input.
  • Resistor (220Ω): Used to limit current to the LED to prevent damage.
  • Breadboard and Jumper Wires: For building the circuit and making connections.
  • USB Cable: To connect the Arduino to your computer for uploading the code.

Circuit Diagram

 

Circuit Connection

The following table shows how to wire the components:

Component Arduino Pin
Potentiometer (Vcc) 5V
Potentiometer (GND) GND
Potentiometer (Wiper) A0
LED (Anode) Pin 9
LED (Cathode) GND
Resistor (220Ω) Between LED and GND

How to Connect the Circuit

  1. Insert the potentiometer into the breadboard.
  2. Connect the potentiometer’s Vcc pin to the 5V pin on the Arduino to provide power.
  3. Connect the potentiometer’s GND pin to GND on the Arduino for grounding.
  4. Connect the middle pin (wiper) of the potentiometer to the A0 analog input pin on the Arduino.
  5. Connect the LED’s anode (longer leg) to Pin 9 on the Arduino.
  6. Place the 220Ω resistor between the LED’s cathode (shorter leg) and GND to limit the current and protect the LED.

Explanation of Circuit:

  • The potentiometer acts as a variable resistor. By turning the knob, you change the resistance and thus the voltage being sent to the Arduino.
  • The Arduino reads the voltage from the potentiometer using the analogRead() function, and the value is mapped to a PWM range to control the brightness of the LED.
  • The LED is connected to a PWM-capable pin (Pin 9) on the Arduino, which allows the brightness to be adjusted based on the potentiometer’s input.

Programming Section

Arduino Syntax:

Topic Name Syntax Explanation
Analog Read analogRead(pin) Reads the voltage (0-5V) from an analog pin and converts it into a value between 0 and 1023.
Analog Write analogWrite(pin, value) Outputs a PWM signal to a pin (0-255), controlling the brightness or speed of a connected device.
Map Function map(value, fromLow, fromHigh, toLow, toHigh) Converts one range of values into another, such as converting the potentiometer value (0-1023) to a PWM range (0-255).

Arduino Code:

The following code demonstrates how to use the potentiometer to control the brightness of the LED:

int potPin = A0;      // Pin connected to the potentiometer
int ledPin = 9;       // Pin connected to the LED
int potValue = 0;     // Variable to store the potentiometer reading
int brightness = 0;   // Variable to store the LED brightness level
void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}
void loop() {
  potValue = analogRead(potPin);               // Read the potentiometer value (0-1023)
  brightness = map(potValue, 0, 1023, 0, 255); // Map the potentiometer value to a PWM range (0-255)
  analogWrite(ledPin, brightness);             // Write the PWM value to the LED
  delay(10);                                   // Short delay to stabilize the signal
}

Code Explanation:

  • The potValue variable stores the potentiometer’s reading (0-1023) using the analogRead() function.
  • The map() function converts the potentiometer value into the appropriate PWM range (0-255), which is then stored in the brightness variable.
  • The analogWrite() function sends the brightness value to the LED on Pin 9, adjusting its brightness according to the potentiometer’s position.

Steps to Upload Code:

  1. Connect your Arduino to your computer via the USB cable.
  2. Open the Arduino IDE and select the correct Board and Port from the Tools menu.
  3. Copy and paste the provided code into a new sketch.
  4. Click the Upload button to transfer the code to your Arduino board.
  5. Monitor the Output: Once the code is uploaded, you can turn the potentiometer to change the LED’s brightness.

Check Output:

After uploading the code, rotate the potentiometer’s knob. You should see the LED’s brightness increase or decrease based on the position of the potentiometer. If the LED remains constant or doesn’t light up at all, check the wiring and connections carefully.

Troubleshooting Tips

  • LED not lighting up? Double-check the polarity of the LED (the anode should be connected to Pin 9 and the cathode to GND).
  • Potentiometer not responding? Ensure the wiper pin (middle pin) is correctly connected to A0 on the Arduino.
  • Code not uploading? Verify that the correct COM Port is selected in the Arduino IDE.
  • LED flickering or inconsistent brightness? Try increasing the delay in the code (e.g., delay(50);) to smooth out the brightness changes.

Further Exploration

  • Control Multiple LEDs: You can expand this project by adding more LEDs and controlling their brightness using separate potentiometers.
  • Add a Button: Try adding a button that switches between manual control (using the potentiometer) and automatic control (using a preset brightness pattern).
  • Serial Monitor: Use the Serial Monitor to display the potentiometer’s value for real-time feedback. You can add the following code to your loop for debugging:
Serial.begin(9600);
Serial.println(potValue); // Print potentiometer value to Serial Monitor

Note

This project introduces the fundamental concepts of PWM, analog input, and controlling outputs with Arduino. Understanding these concepts will prepare you for more advanced projects such as controlling motor speed or integrating more complex sensors.

FAQ

Q1: What is PWM in Arduino?
PWM (Pulse Width Modulation) is a technique used to simulate an analog output by rapidly toggling a digital pin between HIGH and LOW. It’s commonly used to control the brightness of LEDs or the speed of motors.

Q2: How does analogRead() work in Arduino?
The analogRead() function reads an input voltage from an analog pin and converts it into a value between 0 and 1023. This value corresponds to voltages between 0V and 5V.

Q3: What does a potentiometer do in this project?
A potentiometer acts as a variable resistor. In this project, it adjusts the input voltage that the Arduino reads, which in turn adjusts the brightness of the LED.

Q4: What is the difference between analogRead() and analogWrite()?
analogRead() reads an analog input and returns a value between 0 and 1023. analogWrite() sends a PWM signal (between 0 and 255) to a digital pin,