Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This sample code is for DS18B20 Waterproof Temperature Sensor Tutorial.
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define ONE_WIRE_BUS 9
float Celsius = 0;
float Fahrenheit = 0;
LiquidCrystal_I2C lcd(0x27, 20, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
void setup() {
lcd.init();
lcd.backlight();
sensors.begin();
Serial.begin(9600);
}
void loop() {
sensors.requestTemperatures();
Celsius = sensors.getTempCByIndex(0);
Fahrenheit = sensors.toFahrenheit(Celsius);
Serial.print(Celsius);
Serial.print(" C ");
Serial.print(Fahrenheit);
Serial.println(" F");
lcd.setCursor(2, 0);
lcd.print("TEMPERATURE");
lcd.setCursor(0, 1);
lcd.print((char)223);
lcd.setCursor(1, 1);
lcd.print("C:");
lcd.setCursor(3, 1);
lcd.print(Celsius, 1); //display the temperature in 1 decimal place
lcd.setCursor(8, 1);
lcd.print((char)223);
lcd.setCursor(9, 1);
lcd.print("F:");
lcd.setCursor(11, 1);
lcd.print(Fahrenheit, 1); //display the temperature in 1 decimal place
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment