Arduino on the nRF51822 Bluetooth Low Energy microcontroller

Arduino on the nRF51822 Bluetooth Low Energy microcontroller

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  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


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 ( ) and you will also need a USB to serial converter to program it, like this one  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, specifically this zip file

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


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


And also on Windows download the Flash Loader from STM


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 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)





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.

199 Responses

  1. Roger Clark

    Hi Goran

    I have zipped my new repo (its called RBL2 as its from RBL but its my new version 😉

    I have not tried it without the 32khz crystal, but I think it should work, as the latest mbed code (from which its derived) does not need the 32khz crystal.

    Re: I2C
    I have not tried this
    Its possible the I2C scanner will not work, as I think it uses a bit of a trick

    However I found this

    which links to this

    So perhaps you could try that code, even if you have to use mbed

    re: TX and RX

    Take a look in variants/generic_nrf51822/pins_arduino.h

    #define DEFAULT_RX_PIN 11
    #define DERAULT_TX_PIN 9

    In my new version I have remapped the pins so that PIN 9 is P0.09 – this is different from the old repo.

    Let me know if this helps

  2. Roger Clark


  3. Mischa Brammer

    Hello Roger,

    for the HM10 you measured the power consumption of the device. Did you also test it with the nrf51822?
    I’m thinking about building a small battery (3,6V LS14500 AA) driven device which is reading data from a local server to indicate a tiny status led.
    Don’t want to change the batteries to often. Maximum once a year would be okay for me.

  4. Roger Clark

    I’ve not done any tests on the nRF51822 but its something I need to do quite soon.

    TI have an application note on power consumption testing

    I also recall reading another document from TI, which described a method using a capacitor as a small battery to monitor current consumption based on how long it took to discharge the capacitor.

  5. Bram van de Kerkhof

    Is it possible to use the HW pwm in arduino or do you recon an other IDE for this kind of development. Would eclipse and the black magic/maplin be able to program the NRF. I’m a bit searching on the best IDE to use. I already ordered the maplin and nrf.

  6. Roger Clark

    You’d need to post a question to the Blacksphere about the BlackMagic probe integration into various platforms.
    They have a github account and also a gitter lounge

  7. Bram van de Kerkhof

    Can I use the latest arduino version or should i stick with the ones you mention

  8. Roger Clark

    I’d recommend you use or my fork of it, for all new projects

  9. Mircea

    Hi Roger, is there any way to use St-Link V2 (clone) with Arduino IDE to program one of these Chinese nrf51822 with motherboard? From what I understand, it can be done with openOCD and St-Link V2, but isn’t openOCD incorporated in Arduino IDE? Any thoughts on this?

    Many thanks,


  10. Roger Clark

    I think its technically possible.

    OpenOCD does support the STLink dongle and also the nRF51 series

    Sandeep’s nRF5 repo on github uses OpenOCD as its programmer, so I think your best bet is to take a look at that

    I’m slowly migrating away from my old RedBearLab based repo to using Sandeep’s
    I have my own fork of Sandeeps, as I wanted to add JLink and BMP support, but I know that Sandeep wants to limit the upload system to just one program (OpenOCD)

    So its definitely work taking a look at in your case

    BTW. The STLink can be reprogrammed as a BlackMagic probe, (see blacksphere’s official BMP repo on github).
    (I’m hoping to migrate all my BMP boards back to using the official blacksphere repo, as its got a load of bug fixes that my fork, of the BMP, doesn’t have. But the code structure for each “platform” (programmer hardware) has changed since I forked and added additional boards, and its going to take me some time to merge and PR my code back into Blacksphere’s official repo

  11. Mircea

    Hi Roger, thanks for the quick reply. I’ve tried it, but couldn’t get it to work yet. I’m getting the following error when trying to flash the softdevice:

    Open On-Chip Debugger 0.10.0-dev-00254-g696fc0a (2016-04-10-10:13)
    Licensed under GNU GPL v2
    For bug reports, read
    debug_level: 2
    Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
    adapter speed: 1000 kHz
    Info : Unable to match requested speed 1000 kHz, using 950 kHz
    Info : Unable to match requested speed 1000 kHz, using 950 kHz
    Info : clock speed 950 kHz
    Info : STLINK v2 JTAG v21 API v2 SWIM v4 VID 0x0483 PID 0x3748
    Info : using stlink api v2
    Info : Target voltage: 3.275936
    Info : nrf51.cpu: hardware has 4 breakpoints, 2 watchpoints
    nrf51.cpu: target state: halted
    target halted due to debug-request, current mode: Thread
    xPSR: 0xc1000000 pc: 0x00012b98 msp: 0x20001c48
    ** Programming Started **
    auto erase enabled
    Info : nRF51822-QFAC(build code: A1) 256kB Flash
    Info : Padding image section 0 with 2112 bytes
    Error: Cannot erase protected sector at 0x0
    Error: failed erasing sectors 0 to 107
    embedded:startup.tcl:454: Error: ** Programming Failed **
    in procedure ‘program’
    in procedure ‘program_error’ called at file “embedded:startup.tcl”, line 510
    at file “embedded:startup.tcl”, line 454
    Error while flashing SoftDevice.

    Any ideas? Thanks,


  12. Roger Clark

    Sounds like your nRF51822 may be read protected / locked.

    I look in my post on troubleshooting the nRF51 about unlocking, but note the commands in the are for JLink and you’ll need to find an OpenOCD equivalent

    However I don’t use OpenOCD, so you’ll need to post to an OpenOCD forum is thats an issue.

  13. Mircea

    Hi guys, I’m posting this for those who want to use St-link V2 with nrf51822 and Arduino, so far to flash the softdevice successfully with openOCD.

    1. I did what Roger said:
    2. I have encountered the memory protection problem described above and found the solution here:
    3. I have installed openOCD for Windows from here:
    4. I have followed the instructions here:
    I didn’t have to do the Zadig thing, because I have installed the ST-Link V2 SIGNED driver that I have downloaded from STmicroelectronics website.
    5. Open a cmd window and navigate to the openOCD directory. use this command:
    openocd -f interface/stlink-v2.cfg -c”transport select hla_swd” -f target/nrf51.cfg

    more on this page:
    6. open a new cmd window and use this command
    telnet localhost 4444 (as described in the link at point 4.) this window use the “nrf51 mass_erase” command (as described in the link at point 2.) I did this twice just to make sure.
    8.Back to Arduino IDE did the nfr5 flash softdevice and got the Verified OK message.

    After that I’ve tried to load the blink scketch, but I got the following error:

    In file included from C:\Users\Lenovo\AppData\Local\Arduino15\packages\sandeepmistry\hardware\nRF5\0.2.0\cores\nRF5/Arduino.h:5:0,

    from C:\Users\Lenovo\AppData\Local\Temp\arduino_build_687137\sketch\Blink.ino.cpp:1:

    c:\users\lenovo\appdata\local\arduino15\packages\sandeepmistry\tools\gcc-arm-none-eabi\5_2-2015q4\lib\gcc\arm-none-eabi\5.2.1\include\stdint.h:9:26: fatal error: stdint.h: No such file or directory

    compilation terminated.

    exit status 1
    Error compiling for board Generic nRF51822.

    Any ideas?



  14. Roger Clark


    Did you install Sandeep’s repo using his boards manager package, I don’t think it works unless you do that as he uses a different version of the gcc arm compiler from the one normally use

    If you did that, you should post an issue to Sandeep’s repo on github

  15. Bram van de Kerkhof

    I have the same error and am in contact with sandeep. He has now reproduced the error:

  16. Mircea

    @Roger, yes I’ve used his boards manager package.
    @Bram, I’ve tried modifying the platform.txt like sandeepmistry suggested, no luck..:(

  17. Roger Clark

    I installed Sandeeps repo a few months ago, and the only problem was that I found I had to install via the boards manager first because it uses a different version of the GCC ARM compiler to what etc use ( use gcc 4.8 but Sandeep needs gcc 6)

    But I have not installed from scratch recently

  18. JustSomeMathematician

    Seeing all the hassle with the ST Link,
    I’m going to order exactly what you have. Hopefully it goes ok.

    Thank you for sharing all this information. Before finding your YouTube channel, I was under the impression that the ‘Motherboard’ would be sufficient for programming the chip!

  19. Roger Clark

    STLink is great for ST stuff, but not idea for other processors

    Both JLink and BlackMagic probe work OK.

    Flashing the BlackMagic probe onto a STM32F103C8 is the cheapest method.
    There seem to be some JLink programmers on eBay from around $15 but these are grey market, as they are much cheaper than the official price.
    I have a few JLinks I bought from eBay and some appear to be legitimate but one of them I found was a clone and didn’t work.
    Where as the others seem too good to be a clone, e.g. gold plated parts of the PCB.

    If you do go down the JLink route, the one which I have that’s a clone, does not have a pin-out label on the base. But apart from that it looks identical from the top of the box etc.

    (In the end I did manage to get the clone to work, but I had to solder a pin header inside it onto the PCB and reprogram it using another Jlink – i.e this is not something most people would be able to do)

  20. Roger Clark

    Re: Motherboard

    Unfortunately the “Motherboard” does not come with a programmer 🙁 You need an external programmer like a BlackMagic probe (STM32F103C8) or a JLink or something else.

    Like you said, technically a STLink will work, but its a pain to use as it requires OpenOCD and would need a new set of scripts in the Arduino IDE to support it.

    WaveShare really should replace the CH340g USB to Serial converter with a GD32F103 running the BlackMagic probe, but that would add a dollar to the cost, so I can see why they didnt do that.

  21. Bram van de Kerkhof

    As the github environment of sandeep is currently not working on windows could you recon an an other setup to use?

  22. Roger Clark

    I have some old repo’s based on the RedBearLabs repo, that work on some nRF51 boards

    But I don’t generally recommend that anyone uses it any more, as its superseded by Sandeeps work, and Sandeep using the latest SDK from Nordic not the old one that my repo uses. (Plus my repo has a load of legacy code from mbed which clutters things up etc)


    When you say that Sandeeps repo doesnt work on Windows. I’m running on Windows and it works fine for me.

    If you have an issue installing his repo on windows, its best to contact Sandeep not me, as its not my repo

  23. Bram van de Kerkhof

    Did you use this guide and which arduino ide are you using. Sandeep is lookibg into it but no sollution jet.

  24. Roger Clark

    Yes. I use Sandeep’s repo. It works fine for me.

    If it does not work for you, then you need to wait for Sandeep to fix whatever problem affects your installation.

  25. Ganesh Kothavale

    Hi Roger,
    We are trying to develop a ble beacon like device which is able to send advertisement to android phones.We are using Nrf51822,Mapple mini, USB to serial converter and Arduino ide as u have mentioned in the post .will we able to make the device using above software & hardware or are we missing some part of hardware?
    Please guide us for the starting step.

  26. worakarn jenvitayaroj

    ็็Hi, Roger Clark
    I try to follow you step but some link can’t download file. So, I can’t continue.
    This link can’t down load ->

    Thank you

  27. Bo-Erik Sandholm

    Can you point the me to a description on how to flash a nrf51 on a BLE400 motherboard using a BMP on stm32 from sandeeps current distribution?

  28. Roger Clark

    I had to do my own hack to do this, as Sandeep only wants to support uploads via OpenOCD not via GDB (which is what the BMP uses)

    I added this to the bottom of platform.txt

    # blackmagic probe upload
    # blackmagic probe upload

    tools.bmp_upload.upload.params.quiet=-q –batch-silent
    tools.bmp_upload.upload.pattern=”{path}{cmd}” -quiet -cd “{build.path}” -b {upload.speed} {upload.verbose} -ex “set debug remote 0” -ex “set target-async off” -ex “set remotetimeout 60” -ex “set mem inaccessible-by-default off” -ex “set confirm off” -ex “set height 0” -ex “target extended-remote \\.\{serial.port}” -ex “monitor swdp_scan” -ex “attach 1” -ex “file {build.project_name}.elf” -ex “load” -ex “tbreak main” -ex “run” -ex “echo \n\n\nUpload finished!” -ex “quit”

    And I also changed boards.txt to add an upload menu type (Black Magic Probe)

    (I can’t recall if I add the upload_method menu as well, or whether that is part of Sandeeps default installation, so you may need to mess around with that to get it to work)

  29. Roger Clark

    The nRF51822 can operate as a beacon without any other devices.

    I’d recommend you read the documents on Nordic’s website and look at the examples in their SDK download, as they give an indication of the great flexibility of the nRF51 (and nRF52)

  30. Roger Clark

    You seem to have the wrong URL

    The correct URL is

  31. Attila

    Hi Roger,

    I’m trying to flash the Black Magic Proble to my STM32 board, but I get no response to the UART commands send to the board. Currently, I’m just trying to connect to the board with the Flash Loader Demonstrator app, but it times out on connect.

    As an USB UART converter I’m using a FT232R chip from a Chipkit Uno32. I connected the reset pin to GND on the board.

    On the SMT32, the BOOT1 pin is connected to GND and I’m resetting the board while holding the BOOT0 button.

    See my current setup:

    Any idea what I’m doing wrong?

    Which RX/TX pins are you using from the STM32 board? I’m tried all of them (1/2/3), but neither works.


  32. Roger Clark

    You appear to have connected to the second UART port not the first one. (TX2 and RX2).

    Connect to TX1 RX1, pins 25 and 26

  33. Anonymous

    I tried all the 3 UART ports (TX1/RX1, TX2/RX2, TX3/RX3), but non of them work unfortunately.

  34. Roger Clark

    Its definitely UART 1 you need to use

    Boot 1 low is good, as it floats and can cause problems

    Holding down the “Button’ while pressing then releasing reset is correrc

    I would try swapping your TX and RX pins, as different USB to Serial converters label their pins differently, sometimes TX means RX 😉

    I presume you are using ST’s windows gui program to upload?

  35. Goran Mahovlić

    Attila I have tut for bluepill maybe it will help

  36. Roger Clark

    I’ve just noticed that you seem to be powering the Maple mini via Vin.
    I’d recommend you just plug it into a USB socket to power it.

    I suppose you can power it from 5V on Vin from your USB to Serial, but I never trust those devices to be able to supply much current (though their 5V is normally OK as its just straight from the USB power)
    The 3.3V output on the FDTI chip is very weak and could not power the STM32.

  37. Attila

    Hi Roger, Goran,

    I managed to get the UART upload work. I needed to swap the RX/TX pins.

    Now, I’m not sure if I uploaded the correct file to the board.

    I’m following the instructions for STM32 found on the Black Magic Probe wiki page:

    I built the project:
    $ make clean && make PROBE_HOST=stlink

    and uploaded the resulted blackmagic_dfu.bin file:
    $ python ./ -p /dev/ttyUSB0 -e -w -v ../blacksphere/blackmagic/src/blackmagic_dfu.bin
    Bootloader version 22
    Chip id: 0x410 (STM32 Medium-density)
    Write 256 bytes at 0x8000000

    Read 256 bytes at 0x8001A00
    Verification OK

    Unplugged everything except the jumper wire between BOOT1 and GND, connected the board to the PC through USB port. The board is powered and the blue LED lights up (although not with full intensity, I think).

    The next step would be to upload the Black Magic Probe firmware through DFU, but the problem is that the board is not even recognized as an USB device. Tried both Linux (lsusb) and Windows (Device Manager) too.


  38. Bo-Erik Sandholm

    Hi Again – I don’t understand what is neededn in programmers.txt to add gdb as a programmer for nrf51 in addition to the additions to board.txt and platform.txt.
    Can you take time to copy up that.

  39. Roger Clark

    Arduino’s documentation for boards.txt and Platform.txt are on their website

    This explains how those files are authored

    Please post to the Arduino forum or Arduino’s github account if you have any questions about this, as this is not code that I wrote or maintain

  40. Attila

    Hi Roger,

    Are there any pre-built binaries of the Black Magic Probe firmware for STM32?

    For some reason, if I build the firmware for myself the USB is not working at all. The board is simply not listed as an USB device.

    I’m experienced this with the STM32duino-bootloader project too.
    The rest of the functionality seems to ok (the LED is blinking, like with the pre-built firmware).

    Here is a build log for STM32duino-bootloader:

    There are many warnings, but not sure how relevant they are.


  41. JustSomeMathematician

    So my parts finally arrived from Shenzhen today.
    Was going well, following your great instructions, and then I bricked the Maple Mini :((((((((((((((((((((((((((
    Been looking at your STM32 blog for possible fix.
    Don’t mind buying another Maple if need be, but to wait another two months, going to drive me crazy.
    Such is…
    Thanks for all the support you’ve provided!

  42. Roger Clark

    Its virtually impossible to completely brick you Maple mini unless you destroy the hardware, or if it had some sort of manufacturing fault.

    If you are on a PC and have a USB to serial converter, just connecting it to TX and RX, pulling Boot_1 low and holding the button in while toggling the reset button, should make it communicate with STM’s own Flash Loader Demostrator. (GUI exe)

    Although you are not going to use the Maple mini to run Arduino sketches, I’d suggest you register and post on my forum, as there is a thread for the BlackMagic probe on STM32 devices and also people could help you load the bootloader we use for Arduino uploads, just to prove you Maple mini isnt blown up.
    You could then reflash with the BMP binary

  43. JustSomeMathematician

    Thanks Roger. Joined the forum.


    Just had to hold ‘but-32’ when plugging in (as you stated in your instructions ) and the ST Link GUI recognized it again :))))

  44. Roger Clark


    I bet you are relieved 😉

  45. Roger Clark

    I have some pre-built binaries in my various github repos, take a look at those.

    Note. Since I originally posted this blog entry, BlackSpehere radically restructured their code for the BMP, and they no longer make compiled binaries available which can be put onto a blank device.

    Blacksphere now only make “upgrade” binaries available, as these are for users of their new codebase who bought official hardware (but they would work with unofficial hardware if you had already built and flashed their new “bootloader” system onto your device). This is probably because they are now selling an offical BMP board via kickstarter for much much more than you can make it yourself.

  46. JustSomeMathematician

    Thanks will take a look at them. 🙂

    Thank you for the warning!

    Just stuck on GDB/ softdevice.
    COM ports are:
    COM3 ( Silicon Labs CP210x USB to UART Bridge)
    COM4 (Black Magic GDB Server)
    COM5 (Black Magic UART Port)

    GDB only recognises COM3. For COM4 it returns: ” \.\com4: No such file or directory”. Same with COM5.

    Trying COM3 (out of interest) returns:”
    Remote debugging using \\.\com3
    Ignoring packet error, continuing…
    warning: unrecognized item “timeout” in “qSupp
    Ignoring packet error, continuing…
    Remote replied unexpectedly to ‘vMustReplyEmpt
    “monitor” command not supported by this target
    Don’t know how to attach. Try “help target”.

    Before trying the softdevice step, I tried to upload Blink from Arduino IDE. I managed to get the console to return:
    “Target voltage not Implemented!
    Available targets:
    No. Att Driver
    1 Nordic nRF51 0x0084”

    But this only happened once. Since then whenever I try to upload the sketch, the Arduino IDE just stays at halfway through upload showing errors in the console.

  47. John BALUCH

    Hi Roger, what about doing this with an nRF51-DK? It already has a programmer on the board

  48. Roger Clark

    I think you also posted the same question on YouTube, but just in case you’re not the same person…

    Yes. I would recommend you take a look at Nordic’s nRF5 SDK. Version 12.1 or 12.2 (not the V13 alpha as its an alpha 😉

    If you already have gcc installed via the Arduino board manager package etc, you can compile any of the examples in the SDK by digging down to the armgcc folder inside the board name inside each example and then just run Make

    To upload via BMP rather than Nordics nrfjprog you’d need to write a new rule / command in the make file to use GDB instead of nrfjprog but that would not be hard to do as the GDB command for the BMP can be copied from the one in the Arduino core platform.txt file

Leave a Reply