How to program the Mindstorms NXT robots to communicate over Bluetooth Pro Preview

We are showing the basics of communication between LEGO Mindstorms NXT Bricks using a Bluetooth connection. In the next few minutes we will build two programs - one for the master and one for the slave brick, while explaining the essentials for building such programs.

  • #37
  • 25 Aug 2013
  • 11:59

By the end of the video tutorial you would have established a connection between two Mindstorms NXT bricks. On each brick display there will be an image that you can move using NXT buttons. Moving the image on one of the bricks will be communicated over Bluetooth to the other brick where the image will also be moved. Try it!


Bluetooth icon, Thomas Le Bas, from The Noun Project


In this tutorial we'll get familiar with the basics of the Bluetooth communication using the Mindstorms NXT brick. Bluetooth allows the robots to communicate with each other and cooperate in that way. We'll build a simple program that will illustrate the possibilities we have using Bluetooth.

Let me explain what we will try to build in this tutorial. By the end of the tutorial, we'll have two bricks - paired and communicating using Bluetooth. On their screens there will be an image which I'll move using the left and right NXT buttons of the first brick and the image will move on both screens. OK. After we have cleared our vision about the program, let's start programming. The Bluetooth communication in the Mindstorms series is master-slave. This means that if we have two bricks, one is the master and the other one - the slave. The master brick can only send signals while the slave brick can only receive signals. So, in our case we'll have two separate programs: one for the master brick and one for the slave brick. Let's take a look at the slave brick program. I've divided it into two separate tasks which are executed simultaneously. In the first one we receive the coordinates of the image from the other brick and in the second one we visualize the image on the screen according to these coordinates. Now, let's take a look at the master brick program. Here we have three separate tasks. One for sending the current coordinates to the other brick, or the slave brick. Then we modify the current coordinates using the left and right NXT buttons and, finally, we visualize the image on the screen according to these coordinates. Let's take a look at the visualization part of the program. It's one and the same for the master brick program and the slave brick program. Here we have one loop which is repeated forever. Then, inside it, we read the value of the X variable and give it to the display block. In the X variable we will store the current coordinates of the image on the screen. Let's download the program and see what will happen when we run it. Now let's get back to the master brick program. In this My Block we modify the current coordinates of the image. So, first we initialize the X variable at value 0. Then we have a loop which is set to forever and inside it we modify the X variable according to whether the left, the right or no buttons are pressed. So, we have one switch for the right button and one for the left. If the right button is pressed, we add 5 to the X variable. If the left button is pressed, we subtract 5 from the X variable. If no button is pressed, we continue repeating the loop. So, let's again get back to the master brick program. The last things are the Send My Block and Receive My Block which are actually responsible for the Bluetooth communication between the two bricks. But before getting into the Bluetooth communication I'd like to show you some of the blocks responsible for this communication. We start the program and the logo is shown on the screen but it is not moving. Now we are going to get back to programming in order to make it move. Let's open a new program. We have three blocks which are responsible for the communication between the two bricks via Bluetooth. The first one is located under the Action tab and is called "Send message". Then, under the sensor tab, we have the block "Receive message" and under the advanced tab we have the block "Bluetooth connection". In the Bluetooth connection block we can turn on or off the Bluetooth of a given brick and also we can initiate or close connection with another brick. In "Initiate connection" we have to give the name of the brick we want to connect to and on which port we want to connect to. A very important thing to know when you want to initiate connection is that the two blocks should be paired. This can be done using the menu on the brick and I'll show you how to do this in a moment. Another important thing is that if you have already initiated connection and then run the program again and then try to initiate connection again, in most cases the Bluetooth connection will fail. So, I prefer to initiate the connection manually and leave the communication to the program. OK. Now let's take a look at the other two blocks. In the first one we can send a message to another brick by indicating on which connection it is - 1, 2, 3; by indicating whether we want to send some text, number or logic (boolean value True or False) and to which mailbox. Because each connection has 10 mailboxes and you can send simultaneously up to ten messages.

OK. If we expand the block, we can connect numbers, text, number of the mailbox, etc. In the Receive Message Block, we can compare the message we have received and also, if we expand it, we can take this Boolean variable which will be True if there is a message received and False otherwise. OK. If we get back to the Send My Block, things are quite simple. We have this loop which is again repeated forever and we send the value of the X variable to the brick on connection 1. We say that we send a number and we send it to mailbox 1. After we have sent the value we wait for 0.05 sec. This is because otherwise we'll send the values too fast and the other brick will fail. So, now let's get back to the slave brick program. The only thing left is the Receive My Block. We have this Receive Message Block inside it from which I get the Boolean value showing whether the message is received or not. If the message is received, I use a switch block to store the value of the message in the X variable. Otherwise, I do nothing and continue repeating the loop. As I said, the two bricks should be paired before starting the program. So, I'll start both bricks; then go to the Bluetooth menu on both of them and turn on their Bluetooth.

Now both of them have their Bluetooth enabled. This is indicated by this icon. And now I'll go to the search menu of the "Favorit" brick because it will be the master brick. There I will search for the slave brick - in our case "911". Now we wait until it finds the other brick.

It has found 911, so I'll select it. Then I select Port 1. I want to establish connection between the two bricks on Port 1.

OK. Now the two bricks are paired and ready to communicate. I go to My Files and start the programs in order to check how they work.

As you see, the program works perfectly. Now I'll turn off the bricks and then start them once again. Once I have paired the two bricks, all I need to do now is to go to the Bluetooth menu of the master brick and then to My Contacts and select the slave brick (in our case 911). Select Connect and again select Port 1.

The two bricks are now connected, so I can run the program. In the end, I will run the program once again in order to be sure that everything is just fine.

OK. The program is working properly. In this tutorial we have used Bluetooth communication with two bricks only. But the Mindstorms series can communicate with up to five bricks. So, in one of the next tutorials I'll show you how you can establish Bluetooth connection with more than two bricks.