Bluetooth Remote Control RGB LED

The Plan

I wanted to add some cool lighting effects to some of my projects and thought I would start playing around with some RGB LED’s.  I want to be able to use my Android device to control the color of the RGB  LEDs.  I will use an Arduino to control the RGB LED.  I will also need a Bluetooth Module to have communications between the Arduino and my Android phone.

Here is the video demo of the completed project….

The RGB LED Research

From my quick research around the web, it turns out that a RGB LED is a pretty simple device.  It essentially is just 3 small LEDs all bunched into one dome.  Here is a crude diagram to illustrate…

RGBLEDSimpleDiagram

My diagram is for a common cathode RGB LED.  There is one ground pin and whichever color LED has a high enough voltage on its leg, it will turn on and glow.  The brightness of the color will depend on the voltage level. Note:There are also common anode RGB LED’s where there are 3 grounds and 1 positive pin but I find that would be less intuitive to use.

The Supplies

The Hardware Diagram

Here is a Fritzing diagram of my setup. BT_RGBLED_Schematic

The Arduino Sketch

The Arduino sketch consist of a loop that reads all the characters coming in on a SW UART looking for key words.  The SW UART is connected to the Bluetooth module which is paired to an Android phone running a custom app sending requests to control the colors of the RGB LED.  Once a key word is found, the line is parsed to determine the color and value to set.  The Android app sends a command with the letter for the color and value between 0 to 255.  The value is the analogWrite value for the PWM pin connected to the corresponding LED color. You can download the full Arduino sketch here: https://drive.google.com/file/d/0B1a0nPfCQQvKS3lCVkJ3czlpbEk/view?usp=sharing.  You will need to have the companion Android app below to make it work.

The Android App

I used MIT’s AppInventor platform to make the companion Android app to send color request through a Bluetooth interface to my Arduino via a HC-06 Bluetooth module.  I based it off of the AppInventor project described in my previous post “Simple Android to Arduino Bluetooth Communication”.  I added 3 slide bars, 1 for each color LED.  There are also 3 text boxes that get updated with the slide bar position whenever the slide bar position changes.  In addition to updating the text box, the value is also concatenated into a “[color letter]:[value]|” command to be sent through the BT interface which is picked up by the Arduino sketch as described above.  Here is a snapshot of the screen setup… Here are the relevant AppInventor blocks… If you would like to install the app on your device, you can get the .apk file here…. https://drive.google.com/file/d/0B1a0nPfCQQvKOHdwdFdhX2pnajQ/view?usp=sharing If you are familiar with the AppInventor environment and would like the .aia file to look at all the blocks an play around with it yourself, you can get it here… https://drive.google.com/file/d/0B1a0nPfCQQvKeGtXV21nQm5jMXM/view?usp=sharing

The Testing

You can see a quick demo video of the setup working at the top of this post.  After playing around with it some more, I do find there are sometimes glitches in the setup.  The color jumps and then self corrects if I slide one of the colors too fast.  Most likely loss of characters during Bluetooth transfer or the 9600 serial port baud rate SW UART is too slow to keep up.  I could probably put in some error checking in the string received, but this is just for simple demos for now.  If I really want this to be in some more elaborate project, I will add the error handling.

Ardiuno Pro-Mini Bring Up

 The Plan

I have been playing around with various projects using my Arduino Uno and Mega.  Each time I have to rip up my previous project to move on to another to re-use the Arduino.  I would like to keep some projects in tact so I found a really cheap Arduino Pro-Mini platform for $3.08.  I bought it here: http://www.banggood.com/Wholesale-New-Ver-Pro-Mini-ATMEGA328-328p-5V-16MHz-Arduino-Compatible-Nano-Size-p-68534.html.  It is cheap enough that I could buy a few and dedicate them to some of my projects.

In addition to being really cheap, it is tiny.  It can be used on projects that I might want to be kept small.  The entire module is not much bigger than quarter.  Here is a picture…

IMG_0828

The Trade Offs /Limitations

Due to the small size (and likely the low price) this board is missing some things:

  1. This Arduino board doesn’t have a USB to serial module on board.  You will not be able to just plug in a USB cable to a PC and start working.  I plan on using a Arduino Uno’s SW UART to communicate with it.
  2. The Uno and Mega is powered from the 5V supply of the USB from the PC.  Since there is no USB port on the Pro-Mini, I will need to find another way to power the board.  If I am already using an Arduino Uno to communicate with it, I can just use the 5V output of the Uno to power it.

