The Bluetooth interface in MATLAB allows you to connect to devices over the Bluetooth communication protocol and to transmit and receive ASCII and binary data. MATLAB supports the Bluetooth Serial Port Profile (SPP). You can identify any SPP Bluetooth device and establish a two-way connection with that device.
Bluetooth is an open wireless technology standard for exchanging data over short distances by short wavelength radio transmissions from fixed and mobile devices using a packet-based protocol. Bluetooth provides a secure way to connect and exchange information between devices such as LEGO MINDSTORMS NXT robots, USB Bluetooth adapters (dongles), wireless sensors, mobile phones, and computers.
Support for Bluetooth communication in MATLAB is different from Bluetooth Low Energy Communication and the functionalities supported by Bluetooth Toolbox. To understand the use cases for each feature and to decide which one best fits your needs, refer to the documentation for each.
A bluetooth object represents a connection to a Bluetooth Classic device from your computer using Bluetooth Serial Port Profile (SPP). Identify nearby devices to connect to by the "Ready to connect" status in the bluetoothlist output. You must pair the device with your computer first before connecting to it from MATLAB.
device = bluetooth(address,channel,Name,Value) creates a connection and sets additional Properties using one or more name-value pair arguments. Set the ByteOrder and Timeout properties using name-value pair arguments. Enclose each property name in quotes, followed by the desired property value.
Bluetooth device name, specified as a character vector or string scalar. Identify the name of the device you want to connect to using the information returned by bluetoothlist. This property can be set only at object creation.
Bluetooth device address, specified as a character vector or string scalar. Identify the address of the device you want to connect to using the information returned by bluetoothlist. This property can be set only at object creation.
Bluetooth device channel, specified as a positive number. Identify the channel of the device you want to connect to using the information returned by bluetoothlist. This property can be set only at object creation.
Sequential order in which bytes are arranged into larger numerical values, specified as "little-endian" or "big-endian". Set this property at object creation using a name-value pair argument. You can also change it after object creation using dot notation.
Allowed time in seconds to complete read and write operations, specified as a numeric value. Set this property at object creation using a name-value pair argument. You can also change it after object creation using dot notation.
Terminator character for reading and writing ASCII-terminated data, returned as "LF", "CR", or "CR/LF", or a number from 0 to 255, inclusive. If the read and write terminators are different, Terminator is returned as a 1x2 cell array of these values. Set this property with the configureTerminator function.
Bytes available callback trigger mode, returned as "off", "byte", or "terminator". This setting determines if the callback is off, triggered by the number of bytes specified by BytesAvailableFcnCount, or triggered by the terminator specified by Terminator. Set this property with the configureCallback function.
Number of bytes of data to trigger the callback specified by BytesAvailableFcn, returned as a double. This value is used only when the BytesAvailableFcnMode property is "byte". Set these properties with the configureCallback function.
Callback function triggered by a bytes available event, returned as a function handle. A bytes available event is generated by receiving a certain number of bytes or a terminator. This property is empty until you assign a function handle. Set this property with the configureCallback function.
Callback function triggered by an error event, returned as a function handle. An error event is generated when an asynchronous read or write error occurs. This property is empty until you assign a function handle.
An arduino object represents a connection to Arduino or Arduino-compatible ESP32 hardware. Use the arduino function to connect Arduino or Arduino-compatible ESP32 hardware to MATLAB. After you create the object, you can program your hardware and use custom add-on libraries directly from MATLAB using the Object Functions.
Use the arduino object to connect to an Arduino board over USB, WiFi, or Bluetooth, by specifying the different arguments based on the connection type. For the supported connection type for each board, see Supported Boards.
a = arduino recreates the last successful connection to the Arduino or Arduino-compatible ESP32 hardware. If there is no last successful connection or that connection failed, it creates a connection to an official Arduino hardware connected to your host computer via USB.
a = arduino(btname) uses the name of the of the supported Arduino or Arduino-compatible ESP32 hardware Bluetooth device to create a connection to the Arduino hardware. For the list of Bluetooth connection type supported hardware, see Supported Boards.
Hardware port that your Arduino or ESP32 is connected to, specified as a character vector or string array. The port can be a USB serial port. To identify the USB serial port number, see Find Arduino Port on Windows, Mac, and Linux or Find ESP32 Port on Windows, Mac, and Linux. This argument can be specified on its own if connected via USB serial port, or with a board name.
Name of the Arduino enabled board, specified as a character vector or string array. This argument must be specified with a valid serial port, IP address, Bluetooth address, or Bluetooth name. See Supported Boards for all supported boards.
For HC-05 or HC-06 devices, the Bluetooth Classic address is specified as a character vector or string array of the form btspp:// followed by the 12-digit hexadecimal value of the address. To identify the Bluetooth device address, see Pair a Bluetooth Device and Retrieve the Bluetooth Device Address.
Name of the Arduino library, specified as the comma-separated pair consisting of 'Libraries' and a character vector or a cell array of character vectors. Libraries installed by default extend the Arduino environment.
When you first connect to your Arduino hardware, your Arduino connection is created with these default libraries 'I2C' 'Servo' 'SPI'. However, if you create an Arduino connection with libraries other than the defaults, your Arduino connection will have only the libraries that you specified, even after you disconnect and reconnect your Arduino hardware.
Type of reference voltage, specified as internal or external. In the external mode, an external voltage is applied at the AREF pin. In the internal mode, the internal voltage supported by the Arduino boards is used.
In the external mode, you must specify the external voltage applied at the AREF pin using AnalogReference. In the internal mode, you can specify the internal voltage supported on the boards. If you do not specify a voltage in the internal mode, the default internal reference voltage of the Arduino boards is used.
Connect the external voltage to the AREF pin through a 5K resistor to prevent damaging the board. If AnalogReferenceMode is internal and a reference voltage is still connected externally at the AREF pin, you will short the internal reference voltage and the AREF pin, thus damaging the board.
When you change the analog reference voltage, the output of readVoltage may not be accurate. Run readVoltage until you see the expected value. For Arduino Uno and Mega boards, see Incorrect readVoltage results.
If 'ForceBuildOn' is not set as true, MATLAB does not reprogram the Arduino board and uses the existing Arduino server if the libraries that you specify match with libraries that are already on the existing Arduino server. Use this setting to update the Arduino server with different source code when you work with custom Arduino libraries.
Program log and Arduino commands log, specified as the comma-separated pair consisting of 'TraceOn' and either false or true. If you fail to connect to your Arduino board, set 'TraceOn' to true to print compilation and upload errors of Arduino IDE at the MATLAB command window. You can also set 'TraceOn' to true to print the Arduino commands executed on the Arduino board for each MATLAB command.
The rate at which information is transferred in the communication channel. For Arduino Pro Mini, and Digital Sandbox use the BaudRate value of 57600. For all the other boards listed in Find Board Name use the BaudRate value of 115200.
Name of the Arduino enabled board, specified as a character vector or string array. It is the type of Arduino hardware connected to your system. Specify the board type when you create the arduino object. See Supported Boards for all supported boards.
Installed Arduino add-on libraries downloaded to your hardware, specified as a character vector or cell array of character vectors. Check the available libraries with listArduinoLibraries to specify the add-on library you want to use.
Connect the external voltage to the AREF pin through a 5K resistor to prevent damaging the board. If AnalogReferenceMode is internal and a reference voltage is still connected externally at the AREF pin, you will short the internal reference voltage and the AREF pin thus damaging the board.
7fc3f7cf58