Tutorial: RobotC Bluetooth Examples

Bluetooth is a cool feature and it’s especially fun if you can make it work. That’s apparently a bit tricky, but it’s really not difficult if you’ve got a simple place to start. This page will show you two sets of programs that demonstrate the most basic Bluetooth example programs for two NXT with RobotC. (You can use bluetooth with NXT-G, too, but that’s not discussed here).

Set up

  • This code was tested with RobotC 3.08. Newer versions may have issues with this code.
  • Two NXT bricks
  • You can just copy paste the code from below, or download all 4 sample programs..
  • Connect the NXT’s using the menu on the NXT brick:
    • (Only the first time) Go to Bluetooth > Search
    • Go to Bluetooth > My Contacts
    • Choose the NXT that you want to connect to
    • Choose Connect.
    • The <> symbol in the top left of the screen indicates that your NXT’s are connected

Sending and receiving one value

In some occasions you want to send some basic numbers from one NXT to the other. The next examples (SenderSimple.c and ReceiverSimple.c) demonstrate the functionality to send one value from one NXT to another. I assume that your NXT’s are connected via Bluetooth (see “set up” section).

  • Transfer ReceiverSimple.c to one NXT. Use USB for this! (Bluetooth will be in use between your NXT’s.)
  • Transfer SenderSimple.c to your other NXT. Use USB for this!
  • Start ReceiverSimple.c first
  • Then start SenderSimple.c.

When you do this, the number 23 shows up on the receiving NXT. A few seconds later, 17 shows up. Got that right?

I’ve added some comments to the code to help you understand what each portion of the code does. The key is that you have to keep looking for messages on the receiver part. Think of it as emails you get from a friend. You’ll never know you’ve got a new email unless you check. But like email, messages are stored for your review until you open them. So, there’s no need to check every milisecond of the day. You won’t miss it, it will be in the mail box. The only issue here is that this mailbox will hold only one email, so old messages are overwritten when new ones come in.

If there’s no message, you’ll find 0 as the message. So, if you find something that is not 0, that’s the value that was sent by the other NXT. After storing this value in a variable for further usage, use ClearMessage(); to set the values back to 0 (erase your email inbox). If you do that, you can nicely repeat the procedure and wait for a message to come in.

Run the code and play with it a bit. Then move on to the slightly more useful programs below.

Simple Sender

task main()

   int my_number = 23;

   //This sends through one value: 23

   //Let's wait a bit and send something else
   my_number = 17;


Simple Receiver

task main()

  int my_message;

    //Read the message to variable
	  my_message = message;

	  //Let's say something got through if the signal is not zero
	  if(my_message != 0)
		  //Display the value as stored in the variable
		  nxtDisplayBigTextLine(2,"1: %d",my_message);

		  //Clear the message, set it to 0

	  //Checking for a message every half a second is
	  //fine for this simple test




Sending and receiving three values

These are essentially the same programs as above, except these will allow you to send through 3 values where the above programs would send only one value at the time. Run the programs as per the instructions for the samples above, and observe the results on the NXT screen.


task main()

  int number_1 = 5;
  int number_2 = -1;
  int number_3 = 700;

  //This sends through three values: 5, -1 and 700

  //Let's wait a bit and send something else
  number_1 = 2;
  number_2 = 4;
  number_3 = 6;



task main()
  int message_first;
  int message_second;
  int message_third;

    //Read the messages to variables
	  message_first = messageParm[0];
	  message_second = messageParm[1];
	  message_third = messageParm[2];

	  //Something got through if the signals are not all zero
	  if(message_first != 0 || message_second  != 0 || message_third != 0)
		  //display the values as stored in the variables
		  nxtDisplayBigTextLine(2,"1: %d",message_first);
		  nxtDisplayBigTextLine(4,"2: %d",message_second);
		  nxtDisplayBigTextLine(6,"3: %d",message_third);

		  //Clear the messages, set them to 0
	  //Checking for a message every half a second is
	  //fine for this simple test


This entry was posted in RobotC. Bookmark the permalink.

