/* https://create.arduino.cc/projecthub/everth-villamil-ruiz/temperature-sensor-ds18b20-3decfc */ //Include libraries #include #include // Data wire is plugged into pin 2 on the Arduino #define ONE_WIRE_BUS 2 // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) OneWire oneWire(ONE_WIRE_BUS); // Pass our oneWire reference to Dallas Temperature. DallasTemperature sensors(&oneWire); void setup(void) { Serial.begin(9600); //Begin serial communication Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message sensors.begin(); } void loop(void) { // Send the command to get temperatures sensors.requestTemperatures(); Serial.print("Temperature is: "); Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire //Update value every 1 sec. delay(1000); } - - - - - or - - - - - /* https://create.arduino.cc/projecthub/TheGadgetBoy/ds18b20-digital-temperature-sensor-and-arduino-9cc806 */ /* YourDuino TEST: Temperature Sensor DS18B20 * V1.2 08-26-2016 - Connect cable to Arduino Digital I/O Pin 2 terry@yourduino.com */ /*-----( Import needed libraries )-----*/ #include #include /*-----( Declare Constants )-----*/ #define ONE_WIRE_BUS 2 /*-(Connect to Pin 2 )-*/ /*-----( Declare objects )-----*/ /* Set up a oneWire instance to communicate with any OneWire device*/ OneWire ourWire(ONE_WIRE_BUS); /* Tell Dallas Temperature Library to use oneWire Library */ DallasTemperature sensors(&ourWire); /*-----( Declare Variables )-----*/ void setup() /*----( SETUP: RUNS ONCE )----*/ { /*-(start serial port to see results )-*/ delay(1000); Serial.begin(9600); Serial.println("YourDuino.com: Temperature Sensor Test Program"); Serial.println("Temperature Sensor: DS18B20"); delay(1000); /*-( Start up the DallasTemperature library )-*/ sensors.begin(); }/*--(end setup )---*/ void loop() /*----( LOOP: RUNS CONSTANTLY )----*/ { Serial.println(); Serial.print("Requesting temperature..."); sensors.requestTemperatures(); // Send the command to get temperatures Serial.println("DONE"); Serial.print("Device 1 (index 0) = "); Serial.print(sensors.getTempCByIndex(0)); Serial.println(" Degrees C"); Serial.print("Device 1 (index 0) = "); Serial.print(sensors.getTempFByIndex(0)); Serial.println(" Degrees F"); delay(5000); }/* --(end main loop )-- */