#include <OneWire.h>
#include <Servo.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
const int SENSOR_PIN = 13; // Arduino pin connected to DS18B20 sensor's DQ pin
const int SERVO_PIN = 2; // Arduino pin connected to Servo Motor's pin
const float TEMPERATURE_THRESHOLD = 20; // °C
OneWire oneWire(SENSOR_PIN); // setup a oneWire instance
DallasTemperature sensors(&oneWire); // pass oneWire to DallasTemperature library
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
Servo servo; // create servo object to control a servo
DallasTemperature sensor(&oneWire); // pass oneWire to DallasTemperature library
float temperature;
int angle;
float tempCelsius; // temperature in Celsius
float tempFahrenheit; // temperature in Fahrenheit
void setup() {
sensors.begin(); // initialize the sensor
lcd.init(); // initialize the lcd
lcd.backlight(); // open the backlight
Serial.begin(9600); // initialize serial
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(0);
sensor.begin(); // initialize the sensor
}
void loop() {
sensors.requestTemperatures(); // send the command to get temperatures
tempCelsius = sensors.getTempCByIndex(0); // read temperature in Celsius
tempFahrenheit = tempCelsius * 9 / 5 + 32; // convert Celsius to Fahrenheit
if (temperature > TEMPERATURE_THRESHOLD)
angle = 90; // set angle to 90 degree
else
angle = 0; // set angle to 0 degree
servo.write(angle); // rotate servo motor
// print to serial
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("°C => servo angle: ");
Serial.println(angle);
lcd.clear();
lcd.setCursor(0, 0); // start to print at the first row
lcd.print(tempCelsius); // print the temperature in Celsius
lcd.print((char)223); // print ° character
lcd.print("C");
lcd.setCursor(0, 1); // start to print at the second row
lcd.print(tempFahrenheit); // print the temperature in Fahrenheit
lcd.print((char)223); // print ° character
lcd.print("F");
delay(500);
}