TutorialΒΆ

First, make sure that NXT-Python is installed correctly, see Installation.

This is not a Python tutorial, you must know how to program using Python to use NXT-Python.

First step when writing a NXT-Python script is to find the brick. This is the role of the nxt.locator.find() function:

#!/usr/bin/python3
"""NXT-Python tutorial: find the brick."""
import nxt.locator

# Find a brick.
with nxt.locator.find() as b:
    # Once found, print its name.
    print("Found brick:", b.get_device_info()[0])
    # And play a recognizable note.
    b.play_tone(440, 250)

Once the brick is found, the nxt.locator.find() function returns an object to interact with it: the Brick object. Here the script query device information and play a tone.

Now something a little bit more interesting, plug a motor on the port A and try the following script:

#!/usr/bin/python3
"""NXT-Python tutorial: turn a motor."""
import nxt.locator
import nxt.motor

with nxt.locator.find() as b:
    # Get the motor connected to the port A.
    mymotor = b.get_motor(nxt.motor.Port.A)
    # Full circle in one direction.
    mymotor.turn(25, 360)
    # Full circle in the opposite direction.
    mymotor.turn(-25, 360)

Try changing the parameters and see what happen. You can of course drive several motors, just use the get_motor() function for each one.

You can also get information from sensors:

#!/usr/bin/python3
"""NXT-Python tutorial: use ultra-sonic sensor."""
import time

import nxt.locator
import nxt.sensor

# Need to import generic sensors for auto-detection to work.
import nxt.sensor.generic

with nxt.locator.find() as b:
    # Find the sensor connected to port 4.
    mysensor = b.get_sensor(nxt.sensor.Port.S4)
    # Read the sensor in a loop (until interrupted).
    print("Use Ctrl-C to interrupt")
    while True:
        distance_cm = mysensor.get_sample()
        print(distance_cm)
        time.sleep(0.5)

Digital sensors can be automatically detected as long as the corresponding module is loaded. In the retail set, only the ultra-sound distance sensor is digital, all the other sensors are analog. When using an analog sensor, you must give the sensor class explicitly:

#!/usr/bin/python3
"""NXT-Python tutorial: use touch sensor."""
import time

import nxt.locator
import nxt.sensor
import nxt.sensor.generic

with nxt.locator.find() as b:
    # Get the sensor connected to port 1, not a digital sensor, must give the sensor
    # class.
    mysensor = b.get_sensor(nxt.sensor.Port.S1, nxt.sensor.generic.Touch)
    # Read the sensor in a loop (until interrupted).
    print("Use Ctrl-C to interrupt")
    while True:
        value = mysensor.get_sample()
        print(value)
        time.sleep(0.5)

If you run into problems, you can increase the log level. NXT-Python is using the logging module from the standard Python distribution. Try this script:

#!/usr/bin/python3
"""NXT-Python tutorial: increase log level."""
import logging

import nxt.locator

# Increase the log level, must be done before using any NXT-Python function. See logging
# documentation for details.
logging.basicConfig(level=logging.DEBUG)

with nxt.locator.find() as b:
    b.play_tone(440, 250)

You should now have enough information to start playing with NXT-Python. See the API Reference, or the Tips and Tricks pages for more informations.