Arduino with RN4870/1

RN4870_01_800The RN4870/1 is a small (only 12mm wide) BLE module from Microchip. What makes this a little bit special (when compared to modules like the HM-10) are the advanced features that allow you to create your own services and characteristics. This opens up true BLE functionality. It has been available for a while now and I am surprised it is not more popular in the hobby area.

The RN4870 is very different to common hobbyist modules like the HM-10, AT-09, and BT05 and if this is all you have used you may need a refresher on BLE. Especially if you want to use your own services and characteristics.

This is a first look / getting started guide that I will add to as I get time.
Latest update: May 27, 2018

RN4870/1 Features

  • Fully Qualified Bluetooth 4.2
  • Low Energy Module
  • Certified to FCC, IC, CE, KCC, NCC and SRRC
  • On-Board Bluetooth 4.2 Low Energy Stack
  • ASCII Command Interface API over UART
  • Scripting Engine for Hostless Operation
  • Beacon Private Service for Beacon Services
  • UART Transparent Service for Serial Data
  • Over The Air Remote Configuration

The RN48709/1 is configured using ascii commands similar to other BT modules. The main difference s that the The RN48709/1 has it’s own set of unique commands and does not use AT style commands. It also has a mini programming or scripting language built in. This means, depending on what you want to do, you may not need a MCU like the Arduino, the RN48709/1 can be used stand alone.

The UART Transparent Service for Serial Data allows an easy way to link 2 modules together. This can be considered a serial cable replacement, however, if this is all you need you may be better suited with the HM-05/06 or HM-10.

The The RN48709/1 has full BLE 4.2 functionality:

  • Secure AES128 Encryption
  • GAP, GATT, SM, L2CAP and Integrated Public Profiles
  • User Can Create up to Five Public and Four Private Services
  • Software Configurable Role as Peripheral or Central and Client or Server
  • Keyboard I/O Authentication

 
The RN4870/1 is available is a few different sizes/configurations. With shielding, without shielding, with antenna, without antenna, all pins available, only basic pins available.

Image from the Microchip RN784z data sheet.

Image from the Microchip RN4870/1 data sheet.

The ones I have are the RN4870-V/RM118 version. With antenna and all pins available.

These modules are more expensive that the common hobbyist modules like the HM-10 but they far more advanced, well supported with full data sheets and a user forum. Most questions, if not all, will have already been answered on the forum.

Microchip official information

RN4870 product overview
User guide, data sheet, firmwares and update tool. Same page as above, click the Documents tab and scroll down.
Technical details. This is a website with lots of details. Use the menu on the left.

 
 

Getting Started

The RX4870/1 is a very small SMD board which makes it a little difficult for hobbyists. A basic breakout board can be built if you have fairly steady hands. Here is my rather lack lustre attempt. I mounted the module upside down so that I still have access to the pads should I need to add more connections later.
RN4870_BreakoutBoard_03_01_800RN4870_BreakoutBoard_03_02_800

Not the best soldering I have ever done but the board is very small and the iron tip has seen better days.

The jumper is connected between pin 18/P2_0 and GND. With pin 18 brought to GND when the modules start they enter programming mode. This allows you to update the firmware. The jumper is a simple way of connecting pin 18 to GND.

RN4870_02b_usbAdapterThe connections are arranged to match the usb/serial adaptor I am using.

RN4870_02c_usbAdapter_2_800Here is a second board which is slightly smaller. This one has a wire connected to the programming pin. I simply connect the wire to GND when I want to enter programming mode. The extra pin/socket on the end is not used.

You will notice that I used different pins for GND and VCC from the RN4870. This allowed me to use 2 pads to solder the wires which made life a little easier.

I am surprised that there are not more pre-made breakout boards for this module. The only one I can find is the RN4870/1 Click from MikroElektronika.

Jon Raymond contacted me to say he has created his own breakout boards (which look very nice) and is hoping to release the designs at some point. See Jon’s Twitter feed for pictures.

 

Pins & Basic Circuit

RN4870_04_pins