20 Responses to Tutorial: RobotC Bluetooth Examples

  1. Pingback: ROBOTC Bluetooth Tutorial

  2. Mike Partain says:

    I sure wish it was as easy to get messages from a brick to a PC. Any idea what corresponding LCP command(s), if any, might work with these, or do we need to resort to the regular mailbox stuff?

    • Laurens says:

      Hi Mike, I’ve successfully used this resource to communicate with the NXT brick. It works quite well – except it works better with NXT-G than with RobotC in terms of messaging.

      • Angelica says:

        I’ve never run it on a hard floor and don’t recommend it. Actually, in the book I scpiefy that, lacking shock absorbers, it should be used on a rug. In the end, it’s a rather delicate hop—certainly not anything more than experienced by the sumo-bots in competition.

    • Leonardo says:

      I downloaded Brams’s soawtfre..Like a old thing I remember on TV.’TRY IT YOU’LL LIKE IT!I tried it out with my 4 year old Granddaughter. Geeee…. She had so much fun! But my LEGO Robot’s battries ran out and she cried.. LOL.. LOL…Anyway.. I LOVED IT TOO!

  3. Pingback: ROBOTC Bluetooth Examples for the NXT at ROBOTC.net Blog

  4. leona says:

    I need some help I want to connect BT module with a NXT robot ,in other words NXT-other devices via bluetooth, just i want to know if these codes appropriate with other bluetooth chip like rn42

    • Laurens says:

      No, you’ll need different programs — if what you want to do is doable at all. There are some specific smartphone apps for android and NXT with BT.

  5. Pingback: RobotC Bluetooth Ejemplos para el NXT | Robotc México

  6. ashok says:

    i am getting two error mesages in the same statement ClearMessage();

    errors are:

    **Error**:Invalid assignment to a non-variable
    **Error**:Unexpected scanner token-> ‘0’

    • Laurens says:

      These programs are tested to work with version 3.0x. I have not tested these programs with 3.5. Could that be the problem? If so, please report that to RobotC support so they can fix it.


  7. carlos says:

    was testing this code on my robot c 3.5 and still that problem, you have a new code where we can see the connection between the nxt?

    **Error**:Invalid assignment to a non-variable
    **Error**:Unexpected scanner token-> ’0′

  8. John S. Colonias says:


    This has nothing to do with BT, but I need help to understand what it means when I am trying to run a ROBOTC
    program with functions and I get an error
    **Error**:Functions must be defined at main scope level

    Do you have an example of its meaning?

    Thanks very much for the help!


    • Laurens says:

      I’m not sure. The best place to ask would be the RobotC forums: http://www.robotc.net/forums/

    • Lex Hegt says:

      Hi John,

      I expect that you solved the problem, since you reported it a month ago.
      In case you did not yet solve it, below you find some RobotC code which shows what goes wrong:

      task main()

      // The following statement is nested in task main(), which
      // leads to the error.
      // It should be placed outside the task main()
      void notAtMainScopeLevel()


      // This statement is at the main scope level
      void atMainScopeLevel()

      Hope this helps!

  9. Ed Gatzke says:

    Thanks for the nice example! Have you ever connected from a PC to a set of bricks? What did you use?

    I have have managed to connect to 1 brick using a couple of python methodss but I can’t seem to get more than one connected…


  10. Tony says:

    It would be very useful to have a PC based controller using bluetooth, such as “NXT Vehicle Remote” or the standard remote controller, that sends signals to a brick that also interacts with sensors. For example, a PC remote controls a lego car with a proximity sensor to stop it hitting walls.

    How can this be done please?

  11. Henrique Hamisch says:


    I am trying to use more than two NXT robots with the Bluetooth communications in RobotC. I know that the Bluetooth functions of this software are optimized for only two units, but do you know how I can do that? I have read some texts about the subject, and I think the message in the functions (cCmdMessageRead, cCmdMessageWriteToBluetooth) need to be written in byte arrays, right? Can you help me?

    Henrique Hamisch

Leave a Reply

Your email address will not be published. Required fields are marked *