Control an Arduino with Bluetooth

Control an Arduino with BluetoothHow to work with bluetooth-equipped Arduino devicesKenneth ReillyBlockedUnblockFollowFollowingJun 1IntroductionWith the popularity of DIY hobby electronics on the rise, building cool gadgets has become easier than ever.

Small microcomputers like the Arduino were a real game-changer for enthusiasts, as these are much more affordable and easy to use than standard microprocessor development boards.

In this article, we’ll look at how to hook up a Bluetooth module to an Arduino and then control and monitor it from a simple Android application built with Flutter.

This article assumes some knowledge of electronics along with elementary computer programming skills, however these are not required (and can be picked up from taking on and completing demo projects like these).

Bill of MaterialsTo build this project, you will need a few simple electronic parts, some of which can be substituted with not much difficulty.

Various Arduino boards should work fine, and the values of the resistors are not critical, so long as the voltage divider between the Arduino and Bluetooth module is comprised of two resistors with the ground-side resistor being double in value of the other.

Here are the parts you will need:Arduino boardHC-06 BT Module(1) 1k resistor — voltage divider signal-side(1) 2k resistor — voltage divider ground-side(1) 4.

7M resistor — capacitive sensor loadsmall (~2" x 2") sheet of metal foil, wrapped in paper5VDC power, breadboard, handful of jumper wiresAndroid device with bluetooth for testing the appWiring it UpWith the parts list out of the way, let’s take a look at how to hook this all up:Schematic showing Arduino, BT module, sensor, and resistorsHere we have a notepad sketch of the Arduino, HC-06 bluetooth module, metal foil sensor, and a few resistors.

The +5VDC and GND are wired as expected.

D2 on the Arduino receives data from the TX pin on the module directly, while D3 is connected to RX via the simple voltage divider created using the 1k and 2k resistors.

Pins D4 and D8 are bridged with a 4.

7 MΩ resistor with the paper-covered metal foil sensor connected to D8.

The voltage divider allows the Arduino, which utilizes 5V logic levels, to safely drive the input pin of the HC-06, which is designed to work with 3.

3V.

We only have to adjust the voltage in one direction since the 5V component can safely handle the 3.

3V output.

In reality, 5V is highly unlikely to damage the 3.

3V input on the HC-06, however it would be terrible design practice to start doing this just because we can get away with it once or twice.

Operating things outside of their intended range is basically asking for strange bugs.

The capacitive sensor works by raising the level of D4 to HIGH and waiting to see how long it takes for the voltage on D8 to reach the same potential as D4, and then capturing and processing the output of each of these sensor reads.

When someone touches the paper-covered foil sensor, a capacitor is formed between the foil, the paper dielectric, and the person’s conductive hand, which bleeds a small amount current from the D4-D8 circuit as it charges.

Once the foil-paper-human “capacitor” has finished charging, the voltage on D4 and D8 will be the same in reference to ground, and then the Arduino CapactiveSensor library does some math to get the final sensor value result.

Aside from that, we have the built-in LED on the Arduino, which we will turn on and off based on commands from the mobile test app.

That’s it for the hardware side of things, so now let’s take a look at some project code.

Arduino LogicThe Arduino code itself is kept simple by design, with only one short C++ file and two library imports.

Let’s take a look at the file arduino/bt_demo.

cpp:This is the entire program for the Arduino in all of its single-page glory.

First, the libraries SoftwareSerial and CapacitiveSensor are included, and then initialized to connect to the pins outlined previously in the schematic.

Next up is the setup() function, which is called once during initialization when the device is powered on or reset.

During setup, the LED_BUILTIN pin is placed inOUTPUT mode, the capacitive sensor is calibrated, and both the standard and bluetooth serial ports are initialized to 9600 baud.

The main program loop() is where the actual work happens.

First, the sensor library is instructed to grab a raw value over the average of 30 samples and that value is stored as current and then transmitted via bluetooth serial.

After a short delay, the bluetooth device is checked to see if there is any data coming down the line.

If so, a one-byte character is read from the device, and if the character is a ‘1’ then LED_BUILTIN is lit, otherwise it gets turned off.

That does it for the Arduino.

Next up, let’s take a look at the control app.

Demo Android ApplicationThe demo app for this project is a default Flutter app created with flutter create along with some basic UX and custom native code for handling the bluetooth connection itself.

The reason for the custom native code is because I had forgotten to power on my test board prior to attempting to pair it using a community package, and it wasn’t working.

I assumed the package had issues, so I made one right quick before realizing what happened.

C’ést la vie.

????Let’s take a look at the application entry point file, lib/main.

dart:The file lib/main.

dart contains the application UX, which consists of a title bar, the sensor readout, and the power button for turning on and off the LED on the board.

In this file we have some imports for dart:async along with the bt-controller.

dart file, the client side implementation of the native bridge.

When the application starts, the BTController class is initialized and then a list of available devices is requested from it, which is in turn displayed to the user with a non-dismissable dialog box.

When a device is selected, the controller is instructed to connect to it, at which point it will begin forwarding data read from the device to the handler passed into the initialization method.

As the MainPage class receives data from BTController, it will store the data in sensorValue using a setState() command which triggers a screen refresh.

When the FloatingActionButton is pressed, a single alternating character of either a ‘0’ or a ‘1’ is sent to BTController for transmission to the module.

Next up we’ll take a look at the file lib/bt-controller.

dart:The BTController class employs a MethodChannel to enable communication between the Flutter UX code in Dart and the native Android code for low-level bluetooth logic.

The init(Function onData) method initializes the method call handler and redirects calls to it to the onData function to pass messages back upstream to the view logic.

Methods have been defined to encapsulate and forward requests to the platform native code for convenience and readability.

We won’t go into detail about the platform-specific code for implementing the bluetooth logic itself since that is outside of the scope of this article, as this would often be handled by using a pre-made plugin such as flutter_blue or flutter_bluetooth_serial.

If you’re interested and you would like to check it out, the MainActivity.

java file can be found here, or you can choose to clone the whole project from here and take a look or experiment with it.

That wraps up the logic portion of this project, so now it’s time to check out how it works.

Testing It OutCompleted project with BT module ready to pairWith the board powered on and the program has loaded onto Arduino, the hardware is done.

The HC-06 will blink to indicate that it’s ready to pair.

Screenshot of the Android demo appNext, load the app on the phone and start it, and it should display a list of available bluetooth devices.

Select the HC-06 and after a brief moment to connect, everything is hooked up and ready to go.

When running, the app will display the raw value of the capacitor sensor, along with a button for turning on/off the Arduino’s built-in LED.

This button is totally unaware of the actual state of the LED.

Without any kind of feedback logic there is no way to ensure that the button “on” and LED “on” are always in sync.

This is a design limitation of our simple demo that could be solved by passing the LED state back up to the app.

I hope you enjoyed this article as much as I enjoyed writing it.

The open-source embedded tools and designs available today open up the world of electronics prototyping and experimentation to anyone with basic reading and math skills, and offer an incredible path for learning more advanced skills and concepts as time goes by, in a fun and exciting way that is driven by one’s own curiosity and doesn’t require the big up-front investment of a college degree program or on-the-job training (although both of these are excellent options for people considering a full-time career in engineering).

Thanks for reading and good luck with your next electronics project!Kenneth Reilly (8_bit_hacker) is the CTO of The Innovation Group, an R&D firm that specializes in solving challenging technical problems and assisting others with mastering the tools of tomorrow and putting them into production today.

Close-up of Arduino Nano with HC-06 in the background.

. More details

Leave a Reply