C++ Library

The C++ communication library makes it possible to communicate with the NXT using a Bluetooth or network connection. To be able to communicate with the NXT over Bluetooth a partnership between the PC and the NXT has to be established. Use this guide to help you establish a Bluetooth connection. To communicate with the NXT over a network you need to download and run the MonoBrick Tunnel.

With the library it is possible to:

  • Communicates with the NXT using Bluetooth or Ethernet
  • Supports more than 20 analog and I2C sensors
  • Control all three motors
  • Send and receive messages using the mailbox system
  • Set Brick name, get battery level, read firmware version etc.
  • Play tones and sound files
  • Use the NXT’s file system to download and upload files
  • Start and stop on-brick programs
  • Use exceptions to catch sensor and connection errors
  • Open and close connections with multiple NXT units
  • and more..

Getting Started

To get started you need a C++ compiler. I would recommend Code Blocks with GCC compiler and GDB debugger from MinGW – this can be downloaded here.  Once code blocks is installed do the following

  • Download and unzip the C++ library to a folder of your choice
  • Create a new console C++ project in the same folder
  • Add all the files from the library to your project

If you want to communicate over a network remember to link to libwsock32.a – otherwise delete/remove network.h and network.cpp from your project. In Code blocks this is done using project->build option->linker setting->add. The file libwsock32.a is located in the folder (Code Blocks install directory)/MinGW/lib/

To write your fist Bluetooth program simply copy the code below into your main file – remember to change the comport!!. The program will make motor B turn when the touch sensor on port 1 is pressed.

#include <cstdlib>
#include <iostream>
#include <string>
#include <conio.h>
#include "nxt.h"

using namespace std;

//set up the NXT
Connection *connection = new Bluetooth();
Sensor *sensor1 = new Touch(IN_1, connection);
Motor *motorB = new Motor(OUT_B, connection);
int main()
    cout << "Try to connect to the NXT" << endl;
    cout << "Connected" << endl;
    //connection->connect(1500, ""); //Network connection
    cout << "Press touch sensor - hit any key to end" << endl;
    while(!_kbhit()){//hit a key to end
  catch (Nxt_exception& e){
    //some error occurred - print it out
    cout << e.what() << endl;
    cout << "error code: " << e.error_code() << endl;
    cout << "error type: " << e.error_type() << endl;
    cout << e.who() << endl;
  return 0;

If you are able to compiled and run the above program you are ready to start using the library and writing your own programs. To view a complete list of available commands and sensor opportunities please consult the library documentation.

2 comments on “C++ Library
  1. Hello Anders and Lars,

    Please allow me to introduce myself. I am the CTO and founder of
    Tynker. We are a visual programming platform, and we teach
    kids to code. In addition to programming games and apps, we now
    support interfacing with Parrot drones, Sphero robots and Philips Hue
    lights so kids can program those devices from our iPad and Android
    apps using the Tynker programming language, and the devices are controlled over
    Bluetooth or Wifi.

    We intend to to support LEGO EV3 and love the libraries you have built-
    seem like you are experts in the space, and we would love to work
    with you – are you open to consulting for us? I am in San Francisco
    Bay Area. Do you have time for a quick call?

    Looking forward to have a conversation,

Make a donation


C++ Library 2.12927 downloads