Arduino Lesson 8: The Temperature Sensor – Make Your Arduino Read the Temperature!

In this lesson, we’ll learn how to use a temperature sensor to read the temperature and display it on an LCD screen. Get ready to build your own mini weather station!

Objective: Learn how to use a temperature sensor to read the temperature and display it on a connected LCD screen.

Materials:

  • Arduino Uno board
  • Breadboard
  • Temperature sensor (LM35 or DS18B20) – Choose one type
  • LCD display (16×2 or 20×4)
  • Resistors (if needed for specific sensor) – Check the sensor’s datasheet
  • Jumper wires
  • Computer with Arduino IDE software installed

Steps:

  1. Review: Discuss previous lessons and components, including sensors and analog input.
  2. Meet the Temperature Sensor: Introduce the temperature sensor and explain how it works.
  3. Building the Circuit: Connect the temperature sensor, LCD, and Arduino board to the breadboard. Ensure the sensor is connected to the correct input pin and the LCD is connected to the correct pins for data transmission (SDA and SCL).

Step 1: Connecting the Temperature Sensor

  • LM35:
    • Connect the LM35’s output pin (usually marked “OUT”) to an analog input pin on the Arduino: We’ll use pin A0.
    • Connect the LM35’s positive pin (usually marked “V+” or “+”) to a 5V power rail on the breadboard:
    • Connect the LM35’s negative pin (usually marked “V-” or “-“) to a ground rail on the breadboard:
  • DS18B20:
    • Connect the DS18B20’s data pin (usually marked “DQ”) to a digital input pin on the Arduino: We’ll use pin 2.
    • Connect the DS18B20’s power pin (usually marked “V+” or “+”) to a 5V power rail on the breadboard:
    • Connect the DS18B20’s ground pin (usually marked “V-” or “-“) to a ground rail on the breadboard:
    • You might need to add a pull-up resistor to the data pin (DQ): Consult the DS18B20 datasheet for details.

Step 2: Connecting the LCD Display

  1. Find the pins on the LCD: The LCD will have pins for data transmission (SDA and SCL), power (VCC and GND), and backlight control (often marked “A” for adjust).
  2. Connect the LCD’s SDA pin to the Arduino’s SDA pin (usually pin A4):
  3. Connect the LCD’s SCL pin to the Arduino’s SCL pin (usually pin A5):
  4. Connect the LCD’s VCC pin to a 5V power rail on the breadboard:
  5. Connect the LCD’s GND pin to a ground rail on the breadboard:
  6. Connect the LCD’s backlight control pin (A) to a digital output pin on the Arduino (e.g., pin 13):
  7. Set the LCD backlight by writing digitalWrite(13, HIGH); in your code.

Step 3: Programming the Arduino

  1. Open the Arduino IDE.
  2. Download and install the LiquidCrystal library: Go to “Sketch” -> “Include Library” -> “Manage Libraries…” and search for “LiquidCrystal”. Install the library.
  3. Copy and paste the code below (modify for DS18B20 if using that sensor):
#include <LiquidCrystal.h> // Include LCD library

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Define LCD pins (check your LCD's datasheet for correct pins)

const int tempPin = A0; // Temperature sensor connected to analog pin A0 (LM35)

void setup() {
  lcd.begin(16, 2); // Initialize LCD (16 columns, 2 rows)
  digitalWrite(13, HIGH); // Turn on backlight
}

void loop() {
  int sensorValue = analogRead(tempPin); // Read sensor value
  float temperatureC = sensorValue * 0.48828125; // Convert to Celsius (LM35)

  lcd.setCursor(0, 0); // Set cursor to start of first line
  lcd.print("Temperature: ");
  lcd.print(temperatureC);
  lcd.print(" C");
}
  1. Modify the code for the DS18B20 sensor:
    • Replace the analogRead(tempPin) line with a function to read the DS18B20 sensor (you’ll need to research the library and code for this).
    • Remove the Celsius conversion line (temperatureC = …) because the DS18B20 sensor typically provides temperature in Celsius already.
  2. Upload the code to the Arduino board.

Step 4: Test the Temperature Display

  • You should see the temperature reading displayed on the LCD screen.

Congratulations! You’ve built a mini weather station!

Let’s Experiment:

  • Place the sensor in different environments: Try placing the sensor in a warm room, a cold room, or near a heat source to see how the temperature reading changes.
  • Add a humidity sensor: Connect a humidity sensor to read the humidity and display it on the LCD along with the temperature.
  • Design a custom display: Use the LCD’s setCursor() and print() functions to create a more detailed display with time, date, and other information.

Keep experimenting with sensors and have fun with Arduino!