The pins we are initially interested in are:
Pin 3 – GND
Pin 4 – VBAT (3.3v vcc in)
Pin 23 – UART_TX
Pin 22 – UART_RX
These are all you need to use the module but it is a good idea to add a LED to Pin 30 so you know if it is on or not.

Remember that RN4870 RX goes to usb adaptor TX and RN4870 TX does to usb adpater RX.

The data sheet has full details for the other pins.

RN4870_05_BasicCircuit_01c_800
This is a corrected diagram. The previous diagram had the LED on the wrong pin and wired backwards…

New modules have a basic beacon set up and we can use this to test if the module is working or not. For this just add vcc and GND. Remember these are 3.3v devices so use 3.3v not 5v.

Power the module, then use a BLE scanner app to find the beacon. Here I am using Android BLE Scanner by Bluepixel Technologies LLP but there are many similar apps.

Open the app, you should see the RN4870 (it will not have the same name), connect and the LED should change to 2 quick blinks every second.
– Open the first characteristic (Generic Access 0x1800, this is the device name)
– Read the characteristic data by clicking the small R icon. You should get the name again.

RN4870_05_BLEScanner_screens_02_720

Now we know it is working we can try talking to it through a serial terminal program, something like Tera Term or the Arduino IDE.

Talking to the RN4870/1

To talk to the RN4870/1 we need a usb to serial adaptor. I am using this one but other 3.3v versions will work. Make sure it is 3.3v. At a push you could use a spare Arduino but I don’t go in to this here.

usb-serial-adapterHere is a adapter I am using. 3.3v or 5v can be selected.

Connect the RN4870 to the usb adapter and connect to a PC, note that I am using Windows 7, this will give you a COM port. Mine is COM5. Yours will be different.

For the first few examples I am using the Arduino IDE. The Arduino IDE does not have local echo (it does not display what you have entered) but you can turn on local echo in the RN4870 with the “+” command if you wish. Use the “+” command after entering data mode.

RN4870_11_serialMonitor_02If you check the data sheet you see that it has a default baud rate of 115200 and all commands require a carriage return CR character (\r).

The RN4870 can operate in data mode or command mode. It is in data mode by default so we need to change to command mode using the “$$$” command. Small caveat though, Microchip do not class “$$$” as a command and it only works when there is no line end character. This really should be stated in the data sheet. So change the line endings to “No line ending” and enter $$$:
RN4870_12_serialMonitor_03

If the world loves you, or even likes you just a little bit, you will get a “CMD>” reply.
RN4870_13_serialMonitor_04

To return to data mode use “—\r”. This is 3 dash or minus characters + CR.

Now change the line endings back.
RN4870_14_serialMonitor_05

There are a few commands that return data (these are called GET commands). One is the “D” command and another is “V”. “D” returns information about the device and “V: returns the firmware version.

“D” command
The “D” command returns device information:
– Device MAC Address
– The random address (if a random address is used)
– Device Name
– Connected Device MAC address
– Authentication Method
– Device Features as set by the “SR” command
– Server Services
– The fixed pin code (if one is being used)

RN4870_02_02b

“V” command
The “V” command returns the firmware version.

RN4870_15_serialMonitor_06_V
Here you can see that the modules I have are using firmware V1.18.3. This is not the latest and I will look at updating it later. The latest firmware, at the time of writing, is v1.28.3 v1.30.

“GNR” command
“GNR” gets the name of a connected device. Of course, for this to work the module has to be connected to something. With the serial monitor still open. Use the BLE scanner app to connect to the RN4870.

When the module connects it sends the connection details to the serial monitor:
RN4870_16_serialMonitor_07_connect

Now we have a connection we can get the name of the connected device with “GNR”
RN4870_17_serialMonitor_07_2

When you break the connection, the RN4870 outputs a “%DISCONNECT%” message.
RN4870_18_serialMonitor_08

For a full list of the GET commands see the data sheet.

 
 

First Communications

Please note I am using a module with 1.30 firmware.

Firmware 1.30 comes with a set of default services and characteristics built in and we can use these to try out making a connection. I am keeping it simple and using the Arduino IDE with the same serial adapter and BLE Android app as above. The RN4870 is on and in data mode.

