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.
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?”
The list of needed devices/program tools is as follows:
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.
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:
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:
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:
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.
In this part we’ve learned that it is not that complex to make the Bluetooth-driven device with Arduino. Right now we know:
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!
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.