Program with C# and Monobrick

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:

Step 2 – Prepare the microSD with the firmware replacement

  1. 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.
  2. Download and unpack Win32 Disk Imager. You need this application to create the bootable microSD with the Monobrick firmware.
  3. Plug the microSD into the reader.
  4. 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.
  5. Once the process is complete, you can unplug the microSD from the reader and plug it into the Ev3 brick. 
  6. Turn on the Lego Mindstorms Ev3 brick.
  7. 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. 

If you have a WiFi dongle compatible with the Ev3 brick, you can use the Monobrick menu system to select the WiFi network to connect to, provide the WiFi password if needed, and check that the connection is up and running pinging the Ev3 ip address. You don’t need to follow the procedure to update the RNDIS driver in this case.

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:

Step 5 – Write a program with C# and Monobrick

Now we are ready to program with C# and Monobrick.

  1. Open Microsoft Visual Studio.
  2. Create a new Visual C# Console Application.
  3. Add a reference to the MonoBrickFirmware.dll obtained at Step 4.
  4. Copy and paste the following HelloWorld code in the Program.cs  file, and compile it.

 

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

Hello World Program with C# and Monobrick
There is what you see on the LCD display of the Ev3 brick for this Hello World Program with C# and Monobrick

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.

Next steps

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!

Set up your PC to program with C# and Monobrick
Tagged on:

7 thoughts on “Set up your PC to program with C# and Monobrick

    • January 3, 2019 at 9:13 pm
      Permalink

      Hello Rani,

      Thank you for asking. Could you please try to be more specific?
      I will be glad to help.

      Regards,
      Riccardo.

      Reply
  • February 14, 2019 at 1:06 am
    Permalink

    Hey, me again.
    I do have the following problem:
    When I compile my program with VisualStudio, I’m getting an error saying “Exception during execution” when I try to run it via the Brick.

    However, when I write the same Program with MonoDevelop (on my Raspberry Pi) everything works perfectly.

    Also when I run the VisualStudio program via SSH, everything works perfectly.
    That’s pretty confusing to me.
    I will use Xamarin/MonoDevelop if there’s no other solution, but I would prefer to use VisualStudio.

    The program is pretty simple:

    using System;
    using MonoBrickFirmware;
    using System.Threading;
    using MonoBrickFirmware.Display;

    namespace Ev3_framework4
    {
    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine(“Hello World from Visual Studio”);
    Thread.Sleep(10000);
    }
    }
    }
    EDIT:
    Using Mono on my pc also does not work.
    It could be beacuse of Mono and Visual Studio creating a .pdb file instead of a .mdb file in bin/debug. The Mono version on my Raspberry Pi, which I made the working version with, creates such a .mdb instead of the .pdb.

    EDIT 2:
    I don’t know what happened, but it’s working right now with my Visual Studio version using the Brick.

    Reply
    • February 17, 2019 at 10:54 am
      Permalink

      Hello Kim,

      The line Console.WriteLine(“Hello World from Visual Studio”) will not print the string on the LCD Display of the Ev3 Brick, but only just on the Console of Visual Studio. Replace this line with:
      LcdConsole.WriteLine("Hello World from Visual Studio");
      With this you should get the string printed on the LCD display of the Ev3 Brick, however nothing will be printed on the Visual Studio Console window.

      Let me know!

      Ciao,
      Riccardo.

      Reply
  • February 14, 2019 at 1:08 am
    Permalink

    EV3 LCDConsole Error
    Using the LCDConsole.WriteLine function
    Exception during execution An error occurs.
    I do not know what caused it.

    Reply
    • February 14, 2019 at 1:10 am
      Permalink

      Source

      using MonoBrickFirmware.Display;
      using System.Threading;

      namespace ConsoleApp1
      {
      class Program
      {
      static void Main(string[] args)
      {
      LcdConsole.WriteLine(“Hi”);
      Thread.Sleep(5000);
      }
      }
      }

      Reply
  • February 14, 2019 at 7:10 am
    Permalink

    Using the LCDConsole.WriteLine function
    Exception during execution An error occurs.
    I do not know what caused it.

    Reply

Leave a Reply