In the Arduino IDE, select the correct COM port and open the serial monitor.

Start the BLE Scanner app, if it does not automatically start scanning, hit the Scanner button at the bottom. You should see the RN4870 appear in the list.

Hit the CONNECT button, after the connection is made, select the Custom Serice
RN4870_20_01_firstConnection

In the serial monitor you should see the connection message
RN4870_20_02_firstConnection

We can now try sending data to the serial monitor.
For the the custom characteristic 49535343-8841-43F4-A8D4-ECBE34729BB3, hit the write button, a text input box should appear, enter “hello” and click OK
RN4870_20_05b_firstConnection_800

The value of the characteristic changes to hello and you should see hello in the serial monitor
RN4870_20_07_firstConnection
RN4870_20_06_firstConnection

The RN4870 receives data via the 49535343-1E4D-4BD9-BA61-23C647249616 characteristic.
RN4870_20_08_firstConnection

Click the HM-10_SERV+CHAR_003_N_Icon_015 notification button to turn notifications on (The indicate button also works).

In the serial monitor enter “hello to you” and click send and “hello to you” should magically appear in the characteristic.
RN4870_20_09_firstConnection

 
 

Notify or Indicate?

In the above example I use Notify to confirm when data has been received but you can use either Notify or Indicate so what’s the difference? Notify does not need an acknowledgement, Indicate does. This makes Notify a little faster but less reliable. Notify also allows for constant data flow whereas Indicate has to wait after each piece of data for the acknowledgement before it can send the next piece.

Notify
Here is some data, get it while you can.
Here is some data, get it while you can.
Here is some data, get it while you can.
Here is some data, get it while you can.

Indicate
Here is some data, have you got it yet? wait, wait… Reply “I have it” received now I can send the next data.
Here is some more data, have you got it yet? wait, wait… Reply “I have it” received now I can send the next data.
Here is some more data, have you got it yet? wait, wait… Reply “I have it” received now I can send the next data.
Here is some more data, have you got it yet? wait, wait… Reply “I have it” received now I can send the next data.

I like to think the modules have conversations with each other.

 
 

Connecting Two RN4870 Modules Together

The RN4870s have a transparent UART layer built in. This means it is easy to get them connected and start sending data. This is very similar to how the HM-10s (and similar modules) work, and, if this is all you need you might as well go with HM-10s. They are cheaper and come on hobbyist friendly breakout boards.

To get started I am using modules connected to the same computer with usb to serial adaptors. The same ones as above. One of the modules is on COM 7 and the other is on COM 8. I am using the module on COM 7 to initiate the connection. Everything is manual. The modules I am using have firmware 1.30.

Creating a connection is straight forward as long as you have the MAC address of the remote module. If you have the address you can simple use the connect to mac address command. If you don’t have the address then you can scan. Scanning returns the mac address and the name of found modules. The address for the device I want to connect to is D88039F914F5 but I’ll pretend I don’t know it.

On the main device, enter command mode with “$$$” (no EOL characters).
RN4870_05_01

Enter SS,C0 to enable the UART transparent service.
RN4870_05_02

Enter F to start a scan
RN4870_05_03

I am looking for the module named RN4870. It is the second one and its mac address is D88039F914F5. To connect use the Connect “C” command. The “C” command expects 2 parameters;
1 – the type of address, 0 or 1). 0 for public address, 1 for private address.
2 – the mac address

So, to connect I use “C,0,D88039F914F5″
RN4870_05_04

If the connection is successful you will get connection messages in both serial monitors.
RN4870_05_05

And once the connection is made the modules can talk to each other
RN4870_05_06

RN4870_05_07

 
 

Updating the Firmware

The official Microchip information only covers updating the firmware when using the PICtail development board but it is fairly easy to update using just the RN4870 module. You will need:
– A RN4870
– A USB to serial adaptor
– The isUpdate.exe file
– The firmware to upload (4 files)

You will also need to know the COM port the usb to serial adaptor is using. In my case it is COM 7.

