[SOLVED] Problem converting ADC value using Steinhart-Hart equation

Status
Not open for further replies.

qbone

Member level 3
Joined
Jun 2, 2009
Messages
58
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Lyngby, Denmark
Activity points
1,778
Hey guys.
I am trying to build a thermostat for a project, and I wanna calculate the temperature using Steinhart equation, and getting the ADC value from a voltage divider made from a 10k Thermister and a 10k 1% resistor.

I read the ADC value just fine, but the equation doesn't work.
It worked when I was playing with Arduino, so there might be some problem I havent thought of.
Here is my code:

Code:
#include <16F886.h>
#device ADC=10
#fuses INTRC_IO,NOWDT,NOLVP,NOPUT,NOMCLR,NOBROWNOUT,NOCPD,NOIESO,NOFCMEN,NOWRT,BORV21
#use delay(internal=4000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#include <math.h>

long Thermister(long RawADC);

void init()
{
	setup_timer_0(T0_INTERNAL); 
	setup_timer_1(T1_DISABLED); 
	setup_timer_2(T2_DISABLED,0,1); 
	setup_comparator(NC_NC_NC_NC);

}

void main()
{
	setup_adc_ports(sAN0); 
	setup_adc(ADC_CLOCK_INTERNAL); 
	set_adc_channel(0);

	long temp;
	long value;

	while(true) {
		value = read_adc();	
		temp = Thermister(value);
		printf("A/D value: %Ld = %Ld\n\r", value, temp);
		delay_ms(500);
	}
}

long Thermister(long RawADC) //Steinhart-Hart Equation
{
	long Temp;
	Temp = log(((10240000/RawADC) - 10000)); //1024 = ADC Resolution, 10000 = 10kohm Resistor
	Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); //Steinhart Equation
	Temp = Temp - 273.15; // Convert Kelvin to Celcius
	
	return Temp;
}

Any help would be greatly appreciated
 

oh, maybe it helps to understand the code if I say I am programming in MPLAB v8.85 using the CCS C Compiler and library

After hours and hours of searching the web I may have found something, but I dunno if it will work, but don't have chance of testing it till Saturday or so.
It might be the datatypes causing the issues.

The issue I am seeing is that when I heat up the thermistor I get constant output of 54, when its rooms temperature I get constant 29 untill I reach about 15 or lower, then I get constant 7.
Maybe it will help changing all datatypes to float, as my original had the datatype double (arduino code).

This is what I am gonna try
Code:
float value;
float temp;

value = (float)read_adc();	
temp = Thermister(value);

float Thermister(float RawADC) //Steinhart-Hart Equation
{
	float Temp;
	Temp = log(((10240000/RawADC) - 10000)); //1024 = ADC Resolution, 10000 = 10kohm Resistor
	Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp)); //Steinhart Equation
	Temp = Temp - 273.15; // Convert Kelvin to Celcius
	
	return Temp;
}

You are all still very welcome to comment and come with pointers to wether this might work or not
 
Last edited:

I have solved it on my own.
The problem was with the datatypes, when I changed it all to float everything worked out as planned.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…