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.

175 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

    https://developer.mbed.org/questions/69178/NRF51-DK-I2C-not-working/

    which links to this

    https://developer.mbed.org/users/Cannonball2134/code/I2C_Scanner/

    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
    |

    Thanks

  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 http://www.ti.com/lit/an/swra347a/swra347a.pdf

    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 https://github.com/sandeepmistry/arduino-nRF5 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,

    Mircea

  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

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

    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 https://github.com/sandeepmistry/arduino-nRF5 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
    http://openocd.org/doc/doxygen/bugs.html
    debug_level: 2
    0x4000
    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,

    Mircea

  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: https://github.com/sandeepmistry/arduino-nRF5
    2. I have encountered the memory protection problem described above and found the solution here: https://github.com/interoberlin/nrf51-playground/issues/2
    3. I have installed openOCD for Windows from here: http://gnuarmeclipse.github.io/openocd/install/
    4. I have followed the instructions here: https://exploreembedded.com/wiki/Programming_nRF51822_with_STlink_on_Windows
    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: https://devzone.nordicsemi.com/question/1874/can-i-directly-start-programming-a-nrf51822-breakout-board/
    6. open a new cmd window and use this command
    telnet localhost 4444 (as described in the link at point 4.)
    7.in 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?

    Thanks!

    Mircea

  14. Roger Clark
    |

    @Mircea

    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 Arduino.cc 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:

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

  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 Arduino.cc etc use ( Arduino.cc 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

    https://github.com/rogerclarkmelbourne/Arduino_nrf51822

    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)

    BTW.

    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 https://github.com/sandeepmistry/arduino-nRF5 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.

Leave a Reply