The update tool and the firmware can be downloaded from the Microchip RN4870 page. Click the Documents tab. You can download the data sheet and user guide from the same page.

To put the RN4870 in to programming mode we need to bring pin P2_0 low during boot. P2_0 is the bottom centre pin.
RN4870_99_01_P2_0

On the larger breakout board, closing the jumper connects pin P2_0 the GND.

RN4870_99_04b_P2_0_800

On the smaller board, the wire is used to connect P2_0 to GND. This is a very lazy solution and not really recommended. I have killed more than one Arduino because of this technique. It is easy to short something that shouldn’t be shorted.
RN4870_99_05b_P2_0_800

 

To update

I am updating from firmware 1.18.3 to 1.30. FW 1.30 was released on May 4th, 2018.
RN4870_99_FU_01b

Only one program can connect to a COM port at a time so if you have a serial monitor open, close it. Then:
– Remove power from the module,
– Connect P2_0 to GND and
– Re-power the RN4870.
Although there is information online that the LED is solid on, for me the LED is off. Apparently, if you connect the LED the right way round it works correctly, it is solid on when the module is in programming mode. Who Knew? Me if I’d read the data sheet correctly. Thanks to Jon Raymond for pointing this out.

Open the isUpdate.exe file (Installation is not required).
– Set the COM port (mine is COM 7)
– Set the baud rate to 115200
– Set memory type/subtype to flash and Embedded Flash (or Embedded F)
– Set address to 0000

RN4870_99_FU_02

Click Connect. If the module connects you get a “Port connect -> COM7 message” (or whatever port you are using) and the programming buttons become active.
RN4870_99_FU_03

Now click the Flash Update/Browse -> Images, Browse button and select the 4 firmware files. You can select all 4 files in one go.
RN4870_99_FU_04

And then click Update. If everything is fine the update should start.
RN4870_99_FU_05

RN4870_99_FU_07

The “End of Write Memory” message indicates it’s finished and that’s it.

We can now check we have the new firmware:
– Close the isUpdate app or click the Disconnect button to release the COM port.
– Power off the RN4870
– Remove the programming wire
– Restart the module (the LED should be on)
– Open the serial monitor
– Enter command mode with “$$$” (remember no EOL characters)
– Check the firmware with the “V” command (requires the CR EOL character).
RN4870_99_FU_11b

Release notes are included with all firmwares but the notes only include the changes for the latest version (no history). If, like me, you are jumping a few versions you will need to check the release notes for each firmware version to know what all the changes are.

RN4870_99_FU_13After updating, use a BLE scanner app to check the default services and characteristics.

 
 

 
This post will continue as I get time.
 
 

