loading

Control lights on Christmas tree via Arduino, Android, and Bluetooth!

Picture of Control lights on Christmas tree via Arduino, Android, and Bluetooth!
Alrighty, this is my first Instructable and I am pretty excited about it. I got the idea for this project from a combination of these 2 Instructables...

http://www.instructables.com/id/Tweeting-Christmas-Tree/
http://www.instructables.com/id/Android-Arduino-Controlled-Projector-Screen/

The basic concept was to make a way to control the lights on my Christmas tree this year, but after the Christmas season it can still be used as a remote controlled power strip (I must say that is pretty cool). I got this idea because I was recently introduced to the Arduino and immediately saw the endless possibilities it possesses. I love programming, and learning for that matter, so this was a great project to tackle.
 
Remove these adsRemove these ads by Signing Up

Step 1: The Materials

Picture of The Materials
IMG_0027.JPG
IMG_0029.JPG
A List of Materials
Arduino UNO R3 - $30 Sparkfun - 
https://www.sparkfun.com/products/11021
Bluetooth Serial Adapter - $10 eBay - http://www.ebay.com/itm/Arduino-Wireless-Bluetooth-Transceiver-Module-Slave-4Pin-Serial-DuPont-Cable-/321020258404?pt=LH_DefaultDomain_0&hash=item4abe4c6864 
5v Relay Shield - $7 eBay - http://www.ebay.com/itm/5V-4-Channel-Relay-Shield-Module-Expansion-board-For-Arduino-ARM-PIC-AVR-DSP-/320909808154?pt=LH_DefaultDomain_0&hash=item4ab7b7121a
A phone with Android
Jumper Wires (for the Arduino)
Wire (for the outlets )
Outlets


(These pieces are NOT needed, but i used them to test the project)
4 LEDs
Four 100 ohm resistors

Step 2: The Android Application

