MonoBrick is a LEGO Mindstorms communication library written in C# that runs on a wide variety of operating systems including Linux, Mac OS and Windows. The library can communicate with the Mindstorms NXT brick over USB, Bluetooth or Ethernet. Ethernet communication is done using MonoBrick Tunnel or MonoBrick Tunnel for Android. Since MonoBrick works with both Mono and Microsoft’s.Net Framework it can be used with either Visual Studio or MonoDevelop. Although most Windows users will prefer Visual Studio – MonoDevelop is definitely worth a try. MonoBrick has the following features:
- Communicates with the NXT using USB, Bluetooth and Ethernet
- Works with Linux, Mac OS, Windows, Windows Phone and Android (using Mono for Android)
- Supports more that 20 analog and I2C sensors
- Control the motors individual or as a vehicle
- 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..
Writing your first MonoBrick program
Before writing your first program please make sure that you are able to communicate with the Mindstorms brick over either USB or Bluetooth. Mac Os and Linux uses will also have to make sure that Mono is installed. These guides will help you setup communication under Windows, Linux and Mac OS for use with MonoBrick. Once you have a working connection download the MonoBrick library dll using the download link and add the dll as a reference in your project. Another option is to download the test project. The test project includes the sample program shown below where the keyboard is used to read sensor values and control motor A.
using System;
using MonoBrick;
namespace Application
{
class Program
{
static void Main(string[] args)
{
var nxt = new NXTBrick<TouchSensor,Sonar,NoSensor,NoSensor>("usb");
nxt.Connection.Open();
nxt.Sensor2.Mode = SonarMode.Centimeter;
ConsoleKeyInfo cki;
Console.WriteLine("Press Q to quit");
do
{
cki = Console.ReadKey(true); //press a key
switch(cki.Key)
{
case ConsoleKey.D1://1 was pressed
Console.WriteLine("S1: " + nxt.Sensor1.ReadAsString());
break;
case ConsoleKey.D2://2 was pressed
Console.WriteLine("S2: " + nxt.Sensor2.ReadAsString());
break;
case ConsoleKey.UpArrow:
Console.WriteLine("Motor A forward");
nxt.MotorA.On(50);
break;
case ConsoleKey.DownArrow:
Console.WriteLine("Motor A reverse");
nxt.MotorA.On(-50);
break;
case ConsoleKey.S:
Console.WriteLine("Motor A off");
nxt.MotorA.Off();
break;
}
}
while (cki.Key != ConsoleKey.Q);
}
}
}
Documentation
For more MonoBrick examples have a look at the MonoBrick guide and the documentation pages.

