Today, we are going to learn how to create a simple project using Arduino, a temperature sensor (TMP36), an LCD display, and a potentiometer to display real-time temperature readings. This project will help us understand how analog sensors work, how to read sensor data with Arduino, and how to display that data on an LCD screen.
We will be using the following components in this project:
1. Arduino Uno (or any similar board)
2. TMP36 Temperature Sensor – This sensor measures temperature and gives an analog output.
3. 16x2 LCD Display – Used to display the temperature readings.
4. 10kΩ Potentiometer – Used to control the contrast of the LCD display.
5. Jumper wires – To make all the necessary connections.
6. Breadboard – Optional, but useful for organizing and connecting the components.
Let’s break down the circuit connections:
1. TMP36 Temperature Sensor:
- This sensor has 3 pins:
- VCC: Connect this to 5V on the Arduino to power the sensor.
- GND: Connect this to GND on the Arduino.
- OUT: This is the analog output pin of the TMP36. We connect this to Analog Pin A0 on the Arduino to read the temperature data.
2. LCD Display:
- VSS goes to GND.
- VDD connects to 5V on Arduino.
- V0: This is where we connect the middle pin of the potentiometer. This adjusts the contrast of the LCD screen.
- RS (Register Select), E (Enable), D4-D7 (Data pins): We connect these to specific pins on the Arduino (pins 12, 11, 7, 6, 5, 4 respectively).
- LED+ and LED- connect to 5V and GND, respectively, to power the LCD backlight.
3. Potentiometer:
- One pin goes to GND, the other to 5V.
- The middle pin of the potentiometer connects to V0 on the LCD, which adjusts the LCD contrast.
Now, let’s go over the Arduino code that powers this project. The purpose of the code is to read the temperature from the TMP36 sensor and display it on the LCD screen.
Step-by-Step Code Walkthrough:
1. Including the LCD Library:
```cpp
#include <LiquidCrystal.h>
```
- This line imports the `LiquidCrystal` library that we need to control the LCD display.
2. Initializing the LCD:
```cpp
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
```
- Here, we are telling the Arduino which pins are connected to the LCD for control. The `LiquidCrystal` constructor takes pin numbers for RS, E, D4, D5, D6, D7.
3. Setting the Temperature Sensor Pin:
```cpp
const int sensorPin = A0;
```
- This declares that A0 is the pin where the temperature sensor’s output is connected.
4. Setup Function:
```cpp
void setup() {
lcd.begin(16, 2);
lcd.print("Temp Sensor Init");
delay(2000); // Wait for 2 seconds
lcd.clear();
Serial.begin(9600);
}
```
- In the `setup()` function, we initialize the LCD and set it to display a "Temp Sensor Init" message for 2 seconds.
- We also start serial communication to send debug data to the Serial Monitor.
5. Reading the Temperature in the Loop:
```cpp
void loop() {
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
float temperatureC = (voltage - 0.5) * 100;
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print((char)223); // Degree symbol
lcd.print("C");
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
Serial.print("Temperature (C): ");
Serial.println(temperatureC);
delay(1000); // Update every second
}
```
- The `loop()` function continuously reads the temperature sensor and updates the LCD screen.
- analogRead(sensorPin) reads the voltage from the TMP36 sensor and stores it in sensorValue.
- We convert this value to a voltage using sensorValue * (5.0 / 1023.0) (since Arduino uses a 10-bit ADC, giving values between 0 and 1023).
- Then, we convert the voltage into temperature in Celsius: temperatureC = (voltage - 0.5) * 100
- TMP36 gives 500mV = 0°C and has a sensitivity of 20mV/°C.
- We then display the temperature on the LCD and also print the sensor values to the Serial Monitor for debugging.
1. Sensor Data: The TMP36 sensor continuously provides an analog output that changes with temperature. The Arduino reads this output and converts it into a temperature value.
2. Displaying on LCD: The calculated temperature is then displayed on the 16x2 LCD, with the contrast adjusted using the potentiometer.
3. Serial Monitor: Additionally, the Arduino sends debug data to the Serial Monitor, where we can see the raw sensor readings and the calculated temperature.
1. **Potentiometer Adjustment**: When you rotate the potentiometer, you can see the contrast of the LCD screen change. If the LCD text is not visible, adjust the potentiometer until it appears clearly.
2. **Temperature Display**: The temperature in Celsius will be shown on the LCD. For example, it might display:
"Temp: 25.5°C"
3. **Debugging**: You can open the Serial Monitor to see the sensor value and temperature printed as raw data for further understanding.
To summarize, we have created a simple system where the TMP36 temperature sensor reads the temperature and the Arduino processes this data and displays it on a 16x2 LCD. By using a potentiometer, we can control the contrast of the LCD display.
This project demonstrates how sensors work with Arduino, how to use analog inputs, and how to interface with an LCD to display real-time data. You can expand this project by using different sensors, adding more features, or incorporating wireless transmission of data
#include <LiquidCrystal.h>
// Initialize the LCD (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
// Pin for TMP36 sensor
const int sensorPin = A0; // TMP sensor output connected to A0
void setup() {
// Start the LCD with 16 columns and 2 rows
lcd.begin(16, 2);
// Print a welcome message
lcd.print("Temp Sensor Init");
delay(2000); // Wait for 2 seconds
// Clear the display
lcd.clear();
// Start serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the TMP36 sensor output
int sensorValue = analogRead(sensorPin);
// Convert the sensor value to voltage (assuming 5V reference)
float voltage = sensorValue * (5.0 / 1023.0); // Range from 0 to 5V
// Convert the voltage to temperature (TMP36: 500mV = 0°C, 20mV/°C)
float temperatureC = (voltage - 0.5) * 100; // Convert to Celsius
// Display the temperature on the LCD
lcd.setCursor(0, 0); // Set cursor to first row, first column
lcd.print("Temp: ");
lcd.print(temperatureC); // Display temperature
lcd.print((char)223); // Degree symbol
lcd.print("C");
// Debugging: Print to Serial Monitor
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
Serial.print("Temperature (C): ");
Serial.println(temperatureC);
delay(1000); // Update every second
}