The post is about interfacing Arduino with MatLab via Bluetooth module
1). First of all, connect the Arduino with Bluetooth module as per the diagram given below.
Vcc to 5V
GND to GND
RXD to RX
TXD to TX
2). Now you need to upload code to the Arduino board. The code depends on which values you need to send from Arduino or receive to Arduino through MatLab.
Example code for receiving the values from Arduino is
int a1=0;
int a2=0;
int val=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
a1=analogRead(0);
a2=analogRead(1);
if (Serial.available() >0) {
/* whatever is available from the serial is read here */
val = Serial.read();
{
if(val==10)
{
Serial.println(a1);
}
else
if(val==20)
{
Serial.println(a2);
}
}
}
}
Burn this code to the Arduino board.
3). Then pair your Bluetooth device to the computer.
4). Open MatLab and find your Bluetooth device detected as follow
>> instrhwinfo('Bluetooth')
for more information about your device use this
>> instrhwinfo('Bluetooth','HC-05')
create a Bluetooth variable and open it as follow
>>b = Bluetooth('HC-05',1);
>>fopen(b);
After this for getting the two values regularly from Arduino use this code
>>while(1)
fwrite(b,10);
q=fscanf(b,'%d');
fwrite(b,20);
z=fscanf(b,'%d');
end
yes that's all, you will get regular values from Arduino
Remove infinite while loop if you want these values once
>>fwrite(b,10);
>>q=fscanf(b,'%d');
>>fwrite(b,20);
>>z=fscanf(b,'%d');
You can modify this code or make your own code according to your needs.
***if Bluetooth module is not sending and receiving data from Arduino then exchange the tx,rx connections
RXD to RX --> RX to TX
TXD to TX --> TX to RX
***
Thank you for reading.
Ask if any questions in the comments.
//////////////////////////////////////////////////////////////////
My free android apps.
https://play.google.com/store/apps/developer?id=Gaur+Tavm
Thank you.
1). First of all, connect the Arduino with Bluetooth module as per the diagram given below.
Vcc to 5V
GND to GND
RXD to RX
TXD to TX
2). Now you need to upload code to the Arduino board. The code depends on which values you need to send from Arduino or receive to Arduino through MatLab.
Example code for receiving the values from Arduino is
int a1=0;
int a2=0;
int val=0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
a1=analogRead(0);
a2=analogRead(1);
if (Serial.available() >0) {
/* whatever is available from the serial is read here */
val = Serial.read();
{
if(val==10)
{
Serial.println(a1);
}
else
if(val==20)
{
Serial.println(a2);
}
}
}
}
Burn this code to the Arduino board.
3). Then pair your Bluetooth device to the computer.
4). Open MatLab and find your Bluetooth device detected as follow
>> instrhwinfo('Bluetooth')
for more information about your device use this
>> instrhwinfo('Bluetooth','HC-05')
create a Bluetooth variable and open it as follow
>>b = Bluetooth('HC-05',1);
>>fopen(b);
After this for getting the two values regularly from Arduino use this code
>>while(1)
fwrite(b,10);
q=fscanf(b,'%d');
fwrite(b,20);
z=fscanf(b,'%d');
end
yes that's all, you will get regular values from Arduino
Remove infinite while loop if you want these values once
>>fwrite(b,10);
>>q=fscanf(b,'%d');
>>fwrite(b,20);
>>z=fscanf(b,'%d');
You can modify this code or make your own code according to your needs.
***if Bluetooth module is not sending and receiving data from Arduino then exchange the tx,rx connections
RXD to RX --> RX to TX
TXD to TX --> TX to RX
***
Thank you for reading.
Ask if any questions in the comments.
//////////////////////////////////////////////////////////////////
My free android apps.
https://play.google.com/store/apps/developer?id=Gaur+Tavm
Thank you.
It's not working :(
ReplyDeletecode need to be changed according to logic...
Deletewhere problem is coming?
Hello,
ReplyDeleteThanks for your walk-through, it's very useful. When I write something to the bluetooth, for example "10" via fwrite(b,10) the arduino receives 255. This seems to be the same no matter what I send. How do I fix this so that the arduino receives exactly what I send it?
Your help would be very much appreciated.
ps: I am using an Arduino Uno with a BlueSMiRF Gold bluetooth modem.
There may be synchronization problem.
ReplyDeleteas serial devices has serial buffer which stores serial values till they are not used so it provides previous values that are not used.
So please make sure proper use of serial.available()
May be this will help you :)
I can't get it to work the other way arround, matlab -> BT is working fine but the arduino's not receiving any data (no rx nor tx is turn up), what could be the problem?
ReplyDelete**********connect arduino rx to BT tx....and BT tx to arduino rx...actually what bt sends arduino receives and what ardiuno sends bt receives....this might be the problem...try this
DeleteWarning: Unsuccessful read: A timeout occurred before the Terminator was reached.
ReplyDelete//put your code in this
Deleteint value=0;
int value1=0;
void setup()
{
Serial.begin(9600);
Serial.println(10);
}
void loop()
{
if (Serial.available() >0) {
value1 = Serial.read();
if(value1==60)
{
Serial.println(value);
}
}
}
This is matlab code...try this one
Deletes = Bluetooth('HC-05',1);
fopen(s);
disp('Connected');
a=fscanf(s,'%d') ;
while(1)
fwrite(s,60);
q3=fscanf(s,'%d');
if q3==1
disp('1')
else if q3==2
disp('2')
else if q3==3
disp('3')
clock
else if q3==4
disp('4')
end
end
end
end
end
Hi
ReplyDeleteI have the same problem, and I change the arduino's code and matlab's code, bur the problem continues, what I can do?
Greetings from Colombia
try to run simple codes to find where problem is occuring in communication.First try connecting using usb cable,Same code with little change in variable and COM PORT of your ardiuno.
Deleteb = serial('COM16','baudrate',9600);
fopen(s);
disp('connected');
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached.
ReplyDeletethis message shows after this command q=fscanf(b,'%d');
i can't find a solution and your reply answer to someone here who complain from this error too can't help me
try to run simple codes to find where problem is occuring in communication.First try connecting using usb cable,Same code with little change in variable and COM PORT of your ardiuno.
Deleteb = serial('COM16','baudrate',9600);
fopen(s);
disp('connected');
Read my other blog
ReplyDeleteMessage to the Future
halo, thanks for tutorial, but I am still confused, why my matlab code cant run arduino use bluetooth
ReplyDeletethis is my arduino code
int EN1 = 6;
int EN2 = 5; //Roboduino Motor shield uses Pin 9
int IN1 = 7;
int IN2 = 4; //Latest version use pin 4 instead of pin 8
void Motor1(int pwm, boolean reverse)
{
analogWrite(EN1,pwm); //set pwm control, 0 for stop, and 255 for maximum speed
if(reverse)
{
digitalWrite(IN1,HIGH);
}
else
{
digitalWrite(IN1,LOW);
}
}
void Motor2(int pwm, boolean reverse)
{
analogWrite(EN2,pwm);
if(reverse)
{
digitalWrite(IN2,HIGH);
}
else
{
digitalWrite(IN2,LOW);
}
}
void setup()
{
int i;
// for(i=6;i<=9;i++) //For Roboduino Motor Shield
// pinMode(i, OUTPUT); //set pin 6,7,8,9 to output mode
for(i=5;i<=8;i++) //For Arduino Motor Shield
pinMode(i, OUTPUT); //set pin 4,5,6,7 to output mode
Serial.begin(9600);
}
void loop()
{
int x, delay_en;
char val;
while(1)
{
val = Serial.read();
if(val!=-1)
{
switch(val)
{
case 'w'://Move ahead
Motor1(255,true); //You can change the speed, such as Motor(50,true)
Motor2(255,true);
break;
case 'x'://move back
Motor1(255,false);
Motor2(255,false);
break;
case 'a'://turn left
Motor1(100,false);
Motor2(255,true);
break;
case 'd'://turn right
Motor1(255,true);
Motor2(100,false);
break;
case 's'://stop
Motor1(0,false);
Motor2(0,false);
break;
}
}
}
}
and this is my matlab code:
s = Bluetooth('HC-O6',1);
fopen(s);
q = fread(s,'w')
Undefined function or variable 'w'
please help me.
thank you very much
big hug from Indonesia
must use, if (Serial.available() >0) , in arduino code.this will ensure when data is available in serial register only then inside code will run.
Deleteand in matlab code we use fread( ) to read a file, so use fscanf() as per tutorial.
I think that will help.
I was working on a project in which I want to control a avr based Bot using a MATLAB based application via a bluetooth connection. I succesfully made the application to connect with the bot using a bluetooth module (HC-05). The problem rises is with sending commands to the robot in order to make that move in various directions. I am using Atmega8 in the Bot.
ReplyDeleteHere is the code-
AVR code-
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x0C;
ACSR=0x80;
SFIOR=0x00;
DDRB=0xFF;
while(1)
{
char data;
while(!(UCSRA & (1<<RXC)))
{
data=UDR;
if (Data=='F')
{
PORTB=0b00001010;
}
else if (Data=='B')
{
PORTB=0b00000101;
}
}
}
MATLAB Code-
instrhwinfo('Bluetooth','HC-05')
b = Bluetooth('HC-05',1)
fopen(b)
fprintf(b, 'F') %-- on execution of first command button
fprintf(b, 'B') %-- on execution of second command butoon
"fwrite()" command plays role for both sending and receiving data.Try it.
DeleteCan we use this code to read ECG signal and send it via bluetooth to matlab? We are not using high speed rate, it is 200. Do we have to read one analogread and then send it, or analogread a set of data and then send it like packages of data via bluetooth?
ReplyDeleteThis code is used for sending data, you can modify it send any data you want.
DeleteHi
ReplyDeleteIs com16 is for Bluetooth.???
Fixed for all pc.???
No
DeleteIf you still have issue with Bluetooth and Arduino in general, one piece of information I haven't seen, is that we are assuming the Bluetooth module is 5V, many of them are 3V modules. Your Arduino TX has to be dropped to 3V level, and the main V+ for teh bluetooth, I will submit another wiring drawing ASAP to support this.
ReplyDeleteI found this link helpful, hope it helps.
Deletehttp://www.plastibots.com/wordpress/wp-content/uploads/2013/03/BluetoothHookupWeb.png
excuse me sir, this program means arduino no need to connect to computer by using USB port anymore, right?
ReplyDeletelike i can turn on/off led by using matlab on my laptop but, arduino and led is like a meter away from my laptop
Exactly
DeleteDisconnect the Bluetooth device and reconnect it.. Do this every time you execute the matlab code.. This works for me, it should work for u as well
ReplyDeleteHi, I'm trying to read data wiressely from an LSM9DS0 3-axis gyroscope using Arduino MEGA 2560 to MATLAB using the HC-06 bluetooth module.
ReplyDeleteThis is the error I'm getting:
Warning: Unsuccessful read: A timeout occurred before the Terminator
was reached.
I'm using one computer that is connected to the Arduino MEGA 2560 with the Bluetooth HC-06 and LSM9DS0 wired to it.Then the other computer has MATLAB running code to read the gyroscope data wirelessly.
This is my MATLAB code:
%% Interfacting w/ HC-06 Bluetooth Module
% Pair with your bluetooth device before continuing
% Lists available Bluetooth devices
instrhwinfo('Bluetooth');
% Create a bluetooth variable and open it at channel 1
device = Bluetooth('HC-06', 1);
% Pair with device
fopen(device)
%% Write command to HC-06/Arduino
fwrite(device, 'c'); % Prompts lists to do more commands
read = fscanf(device, '%d')
fwrite(device, 'h'); % Makes LED low
read = fscanf(device, '%d')
fwrite(device, 'a'); % Read from accl
%% Reading and Analyzing Values from HC-06
data = [];
for count = 1:10
read = fscanf(device, '%d')
data = [data; read]
end
final = str2double(data);
% Matrix manipulation here
%% Clear Bluetooth Object when Finished
fclose(device);
clear('device')
I have one question.
ReplyDeleteNow, I must connect Bluetooth to Matlab.
So you declared variable "b = bluetooth('HC-06', 1);
But, I received error (Error using Bluetooth (line 100)
Cannot Create: Please make sure Bluetooth adaptor works properly.)
What's the problem?
HC 05/06 works on serial communication. The Android app is designed to send serial data to the Arduino Bluetooth module when a button is pressed on the app. The Arduino Bluetooth module at the other end receives the data and sends it to the Arduino through the TX pin of the Bluetooth module (connected to RX pin of Arduino). The code uploaded to the Arduino checks the received data and compares it. If the received data is 1, the LED turns ON. The LED turns OFF when the received data is 0. You can open the serial monitor and watch the received data while connecting.
ReplyDeleteI want to apply this code in the Matlab program which supports Arduino programming please help me
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second (baud) for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data and store it into variable data
Serial.print(data); //Print Value inside data in Serial monitor
Serial.print("\n"); //New line
if(data == '1') //Checks whether value of data is equal to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') //Checks whether value of data is equal to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}
}
Thanks for posting... your post is really helpfull. . . keep posting such nice and well written post .. .
ReplyDelete