Request a free audit

Specialising in Digital positioning and marketing, we tackle challenging questions that yield tangible value. By the end, you’ll receive actionable tips ready for immediate implementation. Take advantage of our complimentary, no-obligation complete audits.

Social media audit Problem definition workshop ISO/SOC readiness

 

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

Date
February 16, 2018
Hot topics 🔥
Tech Insights
Contributor
David Roman
Make a Bluetooth-driven electronic device with Arduino at its heart — Part 2

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 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.

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.

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!

David Roman

David is one our marketing gurus. He loves working with content but has a good eye for marketing analytics as well. Creativity is what drives him, photography being one of his passions.

Working Machines

An executive’s guide to AI and Intelligent Automation. Working Machines takes a look at how the renewed vigour for the development of Artificial Intelligence and Intelligent Automation technology has begun to change how businesses operate.