How to connect matlab with Bluetooth HC 05
63 views (last 30 days)
Show older comments
Hi, iam wildan. im a mechatronics student. wanti to know about how to connect Matlab (simulink) with bluetooth device in microcontroller. So, i can control microcontroller use matlab wireless. the bluetooth model is HC 05. thanks for your answer.
2 Comments
Alexander Slovak
on 6 Apr 2012
I'm trying to do this for two months!!!
But get only errors:
Error evaluating registered method 'Start' of MATLAB S-Function 'sserialcb' in 'bttest/Serial Configuration'.
Open failed: Port: COM4 is not available. Available ports: COM4.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Model with one block 'Serial Configuration' connect correctly, but when I add serial send or serial recieve block - I get an error again.
Adel Djellal
on 20 Apr 2017
hello
I think you used inadequate 0 you must configure as follows:
>> B = Bluetooth('HC-05', 1);
>> fopen(B);
Answers (4)
Lu
on 15 Nov 2012
Hi guys,
Here's the solution:
HC-05 is actually a bluetooth SPP device, so we cannot use traditional serial function in matlab, because its only support "real" COM port.
However, matlab give us a similar lib named "bluetooth" :
You can apply ccde as below to access the HC-05:
1. first make sure you havn't change the device name of you HC-05, default is "HC-05", you can check it at your bluetooth Properities menu at Control Panel.
2. Code
%creat a bluetooth object
%HC-05 channel default is 1
b = Bluetooth('HC-05',1);
fopen(b);
%write and read function
fwrite(b,Bluetooth_Write,'uchar');
Bluetooth_Read=fgets(b);
%close and clear
fclose(b);
clear(b);
3. try and enjoy
Best,
5 Comments
H W
on 23 Jun 2022
Edited: Walter Roberson
on 24 Jun 2022
Communicate with HC-06 over Bluetooth
This example shows how to communicate with a HC-06 Bluetooth® module using Serial Port Profile.
Hardware Requirements
Windows® 10 or macOS machine with built-in or external Bluetooth 2.0 compatible adapter
HC-06 Bluetooth module
5 V source, such as an FTDI converter or Arduino® board
A few wires
Hardware Setup
This example uses a HC-06 Bluetooth transceiver module configured as a loopback device. On the HC-06 module:
Connect the VCC pin to the 5 V source.
Connect the GND pin to the ground.
Connect the TXD pin to the RXD pin.
A blinking LED on the HC-06 module indicates that the device is powered. Next, pair the device on the host computer to allow connection in MATLAB®. The default PIN code is 1234 unless configured otherwise.
Get Device Information
Identify the Bluetooth device address or name and SPP channel number by calling the bluetoothlist function. Make sure the device status is "Ready to connect", indicating that it is powered on and paired. If you already know the device information, you can skip this step.
bluetoothlist
Connect to Device
Use the device address or name and SPP channel number to connect to the device by calling bluetooth. Specify the device name if it is unique or specify the device address.
hc06 = bluetooth("HC-08", 1)
Write and Read Data
After you connect the HC-06 to MATLAB, write byte data or string data to the device by calling write.
write(hc06, 1:10);
write(hc06, "helloworld");
Since the device is configured as a loopback, the data received on the device is immediately returned to MATLAB. Read the data by calling read.
read(hc06, 10)
read(hc06, 10, "string")
For information on sending and receiving terminated string data, see readline and writeline.
Collect Data Asynchronously
You can use a callback function to read data as it is written to the Bluetooth device. Call configureCallback to read and collect data each time five bytes are sent by the device and received in MATLAB.
function collectData(src, evt)
% Read received data and store it in the UserData property on the bluetooth object
src.UserData = [src.UserData; read(src,src.BytesAvailableFcnCount)];
end
configureCallback(hc06, "byte", 5, @collectData);
Write data to the device. The data is immediately sent back to MATLAB, triggering the callback function to read and display the data.
write(hc06, 1:20);
% Wait for all callbacks to be processed
pause(1);
disp(hc06.UserData);
Disconnect from Device
Clear the device object when you are finished working with it.
clear hc06
Copyright 2020 The MathWorks, Inc.
0 Comments
Hung BV
on 28 Oct 2013
Hi guys,I'm trying to do this use lib "bluetooth" but it not work I used command instrhwinfo('bluetooth') to check the bluetooth device in my laptop, and it return the resuls the name is 'HC-05' so i sure that the device name of my Blutooth device is 'HC-05'. Then i use the command: b = Bluetooth('HC-05',1); fopen(b);
There is an error, it say that the device already in use, but i sure that no device is connected to my HC05
Anyone can help me!
Thanks for your answer!
2 Comments
Jose andres Ardilla
on 21 Oct 2016
cierre el programa de arduino y mire que este bien escrito el nombre del bluetooh como aparece en (instrhwinfo('Bluetooth','HC-05'))
espero te sirva
Borja Roca Romero
on 5 Apr 2018
Hi, I tried this but I still have the same issue as Hung BV, fopen(b) does not work for me. I'm sure nobody else nor my applications r using the device.
liang kai-min
on 15 May 2017
My Bluetooth device type is HC-05. If I use bluetooth function, it is necessary to set the baudrate?
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!