Readtemp12

tjetson

Senior Member
Hello everyone

I have the DS18B20 sensor and I made a little temperature logging doohickey; that uses an RTC and the temp sensor to log the temperature every minute. I had an option to display the temperature in Fahrenheit. My code goes like this:
Code:
	readtemp 2, Celsius
	pause 750
	Fahrenheit = Celsius * 18 / 10 + 32	
	Celsius = 0		

	readtemp12 2,w5
	w5=w5*10/16
	b10=w5/10		'First digits
	b11=w5//10		'Decimal points

	pause 750
	return
As you can see, I use readtemp to get a integer value to convert to Fahrenheit; then get a real Celsius value with the readtemp12 command. I wondered if it were possible to instead use the value read by readtemp12 and convert that to Fahrenheit.
 

benryves

Senior Member
The returned value from the sensor is Celcius * 16. As far as I'm aware the conversion from Celcius to Fahrenheit is C*9/5+32, so we if we factor in an additional multiple of 16 you would use C*9/80+32 to get an integral Fahrenheit reading, or C*9/5+512 if you want a value scaled by sixteen (as per the original value read from the sensor).
 
Last edited:

tjetson

Senior Member
As far as I'm aware the conversion from Celcius to Fahrenheit is C*9/5+32,
This is correct.



you would use C*9/80+32 to get an integral Fahrenheit reading

Does this mean I would do something like this to get a Fahrenheit reading with decimal point?
Code:
	readtemp12 2, Celsius
	pause 750
	Fahrenheit = Celsius * 9/80	+32
	Celsius = 0
 

westaust55

Moderator
Firstly the presumption here is that you are only seeking to monitor temperatures >= 0 degrees C, otherwise added maths are required to check for negative values.
1. You do not need the pasue 750 after the readtemp command. The PICAXE is effectively already waiting for 750ms within the readtemp command for the DS18B20 to perform the temp conversion process and apss the value back to the PICAXE.

2. If you want to read to 1 decimal place
READTEMP12, 2, Celsius
Fahrenheit10 = Celsius * 90 / 80 + 320

Then to display the actual temp
Fahrenheit = Fahrenheit10 / 10
Fraction = Fahrenheit10 // 10
SEROUT pin, N2400, (#Fahrenheit, “.”, #Fraction)


There have been past threads on the use of READTEMP12 and the maths needed to handle sub 0 degC temperatures. You may wish to do a forum search on READTEMP12 in the first instance if you need this feature.
 

hippy

Ex-Staff (retired)
There have also been threads on direct conversion of READTEMP12 to Fahrenheit to 4 bits of decimal resolution. The +32 in the conversion varies from +32 if no bits of decimal to +512 (32*16) if 4 bits of decimal.
 
Top