SparkFun have an excellent range of accelerometer breakout boards for hobbyists, mostly based on the ADXL3xx series of chips. The vast majority of these are happy to run at 2.4V-5.25V and so can easily be plugged directly into the Arduino. Unfortunately the ADXL335, which I believe to be the newest and cheapest chip, does not accept any higher than 3.3V.


Opening a basic serial connection to the Arduino in Matlab is not difficult and is documented. Data is sent to the device using the fprintf() function and received using fscanf().

s = serial('COM3', 'BaudRate', 9600);
fprintf(s, 'Hello World!');
line = fscanf(s);

If we have data continually being sent by our device there are two ways of capturing it. We can create a loop within Matlab to constantly check if any data has been received however this doesn’t seem like brilliant coding practise. Alternatively we can make use of the Matlab’s BytesAvailable event and read the input buffer only when data is received.

Unfortunately, unlike languages like C++, Matlab is only able to pass variables by value and not by reference. So if we create an array in our workspace to hold the received data, our callback function will be unable to update it. However in Matlab it is possible to nest functions within the body of another. The variables defined in the outer function will be accessible to the inner function.

% Calling the function myFunc will display the number 10.
% Demonstrates that nested function has access to variables.
function myFunc

    myVar = 10;


    function innerFunc


The code to establish a serial connection is written into a Matlab function which also contain an array of all received data. The callback function of the BytesAvailable event is defined as a nested function and hence  can add to the data array when new data is received. This allows us to perform analysis on the data in parallel to it being read.

The full sourcecode of my function is shown below:
Read the rest of this entry »

There are plenty of tutorials explaining how to make an LED turn on for a pre-set period of time using the Arduino. However I have come across none which allow us to set the delay time using serial communication. As it turns out, getting an integer value from serial input is a little more complex than one might first think. To understand why, we must first understand how serial communication works.

Serial transfer is where we send each bit of data sequentially, one after another. The Arduino stores each byte (8 bits) it receives into its input buffer array. When we call the function, we grab one byte form the input buffer. So, if we send the number 100 we will read a one followed by a zero followed by another zero. To make things a little more confusing, each number will be ASCII encoded. We must convert each received byte to an integer and then join each individual integer into one value.

After a few attempts to concatenate integers,  I came across this insightful post on the Arduino forums. The following code is adapted from the code suggested by user PaulS.

value = 0;

while (Serial.available() > 0)
  value *= 10;
  value += ( - '0');

We start by initializing an integer to zero, this will hold our read value. Within the while loop, the value is multiplied by 10 to shift the place value of the digits to the left. In line 6, we subtract the ASCII value of character zero (48) from the input byte (which we know is ASCII encoded) and hence obtain a numeric value. It should be noted that each number is placed in the units column and then shifted to the left each time another value is added.

I found that at a baud rate of 9600, the Arduino was executing the run loop faster than the serial bytes were being received. As a result if I sent the number 100, the while loop only ran once and so the value was set to 1. The one ms delay gives the Arduino time to receive the next byte.

Full source code is provided below:
Read the rest of this entry »

If, like me, you find yourself struggling to remember the colour codes for resistors, then you need the ResistorCC app for the iPhone. Unfortunately the free version doesn’t have reverse lookup though this isn’t much of a limitation. Check out the screenshot below…

Read the rest of this entry »

The Arduino

10th February 2010

Arduino Duemilanove

So I have finally succumbed to temptation and bought myself an Arduino Duemilanove from eBay. Having previously played around with AVRs, the range of microprocessors which power the board, they seem extremely easy to use. I’m still playing around with the device at the moment, but expect updates soon!