Do you want to code advanced tasks for your Lego Mindstorms Ev3 brick? If your answer is yes, you are definitely in the right place! In this tutorial you will learn how to set up your environment to start to program with C# and Monobrick Ev3 Firmware Library.
Things to know
The Lego Mindstorms Ev3 brick has a very nice feature: it comes with a microSD port that you can use to boot the brick with a new firmware without permanently affecting your beloved device. This means that you can always revert to the factory settings by just removing the microSD and then power cycling the Ev3 brick.
There are several firmware projects around to choose from if you don’t want to rely only on the original Lego Mindstorms graphical programming tools. The projects I have explored so far are:
- Lejos, is a set of well documented Java libraries to program the Ev3 brick, the NXT brick and the RCX brick using Java.
- Ev3Dev is a Debian Linux-based operating system that runs on several Lego Mindstorms compatible platforms including the Lego Mindstorms Ev3 and Raspberry Pi-powered BrickPi. Several languages are available to choose from including C#.
- The Monobrick Ev3 firmware library is firmware replacement that allows you to program and debug the Ev3 brick using the open source .Net Framework called Mono.
If you are a C# fan, then the right projects are Ev3Dev and Monobrick. In this tutorial I will focus on the Monobrick Ev3 firmware library.
Oh, and why this tutorial despite the all documentation available on the Monobrick website? Because I thought helpful to have all the steps in just one single page.
This tutorial assumes you are using a Windows 7 Pro or later operating system.
Things to have
To follow and complete this tutorial you will need:
- The Lego Mindstorms Ev3 brick.
- A microSD card of the appropriate size. Currently you can choose between 512Mb, 2Gb or 4Gb due to the firmware replacement images available.
- A microSD reader for your PC.
- A Microsoft Visual Studio 2015 or later edition. You can download the Community Edition which is essentially free from this link.
- A USB cable from your computer to the Ev3 brick
- Optionally a WiFi dongle. I am currently using this one.
Things to do
Now let’s proceed with the actual steps.
Step 1 – Get the firmware replacement
First, download the firmware image from the Monobrick.dk website. You need to download the firmware image suited for the microSD card you have:
- For a 512Mb microSD card use this link.
- For a 2Gb microSD card use this link.
- For a 4Gb microSD card use this link.
Step 2 – Prepare the microSD with the firmware replacement
- Unpack the appropriate file for your microSD you have downloaded at step 1. If you do not have an application to unpack the compressed image, you can download and install the open source archiver 7-zip.
- Download and unpack Win32 Disk Imager. You need this application to create the bootable microSD with the Monobrick firmware.
- Plug the microSD into the reader.
- Run Wind32 Disk Imager as administrator. Open the unpacked firmware image and write it to the microSD. Remember that during this process all files the microSD will be lost, so backup them first.
- Once the process is complete, you can unplug the microSD from the reader and plug it into the Ev3 brick.
- Turn on the Lego Mindstorms Ev3 brick.
- If everything went smooth, you should see the Monobrick logo on the LCD screen of your Ev3 brick. Don’t forget that if you want to revert to the original Lego Mindstorms Ev3 firmware, you just need to remove the microSD and reboot the brick.
Step 3 – Connect your PC to the Ev3 brick
Using a USB cable to connect your PC to the Ev3 brick is pretty straightforward: the connection will be set up by means of the RNDIS protocol. This means that the Ev3 brick will be accessible at the IP address 10.0.1.1 with the Ev3 brick emulating an Ethernet TCP/IP device on the USB port.
Just connect the Ev3 brick to the PC with USB cable. Then update the RNDIS driver using the device manager and manually choose Microsoft Corporation/Remote compatible NDIS device.
It’s time to check that the process is completed with success: open a terminal and ping the address 10.0.1.1. If you get an answer, then the connection is up and running.
The following pictures show the procedure, although in Italian.
Step 4 – Get the Monobrick firmware library
To write a program with C# and Monobrick, you need to download the Monobrick Firmware Library which comes as a .dll file. There are several options:
- Get the source files from the original repository on GitHub and compile them yourself. This repository is managed from the Monobrick Firmware library authors.
- Get the compiled .dll from the Monobrick.dk website.
- Get the compiled .dll from Smallrobots.it website (recommended). This last option will let you download the compiled .dll obtained from this forked library. As explained in this post, this version has the PlaySoundFile() bug resolved, so it’s worth to give it a try.
Step 5 – Write a program with C# and Monobrick
Now we are ready to program with C# and Monobrick.
- Open Microsoft Visual Studio.
- Create a new Visual C# Console Application.
- Add a reference to the MonoBrickFirmware.dll obtained at Step 4.
- Copy and paste the following HelloWorld code in the Program.cs file, and compile it.
static void Main(string args)
// Welcome messages
LcdConsole.WriteLine("* SmallRobots.it *");
LcdConsole.WriteLine("* Hello World *");
LcdConsole.WriteLine("* Press *");
LcdConsole.WriteLine("* Escape to quit *");
// Turn on the Green Led(s)
// Busy wait for user
bool escapeButtonPressed = false;
// Actively checks for Escape button pressed
escapeButtonPressed = (Buttons.ButtonStates.Escape == Buttons.GetKeypress(new CancellationToken(true)));
// Turn off the Led(s)
Step 6 – Transfer the program to the Ev3 brick
Copying the compiled program to the Ev3 brick goes through the same procedure regardless of the connection media: using the USB cable or a WiFi connection does not change the process, just the IP address to connect to will be different.
Download and install a SCP client like WinSCP. Open a connection to your Ev3 brick (remember that the IP address to connect with the USB cable is 10.0.1.1). Use the following credentials:
- Userid: root
- Password: blank (no password)
You can ignore the security warning messages.
Last thing to do: copy all the content of your output folder (debug or release) to the /home/root/apps/HelloWorld folder on the Ev3 brick.
Step 7 – Start the program
From the top menu, click Programs/HelloWorld/Run Program to launch the program. You should see the picture shown on the right on the LCD of your Ev3 brick. Click the Escape button to get back to main menu.
Congratulations! You have just learnt the steps to follow to program with C# and Monobrick. Have a look at the Robots page to check out other projects you can try.
If you liked this tutorial, please rate it 5 stars! Thank you!
And don’t forget to comment with questions or suggestions!