Lego NXT Temperature

The Lego Mindstorms NXT temperature sensor can be used to measure temperature and may be connected to a PICAXE using an I2C bus and controlled by appropriate PICAXE I2C commands.

LEGO and MINDSTORMS are registered trademarks of the LEGO Group. I2C is a registered trademark of Philips Semiconductors Corporation.

Related Commands:

Print Page




Using the Lego NXT Temperature Sensor

This is a sample program for the Lego NXT Temperature Sensor version (part 9749) using the AXE216 i2c Explorer Kit with a PICAXE-20X2. Please see the the Texas TMP275 datasheet for technical details about the sensor.

Code Example:
#picaxe 20x2
#terminal 9600

symbol temperature_lowbyte = b0	
symbol temperature_highbyte = b1		
symbol temperature_word = w0		; word is made up of b1 and b0

; Wires on NXT jack plug.
; Wire colours may vary. Pin 1 is always end nearest latch.
; 1 White	No connect
; 2 Black	0V
; 3 Red		0V
; 4 Green	V+ (4.5V or 5V)
; 5 Yellow	SCL
; 6 Blue	SDA
; Do not use i2c pullup resistor - already provided within sensor.

init:	; Slave address is $98
	hi2csetup i2cmaster, $98, i2cslow, i2cbyte

	; TMP275 only has 0.5 degree accuracy anyway, so use fast 9 bit mode.
	hi2cout $01,(%00000000)		; configure sensor to  9 bit
	;hi2cout $01,(%00100000)	; configure sensor to 10 bit
	;hi2cout $01,(%01000000)	; configure sensor to 11 bit
	;hi2cout $01,(%01100000)	; configure sensor to 12 bit

main:	; read temp high byte first

	hi2cin $00,(temperature_highbyte,temperature_lowbyte)	
	; in 9 bit mode whole degrees are in highbyte
	; bit7 of lowbyte gives us .5 or .0
	; bit6-0 of low byte not used
	; bit7 of high byte (which is bit15) indicates negative number 
	if bit15 = 0 then 'positive temperatue
	  sertxd ("Temperature = +")
	  sertxd (#temperature_highbyte)
	  if bit7 = 1 then
	    sertxd (".5",cr,lf)
	    sertxd (".0",cr,lf)
	  end if
	else 				; negative temperature
	  ; reverse the twos complement of high byte
	  dec temperature_highbyte
	  temperature_highbyte = NOT temperature_highbyte
	  sertxd ("Temperature = -")
	  sertxd (#temperature_highbyte)

	  if bit7 = 1 then
	    sertxd (".0",cr,lf)
	    sertxd (".5",cr,lf)
	  end if
	end if
	pause 1000			; wait 1 second then loop
	goto main
Copy Code Submit an Example

Create Module

Lego Mindstorm NXT sensors can be connected using two generic terminal block create modules to carry the SDA and SCL signals of the I2C bus.

Bill of Materials

Lego NXT Compatible socket CON070 1 Buy Now


No Image Selected

Submit Your Own Code!

You must be logged in to submit code examples. Login now.


  • Login to leave a comment.