nrf51822 module

Arduino on the nRF51822 Bluetooth Low Energy microcontroller

Update May 2018

 

The articles about programming the nRF51xxx devices are now 3 years old and are only of use for historic reference.

Please see the work done by Sandeep Mistry https://github.com/sandeepmistry/arduino-nRF5 for the current status of Arduino development on these devices

 

Original post…

This is the first in a series of posts about programming the Nordic Semiconductors nRF51822 Bluetooth low energy micro-controller using the Arduino IDE. The post is a big one as it describes the entire hardware and setup process.

 

The board I’m using can bought bought on eBay, AliExpress or Amazon for well under $10

(Update 20th Oct 2016. I found who actually makes these module. See  http://www.hyyunjia.com/product/html/?14.html  however they are only available via companies like WaveShare and various other vendors on eBay and AliExpress etc)

nrf51822 module

The board does not have either USB or an onboard serial based bootloader, so at least initially, it has to be programmed using an external SWD programmer.

I’m currently using a Maple Mini STM32 board as a programmer

MapleMini

By programming it with a custom version of the Black Magic Probe firmware (more on this later), but any SWD compatible programmer should be able to program this device, as long as the programmer explicitly supports the nRF51822 – One other programmer which I know works is the JLink by Segger.

 

The Maple Mini is available mainly through eBay and AliExpress for around $5 (http://www.ebay.com/sch/i.html?_from=R40&_trksid=p2050601.m570.l1313.TR0.TRC0.H0.Xmaple+mini+stm32.TRS0&_nkw=maple+mini+stm32&_sacat=0 ) and you will also need a USB to serial converter to program it, like this one

http://www.ebay.com/sch/i.html?_odkw=usb+serial+cp2102&_osacat=0&_from=R40&_trksid=p2045573.m570.l1313.TR0.TRC0.H0.Xusb+serial+6+pin.TRS0&_nkw=usb+serial+6+pin&_sacat=0  or any USB to serial that supports 3.3V devices.

 

 

Although not essential, its a lot easier to connect the nRF51822 RF module if you use a “Motherboard” like this one, which are also available on eBay and AliExpress etc, for around $12 (USD) (without the radio module)

nrf51822 motherboard

 

 

The main reason that you should consider buying this board, is that the RF module, uses pin size and spacing which is not commonly used on other development boards, i.e the pins are not 0.1 inch apart, and the pins are shorter and thinner than used on Arduino shields etc.

However if you are happy to solder some wires to the module, you can save yourself at least $10, which is what I’ve done as I’m still waiting for my Motherboard to arrive.

 

Anyway, enough of the hardware you’ll need…

What software is required.

The first thing you’ll need, if you don’t already have it, is a copy of the Arduino IDE. Currently the only versions of the IDE that I’m supporting are 1.6.4 and 1.6.5

Once you have installed the Arduino IDE you need to add either the Arduino Due or Arduino Zero board using the  Boards Manager. The reason this is currently required, is to install the ARM compiler and not for the actual Due or Zero. At a later date I will make a JSON file so that the nRF51822 can be installed without needing to install the Due, but at the moment this is the workaound you will need to use.

The next thing you need to do is download the third party core files from github https://github.com/rogerclarkmelbourne/nRF51822-Arduino/tree/S130_corelib, specifically this zip file https://codeload.github.com/rogerclarkmelbourne/nRF51822-Arduino/zip/S130_corelib

Next, go to your Arduino sketches folder and if there isn’t already a hardware folder, make a folder called hardware

Unzip it, and copy the RBL folder from inside /arduino-1.6.x/hardware into your newly created hardware folder, and restart the Arduino IDE

Now under the boards manager you will have a new board “Generic nRF51822”

 

Now to make yourself a SWD programmer from your Maple mini

 

 

First you will need to download the firmware from github https://raw.githubusercontent.com/rogerclarkmelbourne/blackmagic/master/binaries/blackmagic_maple_mini.bin

Note.

In order to be able to send updates to the Blackmagic probe team, I’m probably going to need to maintain a direct fork of their master version.

In which case I will move the version with working binaries to

https://ww.github.com/rogerclarkmelbourne/blackmagic_archive_version

 

And also on Windows download the Flash Loader from STM

 

http://www.st.com/content/st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-programmers/flasher-stm32.html

 

Connect the USB Serial to the Maple mini by RX <–>TX and using GND and Vcc to GND and 3.3V on the USB to serial connector.

You also need to connect the Boot1 pin to gnd on the maple mini during programming

Then press and hold the button at the end of the Maple mini and then press and release the reset button (this will put it into serial upload mode)

Then use the STM Flash loader to upload the binary file to the Maple mini.

Once its been flashed, connect the Maple mini via its USB connector to your PC and it should be detected as 3 new USB devices.

On windows you will need to install the drivers for these, by downloading http://www.blacksphere.co.nz/downloads/bmp_driver_20130819.zip and unziping, then open the device manager and select each of the 3 unknown usb devices in turn and select update driver software, then browse to the folder you have just unziped and allow windows to install the drivers.

Note in the Windows device manager, which new USB device is shown as the GDB server, as you need to select that COM port number in the Arduino IDE to upload.

 

To connect the Maple mini to the nRF51822, you need to connect the following pins

For the SWD programming of the module use

Pin 6 -> SCLK on the nRF51822 or on the JTAG connector on the motherboard

Pin 7 -> SDO on the nRF51822 or on the JTAG connector on the motherboard

For USB to serial, you can use these pins on the Maple mini

Pin 8 -> P0.09

Pin 9 > P0.11

GND -> Gnd on the nRF51822 or Motherboard

 

You can use the USB to Serial that is on the Motherboard instead of the Maple mini, in which case don’t connect pins 8 and 9

 

 

If you are not using the motherboard or want to power everything from the Maple mini, you will also need to connect

Vdd to Vdd (either on the nRF51822 or the Motherboard)

 

 

 

Finally…

open an example sketch like Blink and upload, and if everything is working, you should see a LED blinking on the Motherboard.

If you won’t have a motherboard, you’ll need to connect a LED to pin P0.15, as this is currently the mapping that I’ve inherited from the RebBearLab files.

281 Responses

  1. hobby16
    |

    Evothings is essentially Cordova, but much faster to write code with. Evothings’ examples “BLE scan” and “BLE discovery” work perfectly at the outset on an nrf518222 board flashed with Sandeep core, I have several low cost smartphones with Android 4 and Android 6 and they all work, that’s why I don’t give up trying it, yet. Hopefully, I’ll get a working Android app where I can send/receive data from the nrf518222 That’s why I’ll try to compile the Redbearlabs BLE examples on Sandeep core because Evothings has precisely nice example projects for Redbearlabs BLE.

  2. Roger Clark
    |

    I had a quick look at Evothings again, and it looks very much like Adobe’s PhoneGap product, were I seemed to need to install their App to use it.

    I initially used PhoneGap as I already had a Adobe account which gave me PhoneGap as a free add-on, however I found that needing to upload via the web to their server and then to download again back onto the phone to give very slow code iterations.

    I also didn’t find the debugging much use.

    I see Evothings BLE plugin is also directly available for Cordova https://github.com/evothings/cordova-ble

    I think I tried it as well, but I can’t remember why I didn’t use it

    Its possibly because the last commit date was Nov 24, 2016 , and I tend not to use projects on github which dont seem to be active.

    There are also 41 open issues on github for the Evothings BLE plugin

    https://github.com/evothings/cordova-ble/issues

    I think some of them should be closed as they were user errors, but as Evothings does seem to reply to bugs dating back to last May, e.g. reporting it doesnt work with Android N
    https://github.com/evothings/cordova-ble/issues/142

    I don’t think I’ll be rushing to us Evothings

    It could be that the Evothings studio does not use the same version as they put in github, but I’m not comfortable with using closed source software etc, when there can be perfectly good open source alternatives

  3. hobby16
    |

    indeed, the low activity on Evothings and a nearly empty forum has been definitively a big doubt factor.
    I don’t give up (yet) because according to them, Evothings is open source and a project written with Evothings can be compiled straight away with Cordova (quite credible claims but I have not yet verified if they are true).
    AFAIK, there is really no alternative to Evothings. To write code with Cordova, you must recompile, generate APK, upload and run, whereas it’s just one click with Evothings. Coming from the RFOBasic world, where changing code and relaunch is just a matter of fraction of second and developping a fully functional app is just a matter of an hour or two, Cordova is definitily a no-go for me. Problem is RFOBasic works nice with Bluetooth 2 but has not implemented Bluetooth LE, grrr.

  4. Roger Clark
    |

    I don’t see the problem with using Cordova

    The only difference seems to be that with Cordova you have to connect your phone to the PC via USB. This works really well, and is a very fast way to update the app.

    I just open a command window and type

    cordova run android

    and it uploads and runs.

    To debug you can use Chrome, as it has a “devices” section where you can connect to any Cordova type browser app that is running and use Chrome to inspect the page and the variables etc, pretty much like you can do with a regular web page

  5. hobby16
    |

    “cordova run android”
    Thank you Roger for the tip. I’ve seen so far only horribly slow Cordova setups in video demos. I thought that Cordova always recompiles and regenerates an apk at each code iteration, I did’nt know that it could be as fast as Evothings (using a usb-debug cable is no problem),
    I’ll give (again) it a try. If ever you are successful using Evothings BLE plugin with Cordova, please let us know.

  6. Roger Clark
    |

    I think I just followed the instructions here

    https://cordova.apache.org/#getstarted

    I had to install Node, but that was all

    I already had the phone drivers installed (I think they installed themselves when I connected the phone)

    You have to enable USB debugging in the developer settings, and you also have to authenticate the PC with the phone.
    I probably already did that buy running ADB for something else, but I suspect when you first try to upload from Cordova it may ask you to authenticate (confirm that the PC is allowed to access the phone)

    Compile and upload is really fast, as it only recompiles what is needed e.g. whats changed.

    Debugging with Chrome is a bit of a pain as even time you upload it breaks the connection to Chrome and you have to re-attach,

    The URL is

    chrome://inspect/#devices

    I suspect I’m doing something wrong and there is an easier way, and that it may be possible to edit in Chrome like you can on normal web pages, but I’ve only managed to change the CSS etc in Chrome and inspect the Javacript and not modified the javascript on the fly

    Anyway, its worth trying Cordova

    BTW. I think Cordova may need to run in Windows Power Shell.

  7. Palash
    |

    Thank you roger for this tutorial.
    i want to convert stm32 bluepill into blackmagic probe.
    The links you have provided for binaries are showing 404 error. Can you please help me by providing alternative way

  8. Palash
    |

    Thank you very much Roger 🙂

  9. Roger Clark
    |

    No worries

  10. Dave Nguyen
    |

    Hi Roger,

    Thanks very much for the fork of RedBearLabs of nRF51822-Arduino. I refer to this repo of yours: https://github.com/rogerclarkmelbourne/nRF51822-Arduino

    I followed the instructions and compiled the example successfully.

    However, I don’t have a Maple mini, instead I do have a J-link.

    Problem is I couldn’t use this J-link to upload sketch to the BLE400 (Import using programmer). When choosing your Generic nRF51822, there are always a couple Java exceptions during upload. But if I choose the Generic nRF51 board from Sandeep Mistry’s repo, the Blink example always upload successfully!

    Could you provide some guide on how to upload your BLE fork with J-link please?

    Thanks in advance!
    Dave Nguyen

  11. Dave Nguyen
    |

    I am using Arduino 1.6.5, which doesn’t have J-link as an external programmer. My successful uploaded were done on 1.6.9 and later versions

  12. Roger Clark
    |

    Hi Dave,

    My posts about the nRF51 are now almost 3 years old and should generally be viewed as historic reference, rather than the current state of Arduino development on the nRF51 etc

    I now use Sandeep Mistry’s nRF5 repo if I’m doing any development work on the nRF51 and now often use the Nordic SDK as it can be used with the free version of Segger Embedded Studio

  13. Roger Clark
    |

    Hi Dave

    See my other reply….The RBL repo etc are now superseded by Sandeep Mistry’s excellent Arduino nRF5 core

  14. Dave Nguyen
    |

    Thanks Roger. Yes Sandeep’s repo is great but unfortunately it doesnt support Central mode which I need. As you said so, I guess I will dig into the Nordic SDK.

    Kind regards,
    Dave

  15. Roger Clark
    |

    Its a shame that Sandeeps repo doesnt support central, but I guess most people use it for client devices e.g. beacons and other smart devices

    Nordic’s site (Devzone) is very good for getting answers to questions about their API, if you need to write your own “central” code

  16. Dave Nguyen
    |

    Thanks for the direction! Cheers, Dave

  17. Roger Clark
    |

    Dave

    You may also want to try Segger Embedded Studio and use Nordic’s SDK directly

    Segger Embedded Studio is totally free to use when developing for Nordic nRF5x

    I think you said you had a JLink, and thats directly supported by SES.
    Even if you don’t have a JLink there are plenty of cheap ones on eBay and you can also make one from a $2 BluePill (STM32F103C8) and a 100 ohm resistor, using some firmware available on github (Look for JLink OBD STM32 github)

    e.g. Possibly this repo

    https://github.com/GCY/JLINK-ARM-OB

    Note. These JLink OBD devices only work with older versions of JFlash, but AFIK they are probably compatible with SES as I think it uses whatever version of JFLash you have installed

  18. Dave Nguyen
    |

    I really appreciate the valuable info. Didn’t know the blue pill could be turned into a jlink too, I have one from Aliexpress coming 🙂

    Looks like it will be a busy day digging these ARM things for me, which is great.

    Cheers,
    Dave

  19. Roger Clark
    |

    The BluePill (STM32F103C8T) is a real “swiss army knife” board 😉

    Its so useful and cheap!

    For debugging, you can run STLink, JLink OBD and also BlackMagic Probe on it.

    And of course Arduino on it etc etc

  20. Dave Nguyen
    |

    Yeah, just learned about it recently, so I order one for myself 🙂
    Anyway, since I will be learning about Embedded Studio + nRF5 SDK for the Central mode, would it be convenient to also do the peripheral mode using these tools, or should the Sandeep’s Arduino lib be more handy in your opinion?

    Dave

  21. Roger Clark
    |

    The Nordic SDK has a lot of good examples

    If you can find an example of Central which does some or most of what you want to do, then I’d recommend using the Nordic SDK

    But it doesn’t have the sort of very simple SPI library etc that the Arduino core has.

    I’ve used SPI using the SDK but not Wire (I2C)

    However the SDK / API can be very complex, as you generally have to follow their conventions, otherwise things don’t work

  22. Dave Nguyen
    |

    After digging into the SDK and accompanying IDE, I realized that the learning curve is very steep, besides the fact that the lack of documentation, tutorial and guides for such combinations is a nuiance. I think I should be looking for some other BLE (chip or SoC) that is easier to program, preferably with Arduino or some simpler toolchain. Do you have any recommendation? Or is it always difficult because BLE is new? All I want to do is to make some smartkey for my motorbike, and such learning curve is not quite worth it 🙂

    Thanks

    Dave

  23. Roger Clark
    |

    Unfortunately, in my experience its difficult to develop applications using Bluetooth.
    I’ve written both the firmware and some Android Apps, and both are problematic.
    You are not writing an App, but I found that different versions of Android operated differently for Bluetooth even when using a wrapper like Cordova.
    I suspect this is why not many Arduino developers bother with Bluetooth, as its just too complicated.

    There are some devices which like the STM BlueNRG and the cheaper HC-05 module, which are designed to work with a separate MCU.

    The HC-05 has its own serial API, (I can’t remember what the BlueNRG uses, its probably SPI or I2C.)

    These can partially simplify the project, but they have a lot of limitations, especially the HC-05.

    I worked on a project what used the HC-05 but we found out quite soon that it had some serious limitations.

    The HC05 type modules can be flashed with BlueBasic https://github.com/aanon4/BlueBasic, but you need special programmer hardware, and also BlueBasic its self has bugs, and its very difficult to fix them because the TI CC2542 chip can only be targetted by the IAR compiler, which is very expensive (because TI only provide BLE libraries for their CC25xx devices when using IAR)

    If you want to just make a smart key consider using Near Field or another similar technology

  24. Dave Nguyen
    |

    I already made some contactless key using RFID (read distance = 5cm) and I am using it. Cool but I think a RF smartkey that doesn’t need to leave your pocket to power the bike on would be even cooler, hence the quest.

    Yes, I have some HC-05 & HC-06 around but they also consumes lots of current, so they don’t fit to run off a coin battery.

    I am currently looking into the HM-10 BLE which supports both central and peripherals mode, and hopefully programming it is easier.

    Thanks!
    Dave

  25. Roger Clark
    |

    OK

    I guess I’ve been working with the Nordic SDK long enough so I understand the way they write their code

    e.g. in SDK 12.3 in examples/ble_central/ble_app_uart.c it clearly scans for BLE devices and on line 380 it calls sd_ble_gap_connect

    But I agree its another mountain to climb

  26. Tom S
    |

    I found this, if anyone may need it. The heart-rate UUID seems to be included:

    public static final UUID SERVICE =
    UUID.fromString(“00001803-494c-4f47-4943-544543480000”);
    public static final UUID RECEIVE_DATA_CHAR =
    UUID.fromString(“00001804-494c-4f47-4943-544543480000”);
    public static final UUID SEND_DATA_CHAR =
    UUID.fromString(“00001805-494c-4f47-4943-544543480000”);
    public static final UUID UUID_DESCRIPTOR =
    UUID.fromString(“00002902-0000-1000-8000-00805f9b34fb”);
    public static final UUID UUID_HEART_RATE_MEASUREMENT =
    UUID.fromString(“00002937-0000-1000-8000-00805f9b34fb”);
    public static final UUID UUID_CLIENT_CHARACTERISTIC_CONFIG =
    UUID.fromString(“00002902-0000-1000-8000-00805f9b34fb”);

  27. Tom S
    |

    The code I posted above came from the NRF51882 iBeacon. I believe this is a version made by a Chinese company:

    https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/5807.NRF51822_2D00_Beacon.pdf

    Here is the project someone presented on this device:

    https://www.linkedin.com/pulse/bluetooth-ibeacon-part-1-search-device-homan-huang/

  28. Yves Accard
    |

    Hello
    Very good information.
    Is it possible to flash nRF51/52 by this way, but with Microphyton, or CircuitPython (from Adafruit) or from the python using in the Mico:BBC ?
    Best Regards
    Yves

  29. Roger Clark
    |

    I’ve no idea. This post is not about micropython.

    I’d suggest you post to a MicroPython forum

  30. Rens
    |

    Hi I had to switch ibeacon with not a kx022, but a lis2dh12 accelerometer. I tried to adapt a lis2dh12 library to the kx022 smartwatch example, because all other libraries don’t work with the nrf51822. I found a datasheet of the lis2dh12 with a nice test example on the last page. (https://www.st.com/content/ccc/resource/technical/document/application_note/group0/55/ef/e4/9f/d1/e9/46/2c/DM00365457/files/DM00365457.pdf/jcr:content/translations/en.DM00365457.pdf). It shows every single step to write what to which register to do a startup and read the xyz data.

    Figure 35: Self-test procedure (with initializing and read xyz).
    Write 00h to CTRL_REG2 (21h)
    Write 00h to CTRL_REG3 (22h)
    Write 80h to CTRL_REG4 (23h)
    Write 47h to CTRL_REG1 (20h)

    Check ZYXDA in STATUS_REG (27h) – Data Ready Bit
    Read OUT_X_L(28h), OUT_X_H(29h), OUT_Y_L(2Ah), OUT_Y_H(2Bh),
    OUT_Z_L(2Ch), OUT_Z_H(2Dh)

    I want to put this into the kx022 smartwatch example, but there are a several things I don’t understand about the kx022 code which hinders the integration.Could you show how to adjust the kx022 example for the lis2dh12? If this is too much to ask, knowing how to write 00h to CTRL_REG2 (21h)’ within the kx022 example would be really helpful.

    Kind regards

Leave a Reply