JonD1988

C_and_F

Dec 6th, 2022
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This sketch is use the DS18B20 Liquid Temperature sensor taking readings at 1 second intervals and printing them to the serial monitor
  2.  
  3. #include <OneWire.h>  //Includes OneWire Library
  4. #include <DallasTemperature.h> //Includes DallasTemperature Library
  5.  
  6. #define ONE_WIRE_BUS 8 //Yellow DS18B20 Signal Wire Connected to Arduino Pin 8
  7.  
  8. OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices  
  9. // (not just Maxim/Dallas temperature ICs)
  10. DallasTemperature sensors(&oneWire); //Pass our oneWire reference to Dallas Temperature.
  11.  
  12. float Celsius = 0; //Creates a float variable (decimal) to store the Celsius Temperature Readings and Initializes it to 0
  13. float Fahrenheit = 0; //Creates a float variable (decimal) to store the Fahrenheit Temperature Readings and Initializes it to 0
  14.  
  15. void setup() {
  16.   sensors.begin(); //Starts the sensor working
  17.   Serial.begin(9600); //Starts the serial monitor for someone where view the readings and debug code
  18. }
  19.  
  20. void loop() {
  21.   sensors.requestTemperatures(); //Send the command to get temperature readings
  22.  
  23.   Celsius = sensors.getTempCByIndex(0); //Stores Celsius Reading to Celsius Variable
  24.   Fahrenheit = sensors.toFahrenheit(Celsius); //Stores Fahrenheit Reading to Celsius Variable
  25.  
  26.   Serial.print(Celsius); //Prints Celsius Variable Value to Serial Monitor
  27.   Serial.print("°C  "); //Prints C Symbol to Serial Monitor - Note: To get degree symbol hold down Alt while pressing 248
  28.   Serial.print(Fahrenheit); //Prints Fahrenheit Variable Value to Serial Monitor
  29.   Serial.println("°F"); //Prints F Symbol to Serial Monitor - Note: To get degree symbol hold down Alt while pressing 248
  30.  
  31.   delay(1000); //Delays 1000 ms = 1 second between readings - i.e. the frequency of readings
  32. }
  33.  
  34. //Reference: https://create.arduino.cc/projecthub/TheGadgetBoy/ds18b20-digital-temperature-sensor-and-arduino-9cc806
Add Comment
Please, Sign In to add comment