25 thoughts on “Arduino with RN4870/1

    • The RN devices only support single connections. For a piconet you would need to use devices like the BTLC1000 and SAMB11. These support up to 8 simultaneous connections.

      • Thanks Martyn for the good work. I was actually thinking a single Raspberry Pi 3 B+ as master with 5 slaves each mikroBUS RN4871. Slaves only need to receive data so I guess each RN4871 is single connection. It’s not going to work, it is? Perhaps you are wondering why mixing Raspberry Pi and Arduino/Teensy over bluetooth but I don’t have a choice as bluetooth slaves must be mikroBUS “click” for Teensy while commands are provided by a very specific HAT master hosted on Raspberry Pi. Any thoughts would be greatly appreciated as I am a bit stuck with these requirements. Many thanks. All the best

        • I don’t know much about the Raspberry Pis but I can’t see why this wouldn’t work.

          The only thing I can’t determine is if the Pi can have more than 1 Bluetooth connection at a time. If not you would need to connect to each node in turn.

  1. Hi Martyn,

    I have RN4871. I followed the all sentences from your article.

    I have a connection between reset and GND via 1uf electrolytic capacitor. Also, there is 4.7kohm between reset and 3.3V. I left the P2_0 pin idle. I connected to USB TTL module. And I try to make communication by tera term.

    My problem is that the device is reboot by itself without any reason simultaneously. Sometimes I write $$$ then it enters the CMD mode. Also, I can write V or D, but it reboots again.

    I couldn’t find any reason. I used ceramic cap for the 3.3V and GND.

    Can you help me.

    Best Regards,

  2. I have been reading all your posts on the HC-05, HC-06 and now the RN4870. I have a piece of electronics that uses an HC-06 to link to Android apps and Windows software. But it will not of course link to anything iOS. Is it possible to set up an Arduino with an HC-05 as a master and link to the HC-06, and then pass the data thru to the RN4870 so that iOS can at least recognize the RN4870.

    • Yes. The Arduino would just act like a relay station very similar to the serial pass through sketch above. You would simply connect the electronic device to the hardware serial and have the Arduino relay whatever is received to the software serial. This can work well if hardware serial is available, especially if the device uses a high baud rate like 115200 or higher (software serials do not handle fast baud rates well).

      If the device uses a lower baud rate to connect to the HC-06 then it could also be done using 2 implementations of software serial. In this case I would recommend using different libraries such as SoftwareSerial + AltSoftSerial so that both can be active at the same time.
      A reliable solution would depend on how much data is being transmitted and fast it needs to be sent. BT classic has a much faster throughput than BLE and handles large volume data much better.

      If you have access to the device it may be possible to replace the HC-06 with a BLE module such as the HM-10 or RN4870/1or any module that talks UART. This assumes the device is not controlling the HC-06 and the HC06 is simply acting as a serial bridge.

      • Hi Martyn,
        I wish I could replace the HC-06 on the device, but it is not doable. That is why I am looking at this “relay” workaround. So if I understand correctly I can have both the HC-05 (master) and the RN4870 wired to the Arduino. Use the HC-05 to link to the HC-06 on my device, and then have the RN4870 link to an app on an iOS item (Ipad, Iphone).

  3. Hello Martyn,

    I am getting started with Bluetooth BLE, and am following your website. I have built a simple breakout board for RN4870 module with a USB to UART interface. I have worked my way through your instructions to about halfway, stopping at “Connecting Two RN4870 Modules Together”, Everything works fine, so thank you for excellent guidance.

    I have also followed the instructions in the
    Microchip RN4870-PICtail-UserGuide.pdf
    using the same breakout board with some pots, LED, switch, added instead of the PICtail board. Again, everything works as expected, including communicating with BLESensorApp on an Android smartphone.

    On my Windows 10 PC I have used the Arduino IDE as you suggest, and also the CoolTerm terminal emulator. They both work fine.

    I would now like to move on to my real objective, which is to connect a digital temperature sensor to my breakout board and capture the readings on my PC.
    I do not foresee any difficulties at the board end, as I am well able to deal with the hardware, and expect to be able to program the RN4870 as required ( Or alternatively to write a script ).

    My problem is what to do at the Windows PC end. Neither of above terminal emulators appears to meet my objective, i.e. to capture temperature readings and present them on screen, and also to save them to a file. I should mention that I plan to capture readings say every 15 mins over a whole day, and save them together with time and date stamp.

    I guess a program could be written in Visual Basic to do this, but my knowledge of VB is very limited. My feeling is that something like this should already exist, perhaps a Windows version of the BLESensorApp, but I havn’t managed to find anything.

    Can you offer any advice or point me towards a suitable program?

    Thanks and best regards, Ken.

    • Hi Ken,

      I replied by email by mistake and not sure if you received.

      Unfortunately I don’t have anything to recommend. I would suggest joining the Microchip forum (if not already a member) and ask in the Bluetooth section. You could also try on other forums like stackoverflow

      A while ago I started to look for a suitable app but got side tracked with work, decided to create an Android app and never looked at VB again. Visual Basic (or C or C++) should not be too hard for you and a google search brings up quite few resources about the Windows BLE services. Not sure how complex using BLE on Windows is though. I suppose it depends on how much of the work the services do for you.

  4. Hello again,

    Apologies, I missed some key info from my earlier comment. My PC connects with Bluetooth with a BLED112 dongle, and I use the Bluegiga BLE GUI. But this also does not seem to meet my needs. In fact, on the Silicon Labs Bluetooth Forum it states:
    “If you are looking for a plug&play BLE dongle that will make your PC BLE capable then BLED112 is not such a product”.

    So perhaps I first need to find another BLE dongle?

  5. Martyn- Thanks you for another excellent article on Bluetooth modules. I would appreciate your comments regarding the appropriateness of the RN4871 for my first Bluetooth project. I’m considering using the RN4871 module as serial passthru device to transfer data from an old serial instrument (TX only, no flow control) to a tablet.

    Question: Once the 4871 is configured via the UART interface (no scripting allowed) and removed from the programming device (PC via Serial), will the 4871 retain functionality to perform as a “stand alone” serial pass through device?

    I’ve ordered and received HM-10 devices and I have found out that I simply cannot operate within the chaotic environment this device has generated.

    Regards and thanks for your efforts

    • You don’t say which tablet but if it is Andriod I feel you would be better with the HC-05 or HC-06. These use the simpler BT SSP profile. They don’t work with Apples IOS though.

      To answer your question. Yes, once set up the RN4871 remembers the settings.

      What was the issue with the HM-10s? I have moved away from these now but in the past I used them in some fairly wifi polluted environments without too much issue. The only thing I really noticed was that the distance they could be used was reduced.
      Without really knowing, I suspect if you have issues with the HM-10 you are likely going to have issues with other BLE modules.

      • Martyn,
        Thank you for the response. I’ve ordered HM-10’s from multiple sources and have received (mostly) counterfeits. It takes a few hours of sleuthing to determine exactly which modules have been sent, and even with that information the command set (usually) doesn’t agree with the data sheet (if there is one). Additionally, emails to HM regarding certified suppliers go unanswered. I’m investigating the RN4871 solely because of the EXTENSIVE data sheet, and availability via a known distribution network.

  6. Hello Martyn,

    Yes, I did receive your email — many thanks and apologies for rather tardy reply to it. Meanwhile, I have bought another dongle, the ASUS USB-BT400 USB Adapter. I picked this one because it seems to be generally recommended as reliable and problem-free. I have installed it and it appears to work properly. I can pair it with my Android phone, and transfer files via it. I can also pair it with my board with RN4870, and I can see from the terminal emulator that the last program I installed for the PICtail board is still running correctly (ie reading sensor info and displaying that on the BLE app on the phone).

    But I am again stuck at this point, and have no clue about how to get the sensor data on to my PC. So I will follow up on the Silabs links you give, and also try the Microchip forum, although my past experience with that is not very good.

    Thanks again for your help, and excellent article. Ken.

  7. I bonded (2) Microchip RN4870 BLE FW V1.3 modules, but found that they can disconnect on their own (even the Microchip Eng told me to code into the Features to “reboot” upon disconnect, which I think confirms that they have this bug).
    In looking at RN4870 internet posts, I found other people having problems with these modules, and someone recommended the Silicon Labs (purchased AckMe) AMS002.
    I purchased 2 of the Bream (AMS002) development boards from Digikey and they seem to work well, but I don’t see yet how to pair them yet (there is an unpair command, so there should be a pairing command).
    My question is, for a serial UART application, only needing to transfer small amounts of serial data back, and forth, what modules would you recommend?
    (2) HC-05’s, HC-05 / 06, SH-HC-08’s, HM10’s, HM-12, HM-13, or HM-17, or another?

    • If power is not a concern then Bluetooth Classic SSP such as the HM-05/06. But if you go this route make sure you get good modules like the ones from Waven. These have a good firmware and are well supported.
      If power is a concern look at BLE modules sans breakout board. I’ve had good results using the HM-11. Good connections and fairly low power use. There are better options if low power consumption is critical though.

    • I haven’t done any distance / signal strength tests but like you I am starting to find complaints online. Don’t know how much these reflect the real situation though. If it was really poor I would expect a lot more complaints than there are.

Leave a Reply to Martyn Cancel reply

Your email address will not be published. Required fields are marked *


− 3 = four

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>