Make a Bluetooth-driven electronic device with Arduino at its heart — Part 2

Written by: David Roman

icon of two teeth

By Sergey Gernyak, Back-end Engineer at WeAreBrain.


In part one of this two-part article series, we discovered how to make Arduino that is Bluetooth-driven and which boards could be used to achieve this. I also created a basic electronics schematic which shows how to connect the Arduino board and BLE board. Then finally we configured the BLE board using AT commands.

In this article we’ll investigate how to take it a step further — controlling the LED using the 3rd party iOS application — nRF Connect.

Prepare your Arduino sketch

In part one, the sketch that was created only allowed us to configure the BLE board, so, in order to perform some real work (in this case merely switch the LED on or off ) another sketch needs to be uploaded into the Arduino board. Taking into consideration what the Arduino needs to do — simply wait for specific information from the BLE board and react accordingly, this sketch will be a pretty simple one to write. The algorithm is formulated as per the diagram below:

The Arduino sketch, which implements the algorithm above is presented below:

#include <SoftwareSerial.h>

#define ledPin 7

SoftwareSerial mySerial(2, 3);

int val;

void setup() {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  Serial.begin(9600);
  mySerial.begin(9600);
}
void loop() {
 if(mySerial.available() > 0){
    val = mySerial.read();
    Serial.print("Received: ");
    Serial.println(val);
    if (val == 0) {
      digitalWrite(ledPin, LOW);
      Serial.println("LED: OFF");
    }
    else if (val == 1) {
      digitalWrite(ledPin, HIGH);
      Serial.println("LED: ON");
    }
 }
 delay(100);
}

So that we’re able to continue I uploaded the sketch into the Arduino board and then I connected the device from my iPhone.

Connect and control!

When I run the nRF Connect application and select the ‘Scan’ button I can see the device in the list:

Please, take note of the device’s name, BLEboard, which I changed using AT commands as detailed in the previous article.

Let’s connect to the device.

And now the iPhone is connected to the Arduino via the Bluetooth channel! Directly after that, I selected theUnknown Service and saw the following screen:

Please, note: the service UUID and characteristic UUID are exactly the same as what we saw when using AT commands in the previous article.

It’s time to control the LED!

By default the LED is on and should shine:

But let’s send a 0 data using Unknown Characteristic channel:

And…right after I selected the ‘Send’ button the LED turns off!

When you send 1 data the LED will turn on again:

If you’ve connected your Arduino board to the computer you’ll probably see a few debug messages in the Serial monitor window. In my case the debug messages were:

So what’s next?

We’ve followed a pretty cool roadmap from the planning device to prototyping the real one using Arduino, BLE board and 3rd party software. It’s a great start at making something more interesting and fun! As for me — I’ve decided I’m going to make a custom application (using either an iPhone app or OS X app) to control the LED because I’m not totally satisfied and happy with sending raw data like I’ve illustrated. So be sure to check in soon to see where I go next with my experiments with Arduino!

Happy hacking and have fun!

(Visited 28 times, 1 visits today)
David Roman
Author info
David Roman
David is our content & social media specialist. He helps with the planning, creation, and distribution of all the social content at WeAreBrain. He also loves green tea, listening to good music and using his analogue camera.
Close