Difficulty Level: Beginner to Intermediate
This project demonstrates how to build a simple digital thermometer using a DS18B20 temperature sensor and a 16x2 LCD display with Arduino.
Follow these steps for wiring the components:
In this project, the DS18B20 sensor measures the temperature and sends the data to the Arduino, which displays the temperature in Celsius on the 16x2 LCD display.
OneWire.h
: This library allows communication with devices using the 1-Wire protocol (like the DS18B20).DallasTemperature.h
: A higher-level library for DS18B20 temperature sensors to simplify reading temperatures.Wire.h
: Used for I2C communication with the LCD.LiquidCrystal_I2C.h
: Used to control the 16x2 LCD screen via I2C.lcd.begin()
: Initializes the LCD screen for I2C communication.sensors.requestTemperatures()
: Requests the temperature data from the DS18B20 sensor.sensors.getTempC()
: Retrieves the temperature in Celsius from the DS18B20 sensor.lcd.setCursor()
: Sets the position of the cursor on the LCD screen (column, row).lcd.print()
: Displays data (like temperature) on the LCD screen.Below is the code to run the digital thermometer:
#include
#include
#include
#include
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Initialize the LCD with I2C address 0x27
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
// Start the LCD and set it up to display text
lcd.begin();
lcd.backlight();
// Start the serial communication for debugging (optional)
Serial.begin(9600);
// Start up the temperature sensor library
sensors.begin();
}
void loop() {
// Request temperatures from DS18B20
sensors.requestTemperatures();
// Get temperature in Celsius
float temperatureC = sensors.getTempCByIndex(0);
// Display temperature on the LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperatureC);
lcd.print(" C");
// Debugging output to Serial Monitor (optional)
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" C");
// Delay between updates
delay(1000);
}
OneWire.h
, DallasTemperature.h
, and LiquidCrystal_I2C.h
from the Library Manager.This project allows you to create a simple digital thermometer using the DS18B20 temperature sensor and display the temperature on an LCD screen. You also learned about I2C communication with an LCD, using temperature sensor libraries, and basic Arduino coding techniques.