MATLAB Serial Interface

So the most important aspect of the project is to be able to successfully be able to connect between the vehicle and the PC base station. There are a few way to do this but the RS232 protocol has been around for many years now and was once the industry standard for electronics communicating with each other, now there are other standards such as SPI and USB which are much faster. However this does not mean that RS232 is obsolete, far from it, it is still the most popular method for amateur electronic hobbyists to interface any of their designs with a computer. As a result there are loads of tutorials and information available on how to use this protocol. My aim was to connect between the on-board PICAXE microcontroller and the MATLAB software. Luckily both platforms have in build support for serial RS232 communication. In MATLAB you need to use the “Serial Object” function. These functions allow you to set the port, Baud rate, terminator type, stop bits…..etc. Once the serial port has been configured you can begin to send and receive serial data through it, either as strings (text) or binary (numeric).

I would be sending and receiving strings. When you receive a string in MATLAB you use string processing code which is similar to C code to extract the information you want. On the PICAXE you use two commands when dealing with serial, “serout” for sending data and “serin” for receiving data. All you need to do is specify the pin which is connected for serial, the baud rate, and the data you want to send, the protocol is fixed as 8n1 on a PICAXE (The more advanced PICAXE chips now also support USB and SPI).

Serial on the PICAXE side is very simple, so communicating between two of them is easy. However MATLAB is slightly more involved. MATLAB has extensive help resources allowing you to understand how to use most of the commands and function, but while trying to use the serial functions I still had to look through forums and on the MATLAB community, plus just figure it out myself to get it to work (don’t even get me started on making serial work within a GUI). I recommend a good terminal program to help you debug and monitor what you are actually transmitting and receiving (see software section).

So here is an example on how to set up the serial on the MATLAB side, transmitting and receiving.

Scenario: Imagine that I am sending and receiving 3 numbers separated by commas and with a terminator which is a line feed followed by a carriage reset.

Setting up the serial/comm port
%Create the serial port object (s) in MATLAB, and select comm port 6 to use
s = serial ('COM6');

%Set baude rate
s.BaudRate = 19200;

%Set the terminator to a line feed followed by a carriage reset
s.Terminator = 'LF/CR';

%Open the serial port for use
fopen (s)
%use the fprintf command function, specify the serial object, then write your
%string, MATLAB will automatically add the terminator to the end of the
%Use fscanf function specifying the serial object to read in the string and save it
%into "data".
data = fscanf (s);

%Use the sscanf command to process the "data" string and input the numbers as
%3 actual numeric values NOT ASCII characters. Save it into "data2"
data2 = sscanf (data, '%i,%i,%i' );
Ending the session

You must properly close the serial session when finished using it otherwise further access to the port will be blocked.

%close the port, delete the object, and clear "s"
fclose (s)
delete (s)
clear s

So the above example is very similar to what I first did with the serial functions. I was transmitting the values of the X, Y and Z axes from the accelerometer to MATLAB via a PICAXE. The numbers are then processed and converted to G-force readings and displayed visually on a bar chart figure.


I am moving the accelerometer around and you can see the forces on each axis. Initially when the accelerometer was flat the Z axis is at 1000 mg i.e. 1G, which is what we would expect.

The MATLAB code used to produce the above video is here aserial as a.txt file.