Lego NXT Compass

The Lego Mindstorms NXT compass can be used to determine heading and direction orientation 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

Share

Schematic

PCB

Using the Hitechnic NXT Compass Sensor

This is a sample program for the Hitechnic NXT Compass Sensor (part MS1034) using the AXE216 i2c Explorer Kit. Please remember that the compass should be kept ready while determining the direction reading. Tilting the compass sensor will give an incorrect reading.

Code Example:
#picaxe 20x2
#no_table

; AXE216 Project Board Setup
; J1	External Power	Fitted
; J2	SCL Pullup		Not fitted (pullup is in sensor)
; J3	SDA Pullup		Not fitted (pullup is in sensor)
; H2	9V Power		Not fitted

symbol heading_2degree = b0
symbol heading_adder   = b1
symbol heading         = w1	

; 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 $02
	hi2csetup i2cmaster, $02, i2cslow, i2cbyte

	; Optional configuration
	hi2cout $41,($00)		; normal mode
	; hi2cout $41,($43)		; hard iron calibrate mode
	pause 100

main:	; read values
	hi2cin $42,(heading_2degree, heading_adder)	
	
	heading = heading_2degree * 2 + heading_adder
		
	debug				; debug display
	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

DescriptionCodeQty
Lego NXT Compatible socket CON070 1 Buy Now

Simulation

No Image Selected

Submit Your Own Code!

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