Created
June 24, 2020 08:40
-
-
Save suadanwar/75cf994936e7bc4abcbc78c12146633e to your computer and use it in GitHub Desktop.
This sample code is for DS18B20 Waterproof Temperature Sensor Tutorial.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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