I checked the below code to rotate the servo motor to the resultant degree celsius value. But I'm getting a different result while compiling(Serial.println), got correct value when i did it in a calculator. Did i miss something?
#include <Servo.h>
int servoPin = 7;
Servo Servo1;
int thermistorPin = A0; // Thermistor analog input pin id
float thermistorValue = 0; // Store Thermistor value
float vin = 4.86; // Voltage in
float vout = 0.0; // Voltage out
int roomTemp = 28+273.15; // Room Temperature (Kelvins)
float r1 = 0.0; // Resistance R1
int r2 = 10000; // Resistance R1
float r0 = 7.2; // Resistance @ Room Temperature
float coefficientThermistor = 3950;
void setup() {
Servo1.attach(servoPin);
Serial.begin(9600);
}
void loop() {
thermistorValue = analogRead(thermistorPin); // converts voltage to no. between 0 and 1024
vout = (thermistorValue * vin) / 1024.0 ; // convert value to voltage
r1 = r2 * ((vin/vout) - 1);
float resistanceRatio = r1/r0;
float logR = log(resistanceRatio); // ln(r1/r0)
float productTerm = (1/coefficientThermistor)*logR; // 1/B * ln(r1/r0)
float sum = (1/roomTemp) + productTerm; // 1/T0 + 1/Bln(r1/r0)
float steinhart = (1/sum); // inverse 1/T
float steinhartCelsius = steinhart - 273.15; // Convert from Kelvins to Celsius
Servo1.write(steinhartCelsius);