Bluetooth Low Energy, also known as BLE or Bluetooth Smart, is a low energy wireless communication protocol suitable for use in embedded systems. Available as of Bluetooth 4.1, BLE is available on nearly all modern mobile devices and is an effective way of communicating with smartphones, tablets, smartwatches, and so on. Despite the ubiquity of BLE, high quality, complete tutorials on BLE are difficult to come by. This post is intended to serve as a detailed, inclusive tutorial on BLE using the RN4020 from Microchip.
Parts, Software, and Tools:
For this tutorial, the following materials will be needed:
- RN4020 Module: One or more copies of the hardware will be needed for this tutorial. For this purpose I created a versatile breakout board for the RN4020, which is available for purchase on <a href=”https://www.tindie.com/products/HadleyResearch/rn4020-ble-development-board/”>Tindie</a>.
- 3.3V USB-to-Serial Cable: The RN4020 features a simple UART interface for sending and receiving commands. In order to interact with the module, a USB-to-Serial cable will be used. For the purposes of this tutorial, a 3.3/5.0V Sparkfun cable is used, though other options are also available.
- 3.3V Regulator: Required if the Sparkfun USB-to-Serial cable is being used, as the cable features 5V Vdd rather than 3.3V. May not be necessary if a different USB-to-Serial solution is being utilized.
- Serial Terminal Utility: In order to communicate with the RN4020, a serial terminal utility is needed. For Windows users, PuTTy is useful for this purpose. For Mac OSX and Linux users, a number of options are available.
- BLE Scanner App: In order to test the effects of configuration changes to the RN4020, a smartphone BLE scanner is useful. Many such apps are available for iOS and Android. For the purposes of this tutorial, the Bluepixel BLE Scanner app is used.
Why use the Microchip RN4020?
The RN4020 is a Bluetooth Low Energy module available from Microchip. Other modules are available, each with a dedicated community and resources, but the RN4020 was chosen for a few reasons.
- Certification: The RN4020 module is pre-certified with the FCC and other international entities, which means so long as the module itself is not modified, a design does not have to go through intentional radiator compliance testing (unintentional radiator certification still applies). Costs for compliance testing can be significant, especially for designs being manufactured in small batches.
- Interfacing: The RN4020 features a small, simple, programmable onboard microcontroller, allowing it to be used as a standalone device in some applications. For more complex applications, communications with the RN4020 can be performed over a simple UART interface; I2C and PWM communication modes are also available.
- Support:Microchip features high-quality documentation and support for their products, and the RN4020 is no exception. The RN4020 features a standard component datasheet, as well as a User Guide which details RN4020 command options and other things.
- Cost:Of the various FCC compliant BLE modules available, the RN4020 is available in large quantities at low cost, making it ideal for commercial embedded hardware applications.
Microchip RN4020 Overview:
The RN4020 is a 3.3V module which comes in a 22-pin castellated PCB form factor. Full documentation for the module is available at the Microchip product page (link). Of particular interest to developers are the datasheet and the user’s guide. A full pinout for the Hadley Research devboard is provided in the figure, below:
Interfacing the RN4020:
For the purposes of this tutorial, a few pins on the devboard need to be connected:
- GND: Pins 1 and 20
- VDD: Pin 19. Make sure 3.3V is provided for VDD; exceeding this limit may damage the chip.
- TX: Pin 5. Connect to the RX line of the USB to UART cable.
- RX: Pin 6. Connect to the TX line of the USB to UART cable.
- SWAKE: Pin 7. Software wake line; tie to +3.3V for this tutorial. When this is done, the blue WAKE LED should illuminated.
- HWAKE: Pin 15. Hardware wake line; tie to +3.3V for this tutorial.
Once the hardware is connected, the device will have to be connected to using a serial utility, such as PuTTy. Sparkfun provides a well-written tutorial on serial terminal basics for Windows, Mac OSX, and Linux (make particular note of which COM port your USB-to-Serial cable is associated with). When interfacing to the RN4020, be sure to utilize the following UART settings:
- Baud Rate: 115200
- Data Bits: 8
- Parity: None
- Stop Bits: 1
- Flow Control: None
Additionally, ensure that newline characters (\n) are interpreted as an implicit carriage return (CR) and linefeed (LF).
Once connected, power cycle VDD to the RN4020 only (do not unplug/replug the USB cable). If everything is connected properly, the RN4020 should respond with ‘CMD’ upon powering up, indicating the module is ready for use.
The RN4020 command set is described in great detail starting in section 2.3 of the user’s guide. For this tutorial, a few simple commands will be performed. First, let us reset the device to its factory configuration, by issuing the factory reset command, “SF,2”. This command resets all device parameters to factory defaults on the next device reboot; issue the restart command “R,1” to restart the device. You should see the device respond with “Reboot”, and after a few seconds, “CMD” should appear, indicating the device is ready to accept additional commands.
Send the dump configuration command (‘D’), to get a detailed listing of the device’s current parameters.
Let’s change the name of the device. This can be done using the set name command, “SN”. Send the command “SN,HadleyResearch”; you should receive a response of “AOK”. Reboot the device with “R,1”.
On your smartphone, open your bluetooth scanner app. Your renamed RN4020 device should appear.
Click ‘Connect’. Device services and attributes should be displayed. Additionally, the green “CONN” LED on the RN4020 module should illuminate.
Click ‘Disconnect’. The green “CONN” LED on the RN4020 module should turn off.