Android, iOS tablets, and smartphones revolutionized the idea of the User Interface (UI). These devices are intuitive, powerful, and omnipresent in our daily lives. So instead of reinventing the wheel for a slick UI to control your next OSHW project, why not simply build the interface as an app? Typically the answer is that creating apps isn't quite as simple as using apps.
But creating an Android app to control your latest OSHW invention can be pretty darn simple thanks to MIT’s App Inventor. In my opinion, it is one of the best-kept secrets in the maker community. Utilizing a combination of a browser-based application and some software that runs on your Mac or PC, you can create Bluetooth-enabled apps to control your hardware in just a few hours. This blog will demonstrate how to establish a Bluetooth connection and give a basic example of controlling an Arduino via a serial connection.
First, you need to head over to the App Inventor website and set up your free account. Now the purpose of this blog is not to be a step-by-step tutorial on using App Inventor, instead I will point you to far better resources such as MITs tutorials. Once you feel comfortable navigating the App Inventor tools, come on back and we can continue on together.
In our example, we’re going to control a strand of RGB LEDs connected to an Arduino Uno coupled with a Bluetooth shield. The control circuitry itself is trivial as the strands of LEDs use the I2C communication protocol to receive commands from the Arduino. The LEDs need only to be connected to Vin (red wire), GND (green wire), and analog pin A0 on the Arduino (black wire). Given all the LEDs we’re trying to control, you will have to power the Arduino via a wall wart power supply capable of delivering 1A at 12VDC via the DC barrel power jack. USB power will not work for this project.
Okay, let’s log in to App Inventor. We start in the Designer mode to create our UI. I found this wonderful tutorial from Luis Picarzo on using Bluetooth and App Inventor, which formed the basis for this project. Code re-use! Another win for open source! For this project, I will use 3 sliders to control the intensity of each of the colors (Red, Green, Blue) of my LED strand. By mixing different values of color intensity, I can create quite a few different colors, just like pixels in your television, smartphone display, or computer monitor. I will also need to add some buttons to enable actions such as the Bluetooth pairing of my devices, establishing a Bluetooth connection, and disconnecting when I am done. There are also 5 non-visible components that need to be added to enable the functionality we are seeking. Non-visible components are the “behind-the scenes” parts of an app that the user interacts with indirectly, as compared to visible components such as buttons, sliders, and text fields. These non-visible components include TinyDB, Bluetooth Client, two Activity Starters, and a Timer. I also tweaked some UI settings such as shading each slider bar with the color it represents. When you are done, you will have this:
Click to enlarge
Next, we switch over to the Blocks mode to “write the code” in order to make our app do useful things. If you’ve ever used the education-focused Scratch programming environment, then you should be quite at home with App Inventor, at least in terms of look and feel. This is really the closest we have gotten to” building block”-esque software. This is what your Blocks section will look like once you’ve added all the functionality needed for this project (in interest of clarity I hid the Blue and Red slider code, just repeat the Green slider code below for the other two colors):
If you followed the earlier tutorial, you should have some software installed on your computer, including an Android device emulator. By clicking “Connect” > “Emulator” in the App Inventor toolbar, you can preview what your app will look like right on your computer. Once you are satisfied with the UI and have the bugs worked out, time to move on to the Arduino portion of the project.
We need to write some code for the Arduino to receive this Bluetooth serial data and then act on it to vary the colors of the LEDs. The code may vary depending on your particular Bluetooth module, but the important thing to note is that to create the 24-bit integer for the LED strip that the blue LED value is logically shifted left 8 bits, green LED is shifted left 16 bits, and red LED is not shifted at all. Logically 'OR' the three values together to create the 24bit integer. See below how to do this.
Now, put it all together. Power up the Arduino, launch the app on your Android device, and pair/connect the two devices. Now, start playing with the sliders and watch the rainbow dance in front of your eyes. So, what are you going to create with your new found Android skills? Leave a comment and a link to your projects in the comments below.
Michael Parks, P.E. is the owner of Green Shoe Garage, a custom electronics design studio and technology consultancy located in Southern Maryland. He produces the S.T.E.A.M. Power podcast to help raise public awareness of technical and scientific matters. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.
Privacy Centre |
Terms and Conditions
Copyright ©2021 Mouser Electronics, Inc.
Mouser® and Mouser Electronics® are trademarks of Mouser Electronics, Inc. in the U.S. and/or other countries.
All other trademarks are the property of their respective owners.
Corporate headquarters and logistics centre in Mansfield, Texas USA.