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 1

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

By Sergey Gernyak, Back-end Engineer at WeAreBrain.

 


I really like electronics. I like doing fun experiments, and they’re pretty interesting to do because of the enormous number of electronics components out there. For me, what makes it especially cool is when I’m able to write some kind of computer program with my experiments because it becomes a tangible result that everyone can use, see and touch.

When I started to deep dive into digital electronics and, in particular, microcontrollers programming, I thought that the best way to learn is to start a specific project and apply what I learnt along the way in a practical format. It’s especially cool when I make the kind of progress and create something I can easily demonstrate to my wife and, even, my little son (my dedicated test subjects) ;-). And in one of these moments, one of my favourite ideas materialised! I have an old remote-controlled car and it is controlled via radio waves. I began to think, what if I wanted to control this car from my phone or, even, my MacBook? So, what I decided I would do was — rebuild the car’s electronic heart using Arduino and add the ability to control it by Bluetooth. Sure it doesn’t sound that easy … Fortunately, I received an Arduino UNO board as a birthday present. Vision becomes reality 😉

In my mind’s eye this car will actually eventually end up with a number of features but for the moment the main aim and desire is for it to be Bluetooth-driven. So let’s start with that.

Ways to use Bluetooth with Arduino

There are several ways to achieve this. The first and possibly the most simple one is to get the Arduino compatible board with a Bluetooth controller on it already. For example, BLEduino or Bluno. However, I already have an Arduino board, just not one with a Bluetooth chip on it. But this is one of the first reasons that I love Arduino — there is a very very large number of boards to extend your Arduino capabilities and, even, libraries for them. So, I’ve decided to buy the additional Bluetooth board. I mean “Hey, why not?”

What we will need?

The list of needed devices/program tools is as follows:

  • Arduino UNO rev 3 board
  • HM-10 BLE board
  • LED
  • 220 Omhs resistor
  • Arduino IDE — to write code and upload it into Arduino board
  • nRF Connect iOS application — to connect and debug the device

Regarding the Bluetooth board I have one specific remark: you’ll need the one which supports BLE standard, which means the Bluetooth version should be 4.0 and above. It’s an important to note requirement because even my iPhone 5S could not connect to Bluetooth devices with versions below 4.0. I made that mistake when bought the HM-5 board which provides Bluetooth 2.0. You live and you learn 🙂

The LED and resistor will be need to be controlled first via the Bluetooth. It may sound a bit obvious, but this is the best starting point. After all, the LED could, of course, be replaced by a circuit with the DC motor which is controlled by a transistor switch.

Electronic schematic

Let’s begin by assembling all electronics parts into a schematic.

We will use PB0 pin of the Arduino to control the LED: when we set the output value to the highest level the led is on and vise versa.

The connection of the BLE board is a touch more complex, but still pretty straightforward:

  • BLE board VCC pin => Arduino 5V pin
  • BLE board GND pin => Arduino GND pin
  • BLE board TXD pin => Arduino PD2 pin
  • BLE board RXD pin => Arduino PD3 pin

As you may notice we use Arduino to supply the BLE board power. We can use a separate power supplier as well.

Also we wouldn’t use WAKEUP and STATE pins of the BLE board as our usage scenario doesn’t need them.

So, it’s time to show my assembled schematic.

The Bluetooth chip, which is the heart of the BLE board, provides some points for configuration — ’so called’ AT commands. Using them you can query or set the following parameters:

  • query module MAC address
  • query/set advertising type
  • query/set advertising interval
  • query battery information
  • query/set baud rate
  • query/set a name of the module
  • query/set service UUID
  • query/set characteristic UUID
  • …. And many more other options

Basically, you can use the BLE board with its default configuration, but lets try, at least, to query some of them and, for instance, change the module’s name. So, right now I’ll use the following configuration options: name of the module, service UUID, characteristic UUID. The last two ones will be useful, when we try to create an application to connect to our Bluetooth-driven device from the Mac OS.

To query/set BLE board’s configuration options we have to upload the special sketch into the Arduino and send AT commands via the serial monitor window to the BLE board. The source code of the sketch is presented below:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX

int i = 0;

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if (mySerial.available() > 0) {
    char reply[100];
    i = 0;
    while(mySerial.available() > 0) {
      reply[i] = (char) mySerial.read();
      i += 1;
      delay(1);
    }
    reply[i] = '\0';
    Serial.write("Received from BLE board: ");
    Serial.write(reply);
    Serial.write("\n");
  }
  if (Serial.available()) {
    char command[100];
    i = 0;
    while(Serial.available() > 0) {
      command[i] = (char) Serial.read();
      i += 1;
      delay(1);
    }
    command[i] = '\0';
    mySerial.write(command);
    Serial.write("Command is sent: ");
    Serial.write(command);
    Serial.write("\n");
  }
}

Here’s a quick overview of the used commands:

  • AT — just a command to test device.
  • AT+ADDR? — query module MAC address.
  • AT+BAUD? — query module baud rate. In our case the module returns 0and it means 9600 (based on datasheet explanations).
  • AT+CHAR? — query characteristic UUID.
  • AT+NAME? — query module’s name. This exact name will be shown in the list of the available devices to connect to.
  • AT+PASS? — query the pin code
  • AT+ROLE? — query the module’s role. The my module returns 0 — it means peripheral .
  • AT+TYPE? — query module bond type. The response contains 0 , which means Not need pin code .
  • AT+UUID? — query service UUID.
  • AT+VERR? — query the version of the module.

The list of all the available commands is very long and you can find all of them in the data sheet for the module. Basically, I tried to query some parameters, but the majority of the commands have the version without a question mark on the end, so you can change some configuration options. For instance, I’ve changed the module’s name.

Conclusions

In this part we’ve learned that it is not that complex to make the Bluetooth-driven device with Arduino. Right now we know:

  • What BLE board we should use
  • Wow to connect it with the Arduino board
  • How to configure the BLE board using AT commands

In part two I’ll write another sketch and use nRF Connect iOS application to connect and send just a bit of information to control the LED’s state, so be in touch with WeAreBrain blog updates!

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.