Published: 27.08.2018 Copyright by Joy-IT 4
Temperature Sensor
The following code example requires two addional libraries. These consist of the OneWire Library by
Paul Storegen, published under the MIT License, and the Dallas Temperature Control Library by Miles
Burton, published under the LGPL License. Both libraries are included in the package and must be copied
to the "library" folder before starng the Arduino IDE. You can nd it by default under the following path
of your Windows installaon: C:\Users\[Username]\Documents\Arduino\libraries
You can download the code example for Arduinos here or copy the following one:
3. CODE EXAMPLE ARDUINO
// Required libraries are imported
#include <DallasTemperature.h>
#include <OneWire.h>
// The input pin to which the sensor module is connected is declared here.
#define KY001_Signal_PIN 4
// Libraries are configured
OneWire oneWire(KY001_Signal_PIN);
DallasTemperature sensors(&oneWire);
void setup() {
// Initialization Serial output
Serial.begin(9600);
Serial.println("KY-001 Temperaturmessung");
// Sensor is initialized
sensors.begin();
}
// Main program loop
void loop()
{
// Temperature measurement is started...
sensors.requestTemperatures();
// ... and display measured temperature
Serial.print("Temperatur: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.write(176); // UniCode specification of a char symbol for the "° symbol".
Serial.println("C");
delay(1000); // 5s Pause until next measurement
}