Arduino + Xbee + MATLAB – wireless data transfer over serial

Recently, I fabricated a crude prototype of the buoy’s sensor circuit board using perfboard and soldering connections to wire the inertial measurement unit (IMU), GPS , RGB LEDs, and Xbee to the Ndogo microcontroller. All the connections have been tested and the individual modules all function correctly so the next step is to write code for the microcontroller that will pull data from each of sensors in a specific sequence and time interval before transmitting the data to a base station wirelessly. For preliminary testing the base station will be my laptop but the concept can easily be scaled for longer ranges.

photo 1 (1)

Xbee Series 1 module for easy wireless communication over serial.

Currently, I am using  two Series 1 Xbee modules for simple point to point communication. These are the simplest type of Xbee radios to use since they only allow for direct communication between two modules (like older walkie-talkies with a single channel) but eventually these Series 1 radio modules could be swapped out for the more powerful Pro Series 2 units to provide longer transmission range and more sophisticated mesh networking with multiple buoys communicating to each-other. I am still learning about Xbees and their protocols but if you are interested in using these  little units in your own projects a great reference is Rob Faludi’s book Build Wireless Sensor Networks. Since the book was published in 2010, the Xbee manufacturer Digi has released a new version of the software XCTU which is used to configure the Xbees. The software can now run on Windows, Mac, and Linux, making it really easy to reconfigure the radios. Previously it was only available on Windows making it difficult/impossible to reconfigure the more advanced Xbees without a Windows machine.

photo 2 (2)

Xbee mounted on Buoy circuit board.

After configuring the two Xbees to talk to each other, you can wire the units directly to the RX and TX pins on an Arduino to send data wirelessly using the Serial.print(). For testing, I mounted one Xbee to a breadboard using an adapter PCB (the pin spacing on the Xbees is smaller than the conventional 0.1″ pin spacing) and wired the Xbee to the RX and TX on an Adafruit Trinket Pro 3.3v running at 12MHz.  I powered the breadboard with 3xAA batteries and ran the regulated 3.3V from the Trinket Pro directly to the Xbee (warning – don’t use 5V to power the Xbee).

Xbee wired to Trinket Pro 3.3V 12MHz with battery pack. IMU not wired yet.

Xbee wired to Trinket Pro 3.3V 12MHz with battery pack. IMU not wired yet.

A second Xbee module was wired to my laptop using this adapter board the FTDI Serial TTL-232 USB cable. Following the instructions from I was able to configure the Arduino Trinket Pro to send data wireless over the Xbees and then import analogRead( ) values directly into the program MATLAB. Now, instead of simply monitoring the outputs from the Arduino over the SerialMonitor, I can stream the data in almost real time and save the values on my laptop memory where the data can be manipulated and plotted. MATLAB is great for performing matrix operations and manipulating large arrays of numbers so it will be  much easier to work with the accelerometer and gyro data in this way and implement FFTs or signal processing.

The tutorials from are great, and provide step by step instructions for writing the code and getting the Arduino and Xbee to communication wirelessly with your laptop. You have to modify the code slightly depending on if you want to use an Xbee or Arduino to connect to the USB port on your computer, but they provide instructors for both setups. Once of their tutorials ever goes over visualization of your acceleration data to create 3D vector plots and continuous streaming of the data signals. I hope to implement some of these visualization codes in the buoy project to help during development of the wave height measurement codes for easier debugging. If you know MATLAB and use Arduino, I would highly recommend you check out the youtube channel and look over all the different tutorial videos they offer.


13 thoughts on “Arduino + Xbee + MATLAB – wireless data transfer over serial

    • Hi Tamay, no the code has not been uploaded yet. On a long list of things I am working on right now, and ideally I would like to post it to Github for easier management. Once I get the code uploaded I’ll be sure to include a link here. Thanks


    • Hello Tamay, thank you for the link to the paper. I stumbled across this on google a while back and it was very helpful. I did consider this type of a design, with a water-tight-cylinder inside a buoyant foam sphere, and think it would also be a very robust setup.

    • Hi James,

      Wow, awesome stuff. Bummer about the laptop battery being the limiting factor. Is the UKHAnet project more about developing the software to support a wide range of hardware or do you have a list of preferred devices?

      Would you have a recommendation for a suitable transmitter/receiver combo for this project if I would like to have nodes 0.5km apart? Also wondering if you had any thoughts on signal attenuation on the water since these buoys are fairly small. I’ve been looking at Xbee radios (simple to implement with arduino and already have mesh networking protocols) but am assuming that the transmission distance will be cut in half when used over water.


      • Yeah it was a shame that we had to stop the experiment – though we were out there long enough to get really sunburnt! UKHASnet is a protocol and idea but we’ve implemented it on the RFM69HW radio module, its easily available, can do up to 100mW and quite cheap. While we mainly use hte RFM69HW some people have got it working as well on chips rather than modules though thats quite a lot of work, there is also a RTL-SDR Software Decoder.
        Here in the UK we use 869.500Mhz so that there is interoperability but the first UKHASnet node in the US is using 434Mhz i think so its up to you. During the weekend of tests we easily managed 0.5km so the RFM69’s should work well, you are right range was limited by the sea but we tried to overcome this by having multiple nodes which would then repeat the packet via different paths as well as short packets so that bobbing on waves didn’t make too much difference.

        There are a group of us on #ukhasnet on if you want to chat more.


  1. Pingback: Visualizing vibration data from accelerometer | Open Source Ocean Data Buoy Project

  2. hello!
    i’m trying to do the same with an Xbee SERIES 1 802.15.4
    with the same matlab script, and getting errors while trying to run it.
    can you please explain me what i need to change in code
    or even share you fixed script code u use if possible?

    thanks ahead

    • Hi there,

      Sure thing, it will take me a few days to double check my code, but I’ll verify that its working and send you a copy. In the mean time, have you been able to connect the arduino to matlab over serial without the Xbee? The MatlabArduino youtube videos are great and I was able to get everything working by watching several of his videos.

      What have you tried so far?

      • Hi Yossidu, here is a link to the code that wrote to stream acceleration data to matlab over Xbee serial communication. Sorry for the delay, hope this helps….

        link to source code:

        I followed the youtube videos from MatlabArduino ( and most of the structure of the code is from his videos. I also added callback functions so that the “stop” and “serial close” pushbuttons would function properly. The “magnitude.m” function creates a connection with the arduino and then sends a “2” across serial to activate the loop that starts streaming data from to the IMU to serial. If you have more questions, please let me know and I can try to address them.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s