Any version of the test program with a Bluetooth connection?
Hi Ryan.(“usb”); (“com2″); (“/dev/tty.Great-DevB “);
Thanks for visiting my website. Soon I will add some more documentation for using MonoBrick on the guide page. Meanwhile you can try to replace the following line in the test program
var nxt = new NXTBrick
with
var nxt = new NXTBrick
if you are using windows or
var nxt = new NXTBrick
if you are using Mac Os. Note that Great is the name of your LEGO Brick. Depending on the OS that you are using you can find help here for creating a bluetooth connection on Windows or Mac. Hope this answers your question.
I really appritiate the quick reply, I have tried using a com port (Windows) but I keep getting a MonoBrick.ConnectionException telling me that it has an Error sending NXT command, it does this despite the NXT being on or off so I imagine it’s not actually connecting.
Are you able to connect with the MonoBrick Remote?
Nevermind, I got it, when you have 15 (or more) other bluetooth devices on your desk things tend to get screwed up.
Thanks
Ok – let me know is there is anything else…
What would you use for a Bluetooth connection with Android?
Fist get a list of bound devices using
var deviceArray = MonoBrick.Bluetooth.BondDevices;
Then use the array to create a brick class
var nxt = new NXTBrick(new MonoBrick.Bluetooth(deviceArray[0]));
Then connect
nxt.Connect();
Sorry for all the questions, I’m getting an error saying that the type “Android.Bluetooth.BluetoothDevice” is defined in an assembly that is not referenced and that I must add it. Is there anything I should add to the initialization other than “using MonoBrick”?
you need
using Android.Bluetooth;
and you probably need to add the following to the manifest file
uses-permission android:name=”android.permission.BLUETOOTH”
uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”
let me know how it goes
Thanks for the reminder on adding Bluetooth Permissions, I’m still getting the same error even after adding Android.Bluetooth.
Works great now, Thanks!
No problem – glad I could help!
Hey Anders
Trying out the test program, I got the motors to work fine. However, whenever I try to access the sensor readings (depressing either 1 or 2), I get an “ArgumentException was unhandled” with an “Invalid NXT Reply” message. Could it be that my bluetooth connection is faulty? If so, what should I do to resolve this?
Hi Gerard – thanks for trying out MonoBrick
Are you able to get it working using the USB connection?
Anders
My Application “NXTDash” is now Public:
http://www.vb-paradise.de/sonstiges/showroom-testcenter/kleinkram/91371-nxtdash-verwaltung-fuer-den-lego-r-mindstorms-r-nxt-c/#post757741
Hi again
Could you please post the code you are running and tell what sensors you have connect to each port and obviously what system you are on Windows, Linux or Mac… thanks.
Oh and also what version of MonoBrick you have downloaded!!
Hi Anders
Sorry I was unable to respond sooner… circumstances kept me occupied.
To answer your last questions, I downloaded the basic MonoBrick library and was just trying to get your sample test program to work i.e. the one displayed higher up on this page. The version of MonoBrick that I’ve been using is contained in the MonoBrickTest-1.0 zip file I downloaded a few days ago.
Your question regarding communication using a USB connection, has lead me to realize that I haven’t been able to establish this, in part because I haven’t been able to install the PhantomDriver. As I had bluetooth connection, and test program seemed to work (at least partially) I thought it mightn’t have mattered as the experiment uses bluetooth communication.
On attempting the autorun.exe for phantom driver install, I get the “No software will be installed” outcome. However, there is content in the files extracted from the phantomdrive download. I have the latest firmware installed in the brick i.e. Firmware v1.31.
Running the test program under these circumstances produce the same exception errors.
I’m at a bit of a loss as to how to proceed.
I get a ConnectionException at “nxt.Connection.Open();” with the USB connection.
I am guessing that you are on Windows – correct?
Based on your earlier post – I guess that you are able to send commands to the NXT over Bluetooth (since you can get the motor moving)… Have you connected the sensors correctly? Senosr 1 is a touch sensor and sensor 2 is the sonar sensor…. If you are on Windows then you need to install the phantom driver to get USB working. Are you able to download programs with the software that comes with your Mindstorms set?
Anders
You are right about my system being a Windows OS, XP SP3 to be precise… sorry about having you guess information that I should have provided up front.
I’ve verified that I have phantom driver 1.1.3 installed and I have subsequently proceeded to set brick in update mode (depress recessed reset button at back of NXT brick) at which point I download firmware v1.31, using software that came with the Mindstorm set and as well, with the Mindstorm interface, I can control motors via USB and bluetooth and read battery level.
When I run Monobrick from Visual Studio, I’ve noticed that when I search for bluetooth connection from brick to my laptop I’m assigned COM8. However, when I establish bluetooth connection the other way around i.e. from laptop to brick I’m assigned COM7. I only get motor action over when I use COM7 in the following command in the test program:
“var nxt = new NXTBrick(“com7″);”.
Somewhere in the setup documentation I thought I saw an example in which COM6 and COM7 were the established com ports. Is this so?
Great that you got things working. Concerning the comport – that will vary from computer to computer – so there is no way to know what it will be… When you pair you device with the PC you will have to notice what port to use.
Anders
I may have identified the port over which I can send commands to the brick. However, the problem remains that I can’t read any information from the brick, without throwing an exception.
Moreover, if I try connection directly with a USB cable, I get a ConnectionException at “nxt.Connection.Open();”. So it’s not completely resolved, though we’re getting there.
By the way, I’ve ensured that Senosr 1 is connect to a touch sensor and that sensor 2 is connected to a sonar sensor.
Let me rephrase my question above for specificity.
When using USB connection, what would cause a ConnectionException at the line “nxt.Connection.Open();” ?
namespace Application
{
class Program
{
static void Main(string[] args)
{
var nxt = new NXTBrick(“usb”);
nxt.Connection.Open();
…
…
…
}
}
}
I just downloaded and installed the test-project on a windows 7 64bit machine and everything is working fine.. Do you have a reference to libusbdotnet dll in your project? Also are you able to use the MonoBrick Remote? I think it could be related to Windows XP… what version of .net framework are you using?
What do you actually mean by:
When I run Monobrick from Visual Studio, I’ve noticed that when I search for bluetooth connection from brick to my laptop I’m assigned COM8. However, when I establish bluetooth connection the other way around i.e. from laptop to brick I’m assigned COM7. I only get motor action over when I use COM7 in the following command in the test program:
“var nxt = new NXTBrick(“com7″);”.
MonoBrick does not let you do a seach… you speficy the comport yourself
Thanks for clarifying my doubts regarding the comport selection.
Yes the project of the test program does have a reference to libusbdotnet.dll and the libusbdotnet.dll file appears along side the MonoBrick.dll file in the project directory.
I’ve downloaded and installed the “mono-2.10.9-gtksharp-2.12.11-win32-0.exe”, and the GTK# for .NET. I have Microsoft .NET Framework 1.1, 2.0, 3.0, 3.5 and 4.
The MonoBrickRemote.exe does not generate an error when run, however, it does nothing observable.
Ok just to clarify – you can use the Remote without trouble – both USB and Bluetooth? But when you compile and run the test project from visual studio you are get the errors described above?
When I run MonoBrickRemote.exe, the program runs (I can see it listed in Windows Task Manager under “processes”) but nothing happens… no user interface appears. Therefore I can’t access the Remote neither can I compile and run the test project.
… compile and run the test project from visual studio.
Have you downloaded version 1.3.1 that contains the latest bug fix? You need to run the exe file from a folder that contains the two dll’s contained in the download… If this does not work place the exe file and libusbdotnet.dll in the GTK# bin folder!!!
Ok so you can compile the program?
Is it possible for you to try it out on vista, windows 7 or windows 8
Wrt your suggestion:
“You need to run the exe file from a folder that contains the two dll’s contained in the download”,
I answer that that was how I had the files when I ran the program and no user interface appeared.
Wrt your suggestion:
“… If this does not work place the exe file and libusbdotnet.dll in the GTK# bin folder!!!”
I got the user interface to appear however, there’s a ‘fail to open connection’ error when I try linking via USB I get an ‘invalid NXT reply’ from error.
The test program compiles without problem. However, it generates exception errors when run as follows:
* With USB connection a ConnectionException
* With Bluetooth connection I get motor control, but when attempting to depress keys ’1′ and ’2′ to read data from the brick, I get exception errors as well.
It won’t be that easy for me to do so,… but I’ll try to see what I can do.
On second thought that might be the quickest solution. I’ll keep you posted.
Thanks!
Please let me know how it goes
Hi there Anders
Upgraded to Windows 7. Everything works as expected! This is so cool!
Thanks for the work and the support… Regards
Great that you finally got things working… Glad I could help