The 1st Power Up

All I did was to power up the Pro-Mini using my Uno.  Here is the Fritzing diagram….

ProMiniPowerUp

When power was applied, the power LED turns on and another LED started blinking.  With some probing around, I see that the digital IO pin 13 is the one blinking the LED.  I am assuming that the Arduino example sketch “Blink” was pre-loaded on the board…..

ProMiniBlinkLED

The Sketch Programming Process

I first tried to program the Pro Mini by following several tutorial on line with no success.  I was able to successfully reprogram the boot loader on the Pro Mini using this tutorial… http://arduino.cc/en/Tutorial/ArduinoISP but when I tried to load a sketch, it wouldn’t work.

I also found a tutorial on how to use an FTDI USB to Serial TTL adapter cable to load sketches to the Pro Mini here: http://arduino.cc/en/Guide/ArduinoProMini.  Reading through it, it looked really simple.

  • The serial communication is just using the standard serial RX and TX.
  • Power is supplied using the 5V supply of the USB cable.
  • The only somewhat complicated portion was using the cables RTS line to reset the Arduino.  This pin would be connected to the pin labeled DTR on the Pro Mini board.

Looking at the Pro Mini schematic (http://arduino.cc/en/uploads/Main/Arduino-Pro-Mini-schematic.pdf), this DTR line is a way to toggle the Atmel chips reset line…..

ProMiniDTR

It also looks like the push button can also toggle this reset line.

The Simplified Sketch Programming Process – My version

I really don’t want to purchase a FTDI cable so I came up with my own hack way of using my UNO board to program the Pro Mini.  In essence, I am going to use the USB to Serial module built on the UNO board as an FTDI cable replacement.  To do that…

  • connect the TX of the UNO to the TX of the Pro Mini
  • connect the RX of the UNO to the RX of the Pro Mini
  • connect the 5V of the UNO (which is being powered by the USB cable to the PC) to the VCC of the Pro Mini.
  • disable the rest of the UNO HW by tying the RESET line of the UNO board to ground.

Here is a quick Fritzing diagram…

ProMiniSketchProgrammingSetup

The only thing missing in this simplified set up is that the Arduino IDE can’t reset the Pro Mini during the programming process.  To get around that , I simply manually reset the Pro Mini during the programming process with the push button on the Pro Mini board (not the UNO board).  The sequence is….

  1. Set the board type to the Pro Mini in thd Arduino IDE.
  2. Set the COM port # to the COM port # of the UNO board.
  3. File -> Upload…
  4. When the IDE status is showing “Compiling”, I press and hold the reset button on the Pro Mini board.
  5. When the IDE status reaches “Uploading”, I let go of the reset button on the Pro Mini board.

You have to make sure to let go soon after the “Uploading” shows up.  The time window is about 2-3 seconds so is not that hard to time.  If you wait to long, you will miss the hand shaking during boot up and it won’t work reprogram the Pro Mini.   There is probably some signal I could find on the UNO that I could solder to to make the reset work but I like not needing to make too many mods to get this working.

With this process I can now reprogram my Arduino Pro Mini with HW I already have set up most of the time.

The Cautions

Soldering Capability:

  • The unit I bought did not have any of the headers soldered on.  I had to solder them on myself.  If you are not comfortable soldering, you might not be able to use the board.

L Header Placement:

  • This board has a convenient header with just the 6 essential signals to get the board running on one edge.  The kit also comes with an L header that is intended to be used for it.  I mistakenly soldered it to the underside of the board like the other 2 straight headers.  This causes a problem when trying to plug the board onto a bread board.  The L bracket gets in the way.  Here is a picture of the problem…
  • ProMiniLHeaderProblem
  • Once soldered on, it is really hard to remove since you would need to melt the solder on all 6 pins simultaneously while trying to pull out the header.  I am just living with it on this first one I bought.  Lesson learned.

Simple Android to Arduino Bluetooth Communication

The Plan

In my last post, I showed basic serial communication between an Arduino Uno and a HC-06 Bluetooth module (Arduino + Bluetooth Module Bringup).  I will now show how to use that to send characters back and forth between an Android phone/tablet and the Arduino over Bluetooth using the HC-06 module.

The Setup

Arduino Side:

I have the same Arduino Uno + HC-06 Bluetooth module setup from the above referenced post.  Here is the Fritzing diagram again…

HC06BasicConnectCorrected

I used the same serial passthrough Arduino sketch as I showed in the above referenced post.

DIAGRAM UPDATED: Someone noticed that I had the HV and LV supply reversed in my original diagram. I have corrected it.

Note: I used a HC-05 Fritzing symbol here (only one I could find). The State and Key pins are not there on my HC-06 module. Not sure what they should be connected to if it were a HC-05.

Andriod Phone/Table Side:

I used an Android tablet for my testing.  To prepare for the testing, I powered up the HC-06 module and Bluetooth paired my tablet with the HC-06 using the standard Android Bluetooth settings menu.  When I searched for devices in the Android Bluetooth menu, I saw a new device with name HC-06.  When I tried to pair with it, I was asked for a pin.  I found the default pin listed in the HC-06 datasheet (http://silabs.org.ua/bc4/hc06.pdf) as 1234.  I entered 1234 and successfully paired my Android tablet with the HC-06 module.  The HC-06 data sheet gives the command to change the pin if needed but I am just going to leave it as the 1234 default for now.

Next was to find an Android App that could send and receive Bluetooth characters/strings.  Since I have been playing with MIT’s AppInventor recently (see some of my previous posts with the AppInventor tag), I decided to just build my own.

The Bluetooth Android App

I have a feeling I will be working with Bluetooth often from now on so I decided to make a very generic Android App that can be set to run as BT server or BT client.  Once a BT connection is made in either mode, I want to be able to send and receive characters to the other side.

I created the app in MIT’s AppInventor environment but it would be a little too long to post a tutorial on all the steps.  If you are interested, here is my .aia file if you want to load it into your AppInventor environment and review it on your own….

https://drive.google.com/file/d/0B1a0nPfCQQvKd3NXdkJYeTk4dGM/view?usp=sharing

If you just want to install and use it, here is the .apk file…

https://drive.google.com/file/d/0B1a0nPfCQQvKTk9PZ0JkVG1sSk0/view?usp=sharing

Here are a couple screen shots…

ManualBTscreenshotManualBTScreen2

The App can actually be used without the Arduino + HC-06 setup.  It can even be installed on 2 Android devices then:

  1. Pair the 2 Android devices first
  2. Launched the app on both devices
  3. Click the “Start as Server” button on the 1st device
  4. Click the “Start as Client” button on the 2nd device
  5. If the Bluetooth link is established, the screen on both sides should change to have Send Text / Received Text boxes and Send button.
  6. Just type a string in the Send Text box and hit the Send button.  You should see the string show up in the Received Text box on the other device.

Here is a quick video demo…

The Android to Arduino Bluetooth Test

I didn’t find any documentation on the HC-06 datasheet on how to connect to a BT server so I assumed it was default set up as a BT server.  I launched my Android App and clicked “Start as Client”.  I had previously already BT paired the HC-06 on that tablet so it popped up as one of options to connect to.  I selected HC-06 and the connection was successful.  The red LED on the HC-06 went from blinking to solid on.  When I sent something from the Android App, it showed up in the Arduino serial monitor window.  Now when I typed something in the Arduino serial monitor window, it popped up in the “Received Text” box on the Android App.  Before the Arduino serial to HC-06 was for AT commands, but as soon as a BT connection established, it looks like the HC-06 automatically switched to serial pass through mode.

To better illustrate, here is a quick video demo….

Arduino + Bluetooth Module Bringup

The Plan

I wanted to add Bluetooth communication capabilities to my Arduino projects.  I found a $5.01 Bluetooth module here… http://www.banggood.com/HC-06-Wireless-Bluetooth-Transceiver-RF-Main-Module-Serial-For-Arduino-p-80364.html.  This post shows the basic setup I did to get communications started.

The Hardware Setup

The voltage for the module from the supplier listed above was set for 3.6V – 6V so the Arduino 5V supply output is ok.

I found the data sheet for the HC-06 module here: http://silabs.org.ua/bc4/hc06.pdf.  Unfortunately the Tx and RX voltage is 3.3V still so I also purchased a cheap $1.82 level shifter here: http://www.banggood.com/Logic-Level-Converter-Bi-Directional-IIC-4-Way-Level-Conversion-Module-p-938774.html.

For the basic communication, I just hooked up the TX and RX through the level shifter to a SW UART on my Arduino UNO.  Here is the Fritzing diagram…

HC06BasicConnectCorrected

DIAGRAM UPDATED: Someone noticed that I had the HV and LV supply reversed in my original diagram.  I have corrected it.

Note: I used a HC-05 Fritzing symbol here (only one I could find).  The State and Key pins are not there on my HC-06 module.  Not sure what they should be connected to if it were a HC-05.

The Arduino Code

For the basic test, I am just using a basic pass through Arduino sketch…

/*
 Software serial test
 
 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 6); // RX, TX

void setup() 
{
 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 mySerial.begin(9600);

 
}

void loop() // run over and over
{
 while (mySerial.available()>0 )
 Serial.write(mySerial.read());
 
 while (Serial.available()>0 ){
 mySerial.write(Serial.read());
 }
}

The Initial Test / Debug

I uploaded the Arduino sketch and opened the serial monitor and tried to send “AT” to the module.  Nothing came back.  Google’d around and found this post http://www.instructables.com/id/AT-command-mode-of-HC-05-Bluetooth-module/?ALLSTEPS.  One of the comments in the post said that you can’t send NL or CR in the serial communication to the HC-06 module.  Changed the serial monitor settings…

HC06SerialWindowBasicComm

Once I changed the serial monitor to not send any line ending, I was able to send AT+VERSION and get the response in the above screen capture.  Sending any line ending character resulted in no response.

The Next Post

I was able to Bluetooth pair the HC-06 with my Android tablet and write an Android app to send text back and forth to the Arduino.  That will have to be covered in a separate post.  I will post those development steps in my next blog post tomorrow (or sometime soon).

Simple Color Selector in AppInventor – Better Solution

The Background

In my last post, I detailed the attempt to use a ListPicker element to make a quick color selector.  It dead ended when the selector brought up only color code numbers instead of a human friendly color blocks to select from (see Creating Color Selector Using ListPicker Element in AppInventor2 – Not a good idea).

The Plan

I am now going to attempt another method to make a more elegant color selector using a second screen.  There is a great tutorial that shows a similar type function at http://appinventor.mit.edu/explore/ai2/colored-dots.html, but that is quite complex and covers several other concepts.  I am going to GREATLY simplify it here.

The Screen Layouts

I am going to leave the ListPicker element and functionality from my previous post (link to the post above) to compare the 2 methods.  First I added another Button element to link my new functionality to.  I then added a second screen by clicking the “Add Screen…” button (circled in the next screen shot) and naming the new screen “ColorSelector” in the dialog box that popped up….

TwoScreenColorSelectorLayout

Next I chose “ColorSelector” that I just created in the screen selector pull down.  On that screen layout, I added 3 buttons and set the default color to some example colors….

TwoScreenColorSelectorLayout2

This sets up both screens.  Now I added the code Blocks…

The Code Blocks

First was to bring up the ColorSelector screen when the button is clicked on Screen1….

  1. Select “Screen1” to work on in the screen selector menu.
  2. Pull out the “when Button1.Click” block.
  3. Add the block to “open another screen”  (under the “Control” group).
  4. Add a text block set to the name of the second screen as the screenName parameter.

Here is a snapshot of my code block….

TwoScreenColorSelectorColorSelectButtonCode

Next is the functionality for the color buttons on the ColorSelector screen.

  1. Select the “ColorSelector” in the screen selector pull down.
  2. For each of the buttons, add the block to “close screen with value”.  This block is under the “Control” group.
  3. Attach the corresponding button’s background color to be sent back as the result back to Screen1 and close ColorSelector screen.  The result will be used later in Screen1 to set the color of the button.

Here is a snapshot of my code block….

TwoScreenColorSelectorColorButtonCode

The final step is to set the button background color in Screen1 to the result that was sent back from the “ColorSelector” screen.

  1. Select “Screen1” to work on in the screen selector menu.
  2. Pull out the “when Screen1.OtherScreenClosed” block from the Control group.
  3. Pull out the “set Button1.BackgroundColor to” and attach the “get result” from that block as the color to be set.

TwoScreenColorSelectorSetButtonColorCode

The Testing – Round 1

I was trying to test this while my phone was connected to the AppInventor environment mode but found out a limitation in this testing mode.  You can’t move from one screen to another using code when running on the phone in this mode.  The phone will just display the screen that is selected in the screen selector pull down.  I couldn’t test the most important features of my new app.

The Multi-Screen Testing Solution

Unfortunately, testing of multi-screen functionality can only be done if the app is fully installed on the testing device.  Luckily AppInventor has an easy method to allow users to quickly build and install the full Android app you are developing to your phone/tablet.  Under the build menu, select “App (provide QR code for .apk)”.  It will build the code and bring up a QR code that you can scan using the camera while in the AI2 Companion app on the device you are using for test.

QRCodeForAPK

Once done building, a QR code will pop up like this….

QRCodeForAPKExample

As the note says, the code is only valid for 2 hours so don’t expect to be able to reuse it later.

The Testing – Round 2

Once I installed this test app on my phone, everything worked as expected.  When I clicked the “SecondScreenChooser” the other screen with 3 color buttons came up.  Once I clicked one of the colors, the first screen came back up and the “SecondScreenChooser” color changed to match what I clicked.

Creating Color Selector Using ListPicker Element in AppInventor2 – Not a good idea

The Plan

I was helping a friend with getting started with using AppInventor and he wanted to make a color selection function.  He came up with the idea of using a ListPicker but didn’t know how to use the ListPicker so I thought I would help him out and post a quick demo on how to do it.  It seemed like a good idea but after trying to implement this, there is a major drawback.

The Attempt

This is just a simple example so I am going to keep this simple.  All I am going to include is a sample ListPicker element.  Once the user selects a color, the ListPicker button will change to the color the user selected.

ListPickerExampleLayout

This first step in the code Blocks is to set the ListPicker elements to a list of colors.  This can be done during the screen initialization.  In the Blocks screen..

  1. click on Screen1 and drag out the “when Screen1.Initialize” block.
  2. click on the ListPicker1 and drag out the “set ListPicker1.Elements to” block out
  3. click on the “Lists” (blue box) and drag out a “make a list” block
  4. click on the “Colors” (gray box) and add the colors you would like to be included in the List block.
  5. You can add more color items to the list block by clicking on the darker blue box in the upper left of the list block

Here is a snapshot with some notes…

ListPickerExampleScreenInitBlock

Now to add the functionality to change the color of the ListPicker button, click on the ListPicker and add the blocks in this diagram…

ListPickerExampleAfterSelect

This will change the ListPicker button color to what was selected after a user selects a color….

The Problem

The problem with this approach is that the colors come up as a color code number instead of a list of colors to select.  Here is a screen shot of my phone running the above project….

IMG_20150114_131607

As you can see, this is not really useful to a human.  I tried digging around to see if there was a simple solution but couldn’t find one.  Unfortunately what seemed to be a good simple solution turned out to be a dead end.  I will attempt to create a better but unfortunately more complicated solution in my next post.

Arduino + ESP8266 remote control LED using an Android App with Camera Live View

The Plan

In several of my previous post, I showed…

  1. …my setup to control LED’s using an Arduino + ESP8266. (LED’s Control Through ESP8266 + Arduino Web Page)
  2. …using the AppInventor environment to send a Web.PutText to send a message to the Arduino. (AppInventor2 Web Interface Trial)
  3. …embedding a live stream of a Foscam Wireless Camera into an AppInventor application. (Real time Wireless Camera view using AppInventor2)

My plan is to now mash up all three into one project.  I will make an app that will send Web.PutText to the Arduino through the ESP8266 to trigger LED light changes and have an embedded live Foscam Wireless window all on the screen on an Android App built in the AppInventor environment.

The Android App (built in AppInventor2)

The screen layout is made up of

  • 6 buttons to turn on/off 3 LED’s.  The buttons are just sending custom WebPutText keywords.
  • A WebViewer element which loads will load web page defined in text box element when the Go button is clicked.
  • Another text box to specify the web address of the Arduino + ESP8266 system.

Here is what is looks like….

RemoteLEDWithLiveViewScreenLayout

The AppInventor code to point the Web component to the ESP8266/Arduino server will set the Web1 URL to point to the web address that is entered in the corresponding text box.  I was running out of screen space and didn’t want to have to add another button so I just have the code run after the address is typed into the box and something else on the screen is touched.  It looks like this…

ArduinoAddressBlock

The code to start up the live camera view is really just creating a Webviewer window which will display the web site specified in the corresponding text box whenever the Go button is clicked.  The rest is really up to the Foscam camera to serve up the live stream….

CameraViewCode

The LED controls are just sending some custom strings using the Web.PutText function.  I am using “PtcApp” as a keyword that is not likely to be seen by the server that I can key off of.  I follow up “PtcApp” keyword with a code for the {first letter of the LED color}:{1 for on / 0 for off}.  The magic will happen on the upcoming Arduino code.  Here is the AppInventor code….

LEDControlCodeBlocks

The Arduino Code

I am only going to cover the additional Arduino code I add to implement the AppInventor functionality here.  The rest of the program has been detailed in many of my previous post.

The first step is really simple.  I just added another parser block to look for “PtcApp” in the incoming line from the ESP8266.  I left all the previous parsing blocks and function so that all the previous features will still continue working (ie: the web page interface is still fully functional).   Once the “PtcApp” is found, I call a function to parse out which color LED to control and if it is a request for On or OFF.  Here is the code snips….

 if (InLine.indexOf("PtcApp ") != -1) {
    CommandQue[QueIn++]=PUT_REQUEST;    //** See next section in post for what this is for **
    ParseCustomAppRequest(InLine);
    UpdateLCDStats();
    NumberAppReq++;
    UpdateLCDStats();
 }
....
//*** This parses out the LED control strings from custom Android App and sets the appropriate state vars *** 
void ParseCustomAppRequest(String InLine){
 if (InLine.indexOf("R:0")!=-1) RED_State=false;
 if (InLine.indexOf("G:0")!=-1) GREEN_State=false;
 if (InLine.indexOf("B:0")!=-1) BLUE_State=false;

 if (InLine.indexOf("R:1")!=-1) RED_State=true;
 if (InLine.indexOf("G:1")!=-1) GREEN_State=true;
 if (InLine.indexOf("B:1")!=-1) BLUE_State=true;
}


The First Test

This all actually started working very well initially.  Everything worked pretty much on the first attempt, but after 5 LED change requests from the Android App, the ESP8266+Arduino stopped seeing any more requests.

Debugging the logs, I found that each PUT request was holding an additional IPD channel open.  (In case you haven’t been following my previous posts, an IPD channel is the ESP8266 way of handling multiple TCP clients.  Each channel corresponds to a client so the server can send messages to a specific client).  The ESP8266 seems to have a limit to only be able to hold open 5 open IPD channels and then stop accepting any more connections.

I remembered another similar situation where the client wasn’t closing the connection.  It was because I wasn’t sending an HTTP header back for a GET request (see the “Extra Fix” section in my post “Arduino + ESP8266 Mini Server with Command Que“).  Hoping it was the same problem I made a very simple HTTP header with 0 length response and send it back each time I get a PUT request…

String PutResponse ="HTTP/1.0 200 OK \r\n"
 "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n"
 "Content-type: text/html\r\n"
 "Content-length: 0\r\n"
 "Connection: Closed\r\n"
 "\r\n";

...

 case PUT_REQUEST:
    SendCIPChunk(PutResponse,CommandQueIPD_CH[QueOut]); // Send the CIPSEND command to respond to Put request 
    break;

Once I sent back the proper HTTP header, the client automatically closed the connection and all the requests from the AppInventor Web.PutText button clicks were coming in on IPD channel 0 consistently.  I was able to get the LED to change +100 times through a day of testing so I think it is pretty stable now.  🙂

BTW – I will include a link at the end of this post for the full Arduino code to get the complete picture.

The Demo

Here is a short video demoing the setup…

The Arduino Code

If you want the code…

https://drive.google.com/file/d/0B1a0nPfCQQvKenVWUGtvU0RxNEU/view?usp=sharing