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.

224 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

Leave a Reply