Adding an External TemperAture Sensor
Sometimes it's useful to be able to measure temperature, whether that is ambient temperature in a room, or the temperature experienced by some device or system. (Note that Scamp (Rev C) and Scamp2 computers have an onboard temperature sensor. For more information, see Temperature Sensor.)
The PCT2075 is an I2C-based temperature sensor (datasheet) which has a range of -25°C to +100°C with an accuracy of ±1°C, and an extended range of -55°C to +125°C with a lesser accuracy of ±2°C. It has an on-chip band-gap temperature sensor and Sigma-Delta ADC. The temperature sensor returns an 11-bit two’s complement reading, giving a temperature resolution of 0.125°C. This high temperature resolution is particularly useful in applications of measuring precisely the thermal drift.
The schematic for interfacing the PCT2075 is straight forward:
The PCT2075 is an I2C-based temperature sensor (datasheet) which has a range of -25°C to +100°C with an accuracy of ±1°C, and an extended range of -55°C to +125°C with a lesser accuracy of ±2°C. It has an on-chip band-gap temperature sensor and Sigma-Delta ADC. The temperature sensor returns an 11-bit two’s complement reading, giving a temperature resolution of 0.125°C. This high temperature resolution is particularly useful in applications of measuring precisely the thermal drift.
The schematic for interfacing the PCT2075 is straight forward:
The PCT2075 requires only power and ground, decoupling caps on the power supply, and a connection to the I2C interface on the Scamp. By tying the address inputs A0..A2 to ground (GND), the sensor is configured to be addressed at $48 on the I2C bus.
The following word definition will read the 11-bit data from the sensor. This is spread across two bytes.
The following word definition will read the 11-bit data from the sensor. This is spread across two bytes.
$48 constant PCT2075
: gettemp ( -- n1 n2, returns 11-bit temp data as two bytes )
start
PCT2075 read drop
receive ack
receive nack
stop
;
The first byte (2nd on the stack) is the integer temperature value. The second byte (top of the stack) is the fractional temperature reading. For most applications, and since the accuracy of the sensor is ±1°C, it's only the first byte that is of interest, and the fractional value on top of the stack can discarded. Since the value read is an 8-bit two's-compliment, we need to sign extend it to 16 bits (using the extend word) before printing it out.
We can define a word to read the sensor and display the current temperature:
We can define a word to read the sensor and display the current temperature:
: temp ( -- , display temp, integer degrees C only )
gettemp
drop
extend .
." C"
;
For the full resolution of the sensor, we can define the following word to combine the 11 data bits spread across two bytes into a single, sign-extended value:
: convert (n1 n2 -- n3 , converts sensor data to 16-bit value )
#32 u/
swap
$8 *
or
dup $3ff >
if
$f800 or
then
;
To read and display the temperature reading to 0.1°C resolution, we can define the following word:
: temperature
gettemp
convert
#125 m* #100 sm/rem
swap drop
dup abs 0 <# # #46 hold #s rot sign #> type ." C"
;
Note that, at best, the accuracy of this sensor is ±1°C, so the 0.1°C resolution is useful for tracking relative changes in temperature, not as an absolute measure of temperature.