#include <OneWire.h>
#include <DallasTemperature.h>
const int thermistorPin = A0; // Analog pin connected to the thermistor
const int resistorValue = 10000; // Resistor value in ohms (10K ohms for a typical NTC thermistor)
// Data wire is connected to pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Create a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass the OneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);
void setup() {
// Start serial communication
Serial.begin(9600);
// Initialize the DS18B20 sensor
sensors.begin();
}
void loop() {
// Request temperature readings from the DS18B20 sensor
sensors.requestTemperatures();
// Read the temperature in Celsius
float DStemperatureCelsius = sensors.getTempCByIndex(0);
// Check if the temperature reading is valid
if (DStemperatureCelsius != DEVICE_DISCONNECTED_C) {
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(DStemperatureCelsius);
Serial.println(" °C");
} else {
Serial.println("Error reading temperature!");
}
// Add a delay to control the rate of temperature readings
delay(1000);
// Read the raw ADC value from the thermistor
int rawValue = analogRead(thermistorPin);
// Convert the raw ADC value to resistance using the voltage divider formula
float resistance = (resistorValue * (1023.0 / rawValue - 1.0));
// Use the Steinhart-Hart equation to convert resistance to temperature in Celsius
float temperatureCelsius = 1.0 / (log(resistance / 10000.0) / 3950 + 1.0 / 298.15) - 273.15;
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureCelsius);
Serial.println(" °C");
// Add a delay to control the rate of temperature readings
delay(1000);
}