卄卂尺ᗪ山卂尺乇 101

LearningHub


Chapter 4: Microcontrollers and Arduino


Topic 3: Simple hardware projects using Arduino


Arduino Temperature Display Tutorial


Project Overview

In this tutorial, we will build a temperature display using an Arduino board, a DS18B20 temperature sensor, and a 16x2 LCD display. The Arduino will read the temperature from the sensor and display it on the LCD screen.

  • Arduino board (e.g., Arduino Uno or Arduino Nano)
    Arduino Board

  • DS18B20 temperature sensor
    DS18B20 Sensor

  • 16x2 LCD display (compatible with the Hitachi HD44780 controller)
    LCD Display

  • 4.7kΩ resistor (for the DS18B20 sensor)
    4.7kΩ Resistor

  • Breadboard and jumper wires
    Breadboard Jumper Wires

Connections

DS18B20 Temperature Sensor

  • DS18B20 VCC to Arduino 5V
  • DS18B20 GND to Arduino GND
  • DS18B20 Data to Arduino digital pin 2
  • Add a 4.7kΩ resistor between the DS18B20 Data and VCC to act as a pull-up resistor.

DS18B20 Temperature Sensor

  • LCD VCC to Arduino 5V
  • LCD GND to Arduino GND
  • LCD SDA (data) to Arduino analog pin 4
  • LCD SCL (clock) to Arduino analog pin 5

Code

                                    
                                    #include <OneWire.h>
                                    #include <DallasTemperature.h>
                                    #include <LiquidCrystal_I2C.h>
                                    
                                    // Data wire for DS18B20 temperature sensor
                                    #define ONE_WIRE_BUS 2
                                    
                                    OneWire oneWire(ONE_WIRE_BUS);
                                    DallasTemperature sensors(&oneWire);
                                    
                                    // Set the LCD address and dimensions
                                    LiquidCrystal_I2C lcd(0x27, 16, 2);
                                    
                                    void setup() {
                                      // Initialize DS18B20 temperature sensor
                                      sensors.begin();
                                    
                                      // Initialize LCD
                                      lcd.init();
                                      lcd.backlight();
                                    
                                      // Print a welcome message on the LCD
                                      lcd.setCursor(0, 0);
                                      lcd.print("Temperature:");
                                    }
                                    
                                    void loop() {
                                      // Request temperature from DS18B20
                                      sensors.requestTemperatures();
                                    
                                      // Read temperature in Celsius
                                      float tempC = sensors.getTempCByIndex(0);
                                    
                                      // Display temperature on LCD
                                      lcd.setCursor(0, 1);
                                      lcd.print("Temp: " + String(tempC, 1) + "C");
                                    
                                      delay(1000); // Update every 1 second
                                    }
                                    
                                 

Libraries

You'll need to install the required libraries to compile and upload the code to your Arduino board. You can install these libraries using the Arduino Library Manager:

  • OneWire
  • DallasTemperature
  • LiquidCrystal_I2C

Step-by-Step Instructions

  1. Connect the DS18B20 temperature sensor and the LCD display to the Arduino as described in the "Connections" section above.
  2. Upload the provided Arduino code to your Arduino board.
  3. Adjust the I2C address (0x27) in the code if your LCD has a different address.
  4. Power up your Arduino, and you should see the temperature displayed on the LCD screen in Celsius.