*BLEthingy* is a battery-powered, wireless sensor node device. The device is powered by a CR2032 coin cell battery and is designed to consume as little power as possible. Wireless connectivity is provided via Bluetooth Low Energy (BLE), which allows for low-power transmissions e.g. as a so called [Beacon](https://en.wikipedia.org/wiki/Bluetooth_low_energy_beacon). The BLE functionality is provided by a RN4871/I module. It communicates via UART with the main microcontroller. The microcontroller used is an ATtiny3216 which is part of Microchip's new tiny-1 series. Despite the name, it's very different than the previous ATtiny's (like the ATtiny85, ATtiny13 etc.) and has a lot more features and peripherals. Another feature I wanted to include is motion detection, for this purpose an accelerometer ADXL345 is used. In this part I'll talk about the hardware design of the device.

**BLE module RN4871/I**

This module provides the BLE communication for *BLEthingy*. It communicates with the ATtiny3216 via UART with commands (like using the terminal on the PC). Additionaly the reset-pin and the wakeup-pin of the module are driven by the microcontroller. Using the wakeup-pin, we can put the RN4871/I to sleep and wake it up if we need to. This way we can minimize power-consumption drastically. The module is mounted on the PCB with castellated pads. During the PCB layout, it was necessary to respect some special requirements. There are areas beneath the module that shouldn't be filled with a ground plane, luckily in the KiCAD library the component was already available and the footprint was correct. Additionally, the datasheet advises to let the antenna side of the module stand out of the PCB for better performance. In my case I didn't do that because I wanted the PCB to be round. Instead I added a cutout in the PCB beneath the antenna area.

Soldering the module by hand turned out a bit tricky because of the fine pitch of the pins, but in the end it turned out ok.

**microcontroller ATtiny3216**

The ATtiny3216's UART lines are connected to the RN4871/I module. The ADXL345 accelerometer is connected via SPI. Additionally, a small pushbutton and an LED are connected to the GPIO pins.
One thing to note is that the ATtiny3216 (like all members of the new tiny-1 series afaik) uses a new programming interface called **UPDI** instead of the the old **ISP**. One advantage of it is, that it uses only a single pin (in addition to ground and Vcc naturally). Alas, I feared that I would need a special programmer. Luckily I found the [**pyupdi**](https://github.com/mraardvark/pyupdi) project. As it turns out, the UPDI protocol can be implemented using a simple USB-Serial adapter by connecting the TX and RX lines with a resistor. Then using the pyupdi python script, the program can be flashed. It worked very well, with minor inconveniences: for me, it worked only with a low baud rate. Thus, programming took quite long. Sometimes it happened, that after programming a few times, it didn't work anymore. Then I had to unplug and plug in the USB again for it to work. But still it is a great project that allows to use these new microcontrollers without buying a dedicated programmer.

**accelerometer ADXL345**

The ADXL345 is a 3-axis accelerometer. It can communicate via I²C or SPI, I chose SPI because of the much higher data rate. This means that the microcontroller and the accelerometer have to stay awake for a much shorter time period which reduces power consumption. The ADXL345 comes in a really tiny package, which is almost impossible (for me at least) to solder by hand. Thus I chose to use a breakout board with the accelerometer and mounted the board on the PCB with pin headers. I used a breakout board named *GY-291* which can be found easily and is very cheap (almost the same price as the accelerometer itself in sigle quantity!). To use the breakout board, I had to modify it slightly: the GY-291 board has a 3.3V voltage regulator which I didn't want to use since I use the 3V from the battery. Thus I removed the regulator IC and bypassed the voltage supply with a small wire. Additionaly, I removed the pull-up resistors from the board. Since I use SPI and not I²C I don't need them, and they would dissipate power unneccessarily. Additional info about the required modification is [here](https://github.com/MarcelMG/BLE_thingy/blob/master/hardware/GY-291_mod.md).
The ADXL345 has several advanced features, one is the activity detection feature. The accelerometer can be put in sleep mode, where it samples the acceleration values and compares them to a preprogrammed threshold. If they exceed the threshold, activity is detected and the INT pin is triggered, which can wake up the microcontroller from sleep. This way, a very low power-consumption can be achieved.

**power**

Power for the device is provided by a CR2032 lithium coin cell battery. Those have a nominal voltage of about 3V and a capacity of roughly 200mAh when discharging down to 2V. I chose them for this project because they are readily available and cheap, and because the voltage is appropriate to drive the electronics directly without regulation. To protect the electronics against reverse polarity when the battery is insterted the wrong way, I used a circuit with a MOSFET. This has the advantage of much less power loss compared to a diode. You can read more about this in this [Application Note](http://www.ti.com/lit/an/slva139/slva139.pdf) from TI.

In the next part, I'll talk about the software side of this project.

-------------------------------------------------------------------------------------------------------------

In this part I will talk about the software part of my recent project *BLEthingy*. All the software for the microcontroller was written in C using ATMEL Studio. At first, I wrote some code to use and test some features of the microcontroller, like the UART (which is needed to communicate with the BLE module), the internal Real Time Clock (RTC) which can periodically wake up the microcontroller from sleep and the SPI peripheral needed to cummunicate with the accelerometer. Since the UART was already in use connected to the RN4871 module, I used a simple software UART transmitter to send messages to the PC for debugging.

The RN4871 module can be configured with commands sent via UART. [Here](https://github.com/MarcelMG/BLE_thingy/tree/master/software/Beacon_test) is an example that shows how to configure the module as a non-connectible BLE Beacon using sleep mode. This mode allows only to transmit the ID of the Beacon plus some additional data bytes. The advantage is, that no permanent connection (pairing) is needed and by varying the advertising (i.e. transmission) delay, we can achieve a very low power consumption. The BLE beacon advertisement data packet consist of a preamble (I chose the iBeacon format), the UUID (unique identifier of the device), two 2-byte variables called major and minor and one 1byte value to calibrate the TX-power to approximate the distance to the beacon. In my application I used the major and minor bytes for the data.
[This page](https://os.mbed.com/blog/entry/BLE-Beacons-URIBeacon-AltBeacons-iBeacon/) explains the different types of beacons and their data structure.

To use the ADXL345 I wrote a [library](https://github.com/MarcelMG/ADXL345_lib) for it from scratch. [This example](https://github.com/MarcelMG/BLE_thingy/blob/master/software/low_power_test_1/main.c) shows how to use the accelerometer's activity detection feature to wake up the microcontroller from sleep.

When finally all components worked well, I assembled them into a [demo application](https://github.com/MarcelMG/BLE_thingy/tree/master/software/BLE_beacon_activity_sensor). This application realizes the following features:
* motion detection with accelerometer
* use onboard pushbutton
* temperature measurement via the ATtiny3216's internal temperature sensor
* battery voltage measurement

The advertisement interval is set to 10s, i.e. every 10s an advertisement package is sent via BLE. The microcontroller is woken up by the RTC every 32s to measure the temperature and the battery voltage and to send those values. The accelerometer detects activity (motion) and can then wake up the microcontroller to send a message. So does pressing the pushbutton. The BLE RF-module is also in sleep mode unless woken up by the microcontroller. The average current consumption is approx. **190µA** with the above settings (intervals). During BLE transmission and when the LED is on, the maximum current is about 14mA. The current was measured with the oscilloscope across a 10 Ohm shunt resistor. This amounts to a battery life of roughly 1 month with the CR2032 coin cell battery.

Now, the data sent by the *BLEthingy* has to be received and processed for it to be useful. During the development for testing I used various smartphone apps for BLE and BLE beacons for debugging. For the final demo application, I wanted to process the data on the PC. Therefore I used a Bluetooth USB Dongle that supports BLE. I found a very cheap (4€) one labeled "CSR V4.0" that is supported also under Linux. You have to verify that it uses the CSR8510 (A10) chipset. First, it didn't work for me under Ubuntu. Then I found the solution in this [blog comment](http://blog.ruecker.fi/2013/10/06/adventures-in-bluetooth-4-0-part-i/#comment-318). You have to use a software called "BlueSuite" under Windows to configure the dongle in another mode (only once). Then it works fine under Linux.

To receive the data advertised by the BLE Beacon, I used the command-line tool *hcidump* to get the raw data. Then I wrote a small C++ program which reads in the raw data, extracts and parses the meaningful parts. Then it prints out the temperature and voltage and indicates if an event (motion or buttonpress) has happened. The program is exectuted with a script, which launches the BLE scan and pipes the raw data to the C++ program. The C++ program and the script can be found [here](https://github.com/MarcelMG/BLE_thingy/tree/master/software/BLE_Beacon_PC_application).

**Videos of the demo application**

Motion detection

Button press

This demo shows what can be done with the *BLEthingy*. One application would be e.g. to monitor if something has been moved, or a person fell or something similar. Using the free pins available on the PCB, additional sensors can be connected, e.g. to monitor environmental parameters and use the device as a sensor node. *BLEthingy* can be considered as a platform for many possible applications.