Bluetooth Low Energy (BLE) Shield for Arduino v1.0.0
The Bluetooth Low Energy (BLE) Shield for Arduino is intended for makers to connect an Arduino project to Bluetooth Smart Ready devices such as the iPhone 4S. At this time there are some solutions available on the market to connect an iPhone to hardware projects, such as the headphone jack, Wifi or a serial cable connected to the 30-pin connector.
Now is the time for new BLE accessories, since Bluetooth 4.0 is now available on the most recent Mac Mini and MacBook Air as well as the iPhone 4S. The new CoreBluetooth framework has become part of MacOS 10.7.3 and iOS 5.0 opening the doors for software developers to communicate with accessories and sensors of any kind.
I will not describe the new Bluetooth 4.0 standard here, which is also known as Bluetooth Low Energy (BLE), but focus on one of my latest development projects which is dedicated to the Arduino community. Maybe you are already familiar with one of my “Classic Bluetooth” hardware development projects: The Bluetooth RFID Tagreader (http://www.mkroll.mobi/?page_id=202) which I finished some weeks ago.
Right after my first contact with BLE technology I was thinking of a BLE Shield for the Arduino. I really liked the Arduino BT or the Bluetooth Shield from Seeed Studio, but since these boards are not “Made for iPod”, they are not connectable to iOS Devices. The idea I had in mind was to create something similar to the Seeedstudio Bluetooth Shield where the Arduino’s Serial RX/TX pins or Pins 2/3 (with SoftwareSerial) can be used to read data from and send data to an iOS device. That said, I started a first prototype of an Arduino Shield using Bluegiga’s BLE112 Bluetooth 4.0 single mode Development Kit. Once the firmware for the BLE112 module was finished and my first Arduino sketch could read and write to my iPhone 4S, l then I created my first real Arduino Shield PCB (see below).
The Arduino Shield is equipped with a small switch to change the RX/TX pins from 0/1 to 2/3 and a TXB0104 level shifter, so that the shield can be used with 5V and 3.3V Arduinos and Arduino Clones as well. I tested the BLE Shield with an Arduino UNO, Arduino Diecimila and a Arduino Pro (3.3V/8MHz). Once connected to a Bluetooth Smart Ready device such as an iPhone 4S the blue “CONNECTED” LED will turn on, indicating that the BLE Shield is connected. (On the initial prototype shown in the images above, the Reset switch is not yet populated, since the order has not yet been delivered.) The UART of the BLE112 module is configured to run at a baud rate of 19200 with 8 data bits, 1 stop bit and no parity.
Finally I came up with a first firmware for the BLE112 module allowing to read 16bytes from the iPhone 4S of write 16 bytes of data to the iPhone 4S (any other BLE capable smartphone, notebook or desktop computer may work). 16 bytes may not sound like much, but BLE is intended for exchanging small amounts of sensor data, not for for streaming data. I decided to use a 16 bytes buffer, since the iOS implementation of CoreBluetooth on the iPhone 4S supports just 21 bytes per characteristic; so 16 bytes seems to be a good compromise.
I created the following GATT database for the BLE Shield:
- Generic Access Profile (UUID 1800)
- Device Name (UUID 2A00)
- Appereance (UUID 2A01)
- Device Information (UUID 180A)
- Manufacturer Name String (UUID 2A29)
- Model Number String (UUID 2A24)
- Firmware Revision String (UUID 2A26)
- Hardware Revision String (UUID 2A27)
- BLE Shield Service (UUID F9266FD7-EF07-45D6-8EB6-BD74F13620F9)
- BD-Addr (UUID 38117F3C-28AB-4718-AB95-172B363F2AE0) “READ”, fixed size of 6 bytes
- RX (UUID 4585C102-7784-40B4-88E1-3CB5C4FD37A3) “READ/NOTIFY”, fixed size of 16 bytes
- RX Buffer Count (UUID 11846C20-6630-11E1-B86C-0800200C9A66) “READ”, fixed size of 1 byte
- RX Buffer Clear (UUID DAF75440-6EBA-11E1-B0C4-0800200C9A66) “WRITE”, fixed size of 1 byte
- TX (UUID E788D73B-E793-4D9E-A608-2F2BAFC59A00) “WRITE/READ”, variable size up to 16 bytes
The BLE Shield Service is a 3rd party service, so it must use a 128 bit UUID for the service and characteristic as well. The RX Characteristic’s attribute is set to read and notify as well. So the connected device can enable this characteristic to be notifiable, where the BLE Shield “notifies” for instance a connected iPhone, that there is new data available.
Currently the firmware for the BLE Shield has a small protocol implemented, such that the BLE112 module must receive a complete array of 16 bytes before it is notified.To read the status of this 16 byte buffer, the BLE Shield Service provides a characteristic “RX Buffer Count”. An additional “RX Buffer Clear” characteristic is provided to reset this count.
For testing the BLE Shield connectivity I created a first version of an iPhone 4S app called BLExplr acting as a generic Bluetooth Low Energy discovery/explorer application.
A simple sketch, for Arduino 1.0 using SoftwareSerial, to test the BLE Shield with S1 on the Tx2/Rx3 position is provided here:
#includeSoftwareSerial bleShield(2, 3); long previousMillis = 0; long interval = 1000; void setup() { // set the data rate for the SoftwareSerial port bleShield.begin(19200); Serial.begin(19200); } void loop() // run over and over { unsigned long currentMillis = millis(); if(currentMillis - previousMillis > interval) { previousMillis = currentMillis; bleShield.write(0x4D); bleShield.write(0x2A); bleShield.write(0x10); bleShield.write(0x20); } if (bleShield.available()) { Serial.write(bleShield.read()); } }
Another simple sketch, for Arduino 1.0 using the hardware serial port on pin 0/1, to test the BLE Shield with S0 on the Tx0/Rx1 position is provided here:
long interval = 1000; void setup() { Serial.begin(19200); pinMode(13, OUTPUT); } void loop() // run over and over { delay(interval); // wait for 1 sec digitalWrite(13, HIGH); // set the LED on Serial.write(0x4D); Serial.write(0x2A); Serial.write(0x10); Serial.write(0x20); delay(interval); // wait for 1 sec digitalWrite(13, LOW); // set the LED off }
Now attach the shield to an Arduino where the above listed sketch is installed. Power on the Arduino and you see that the “CONNECTED” LED on the BLE Shield is not yet flashing.
Turn on the BLExplr application on the iPhone 4S and pull down the “Scan” table to enable scanning for new devices for about 20 seconds.
On the right screenshot you can see two BLE Shield entries. The second one contains the Service UUID (F9266FD7-EF07-45D6-8EB6-BD74F13620F9) of the shield which is advertised. Select this entry in the table and BLExplr will connect to the BLE Shield. The blue “CONNECTED” Led on the shield will now turn on and the UI in BLExplr will show the services of the new connected BLE Device (Peripheral).
The BLE Shield Services and Characteristics as already discussed earlier about the GATT database.
On the right screenshot where the characteristics are listed you can see the “Read/Write/Notify” attributes of the defined characteristics. BLExplr has a dictionary included, so the app knows which UUID corresponds to the human readable service and characteristic name. In order to text the TX characteristic I started a Arduino IDE with serial console opened with the correct baud parameters (19200) and serial port selected. To send some bytes (max. 16 at once) tap on the TX table view entry to open the dialog to enter TX data:
After hitting the “write” button the data is immedeately written to CoolTerm listening for data on the serial line. “Hello World” is sent and as illustrated in the screenshot, received over the serial line.
On the receiving part there are some more steps on BLExplr to get the the “RX” characteristic enabled to be “notifiable”, but just one tab away. In the characteristics table just tap on the RX characteristic. BLExplr will read the data which are currently stored in this characteristic. Since the Arduino Sketch will write 4 bytes 0x4D, 0x2A, 0x10 and 0x20 every second, the buffer is already filled, but not notified since it needs to be enabled. Tap on “Enable Notify” to enable this characteristic to be notified.
Now you can even close BLExplr by clicking on the iPhone’s home button. The Bluetooth Stack will notify you if there is new data (every 4 seconds) from now on. If the app runs in the background an a characteristic is to be notified, the app behaves like shown in the following screenshots. If you open BLExplr the data of the RX characteristic is again shown to the user.
The simple Arduino sketch and BLExplr are just used to show the functionaltity of the BLE Shield. They are not intended as a “real-world” application. More sophisticated sample applications are in preparation, and final fixes of the BLE Shield needs to be done as well. Please comment on this using the Blog’s build in comment functionality in order to let me know about additional functionality, or how you’d like to use the board.
I will have a small amount of revision B prototypes available for purchase here on this blog’s shop which will be enabled once the boards are available.
Update 2012-03-28: The Arduino Sketch seems to have some errors. I updated the Sketch code and added a side note that Arduino 1.0 is necessary to compile it.