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

242 Responses

  1. Roger Clark
    |

    No worries.
    Its hard to find time for this nowadays, but hopefully I’ll be able to make some new blog posts soon.

  2. mamrezo p
    |

    hello
    i have QFAAH1 and accidentally erased the chip and also MBR section with jlink, what should i do for bring MBR back.

  3. Roger Clark
    |

    AFIK there is no such thing as a Master Boot Record (MBR).

    When you say MBR, I presume you mean the SoftDevice and possibly the bootloader. The SoftDevice is flashed into the start of flash, and can use a vector address, stored near the top of flash, to call a bootloader. I think you can put the bootloader in any free location in Flash as long as you set the vector address to the same address in Flash that you put the bootloader

    So if you’ve just erased the SoftDevice (SD), just download the version of the SD from Nordic’s site and upload it back into the start of the Flash

    If you used a bootloader as well as a SD you will need to find the original file that is used to flash the SD + bootloader.

    If you don’t have that, you would need to do a lot of detective work to figure out what version of the SD you needed etc etc

  4. mamrezo p
    |

    thanks for your reply
    my problem is when first time i flashed SD s130 v1 hex file with jlink and then uploaded a blink example (using arduino) with jlink (because BMP didn’t work for me) the program worked, but when i accidentally erased all chip and then did all these steps, my programs didn’t work.

  5. Roger Clark
    |

    Which board are you using?

    Did you try to reflash the S130 SD ?

    BTW. I use JLink as well as BMP

  6. mamrezo p
    |

    yes i reflashed the S130 SD but not working 🙁

  7. Roger Clark
    |

    I dont think my repo based on the RedBearLab repo has ever worked with those module.

    The don’t have the 32Khz RTC crystal which the RedBearLab repo assumes is present.

    My repo has been superseded by Sandeep Mistry’s Arduino Core for nRF5

    https://github.com/sandeepmistry/arduino-nRF5

    I think his repo supports boards without the 32Khz crystal.

    Or, you will have to solder an external 32Khz crystal and appropriate capacitors to your board (I did this on one of those boards and it made it work), but I’d still recommend you switch to using Sandeep’s repo as its much better than my version

  8. mamrezo p
    |

    You are right but why first time (blink example) worked?
    When I upload blink example to address 0x00000 it works, but at address 0x1c000 (sd s130 v1) I doesn’t work.

  9. mamrezo p
    |

    by using this repo https://github.com/sandeepmistry/arduino-nRF5 my problem solved
    thank you very much

  10. Roger Clark
    |

    Depends on how you build it.

    Normally you can’t simply put the code binary anywhere in flash, as the compiler (linker) will have absolute addresses in some parts of the code.

    So to run at a different location in Flash you usually have to change the linker file (.ld) and often a Vector Table #define value or variable.

    The reason the code does not run without the external 32khz crystal osc, is that the original RedBearLab cod, came from MBED (www.mbed.org), and at the time that RBL took their copy of the mbed code, the startup function waited for the 32kHz oscillator to startup, and there wasnt a time-out on this.

    My guess is that the startup code must be different from running Blink without SD from the build options for it with SD, and the startup code probably sets the SD configuration to use the external 32Khz osc rather than using alternative methods (the internal RC oscillator) – hence the startup code had to wait for it to start

    I think in later versions of the mbed core code, this was changed, so that if the 32Khz osc did not start within a defined timeout, it was assumed to be not fitted and the rest of the code configured the SD to run using the RC osc

  11. Roger Clark
    |

    Excellent 😉

  12. Roger Clark
    |

    See my other reply about the 32kHz crystal 😉

  13. mamrezo p
    |

    Actually when I used https://github.com/sandeepmistry/arduino-nRF5 and uploaded sd s130 v2.0.1 with nRF5FlashSoftDevice.jar the program worked. After that i uploaded sd s130 v1 with jlink and now I am using your repo with no problem without using any external 32khz Crystal.

  14. dev tester
    |

    Thank you Roger for this excellent tutorial. I am about to purchase all the components you listed but I have a question regarding the end result. I’m guessing that this would be a good solution because it comes with a battery holder: http://www.ebay.com/itm/NRF51822-Bluetooth-4-0-Wireless-Module-for-Intelligent-Control-System-/332265862329?hash=item4d5c96b4b9:g:OP4AAOSwlMFZHlcw

    My question is how do you flash the program into this nrf51822 module? And would you be willing to make a tutorial? I’ll pay for the parts:)

    Devorrah

  15. Roger Clark
    |

    You need a SWD programmer.

    If you have a USB to Serial converter you can but a $2 STM32F103C8 minimum system development board ( also known as a BluePill )
    And flash the Black Magic probe firmware onto it.

    http://www.ebay.com/itm/STM32F103C8T6-ARM-STM32-Minimum-System-Development-Board-Module-For-Arduino-/311156408508

    Or alternatively look on eBay and buy a Jlink compatible programmer.
    I recently bought a STM32 based JLink compatible programmer for about $6, which is probably the cheapest pre-programmed programmer you could get hold of

    e.g.

    http://www.ebay.com/itm/1PCS-Jlink-for-SWD-Jlink-3-Wire-for-STM32-on-SWD-Debug-NEW-/152246311385

    But…

    You need to check if those boards have pads to access the 2 pins you need to program the device, i.e SWCLK and SWDIO

    Its really hard to tell from that photo whether the SWD pins are available, but there are 2 pads near the edge, and there is strong probability that those as the SWD pins.

    Also be aware that this module is the 16K RAM version (there is a 32k). 16k is OK for most things, but from what I recall its not enough to use a mesh node

    Also…
    I’m not sure what use that device has, because if 2 of the 3 available pads are for SWD, them only 1 pad is potentially available for your use. So this can only be used as a beacon.

    BTW. Sorry, I don’t have time to do any tutorials at the moment, as I have so many other community demands from STM32 and nRF5x

  16. dev tester
    |

    Thank you for the reply, Roger. You’ve been a tremendous help and you have enough material on the blog to set me on my way. I thank you. May I ask? How can you tell it is a 16k version? It is not in the specs. Regarding “mesh node” – Hmm. I’m not quite there yet but it sounds very interesting. I’d like to get the 32k versions just in case.

  17. Roger Clark
    |

    The part number printed on the device

    its the part that says QFAA

    QFAA = 16k RAM 256k flash
    QFAB = 16k RAM 128k flash
    QFAC = 32k RAM 256k flash

    I would never buy the QFAB version as the flash is only just enough to contain the SoftDevice, and leaves very little space for your own application code

  18. dev tester
    |

    I see. Thank you Roger. QFAC it is. I’m subscribed to your youtube channel and will be checking the blog regularly. When you get a chance to post new content, please know that you are an invaluable source of information.

  19. Roger Clark
    |

    No worries

    I have not had much time recently to do more video’s, but I have posted some blog entries about nRF51 and nRF52 smart watches, and some other things

    I hope to do more videos, but probably not for a few weeks.

  20. dev tester
    |

    Hi Roger, what is your preferred BLE device for production beacons?

  21. Roger Clark
    |

    I presume when you say production, you mean a commercial product

    In which case you need to choose a beacon which is certified for your target market e.g. USA requires FCC, but some markets have their own requirements.

    This also depends on your requirements, e.g is it just a beacon, how long does the battery need to last etc, does it need to measure anything etc etc

  22. dev tester
    |

    Hi Roger,

    Thank you for the reply. I meant to call it a production beacon because I wouldn’t be selling them as a product but rather I’d like to use them as part of a service for businesses that want to leverage this technology. I would need the barest of advertising data from the beacon i.e. UUID, Major, Minor, Power(not absolutely necessary) and I would maintain them in a way that is invisible to a potential client. I would like to customize the strength of the signal and frequency based on a ‘as need’ basis dependent on the project needs. The beacons would not need to measure anything. Also the less soldering I have to do, the better even though I am not entirely adverse to it should it be necessary all other things considered.

    Thanks, waiting for your reply

    Devorrah

    ALSO have you seen this SDK? http://newaer.com/ It looks very interesting.

  23. Roger Clark
    |

    Pretty much any nRF51822 based beacon would do this.

    BTW.
    You can control the transmission power, but controlling which BLE channels the devices uses is not normal. You’d need to go deep into Nordic’s SDK / API if you wanted to do that.

    You can measure battery voltage internally to the nRF51 so all beacons would do that.
    I think there is also an internal temperature sensor in the nRF51 (and nRF52) but I’ve not personally used it (though I will in the future)

    Re:
    http://newaer.com/

    This appears to be a SDK / API for App development

  24. dev tester
    |

    I C ok. Thank you Roger. Patiently waiting for the parts.

  25. Ofer Talmor
    |

    Hi Roger,

    Firstly, thanks a lot for your blog and videos: I have a system with S130 V1 SD and I can compile and upload to Waveshare BLE 400 easily using the Arduino environment.

    Following your remark about changing the nRF5 Arduino core from your repo to Sandeep Mistry’s one, I have tried the steps described in the readme file with very limited success. Going over the issues, I saw you had (similar?) problems (Issues #21 (https://github.com/sandeepmistry/arduino-nRF5/issues/21) and #22 for example. It would be great if you could provide some additional info about how you are using the https://github.com/sandeepmistry/arduino-nRF5:

    1. I’m using the Maple Mini STM32 with your modified BMP. Now, in the readme file there is a step installing installing the mbed serial driver, which refuses to install as there is no mbed microcontroller found. Looking at mbed Windows Serial driver supported platforms, it seems that STM32 is not supported.

    Did you skip this stage? Is it necessary if I wish to upload with the BMP?

    2. Flashing the S130 V2.0.1 SD:
    I could not do it following the readme instructions, however flashing it using the GDB worked OK. Is that OK? The Arduino Tools->Programmer has the ST-Link V2 which I assumed is close enough to the Maple Mini STM32 – is that right? Anyways, trying to upload a code (simple “blink”), caused some errors:

    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
    Error: open failed

    3. Next, I saw in issues #21 and #22 that you flashed the binaries directly (I guess with GDB?). What was the process? Does the target address being defined in the binaries? I saw that in Arduino IDE there is the Sketch->Export compiled binaries option. is that the first step? and then what’s next?

    BTW, why have you decided to move away from your own nRF51822-Arduino repo?

    Many thanks! Ofer

  26. Ofer Talmor
    |

    Hi Roger,

    Following my previous question: I can see that I can load via gdb the result .elf of a verify/compile (ctrl+R) within the Arduino IDE, on top of the S130 SD loaded previously. Is that how you use it or there is a better way?

    Thanks again, Ofer

  27. Roger Clark
    |

    Have you tried using my fork of Sandeeps Repo ?

    https://github.com/rogerclarkmelbourne/arduino-nRF5

    I added support for BMP uploads.

    But its been a while since I used it, so I can’t guarantee it works, but I think some other people told me it worked for them.

    Note, my fork of Sandeeps repo is a bit out of date as I’ve not had time (for a couple of months) to integarate bug fixes from sandeeps repo into mine

    Note. We did ask Sandeep to support the BMP but he said he didn’t want to do that 🙁

  28. Roger Clark
    |

    The SD sits in the start of flash and should not get overwritten by the application.

    So you only need to flash the SD once, (manually) and after that the upload of the application should just work, with no need to upload the SD again

  29. Ofer Talmor
    |

    It took me some time because I wanted to eliminate the option that what I’m experiencing is due to a too old Arduino IDE version, so I upgraded to the latest IDE.

    First, I removed the boards from Sandeep’s repo (when installing, I followed the readme file and provided a URL to the additional boards manager URL under preferences).

    Then, I downloaded the zip file from your repo at https://github.com/rogerclarkmelbourne/arduino-nRF5, extracted it and put the directory under the “hardware” directory.

    When opening the Arduino back (now, already in version 1.8.3, up from 1.6.13), there were no new boards (I see the ones from your previous RedBear Lab based core). running Tools -> Boards -> Boards manager gave the following warning:

    WARNING: Error loading hardware folder Z:\arduino_sketches\hardware\arduino-nRF5-master
    No valid hardware definitions found in folder arduino-nRF5-master.

    Doing the same with Sandeep’s repo (extracting the zip file and putting the directory under hardware directory), gave similar results (putting the URL as described in the readme file worked OK and I saw the NRF boards under tools->boards).

    Have you seen that previously? Unfortunately, i still can’t verify if I can use BMP directly from Arduino IDE with Sandeep’s repo. Any ideas please?

    As for the SD – I flashed it just once. At the moment I’m loading the .elf from the temp directory manually with the BMP (similar to the procedure you’ve described in issues #21 and #22).

    Many thanks!

  30. Roger Clark
    |

    If you download Sandeeps repo as a zip you need to put it into a sub folder e.g. make a folder in your Arduino/Hardware folder called nRF5, then put sadeeps arduino-nrf5 folder inside that folder

    (Well my fork of his repo)

    Now restart the IDE and you should see the boards

  31. Ofer Talmor
    |

    Thanks a lot for the hints.
    It took me some time to figure out that I still need to put “https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json” in the preferences (additional board manager) and install the nrf boards from the board manager as it “pulls down the toolchain” to “…/Arduino15/packages/sandeepmistry/tools” (see https://github.com/sandeepmistry/arduino-nRF5/issues/110).

    In addition, I think that for using BMP, you still need to install SAM or SAMD boards from the board manager.

    Using your forked repo, I’ve updated boards.txt and enabled the “Upload method:” menu item to be seen in the IDE (by adding menu.upload_method=Upload Method) and also added the BMP relevant lines to the Waveshare_BLE400 and the Generic_nRF51822.

    Now, I can use the BMP directly from Arduino where Tools->Softdevice: “None”. I use an example sketch to blink some led on the Waveshare_BLE400.

    However, if I select Tools->Softdevice: “S130” (the S130 SD version. 2.0.1 is already flashed with the BMP), with the same blink sketch, compilation is OK but uploading with BMP gets stucked and eventually fails.

    What do you think? Is it something related to tools.bmp_upload.upload.pattern in platform.txt? Some other thing?

    Many thanks!

  32. Roger Clark
    |

    I would try running gdb manually from the command line

    If you put the arm compiler bin folder in your path, you can run these commands in a bat file, and it should find the BMP and run GDB and then scan for the nRF5x and attach to it

    @ECHO off
    setlocal enabledelayedexpansion
    for /F "skip=1 tokens=*" %%a in ('wmic path Win32_SerialPort Where "Caption LIKE '%%Black Magic GDB Server%%'" Get DeviceID') do if not defined id set id=%%a
    Call Set "commport=%%id: =%%"
    arm-none-eabi-gdb -q -ex "target extended-remote \\.\%commport%" -ex "monitor swdp_scan" -ex "attach 1"

    You should be able to see where its failing, if its a connection thing.

    If it connects you will need to try using the loadbin command to upload a binary and see if that works

    Note. Check that the response to the connection is that it finds a nRF51 or nRF52,

  33. Ofer Talmor
    |

    Thanks a lot for the answers, Roger.

    It works with the CLI and the overhead is bearable: I run the verification process within the IDE where the SD S130 is selected and then copy the .elf file from the temp directory and manually loading it with gdb command.

  34. Roger Clark
    |

    I’m sure you could script it, to do the same thing as the command line

  35. Jun-jun Tan
    |

    Hi Roger.
    After I flash the binary file to the stm32 blue pill and use the microUSB to plug in my computer, I only have 1 unknown device. How come I didn’t have 3 unknown devices? Hope you can help thank you 🙂

  36. Roger Clark
    |

    I did not write the BlackMagic Probe firmware.

    If you have problems, you should contact Blacksphere.

    I see you already posted to gitter https://gitter.im/blacksphere/blackmagic so you need to wait for their response

  37. Radu-Nicolae Botezatu
    |

    Jun-jun Tam, avem you compile the blackmagic you need to compile it for stlink. For more details, read this article: https://gojimmypi.blogspot.de/2017/07/BluePill-STM32F103-to-BlackMagic-Probe.html

  38. Bram
    |

    Hi,

    I started with the sandeep repo and flashed the soft device. Then a blink sketch which worked. Now I wanted to try the simplechat from your repo but when compiling it gives the error: ‘BLE’ does not name a type

    when compiling the simplechat from redbear for their bord it goes wel as this include is added #include But when selecting the waveshare ble400 board of which i’m using it gives the error that the ble_api.h can’t be found.

  39. Roger Clark
    |

    Sandeep’s repo does not have built in functions for the BLE underpinnings which SimpleChat uses, and I also suspect that his BLE API is different from the RBL BLE API

    So you’ll need to download and install Sandeeps BLE lib(s) and also port SimpleChat to use his API.
    (Sandeep may already have ported SimpleChat or have a similar example that you can convert)

  40. hobb
    |

    Hi,
    I flashed successfully and detected as well. But I am not able to find out which are the pins PIN 6 and PIN 7 of STM32F103C8T6?
    I checked maple mini c8t6 diagram but there is not such pin mentioned.

  41. Roger Clark
    |

    I used the Maple Mini clone which has pin numbers, but the generic boards like the BluePill have STM’s own official pin names.

    Take a look at the pin mapping

    http://docs.leaflabs.com/static.leaflabs.com/pub/leaflabs/maple-docs/latest/hardware/maple-mini.html

    I think 6 is D6 = PA5, 7 is D7 = PA4

Leave a Reply