Picture of The Android Application
block.png
The project reads a 'byte' of data that is sent from a phone running an Android app, and interprets it to do specific things. I used a lot of similarities from the Android app bwrussell used in his Bluetooth Controlled Projector Screen (he did a great job on that 'ible btw, there is a link to it up there ^ and a little blurb about him in the next step). So I used the App Inventor from MIT, it's really freaking cool, and surprisingly easy to use. http://appinventor.mit.edu/

The block editor is where the magic happens though, when making the app, make sure that each button that you make sends a different 'byte'. As you can see in my block editor, each color button send a different number (i.e. 1-8). Our Arduino will be receiving this specific 'byte' and will turn certain pins on and off based on what it receives. Be sure to use a simple bluetooth app to find your module's address so you can pair your device to the module. Enter that number into the block editor 

Step 3: The Code

Picture of The Code
The code was largely created because of how exactly I wanted this to work. Also, I am really picky about making things neat, that's why this is all in functions, it really doesn't have to be, that's just how I prefer it. A lot of the code can be omitted if you aren't as picky as I am about how it works. Make sure to read the comments to find any unnecessary pieces of code for your project.

I really need to thank bwrussell for his help with this project. I asked him multiple coding questions and he answered all of them in great detail so I could understand them. He knows a lot more about Arduino, and coding than I do, and I wouldn't have been able to do it without him. He has been a fantastic resource and just a very nice fellow Instructable-maker.

Here is the code I used...


/*This code is for my Arduino/Bluetooth/Android controller. An Android application will send a specific 'byte' of data
to this code, the switch.. case statement receives the 'byte' and turns pins on or off depending on what it receives.

Algorithm: 4 pins are declared, pinA - pinD. These are pins 4-7 on the Arduino. Command is the byte of data that the code
receives. rHigh and rLow are used for the minimum and maximum random delay values. The setup sets pinA - pinD as outputs
and sets them all as off. It then begins the serial transfer, if a byte is received, it is set as command. In the loop
cases 1 - 4 simply turn on or off pinA - pinD. Case 5 sets all pins as random. Case 6 turns all pins on. Case 7 turns
all pins off. Case 8 starts a flashing pattern. */

const int pinA = 4;
const int pinB = 5;
const int pinC = 6;
const int pinD = 7;
byte command = 0;
const int rHigh = 555;
const int rLow = 100;
int randomPin;
void pinDo(int pin);
void allOn();
void allOff();
void randomStart();
void flashThrough();

void setup()
  {               
  pinMode (pinA, OUTPUT); // set the relays pin as an output
  pinMode (pinB, OUTPUT); //
  pinMode (pinC, OUTPUT); //
  pinMode (pinD, OUTPUT); //
 
  allOff(); // make sure all pins are off to start
 
  Serial.begin(9600);
 
  if (Serial.available() > 0) // Start serial transfer
    {
    command = Serial.read(); // Set command to equal the serial data
    }
  }

void loop()
  {
   if (Serial.available() > 0)
     {
    command = Serial.read();
    switch (command)
      {
     case 1: // Relay 1
       pinDo (pinA);
       break;
     case 2: // Relay 2
       pinDo (pinB);
       break;
     case 3: // Relay 3
       pinDo (pinC);
       break;
     case 4: // Relay 4
       pinDo (pinD);
       break;
     case 5: // The random function
       randomStart();
       break;
     case 6: // Turn all pins on
       allOn();
       break;
     case 7: // Turn all pins off
       allOff();
       break;
     case 8: // Start flashing pattern
       while (command != 6 && command != 7)
         {
           flashOn();
           command = Serial.read(); // Check for new serial data
        }
      }
    }
  }

void pinDo(int pin)
  {
    if (digitalRead(pin) == LOW)
      {
      digitalWrite(pin, HIGH);
      }
      else
      {
      digitalWrite(pin, LOW);
      }
  }

void randomStart ()
  {
    while (command != 6 && command != 7)
      {
      randomPin = random(pinA, pinD + 1);
      if (digitalRead(randomPin) == LOW)
        {
      digitalWrite (randomPin, HIGH);
      delay (random(rLow, rHigh));
        }
      else
        {
      digitalWrite (randomPin, LOW);
      delay (random(rLow, rHigh));
      command = Serial.read(); // Check for new serial data, if so, exit the random flashing
      /* These 2 if statements are present in the 5th case because I wanted the pins to immediately
      turn on or off depending on the command it receives while in the random portion of code.*/
      if (command == 6)
        {
          allOn();
        }
      if (command == 7)
        {
         allOff();
        }
        }
      }
  }
void flashOn ()
  {
    digitalWrite(pinA, HIGH); // all on
    delay(150);
    digitalWrite(pinB, HIGH);
    delay(150);
    digitalWrite(pinC, HIGH);
    delay(150);
    digitalWrite(pinD, HIGH);
    delay(400);
    allOff();
    allOn();
    allOff();
    allOn();
    allOff();
    flashThrough();
    flashThrough();
    flashBack();
    flashBack();
  }

void allOn ()
  {
    digitalWrite(pinA, HIGH);
    digitalWrite(pinB, HIGH);
    digitalWrite(pinC, HIGH);
    digitalWrite(pinD, HIGH);
    delay(300);
  }

void allOff ()
  {
    digitalWrite(pinA, LOW); // next phase
    digitalWrite(pinB, LOW);
    digitalWrite(pinC, LOW);
    digitalWrite(pinD, LOW);
    delay(350);
  }

void flashThrough ()
  {
    digitalWrite(pinA, HIGH); // next phase
    delay(200);
    digitalWrite(pinA, LOW);
    digitalWrite(pinB, HIGH);
    delay(200);
    digitalWrite(pinB, LOW);
    digitalWrite(pinC, HIGH);
    delay(200);
    digitalWrite(pinC, LOW);
    digitalWrite(pinD, HIGH);
    delay(200);
    digitalWrite(pinD, LOW);
  }

void flashBack ()
  {
    digitalWrite(pinD, HIGH); // next phase
    delay(200);
    digitalWrite(pinD, LOW);
    digitalWrite(pinC, HIGH);
    delay(200);
    digitalWrite(pinC, LOW);
    digitalWrite(pinB, HIGH);
    delay(200);
    digitalWrite(pinB, LOW);
    digitalWrite(pinA, HIGH);
    delay(200);
    digitalWrite(pinA, LOW);
  }

Step 4: The Hookup

Picture of The Hookup
Photo Dec 13, 11 10 20 PM.jpg
This is a very simple hookup, its almost self explanatory to an extent. The Bluetooth Module (at least the one I have) runs on 3.3v, how handy, the Arduino has a pin for that. Make sure to give it the 3.3v and GND, but this next part is important, the TX on the module goes to the RX on the Arduino, and the RX on the module goes to the TX on the Arduino. From there, the 4 pins you chose for the code are each going to be attached to a relay, but for this test each one will have an LED. In my case, those pins are 4-7, just make sure they all have GND as well. Once that is done, there is no more up which can be hooked!

Step 5: The Test

Picture of The Test
I thought it to be inefficient to hook the relay shield up and do all the testing that this project required. So i came up with a very simple, yet efficient way to test and make sure the Arduino is using the code properly, if you are following the steps, you already have it hooked up to do this awesome test. Since each pin will be going HIGH and LOW to energize the relay, you can use the exact same code for LEDs. 


Step 6: The Relays

Picture of The Relays
Once the code is working exactly how you want it to, you can take off those silly LEDs and hook this puppy up to some relays. Now the relays I used, and the ones I posted at the beginning are 10A 250VAC, 10A 125VAC relays, so they will be perfectly find for handling your wall electricity. 

CAUTIONThis project involves high currents coming from the wall outlet in your house. Please be careful when doing projects that manipulate or involve the high voltage. If you are unsure of what to do, please find someone who does and ask them for help. Do not think that you can just figure it out as you go. 

This relay board takes 5v, so the male voltage pin goes to the 5v pin on the Arduino. GND goes to GND (I hope you picked that one up by now) and in1- in4 goes to your 4 pins on the Arduino. 

The next part is important if you have never worked with relays before, fortunately my father was an electrical contractor for 30 years so he made sure i was doing it correctly. The curved lines in front of the relay contacts that are facing each other and make a semi circle signifies the normally closed circuit. So hook the hot and GND wires from the outlet to the normally open contacts (this will be the two lines that aren't facing one another) When the relay is not energized the closed circuit will be unhooked, but when the relay becomes energized it will close the circuit and allow electricity to the outlet. 

Step 7: The Assembly!

Picture of The Assembly!
Photo Dec 25, 10 40 05 AM.jpg
Photo Dec 25, 10 57 10 AM.jpg
Photo Dec 25, 11 16 31 AM.jpg
Photo Dec 25, 11 41 51 AM.jpg
Here is the part where all that hard work comes together, the assembly. I decided that since I may not always be controlling Christmas lights, I will never turn down an automated power strip. So I made my project so that the outlets, the relays, and the box are all permanent, but the Arduino is easily retrieved. 

I picked up 2 plastic outlet boxes, and connected them back to back. One will have the outlets and the relays (with the plug being fed in a hole that is pre manufactured in the box) and the second box will hold the Arduino/ Bluetooth module. I drilled 3 holes in the box with the outlets, one goes all the way through to the other box for wires to and from the Arduino and relay shield. The other 2 holes are to mount the relay shield. 

Step 8: The Finale

Picture of The Finale
Now it is time to test it! If you hooked it up perfectly the first time (I didn't, like usual) then it will be working just like the LEDs were, if not, just reevaluate it, and fix the problem. If you use my Android app, or you make your own then uses a "random" or a "pattern" function, then try them, they look really great. The practical thing about this is, you can connect your phone to the Arduino, make the lights do what you want and disconnect it, and it will continue with your command. So if you choose the random function and then disconnected, it'll stay in the random function. 


Here is the finished product!

Thanks for taking the time to read all the way through this! If you have any questions at all please ask them, I would love to help other people out with this project or even something that isn't that similar.
sogseal made it!1 year ago

very nice, definitely going to find use for this! im still learning on how to sketch, so half of the sketch im really hazed on whats going on

20140214_194919 [1600x1200].jpg
3tuxedo (author)  sogseal1 year ago

Thanks so much. Which part of the code is confusing? I'd be more than happy to explain it.

I actually got it down after watching some tutorials on youtube, i mean i understand as i read it which is nice for me. Now im trying to figure out when my LED is on or off, to control brightness (PWM). i had it before but couldnt made android send text to adjust it, i believe that BT uses to send byte not text to work with PWM. Thanks man for the reply, waiting for more tuts , cheers

bwrussell2 years ago
Very well done, both on the project and the write-up. Great to see it in action!