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.
#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
}
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: