Lego NXT Ultrasonic

The Lego Mindstorms NXT ultrasonic sensor can be used to measure distances 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 Ultrasound Sensor

This is a sample program for the Lego NXT Ultrasound Sensor (part 9846) using the AXE216 i2c Explorer Kit with a PICAXE-20X2.
Please note - This sensor is much more difficult than other Lego sensors to use.

  • You must provide both 9V and 5V supplies .
  • Also i2c needs to be 'hacked' due to a feature in the ultrasonic i2c firmware which makes it non-compliant with the industry standard i2c protocol.
This means only the ultrasonic sensor can be used with the i2c bus as we are now using a non-standard i2c protocol.
We recommend using the SRF005 instead!

Code Example:
#picaxe 20x2

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

; Lego Ultrasonic Sensor 
symbol distance = b0			; distance in cm

; Wires on NXT jack plug.
; Wire colours may vary. Pin 1 is always end nearest latch.
; 1 White	+9V
; 2 Black	0V
; 3 Red		0V
; 4 Green	+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

	hi2cout $41,($02)		; continuous mode
	pause 100

main:	; read values

	; This single line should work:
	; hi2cin $42,(distance)
	; but does not as dodgy firmware in ultrasonic sensor

	; therefore here is the workaround

	hi2cout ($42)		; address $42
	low B.7			; fake an extra pulse
	input B.7		; back to input
	hi2cin  (distance)	; read distance
	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

Lego NXT Compatible socket CON070 1 Buy Now
PP3 Battery Clip BAT016 1 Buy Now


No Image Selected

Submit Your Own Code!

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