Solved: Initializing Bluetooth HC-05 to Arduino connection (R2018a)
1 回表示 (過去 30 日間)
古いコメントを表示
After a long debugging session and major research, this is a tip for others like me.
I tried to connect MATLAB to an Arduino Nano through a Bluetooth HC-05 link with no success for several days. The arduinosetup would successfully initialize the Arduino Nano through USB however trying to initialize the HC-05 via FTDI (don't forget to enter AT mode, slow flashing LED) would fail. Any attempt to communicate with the Nano through the BT link also fails with:
>> a=arduino('btspp://0018E4003495','nano')
Failed to open a connection at device address 0018e4003495. Make sure the port or address is correct and the
Arduino hardware is properly plugged in. For troubleshooting, see Arduino Hardware Troubleshooting.
- First Note: Setting up a=arduino(...) will NOT work unless the HC-05 is initialized!don't even try
- I have the HC-05 module connected properly to the Arduino Nano(5V, Gnd, TX(HC-05)->RX(Nano), TX(Nano)->RX(HC-05) via 1K/2K voltage divider, Both from Aliexpress)
- I verified the SPP connection using a BT terminal loopback (Shorted TX-RX on HC-05, used "Bluetooth Serial Terminal" windows app, echoed correctly any text I transmitted) Next I initialized BT in Matlab and successfully opened a port:
btInfo = instrhwinfo('Bluetooth')
btInfo.RemoteNames(1);
btInfo.RemoteIDs(1);
b=Bluetooth('HC-05',1);
fopen(b)
b
This only to prove that BT is working. It does. I measured the baud rate on an oscilloscope to be the default 9600 bps - so slow: first clue!
- I then used a COM port sniffer to see what's going on while MATLAB is trying unsuccessfully to init the HC-05.the HC-05 returned an error when MATLAB tried to (re)initialize the pairing code:
AT+PSWD=1234
->Error(1D)
whereas it SHOULD have used quotation marks:
AT+PSWD="1234"
and I couldn't figure out how to fool the application with an "OK" answer or change the AT command. Since "1234" is default anyway, I had to guess what other AT commands were NOT done yet...
- Next I used the 'scope to measure the baud rate of the Arduino AFTER arduinosetup initialization and it was 115,200 bps - so fast: second clue !
- I then used a serial terminal to MANUALLY initialize the baud rate of the HC-05:
AT+UART=115200,0,0
and now it works. note that the baud rate of the FTDI COM port (in device manager) should be 38,400 for AT mode.
It took 3 days of work to understand this! You're welcome!
Tl;dr use AT mode to manually init the HC-05 to 115200 baud rate via FTDI
0 件のコメント
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で MATLAB Support Package for Arduino Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!