Wireless Control with Bluetooth Low Energy 4.0
It’s becoming harder and harder to justify not incorporating either Wi-Fi or Bluetooth within any project or design. Fortunately, the process of including Bluetooth Low Energy 4.0 (BLE) in your designs is fairly straightforward. This article presents a wireless BLE control system for an electric longboard.
Consumer devices can easily be categorized as those with wireless capabilities and those without. Today, it’s hard to justify not incorporating either Wi-Fi or Bluetooth in a design. The good news is that it’s now possible to customize virtually any design to include Bluetooth Low Energy 4.0 (BLE).
As part of our capstone project at Camosun College, we designed a wireless control system for an electric longboard. We implemented BLE connectivity using Sparkfun’s BLE Mate 2. It was so easy we want to share what we now know so you can build your own Internet of Things (IoT) device!
In this article, we’ll explain how we used a simple yet robust protocol to transmit throttle, brake, and battery information across a secure Bluetooth connection using the BLE Mate 2. The transmitted data enables a wireless hand controller to communicate with a Texas Instruments LaunchXL-F28069M C2000 development kit and control a brushless DC motor, thereby regulating the longboard’s speed.
BLE MATE 2
SparkFun’s BLE Mate 2 (BC118) is a low-cost, low-profile, high-performance BLE transceiver module featuring the BlueCreation BC118. Measuring only 25 mm × 50 mm and a few millimeters tall, the BLE Mate 2 will fit easily into most projects. As well, the BLE Mate 2 has the flexibility to work within your specific needs by operating from 3.3 to 4.7 V. Lastly, due to the nature of BLE communication, the BLE Mate 2 has a low average current usage of only 16 mA, giving you many hours of operation with standard 500-mAh batteries. SparkFun provides both the necessary information to design a project for the BLE Mate 2, along with virtually all the required support documentation to implement the device.
The BLE Mate 2 uses UART for communication with its host device. To configure the BLE Mate 2 with a PC, we used a DLP Design USB-232M USB adapter, which uses an FTDI FT232M USB-UART chip and enables the user to communicate with the BLE Mate 2 using a terminal application interface.
The BLE Mate 2 ships configured for 3.3-V logic. To physically connect the BLE Mate 2 with the DLP module, we consulted the USB-232M’s datasheet and implemented a bus-powered 5-V system with a 3.3-V logic interface configuration. We used the schematic from the datasheet as a guide with the BLE Mate 2 acting as the microcontroller (see Figure 1). The circuit was connected on a bread board.
— ADVERTISMENT—
—Advertise Here—
The circuit schematic shows the DLP-USB232M module connected to the BLE Mate 2. In our application, the DLP module is connected in a bus-powered 5-V system to configure the parameters on the BLE Mate 2. The BLE Mate 2 is powered by the 3.3-V output of the DLP module.
For configuration, the flow control pins were not used and only TX and RX were connected. The BLE Mate 2 was powered using 3.3 V from the DLP module, which was powered with 5 V from the USB connection with the PC. Once the configuration circuit is connected, open a terminal program such as PuTTY or TeraTerm. Select the correct COM port for the DLP module. (It will appear as “USB Serial Port (COMx)” in the Windows Device Manager. “x” will be the number of the port to which the device is connected.) The default serial parameters for the BLE Mate 2 device are: 9,600 bps, 8 data bits, 1 stop bit, no parity, and flow control disabled. Power cycle the device and you should see a message like the one in Figure 2.
The BLE Mate 2 “Start Up” message lets you know that your device is ready to be configured.
The BLE Mate 2 uses the BC118 Bluetooth module running BlueCreation’s Melody Smart firmware. The Melody Smart User Manual contains a comprehensive overview of the device and should be consulted for other options or commands. For our application, we were only concerned with a few commands, some of which are listed in Table 1. Note that the BLE Mate 2 looks for a carriage return (\r) only after each command. If a carriage return and line feed (CRLF) are sent, the device will respond with an “ERR” message. Make sure that your terminal program is configured for a carriage return only to ensure proper communication. This option can be found in the terminal program Setup/Settings menu.
These basic commands are used to properly configure the BLE Mate 2. Use the WRT command to store the parameter settings into memory and then the RST command to restart the device. Check the parameter settings using the CFG command.
Commands to change parameter settings are sent to the BLE Mate 2 using the following format:
set command=(parameter 1)(parameter 2) \r.
For example, to set the device to Central mode, the command would be entered as:
set cent=on\r.
If you set up the terminal correctly, the carriage return will be appended to the message when you press the Enter key. If the command is received properly by the device, it will respond with an “OK.”
For a command to be stored in the device, it needs to be written into the memory. This is achieved by sending the WRT command after the desired parameter has been updated. To finalize the setting procedure, a restart is required. This is done by sending the RST command.
To display all the device’s current settings, type the CFG command and press Enter. The BLE Mate 2 will respond with a list of all the current configuration parameters followed by an “OK” response (see Figure 3).
This terminal screen capture shows some of the configuration parameters that you may want to modify.
Consulting the tables from the Melody Smart User Manual and the response from the BLE Mate 2 (see Figure 3), you can interpret the current device configuration and then decide which changes to make. This device is currently configured as a “Peripheral” device (CENT=OFF), which is not “Bonded” (ADDR=000000000000) and will accept connections from any device. Finally, we see that the device is set at a “Transmission Rate” of 9,600 bps (UART=0028), “Auto-Connection” is disabled (ACON=OFF), “Constantly Connectable” is enabled (CCON=ON), and “Transparent Mode” is disabled (TRSE=OFF).
Prior to configuring the BLE Mate 2 for a particular application, you need to decide on how you want the two paired BLE Mate 2 devices to interact with each other. Bluetooth devices can be configured for different roles depending on the connection needs for the application. There are four main roles for a Bluetooth device; but for the purposes of this article, we will focus on just two: Central and Peripheral. When configured for Central role, this device acts as a host device and is responsible for scanning and initiating connections with other devices. A Peripheral acts as a slave device that advertises its capabilities and establishes connections with Central devices.
— ADVERTISMENT—
—Advertise Here—
At this point, the user needs to decide on how they want their Bluetooth device to function. If the device is to be paired with a smart phone, for example, then it would be appropriate to configure the device for a Peripheral role and let the smart phone handle to Central role tasks. We will show configuration for a Central role in this example as the BLE Mate 2 comes set as Peripheral. For a device to have a Central role, the “CENT” parameter must be initialized. To do this, the command CENT=ON is sent to the device followed by a write (WRT) and finally a Reset (RST). After the device reboots, the CFG command should be used to ensure the parameter has been successfully changed (see Figure 4).
Next, decide if you want the device to connect to only one device or multiple devices. The parameter associated with this functionality is the ADDR=xxxxxxxxxxxx parameter, where xxxxxxxxxxxx represents the 12-digit MAC address of the device to be bonded to. When the BLE Mate 2 has a MAC address set in the ADDR field, the device is “bonded” to that address. The BLE Mate 2 will only connect to the device bearing that address and reject all connections from any other devices. After the SET ADDR=xxxxxxxxxxxx command is sent and written, confirm the appropriate MAC address will be bonded by reviewing the BLE Mate 2 configuration with the CFG command (see Figure 5).
This terminal screen capture shows device bonding by having a specific MAC address in the address field (ADDR).
The final parameter we will show how to configure is “TRSE.” This parameter controls the Transparent Mode. When the device is configured for Transparent Mode, data that is present at the device’s RX pin is directly forwarded across the Bluetooth channel and the device essentially mimics the function of traditional UART lines. Before entering Transparent Mode, it is advisable to enable “ACFG” and “GPIO” parameters so that if the user wants to exit transparent mode it can be done by using the GPIO as described in the Melody Smart User Manual.
To enable Transparent Mode, simply send the command TRSE=ON followed by the write and reset commands. Again, confirm the parameter has been successfully established by sending a CFG Command (see Figure 6). Failure to check configuration settings at each step may lead to an improperly configured device.
This terminal screen capture shows Transparent Mode enabled (TRSE = ON). This means that all data received by the BLE Mate 2 is directly transmitted out the RXO pin, mimicking traditional UART lines.
When the BLE Mate 2 is set to Transparent Mode, the use of flow control is recommended to improve the reliability of the link. In our application, we elected to use software flow control within the Texas Instruments microcontroller.
TEXAS INSTRUMENTS UART
To send and receive the data through the BLE module, we had a processor at both ends configured to transmit data via UART. For the longboard, we chose the Texas Instruments LaunchXL-F28069M C2000 development kit because our overall design had us controlling a mounted three-phase brushless DC motor.
An application like ours requires user safety to be paramount. We needed to ensure that the board and controller were in constant communication and would predictably respond to various scenarios and situations. The TI C2000 development kit allows for many different configurations. As you can see in Figure 7, many peripherals can be configured. For our electric longboard we needed to be able to send commands wirelessly from the hand controller to the motor to control the motion of the board (acceleration and braking). We also wanted to tell the hand controller various status conditions like battery levels.
A screen capture showing the peripherals that may be configured on the Texas Instruments LaunchXL-F28069M C2000 development kit.
Perhaps most importantly, the mounted motor controller was constantly monitoring the UART receive. If too much time passed without receiving any data, the connection was assumed to be lost and the board would apply a gentle brake to stop.
Since the BLE module is designed to allow users to transmit a UART signal via Bluetooth, we needed to set the TI C2000 kit up do some sending and receiving. If you prefer synchronous serial data communication, there are Bluetooth devices that can support SPI or CAN interfaces like the Adafruit BLE SPI Friend.
UART CONFIGURATION
TI’s LaunchXL-F28069M is specifically designed for brushless DC motor control applications. They provide Code Composer project files through a software package called Motorware. This allows you to get your motor spinning and customize the software for your application using TI’s Code Composer IDE. If you are working with a different microcontroller or another application of BLE, there are many examples online for setting up UART. However, if you are working towards a motor control application using the TI LaunchXL-F28069M C2000 development kit, we highly recommend working through all the Motorware lab examples as that would initialize all the essential peripherals.
For our project, we decided to configure the UART in the following way: 9,600 bps, 8 data bits, 1 stop bit, and no parity. Setting the UART transmission rate to 9,600 bps was fast enough to give us smooth control of the board while minimizing the number of errors associated with higher baud rates. We configured the receive side to be interrupt driven to ensure the motor microcontroller would prioritize signals received from the wireless hand controller. We concluded that transmitting data back to the wireless hand controller didn’t need to be interrupt driven, but could transmit at a fixed rate every few seconds.
The code snippet shown in Listing 1 is an example of how to configure the LaunchXL-F28069m UART peripheral to have the specifications we needed. The Motorware project examples come pre-loaded with the header files to make this work as well as insuring that the corresponding peripheral clocks and interrupts are setup ahead of time.
Listing 1
This code snippet shows how to configure the UART with an interrupt triggered with any received data.
void HAL_setupSCI(HAL_Handle handle)
{
HAL_Obj *obj = (HAL_Obj *) handle;
// SCI stop bit, parity, loopback, char bits, idle/address mode
SCI_setNumStopBits(obj->sciaHandle, SCI_NumStopBits_One);
SCI_disableParity(obj->sciaHandle);
SCI_disableLoopBack(obj->sciaHandle);
SCI_setMode(obj->sciaHandle, SCI_Mode_IdleLine);
SCI_setCharLength(obj->sciaHandle, SCI_CharLength_8_Bits);
// TX enable, RX enable, RX ERR INT disable, SLEEP disable, TXWAKE disable (SCICTL1 = 0x03)
SCI_disableRxErrorInt(obj->sciaHandle);
SCI_disable(obj->sciaHandle);
SCI_disableTxWake(obj->sciaHandle);
SCI_disableSleep(obj->sciaHandle);
SCI_enableTx(obj->sciaHandle);
SCI_enableRx(obj->sciaHandle);
// TXINT disable, RXINT enable
SCI_enableRxInt(obj->sciaHandle);
SCI_disableTxInt(obj->sciaHandle);
// SCIH-SCIL BAUD - SCI_BAUD = (LSPCLK/(SCI_BRR*8)) - 1
SCI_setBaudRate(obj->sciaHandle, SCI_BaudRate_9_6_kBaud);
// Reset SCI
SCI_enable(obj->sciaHandle);
// enable SCI interrupt
PIE_enableInt(obj->pieHandle, PIE_GroupNumber_9,PIE_InterruptSource_SCIARX);
// enable CPU interrupt
CPU_enableInt(obj->cpuHandle, CPU_IntNumber_9);
}
To enable the read and write operations, the function calls shown in Listing 2 were used. These functions are defined within the SCI (serial communication interface) header file provided as part of the Motorware project files. For our application, the read function (to receive data) was called within the receiver interrupt subroutine and the write function (to transmit data) was called within the main program loop.
Listing 2
Code snippet showing the functions for reading the received data and writing the data to transmit.
static inline uint16_t SCI_read(SCI_Handle sciHandle)
{
SCI_Obj *sci = (SCI_Obj *)sciHandle;
// get the data
uint16_t data = sci->SCIFFRX;
return(data);
} // end of SCI_read() function
static inline void SCI_write(SCI_Handle sciHandle, char data)
{
SCI_Obj *sci = (SCI_Obj *)sciHandle;
// write the data
sci->SCITXBUF = data;
return;
} // end of SCI_write() function
With the configuration of your UART completed, and your read/write functions defined, you can now hook up the BLE modules to your microprocessors and give it whirl! The only thing you really need to do to connect the devices together is to connect the TX and RX pins and ensure the microcontroller and BLE device share a common ground. One thing to keep in mind when connecting the pins is to remember the transmit pin of one device will connect to the receive pin of the other device. It’s easy to overlook this but try to keep it in mind when connecting everything.
WRAPPING IT UP
Initially, you might feel overwhelmed with the idea of getting your wireless devices up and running. But with a good design and access to resources from SparkFun (for the BLE Mate 2) and Texas Instruments (for the TI C2000 development kit), wireless connectivity is now easier than ever to implement. An electric longboard is but one example of how to take advantage of low energy Bluetooth devices. Your imagination is really the only limitation to what you can do here.
— ADVERTISMENT—
—Advertise Here—
RESOURCES
Blue Creation, “Melody-Smart 2.6.0 Manual Rev. E,” https://cdn.sparkfun.com/datasheets/Wireless/Bluetooth/Melody-Smart-V2_6_0-UserManual-Rev_E.pdf.
DLP Design, “DLP-USB232M-G-USB User’s Manual,” 2002, www.dlpdesign.com/usb/dlp-usb232mv16.pdf.
Sparkfun, “BC118 BLE Mate 2 Hookup Guide,” https://learn.sparkfun.com/tutorials/bc118-ble-mate-2-hookup-guide.
SOURCES
DLP Design DLP-USB232M-G-USB Module
DLP Design, Inc. | www.dlpdesign.com
BC118 BLE Mate 2
SparkFun Electronics | www.sparkfun.com
C2000 Piccolo LaunchPad
Texas Instruments | www.ti.com
PUBLISHED IN CIRCUIT CELLAR MAGAZINE • JUNE 2016 #311 – Get a PDF of the issue
Sponsor this ArticleJim Mercer graduated from Camosun College’s Electronics and Computer Engineering Technology – Renewable Energy program in Victoria, BC. Jim spent 6 years in the Navy before school.
Kris Gordon graduated from Camosun College’s Electronics and Computer Engineering Technology – Renewable Energy program in Victoria, BC. Kris was a European car technician before his interest in electronics drove him into pursuing a career in the field.
Phil Vreugdenhil graduated from Camosun College’s Electronics and Computer Engineering Technology – Renewable Energy program in Victoria, BC. Phil taught high school physics and math in Canada, Turkey, and Mexico before returning to school to upgrade his skills.