I did a post a while back for Mouser Electronics about controlling an Arduino-based LED strip via a Bluetooth-enabled Android app.  The app had three sliders, each controlling one of the three colors used in an RGB LED strip.  The code necessary to drive the LED light strip on the Arduino side was especially complicated, thus making the overall code a bit complex to follow.  Since that blog hit the Internet I have received numerous emails from folks asking me to solve problems with their code.  Most people are interested in simply making a Bluetooth connection and sending data from the Android device to the Arduino.  With that in my mind I set out to write a simpler project to highlight the minimum code needed for making the Bluetooth connection and sending simple strings.  In honor of David Tennant’s first appearance as The Doctor, I made a “Great Big Threatening Button” app.  In case you are not following, allow me…


Doctor Who – Great Big Threatening Button by rockerman82

I went with a “Big Red Button” to be less, well, threatening.  It really is quite simple to control an Arduino from Android device by using the App Inventor toolbox.  Assuming you already have an Arduino and an Android device, the only other investment you will need to make is a Bluetooth device that can cost as low as $5.  It is quite simple using a SeeedStudio Bluetooth shield – it reduces Bluetooth communications to being no different than normal Arduino serial communications.  I used it because I had it laying around.  I know another very popular Bluetooth module with makers is the HC-05.

In the first apps I wrote with App Inventor, I had to manually enter the Bluetooth MAC address of the device I wanted to control.  It was, in short, a pain in the butt.  Thanks to Luis Picarzo, I discovered how easy it is to pair a Bluetooth device using App Inventor.  Simply pair your device, then connect to it.  So let’s take a look at the App Inventor side first, specifically let’s start with the Layout Designer view…

layout
Notice that we added a combination of both Visible and Non-Visible components.  Here is a list of the components:

  • 3 buttons, one for the Big Red Button, one for Pairing with a Bluetooth device, and one for Connecting to previously paired Bluetooth device.
  • Horizontal Arrangement to align the Bluetooth Pair and Connect buttons.
  • TinyDB to store the Bluetooth MAC Address
  • Bluetooth Client component to connect to the Arduino
  • 2 Activity Starters to launch the Android Settings Bluetooth tools, one to turn the Bluetooth on if it is turned off on the device.  And the other to launch Bluetooth Pairing.
  • Timer to control setting Bluetooth connection.

Now let’s look at the logic used to wire those various components.  Note, Button1 is the “Big Red Button”, I thought I had renamed this to be more obvious but seems I did not.

blocks

In short, if the app detects Button1 is pressed it sends the string “TOGGLE” and the end-of-line character “n” via the Bluetooth serial stream.  This will be picked up on the Arduino side to toggle an LED.  Obviously at this point you can imagine all the other things you can do to tweak this app.  You can add buttons each sending a different string.  You can read Android device sensors like the accelerometer and send that data along.  All that matters is you are now sending serial string data that Arduino will later parse to trigger it’s various output pins.  So with that let’s look at the Arduino side.  Whatever Bluetooth module you choose, be sure to read how to interface with your Arduino.  Regardless of what Bluetooth device you choose they will all rely on establishing a serial stream, no different than the serial stream you can read over USB when your Arduino is connected to a computer.  Then it’s just a matter of parsing the serial communications.  Each word received could trigger different outputs.  For example if you are doing a remote webcam controller you could use keywords “LEFT” and “RIGHT” to turn accordingly.  The possibilities are endless!  Just remember to keep the strings as short as possible to reduce the appearance of lag.  My sketch listens for any string to come in over serial and the toggles pin 13 LED.  I could do string comparison and look for different strings and toggle different LEDs based on which string is received.  But as I said, I wanted to do the absolute minimal coding so people could alter to suit their needs.

arduino

Lastly, here is the GitHub repository containing the Arduino .ino file, the .apk to install on your Android device, and .aia you can load into App Inventor so you can modify the app however you wish.

So what are you going to build?  Let me know over on Google+!