Arduino to Matlab – Read in sensor data!
Matlab is by far the best software I have ever seen when it comes to plotting data and showing it in a visual format. I figured it would be awesome if I could get Arduino and Matlab to work together!
Here’s the code that I came up with that works reasonably fast. It doesn’t wait for the buffer to be filled to then store it to a variable. Here, it is asynchronous communication.
This is a basic screenshot of what you will get, minus the green stars.
Here is the code. Let me know if you use, it would be neat to see what everyone would come up with!
*Note: The code highlighting for Matlab was buggy, so the below is using C code highlighting. Once you paste it into Matlab, it will be fine.
-
% Basic Arduino and Matlab
-
% Communication with an external hardware device
-
% ———————————————-
-
%
-
% Erin Kennedy
-
% Jan. 18, 2010
-
%
-
-
-
clear all; clc; close all;
-
-
-
% Try-catch is to prevent Matlab from crashing when the program is finished
-
try
-
-
-
% Initialize serial port
-
s = serial(‘/dev/tty.usbserial-A4001lVG’);
-
%set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
-
set(s,‘BaudRate’, 9600);
-
set(s,‘DataBits’, 8);
-
set(s,‘StopBits’, 1);
-
fopen(s);
-
s.ReadAsyncMode = ‘continuous’;
-
-
-
% Various variables
-
numberOfDatas = 50;
-
data = zeros(1, numberOfDatas);
-
i = 1;
-
-
-
% Main graph figure
-
figure(1);
-
hold on;
-
title(‘Incomming Data from External Device’);
-
xlabel(‘Data Number’);
-
ylabel(‘Analog Voltage (0-1023)’);
-
-
-
% Start asynchronous reading
-
readasync(s);
-
-
-
while(i<=numberOfDatas)
-
-
-
% Get the data from the serial object
-
data(i) = fscanf(s, ‘%d’);
-
-
% Plot the data
-
figure(1);
-
plot(i, data(i), ‘m*’);
-
-
% Ensure there are always 10 tick marks on the graph
-
if(i>10)
-
xlim([i-10 i]);
-
set(gca,‘xtick’,[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
-
end
-
-
% Draw and flush
-
drawnow;
-
-
%Increment the counter
-
i=i+1;
-
-
-
end
-
-
-
% Give the external device some time…
-
pause(3);
-
-
return;
-
-
catch
-
-
% Some of these crash the program – it depends. The serial port is left
-
% open, which is not good.
-
stopasync(s);
-
fclose(s); % bad
-
%delete(s);
-
%clear s;
-
-
fprintf(1, ‘Sorry, you"re going to have to close out of Matlab to close the serial port\n‘);
-
-
return
-
-
end
The code for the Arduino is this:
-
//
-
// BubbleBoy -> Matlab
-
// ——————-
-
//
-
// Read LDR data, print them to Serial (where Matlab will receive them)
-
//
-
-
int LDRpin = 0;
-
-
void setup() {
-
-
Serial.begin(9600);
-
pinMode(LDRpin, INPUT);
-
-
}
-
-
void loop() {
-
-
int photocellReading = analogRead(LDRpin);
-
-
Serial.println(photocellReading, DEC);
-
delay(200);
-
-
}
Enjoy, happy matlabbing!
MANOI in 2010 and beyond +
2009 was a pretty good year in terms of getting MANOI to do stuff! I figured out how to make MANOI write, walk, and dance around and be on TV. I have a pretty good template for programming MANOI too, and it seems quite rugged. 2009 also gave me the chance to learn the limitations of this robot, that being the knee servos.
I was always under the impression that “great” humanoid robots can walk, run, and kick very well, basically being the best robots in RoboCup. MANOI will never be a RoboCup robot, since the knees are too weak and the stronger knees cost $300 a piece. I realized this a few months ago, admitting it is sort of harder
But, MANOI can be an interactive robot and do funny things… so in 2010, I’m going to focus more on the appearance and movements of MANOI. I want it so that the person interacting with MANOI can become attached to it, feeling that it exhibits an actual persona, instead of a programmed robot. I really feel that this was evident in MANOI’s Holiday Xtravaganza, but it still needs some improvements.
The first few steps would be to decorate MANOI’s head with two ears and two eyebrows. I’m considering antennae instead of eyebrows, because I don’t really want MANOI to come off as an uncanny human, but rather a friendly alien. I already have the two servos needed for the ears, so I’ll probably do those first.
Getting various sensor data will also be key in making MANOI more sociable. Currently there are three sensors on MANOI- the IR sensor, the gyro, and a LDR which is used for shaking hands. I’m thinking that those sensors alone are going to useful enough, for the time being.
It might be cool to have MANOI communicate back to a Mac so that the computer could produce audible results (like my Robbie the Robot science fair project).
There’s lots of possibilities out there, so this is pretty much a blog post of my scrambled thoughts.
2010 will be interesting, for sure!
MANOI’s Holiday Xtravaganza
MANOI on Discovery Channel – Daily Planet!

On December 17th, MANOI made its TV-screen ‘debut’ to gazillions of viewers in Canada! Daily Planet, a news style of a show for DIY’ers and scientists, featured MANOI’s holiday Xtravaganza warmup in the PlanetYou clip!
You can see the clip online by clicking here. It’s after the iPhone App part.
It was pretty awesome to see MANOI on TV, and a great way to ring out the year. WOOT!
The actual version of MANOI’s Holiday Xtravaganza will be posted soon
Friday Night Robotics * 3 – MANOI’s Holiday Xtravaganza Warmup
This weekend really gives patronage to how challenging the sport of robotics is (Technically it’s not a sport since it’s not in the Olympics), and how frustrating everything can be!!!!
Last year, MANOI barely showed off any holiday spirit. Just a seemingly 2D stocking sticky-tacked onto its head.

This year I really wanted to improve on that because… well, I don’t really have a reason. I just wanted to. I wanted to see what it would be like for MANOI to ring bells
The decorating started off with just a hat, followed by the hunt to find jingle bells that are a circle, not a bell. My mom eventually figured out the type of bell that I was looking for, and bought me some festive bells and bows!
Et voila, MANOI! C’est magnifique! (Tada, MANOI is magnificent!)

I spent an enormous amount of time getting various actions set up so that I can string them together into a final product. In total, there are 7 different movements. My favourite of which, called ‘tango’ can be seen in the video below where one of MANOI’s arms is outwards, and another is inwards, perpendicular to its front shell with the bow. Surprisingly enough, its only two motion frames:
-
int tangoHeadTilt = 400;
-
int tangoHandTilt = 200;
-
int tangoHandIn = 120;
-
int tangoHandUp = 500;
-
int tangoArmUp = 500;
-
int tangoArmTilt = 600;
-
int tangoHipTilt = 200;
-
int leanBack = -25;
-
-
int tango_keyframe1[17] = {
-
HOME0-tangoHeadTilt,
-
HOME1+tangoHandTilt,
-
HOME2-tangoHandIn,
-
HOME3-tangoHandUp,
-
HOME4,
-
HOME5+tangoArmUp,
-
HOME6+tangoArmTilt,
-
HOME16+tangoHipTilt,
-
HOME17+leanBack,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21-tangoHipTilt,
-
HOME22+leanBack,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
int tango_keyframe2[17] = {
-
HOME0+tangoHeadTilt,
-
HOME1,
-
HOME2+tangoArmUp,
-
HOME3+tangoArmTilt,
-
HOME4+tangoHandTilt,
-
HOME5-tangoHandIn,
-
HOME6+tangoHandUp,
-
HOME16-tangoHipTilt,
-
HOME17+leanBack,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21+tangoHipTilt,
-
HOME22+leanBack,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
void tango(int repeat) {
-
for(int i=0; i<repeat; i++) {
-
setFrame(tango_keyframe1, 800, 50);
-
setFrame(tango_keyframe2, 800, 50);
-
}
-
}
It’s weird because tango looks way more complicated than a movement that has 6 frames, the ‘both arm jingle’. This is where MANOI jingles both of its arms, so one is back and the other is forwards, then the hands both jingle.
-
int bothArmJingle_frame1[17] = {
-
HOME0+200,
-
HOME1,
-
HOME2+leftArmOut,
-
HOME3+leftArmForward,
-
HOME4,
-
HOME5+rightArmOut,
-
HOME6+rightArmForward,
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
int bothArmJingle_frame2[17] = {
-
HOME0+200,
-
HOME1-leftWristMovement,
-
HOME2+leftArmOut,
-
HOME3+leftArmForward+50,
-
HOME4-rightWristMovement,
-
HOME5+rightArmOut,
-
HOME6+(rightArmForward-170),
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
int bothArmJingle_frame3[17] = {
-
HOME0+200,
-
HOME1+leftWristMovement,
-
HOME2+leftArmOut,
-
HOME3+leftArmForward,
-
HOME4+rightWristMovement,
-
HOME5+rightArmOut,
-
HOME6+rightArmForward,
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
int bothArmJingle_frame4[17] = {
-
HOME0-200,
-
HOME1,
-
HOME2+leftArmOut,
-
HOME3-leftArmForward,
-
HOME4,
-
HOME5+rightArmOut,
-
HOME6-rightArmForward,
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
int bothArmJingle_frame5[17] = {
-
HOME0-200,
-
HOME1-leftWristMovement,
-
HOME2+leftArmOut,
-
HOME3-(leftArmForward+50),
-
HOME4-rightWristMovement,
-
HOME5+rightArmOut,
-
HOME6-(rightArmForward+50),
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
int bothArmJingle_frame6[17] = {
-
HOME0-200,
-
HOME1+leftWristMovement,
-
HOME2+leftArmOut,
-
HOME3-leftArmForward,
-
HOME4+rightWristMovement,
-
HOME5+rightArmOut,
-
HOME6-rightArmForward,
-
HOME16,
-
HOME17,
-
HOME18,
-
HOME19,
-
HOME20,
-
HOME21,
-
HOME22,
-
HOME23,
-
HOME24,
-
HOME25
-
};
-
-
-
void bothArmJingle(int repeat) {
-
-
for(int i=0; i<repeat; i++) {
-
-
setFrame(bothArmJingle_frame1, 500, 0);
-
for(int i=0; i<5; i++) {
-
setFrame(bothArmJingle_frame2, 20, 0);
-
setFrame(bothArmJingle_frame3, 20, 0);
-
}
-
setFrame(bothArmJingle_frame4, 500, 0);
-
for(int i=0; i<5; i++) {
-
setFrame(bothArmJingle_frame5, 20, 0);
-
setFrame(bothArmJingle_frame6, 20, 0);
-
}
-
-
}
-
-
}
Crazy right? I’ve found that in most of my other MANOI motions, the movement was very focused, with no other moving parts. So I made sure to make the most things move at once.
I think it gives off a more exciting vibe.
Check out the video!
It really wasn’t as easy as the video makes it look. There were a lot of times where various movements wouldn’t match up, so they would catch the balance off-guard. Also, one of MANOI’s ankle servos went crazy on me, so it let the other foot down, causing a fall.
It’s super challenging to fix this because when MANOI falls, there are at least two things that temporarily break. You fix those things, but while you’re fixing them, other things break. While you’re trying to fix the new things, you can’t get a good grip on the robot because its entire body is made up of servos, and if you set the robot down, another thing will break. I would be willing to wager that working in no-gravity is a cinch compared to this!
Once everything is working again, you go and fix the motion. Fixing the motion is a whole separate routine of trial and error- you change some of the values of the servos and see what happens. There are two options for the next step, either have super fast reflexes, or only program with one hand on the keyboard. Since macs are not very one hand friendly, I have to have super fast reflexes to turn the power switch off on MANOI so that it won’t fall. If it does fall, you have to go back and fix everything.
In any case, once the motion is fixed, you go back and try it with the other movements. Just your luck, the battery runs out! MANOI falls, and you have to fix everything again.
It’s so unbelievably frustrating!!
Especially doing it for 3 days straight! There have been three times where MANOI has fallen off the table, about 1 meter (3 feet) tall. The risk of this is MANOI getting shocked by me, the air, and the carpet! It’s really nerve wracking, but thankfully MANOI is alright. Here’s a video of all of MANOI’s falls from the various videos that I tried to take:
At the end of the day, I really enjoy seeing MANOI move around. I think of it this way: it is my frustration and patience that has given an otherwise inanimate object LIFE. How cool is that?
There will be more videos of MANOI in its holiday gear showing up soon, with more blinky lights too!






