How to Use a BLE Module — A RN4020 Tutorial
When it comes to connecting any electronic project, choices you have will depends on how far you want your gadgets to connect and the source of your energy.
Want your gadget to connect only way one from your sender to your receiver? Easy. The ol’ reliable RF 433.XX MHz module available for cheap on different online stores. Maybe you need a two way communication with a little bit more range? Piece of cake, the venerable nRF24L01 series from Nordic Semiconductor can help you with that with tons of resources available online, it is easy to get started in no time. Or maybe if you want to connect it with your phone? The HC-Bluetooth modules to the rescue! With plenty of options to choose from, from the HC05/06 with Bluetooth 2/2.1, which are very popular among the hobbyist community and tinkerers.
For my project I wanted to try a different approach, instead of using devices used mostly by hobbyist, I decided to use the ‘big boys ICs’. I needed to send the data over my computer from my data logging device to better visualize it and analyze it later, and although my PC featured Bluetooth capabilities, I wanted to create a device to provide Bluetooth functionality, in case I wanted to use it on a computer without it and not did not feel like fiddle around with Windows BLE APIs.
A quick search of “Bluetooth Module” in Mouser provides me the following results:
Wow! That’s too many results, let’s try to narrow our search a bit..
Checking out some of the results there seems to be a prevalent proposition from manufacturers like Silicon Labs, Cypress Semiconductor, Taiyo Yuden, Microchip. But we need to find something in good enough quantities so we aren’t left without stock, with good documentation, example usage and a good demo.
And some offerings from Microchip seemed to fit the bill pretty well, specially the RN4020, a first look into the IC’s datasheet and user manual provides valuable information on how easy this device is to use (sort of).
While Bluetooth Low Energy provides much lower power consumption than the standard Bluetooth classic, it does not offer a standardized way to transmit data over the air, so manufacturers are left to themselves or to end user to find a way to do so, Microchip developed their own called Microchip Low-energy Data Profile (MLDP) which in a nutshell is a private profile to ease the burden.
A quick look at the internal block diagram reveals that this is a 1.8V — 3.6V device, meaning your microcontroller power must be within that range, otherwise you can damage your module.
Following the basic connectivity diagram, a small ATtiny412 (great little devices) from microchip to do the basic configuration coupled with a Serial to USB IC to do the heavy lifting of receiving the incoming data from our other device did the trick for the computer side.
RN4020’s manual explain on how to configure a device as peripheral. The command you issue determines if it becomes a peripheral or a central module.
To enable MLDP communication you must use the SR command to set the Enable MLDP, Auto Advertise, and UART Flow Control bits on the peripheral module; SS command to set Heart Rate and Health Thermometer services. Always reboot the device for the changes to take effect!
Flow control is recommended if you plan or transmitting data as fast as the module allows, otherwise your data or commands will be simply ignored.
For the central device we must issue similar commands.
To enable MLDP in the central device you need to issue SS command to set Device information and Battery services. SR command to set Enable MLDP and UART Flow Control. Always reboot the device for the changes to take effect!
Once both modules are configured, you can issue the ‘F’ command to find the device. You should have a response from your central device like this:
Once the device is found, we can send the ‘X’ command stop the scan. The string marked in yellow is the peripheral MAC address, which is what we need to connect to the device, the other 3 parameters show the MAC address type, name, and signal strength.
To connect to your peripheral we must issue the ‘E’ command, followed by the target MAC address, like so:
Once connection has been established, you will receive a ‘connected’ response. And to finally enable MLDP and have your super awesome over the air serial channel, you simply set the CMD/MLPD pin high, and you will be greeted with the ‘MLDP’ response and your data should start flowing.
Scaling up
Now that you have the basics, you can integrate this module to any of your projects and start communicating them with other gadgets to your heart desires.
Selecting devices like the RN4020 enhances your design for manufacturing capabilities, which will allow you have your device assembled by one of the low cost services without breaking the bank.
I hope you’ve enjoyed this quick peek into the wild world of BLE modules!
If you liked this post, you can also have a look at my own website nicatronix.com that I am currently building to share more related stuff on Electronics!