// KY-013 NTC Temperature Sensor Example

const int ntcPin = A1; // Sensor input pin

void setup() {
  pinMode(ntcPin, INPUT);       // Initialize sensor pin
  Serial.begin(9600);           // Initialize serial monitor
  Serial.println("KY-013 NTC Temperature Test");
}

void loop() {
  // Read analog value from sensor
  double rawValue = analogRead(ntcPin);

  // Convert analog value to voltage
  double voltage = rawValue * 5.0 / 1023.0;

  // Convert voltage to resistance
  double resistance = (voltage / 5.0) * 10000.0 / (1.0 - (voltage / 5.0));

  // Calculate temperature in Kelvin using the B-parameter equation
  double temperatureK = 1.0 / ((1.0 / 298.15) + (1.0 / 3950.0) * log(resistance / 10000.0));

  // Convert temperature to Celsius
  double temperatureC = temperatureK - 273.15;

  // Print temperature to serial monitor
  Serial.print("Temperature: ");
  Serial.print(temperatureC);
  Serial.println(" °C");

  delay(1000); // Wait 1 second before next reading
}