The NRF52 chips from Nordic should be much better
for Bluetooth Low Energy projects than the ESP32. Is this true? Can we use
them with the Arduino IDE? And which boards are available?
Let’s have a closer look.
Grüezi YouTubers. Here is the guy with the Swiss
accent. With a new episode and fresh ideas around sensors and microcontrollers. Remember: If you
subscribe, you will always sit in the first row.
I used Bluetooth Low Energy or BLE on the ESP32
as a presence detector or to play with the Polar Heart Rate Monitor. When I measured the energy
consumption, it became clear that the architecture of the ESP32 is not made for battery-operated
BLE. It consumed way too much energy. Back then, I already knew that Nordic Semiconductors
specialized in BLE chips. But they used a proprietary IDE which was not acceptable to me.
Times have changed since then.
Let’s start with the boards. A few years ago,
Adafruit and others made boards with the nRF52832. It offered BLE and a powerful ARM Cortex M4F
but only had 512KB flash and 64KB SRAM. Later, NRF52840 boards came to the market. They had
the same processor but 1MB flash and 256KB SRAM. The BLE transmitter also has 8dBm power
instead of the 4 of the predecessors, leading to a bigger range. So it makes no
more sense to buy a board with the old chip, and we focus on boards with the NRF52840.
To my knowledge, Arduino had the first board with this chip: The Nano 33 BLE. It has
a ublox module aboard. I showed this board already in video #298 in November
2019. But I have never used it so far.
Adafruit offers this chip on several
boards: The Feather and the ItsyBitsy. Both use the same module. They only differ
in the form factor and the peripherals.
The next board I found is the Makerdiary. It
has the shape of a USB dongle. Because all NRF52 boards offer native USB support, they
do not need a separate USB to serial chip.
Then I got this tiny XIAO board from Seeeds. It seems to be ideal for a beacon
or a sensor application. Sparkfun and others also have boards that I do not
own. So we have a choice of boards available.
And now comes a more complex topic: How to
program these boards with the Arduino IDE.
In 2016, Sandeep Mistry already started to
write an NRF52 support for the Arduino IDE.
The Arduino Nano 33 BLE was introduced
around 2018, and it was supported by the “Arduino Mbed OS-Core” and a “Bluetooth library.”
I did not find a reference to Sandeep’s work. So I assume this is an independent development.
The Arduino version only supports their boards.
Then came Adafruit with their version,
and they included another BLE library: Bluefruit. The Adafruit core also supports
a few non-Adafruit boards. And the Bluefruit library seems to be quite comprehensive, with
lots of examples and good documentation.
The Adafruit version seems to be based on
Sandeep’s version. At least they support his project, and I found his copyright in some of
the Adafruit files. Maybe you know more about the relationship between the two and which
one is better. Sandeep’s core supports a few boards but not the ones from Arduino or Adafruit.
Then came Seeeds. Yes, with three “e.” They came up with yet another core. Also, here, I
do not know if it is based on Arduinos, Sandeep’s, or Adafruit’s work. But it
only supports its own XIAO boards.
So the situation is not good. Too much choice
if you ask me. It seems that Nordic is not interested in the Maker community and does not
step in as Espressif or STM did. So I had to decide. Because I have two Adafruit boards and the
Bluefruit examples, and the documentation is good, I went with Adafruit. Because I made a mistake
while upgrading the bootloader of one of my Adafruit boards, I discovered that I could also
use the XIAO board with Adafruits code. I just flashed their bootloader to the XIAO, tweaked
the boards.txt, and created new variant files. Because I did not want to use the GPIOs of
the XIAO board, I mainly fixed the LED pins. Maybe somebody else already has those files or
creates and shares them with the community?
So we can use the NRF52
boards with the Arduino IDE.
A significant difference to most other boards we
use is that they have native USB support in the chip, and their bootloader creates a disk drive
when plugged into the PC. Upgrading the bootloader is easy. Just drag and drop the new version to
this disk and wait for a few seconds. The chip reboots and starts with the upgraded version.
Cool. Please refer to Adafruit’s documentation if you have an old board version.
The programming is done as usual with the Arduino IDE via a serial connection.
Because we have plenty of other good chips for projects without Bluetooth, I would not
choose the NRF52 chip for such projects. The chance that one or the other library does not
work is high. But BLE seems to be its strength. So let’s build a Bluetooth temperature sensor.
You remember from video #174: BLE offers two roles: Central and peripheral.
Peripheral devices are low-power, resource-constrained devices like heart rate
monitors or beacons. They connect to more powerful central devices like smartphones or tablets with
more processing power, memory, and battery.
The communication between these roles is based on
standardized Generic ATTribute or GATT Profiles. You find profiles for many applications
like the one for environmental sensing. Each of these services has a UUID.
Environmental sensing has 0x181A hex, for example. These services offer characteristics
like temperature with its assigned number 0x2A6E.
Now we only need a sensor and a sketch.
I found this BlueTherm sketch which reads the temperature and transfers it via
BLE. Let’s have a look at the sketch.
First, we have to define the service. Its name
is es_svc, environmental service. This is its UUID: UUID16_SVC_ENVIRONMENTAL_SENSING.
We found it here:
https://github.com/adafruit/Adafruit_nRF52_Arduino/blob/master/libraries/Bluefruit52Lib/src/BLEUuid.h
Then we have to define the characteristic.
We find its UUID at the same place. The UUIDs are standardized, but the names can
be defined by you. This part of the sketch is for reading the temperature from this 1-wire
sensor. We will not cover it in today’s video.
In setup() we start and read the sensor and start
BLE, as well as set some names. Then we define some parameters and start the service. It
will transport a two-byte integer value.
Because we want that our sensor
continuously transmits values, we start advertising. Now it should transmit
a temperature reading every 160x0.625ms=100ms. After 30 seconds, the interval is increased, BTW.
In “loop,” we regularly measure the temperature and replace the transmitted
value. No sleep function is used.
After uploading the sketch to an ItsyBitsy board,
we can check if it works. We start NRFconnect on our Smartphone. And really, we see a device
called “BlueTermDevice,” the name given to our ItsiBitsy. Its signal strength is -64dBm,
which suggests that it is close to the iPhone. We can now connect to it and see the environmental
service with the UUID 181A. And we see the Model number string we defined before. But the essential
characteristic is the temperature, of course. If we heat the sensor, the value changes. So Our
BLE sensor works. Now we could create or find an app that displays the temperature.
Next, we want to create a device that receives BLE messages. The typical
example is to receive the signal of a polar heart rate monitor. Here I use the example
file. It displays my heart rate. You see: Creating YouTube videos is not arduous.
The heart rate stays relatively low!
So the NRF52 chip can be used to create
sensors as well as receivers for messages. This is very similar to the ESP32. No
need to change to a new chip family. Now comes the most crucial question: Can we create
a really low-power battery-operated beacon?
Let’s use the XIAO for this test and create a
standard Apple beacon using the Adafruit example file. To test it, I use the NRF beacon app.
It should show the Mona Lisa when the iPhone approaches the beacon. You can imagine that such
a beacon could be used for many applications. But it has to be low power if we want to run it
on battery. Looking at the sketch, we see that the loop is empty. So the loop does not consume
energy. Very good! And even better, a command called suspendLoop() promises even to save more
power. So let’s try. I connect the tiny XIAO board to the power profiler Kit II to measure its power
consumption. Here is the result: Large peaks up to 15mA, small peaks of about 2mA, and nearly nothing
in between. The large peaks appear every 100mS. This is what we selected here: 160x0.625mS. The
average power consumption is 590 microamperes, which is very low compared to the ESP32, which
consumes many milliamperes for the same purpose.
Let’s stretch the time between peaks to 200mS. Now
the power consumption is 100 microamperes less. If we extend the interval to 1 second, it only
consumes 400 microamperes on average. Using 2 AAA batteries with a capacity of around 1000mAh, it
would run 1000/0.6=1666h or 69 days. With a 1sec interval, it would be 1000/0.4=2500h=104 days.
Not bad for a device that continuously transmits.
The temperature sensor consumed 890 microamperes
with fast advertising and 717 microamperes with slow advertising. Also a good
value compared with the ESP32.
Did we reach what we wanted? Yes and no.
- We found different boards with the NRF52840 chip in different form factors
- This chip is supported by the Arduino IDE. Unfortunately, at least three different
solutions exist. All of them support different boards. So we have to decide which one we choose
according to the hardware we want to use.
- It would be much better if Nordic would
decide on one and support it for all boards
- I decided on the Adafruit solution because it
has a lot of examples and good documentation
- I was able to create a BLE temperature
sensor and a Polar Heart Rate Monitor. So both scenarios, peripheral and central, are supported
- And I measured the power consumption of a simple beacon. It was below 600 microamperes which is
much, much better than what I measured with the ESP32. But it is still higher than a standard
beacon which runs for one year on a simple coin cell. Some of those beacons also use the NRF52840
chips. So I assume it should be possible to reduce the consumption further. One possibility was to
extend the transmitting interval to 1 second. This led to a 30% reduced power consumption
- For sure, the microphone and the IMU sensor of the XIAO sense consume some current,
but I could not find out how much. And I have no standard XIAO board available
- Maybe you know some tricks to reduce current?
This was all for today. As always, you find
all the relevant links in the description.
I hope this video was useful or
at least interesting for you. If true, please consider supporting the channel
to secure its future existence. Thank you! Bye