/*
* This ESP8266 NodeMCU code was developed by newbiely.com
*
* This ESP8266 NodeMCU code is made available for public use without any restriction
*
* For comprehensive instructions and wiring diagrams, please visit:
* https://newbiely.com/tutorials/esp8266/esp8266-temperature-sensor-lcd
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define SENSOR_PIN 14 // The ESP8266 pin connected to DS18B20 sensor's DQ pin
OneWire oneWire(SENSOR_PIN);
DallasTemperature DS18B20(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27 (from DIYables LCD), 16 column and 2 rows
float temperature_C; // temperature in Celsius
float temperature_F; // temperature in Fahrenheit
void setup() {
DS18B20.begin(); // initialize the sensor
lcd.init(); // Initialize the LCD I2C display
lcd.backlight(); // open the backlight
}
void loop() {
DS18B20.requestTemperatures(); // send the command to get temperatures
temperature_C = DS18B20.getTempCByIndex(0); // read temperature in Celsius
temperature_F = temperature_C * 9 / 5 + 32; // convert Celsius to Fahrenheit
lcd.clear();
lcd.setCursor(0, 0); // display position
lcd.print(temperature_C); // display the temperature in Celsius
lcd.print((char)223); // display ° character
lcd.print("C");
lcd.setCursor(0, 1); // display position
lcd.print(temperature_F); // display the temperature in Fahrenheit
lcd.print((char)223); // display ° character
lcd.print("F");
delay(500);
}