Negative temperatures in S2P

stevesmythe

Senior Member
According to the S2P documentation, the Temperature sensor (based on a DS1B20 sensor) can measure temperature in degrees Celsius from 0-120. I was wondering why there is this limitation, when the DS1B20 sensor operates from -55 to +125 degrees Celsius and Scratch can handle negative numbers?
 

eggdweather

Senior Member
The most significant bit is the sign bit, so:

ReadTemp pin, temperature
if temperature > 127 then
temperature = temperature - 127
serout LCD, 2400, ("-")
end if
serout LCD, 2400, (#temperature)

Should work OK, so in the SCRATCH environment there must be limitations in number handling to 0 to 127 perhaps.
 
Last edited:

hippy

Technical Support
Staff member
Not sure why there is a limitation, but it could be a technical limitation. Scratch programs can also be converted to run on PICAXE chips which do not support negative numbers so it may be a necessary compromise to ensure compatibility with both.
 

stevesmythe

Senior Member
Yes, I think you could be right about that. However, there are plenty of S2P blocks that don't get converted (i.e. those that draw to the screen) when used in "remote run" mode. Also, 120 is a slightly unusual upper limit....

Maybe Technical can shed some light on this?
 

hippy

Technical Support
Staff member
The difference with those other commands which are not converted is that they would not be expected to work with a PICAXE chip while a Read Temperature would.

As to the upper limit of the 0-120 range that may be because it is a more rounded number than 125 to suit the audience Scratch is mostly aimed at or it may have simply been an oversight. Most people will not be encountering such high temperatures but I believe the returned temperature reading value can go up to 125 as per the DS18B20 spec.
 

stevesmythe

Senior Member
In case it's relevant, what I am doing (for a classroom experiment) is using S2P as a data logger and plotting a cooling curve of temperature against time for a waterproof DS18B20 in a glass of iced water. It doesn't really matter too much that it stops at 0 deg but would be better if it didn't.
 

hippy

Technical Support
Staff member
Sounds like a reasonable project. Have you actually checked to see what happens with a negative temperature ?

I suspect it may actually deliver some usable value which can be converted to the correct negative temperature but have not tested that.

A quick perusal of the code suggests it's a READTEMP12 in the PICAXE, the result passed back as an unsigned 16-bit value as READTEMP12 would normally return, which is then divided by 16 in S2P before being passed over to Scratch as an integer temperature.

I would guess -1C might give a value 255 ( or perhaps 4095 ) with the value decreasing by one for every 1C drop in temperature.

The proof is in the pudding so it might be worth trying it, seeing what values you do get back.
 

hippy

Technical Support
Staff member
Excellent news. I expect that "0-120" is "stick to that and it's all going to be fine" guidance rather than an absolute specification.
 
Top