Arduino + Android Plus Bluetooth Home Automation

Introduction: Arduino + Android Plus Bluetooth Home Automation

Updated 31/07/2014: a little bit change on the App, I delete my name and add the word Imagine so it doesn't look that private, and I want you be creative and build some cool stuff belongs to yourself :) Plus when you shake your device it will sing a mono song: Welcome to use this app! I also add more explanation about the codes as well as the details on its circuit. And the PDF files of PCB layout are available so you can download it and make your own PCB at home ;)

Well, I gotta confess I have a very bad memory, I always forget to turn off those lights somewhere in my room when I go bed, and I'm too lazy to get up to turn them off, especially in those freezing winters. After I wrote my first app on Android devices to teach my foreign friends Chinese swear words. I kind of think about writing an app to control those lights in my room via Bluetooth, it's pretty easy to write apps using MIT App Inventer, a web-based visible programming environment, and I use an Arduino, a HC-06 Bluetooth module(HC-05 also works, but HC-06 is much easier to find here), and a realy, with this opto-isolator to keep the circuit safer, to control my table lamp, it's a small project though, you can use the idea here and build other cool stuff, there is no ending on the way to creating and the answer is always not the only one, done ravings, let's make it :)

Step 1: Build Your Circuit & Hardware!

I use Altium Designer to design this circuit, each part is straightforward, an AMS1117 5.0V voltage regulator gets 5V from a 9V battery, I like to use this kind of rechargeable 9V battery in my projects. Also, I add a red LED in this circuit, when you turn on the switch it will give you a sign. You can see the typical application of AMS1117 in Picture 2, also you can use LM7805 to substitute for AMS1117 in project, since AMS1117 only gives this one SOT-223 footprint, which it's not that convenient if you build your circuit on breadboard(not the stuff you use to make bread :P) Check out this link below: http://en.wikipedia.org/wiki/Breadboard

The brain of this circuit is an Arduino Mini, as you can see the Tx and Rx on Arduino connected to the Rx and Tx on HC-06 Bluetooth module, because Arduino receive the signal that Bluetooth Module transmit. I mainly use the port D2 to control a relay here, so I simply draw other ports out and connect them on some pin headers, you can use the rest of ports to control other electric devices in your room, like a fan or something, or your PC speakers, my dad once bought these two lousy PC speakers which even don't a switch on them. Picture 4 is how an Arduino Mini and a FTDI FT232RL programmer look like. You can use Silicon Labs CP2102 or Prolific Technologies PL2032HX or program your Arduino Mini as well, but FT232RL chip will give an automatic reset signal, it's much easier that you don't need to wait for the opportunity to push the Reset button like a nervous egg. About these three kinds of programmer here is a pretty good video on YouTube(you can skip it or watch it only you are interested in):

As for the relay part, I use an opto-isolator to isolate the micro-controller and the relay, you can see it in Picture 5 here. Header P1 is used to connect to the relay, when D2 is set to be Low, the LED in this opto-isolator will turn on, current flows through the transistor in this opto-isolator to open another transistor Q1, which controls the On/Off status of this realy. I use this kind of realy which can be controlled by 5V signal. You can build your own circuit like I did or buy this kind of relay module with opto-isolator on eBay or SparkFun, here goes one on eBay, pretty budget: http://www.ebay.com/itm/5V-Two-2-Channel-Relay-Mo...

Picture 6 is how my PCB(Printed Circuit Board) looks like, as for how to make your PCB at home, I recommend this video on Make Magazine YouTube channel. You can use a household clothes iron to transfer the circuit layout you printed on the OHP paper, or other kind of slippy paper, rather than use the fancy UV light or some other things here. Also, you can use a pan to solder those SMD chips and other stuff, it works I swear.

As I find this little pretty looking plastic box I put all my stuff in, I also drill holes on the top to insert switch and this red LED, you can use any kinds of drills to get this work down, just make sure it looks good :)

You can download the PDF files next to those photos below, simply use a laser print to print them on OHP paper then transfer them onto Copper Clad Laminate(CCL), I always use mom's household clothes iron to do that. When you make PCB with two layers, after you transfer one layer use these little holes on board to fix the other layer then transfer it, or let's say make their position right. You can find the value of those components in the schematics above, note that U2 is a opto-isolator, a pad with "+" on its left is 9V battery's positive input, another pad on its right is battery's negative input. Below the positive input pad there are two pads for adding a switch to start this circuit.

If you don't wanna make a PCB you can build your circuit on breadboard according the schematic above, that's pretty cool, too, sometimes you can also make your circuit a work of art with breadboard :)

Step 2: Write Your Own App on Android!

Things become pretty easy if you use MIT App Inventor to write your Apps on Android devices, I'm not a very good programmer I gotta confess, but I do get things done with MIT App Inventor, it's not difficult, everybody can build their own app. You can watch their tutorial videos here: http://appinventor.mit.edu/explore/ai2/tutorials....

From the first picture in this section here you can see the programming interface, different from other programming environments it uses visual language rather than complex codes, you can drag what you want in the left list, like Button, Label, Image and etc. You can add Sensors(such as accelerometer) or Connectivity(such as Bluetooth) in your project as well.

After you finish the user interface design click "block" button on top right corner, it's time for programming. Wait, programming, ain't that something damn nearly driving people crazy? But here you program by dragging all those blocks, just like jigsaw puzzle game you played in your childhood. I add two elements called ListPicker and BluetoothClient, as you can see I drag the block "when ListPicker1. BeforePicking" & "when ListPicker1. AfterPicking", and I attach BluetoothClient1's addresses and names as its elements, which means when you click the ListPicker1 button, it will show you a list of BluetoothClient1's addresses and names. After you choose a Bluetooth the text will be set as "Connected", otherwise it's "Not Connected". What you need to do next is to drag two buttons, one you touch it will turn on your table lamp, the other is off. As you can see the third photo in this section, when you click button one, it will change its color to red, and send a text message, I use "On" here, to BluetoothClient1, to tell it to turn on the lamp; when you click button two, it will change its color to Blue, sending a text message, I use "Off" here, to BluetoothClient1, to tell it it's time to turn off the lamp.

Almost there, you can also add other interesting features, trust me, build your own app in this way is quite easy & funny, the only limitation is your imagination. If you don't wanna write your own app you can download .apk file from my Google Driver, you can also find an App teaching you Chinese swear words there, lol, copy this link to your browser and download the app(updated on 31th, July): https://drive.google.com/file/d/0B461uZ-xqRZOa2dad3FMYXptVVU/edit?usp=sharing

Step 3: Program for Arduino

Program for Arduino in this case is fairly easy here, remember we connected Tx and Rx on Arduino to Rx and Tx on HC-06 Bluetooth module, so simply the Arduino just receives the texts from its Rx and do something according to the texts. Considering power saving I add this LowPower library into the code, you can download the library from GitHub, link goes here: https://github.com/rocketscream/Low-Power

I put Arduoino sleep mode into idle, only in this mode you can still keep USART(Rx & Tx) awake, when the texts come in it will wake up Arduino and ask it do some stuff, here is how the code looks like:

Explain the code: most of the time your Arduino is in idle mode, only when signal comes from Rx pin it will start to work. You can set up a string named readString here, to store the text coming from Rx pin. As we talk in last section, when you touch those buttons on the app, it will send texts via Bluetooth. So when your Arduino receives the message "On", simply set Pin2 as low, this signal will let the relay get through, on the other side, when your Arduino receives the message "Off", set Pin2 as high, this action will turn off your table lamp.

More Info: Arduino sleep modes can be little bit spooky, since ATmega 328 chip supports six sleep modes, but only in Idle mode USART will still keep awake, to receive signals from Rx pin to wake the chip up. Normally when an Arduino works in Idle mode its power dissipation will drop around 60%, low power is a pretty cool area to discover.

Step 4: Assemble Everything Together!

It takes a little bit time to assemble everything, I took my table lamp down and added this circuit in. Make sure to use heat shrink tube or electrical tape to cover the 220V parts, if you don't wanna let your table catch a fire. Actually the electrical current is quite low here, I have this 5W LED bulb which only consumes 5W/220V = 23mA, but always make sure your project is safe! You can use a heat gun or a hair drier to deal with heat shrink tube, the only difference is the later one may take a little bit long time.

Step 5: Give It a Try & Have Fun!

I shot this not-so-fancy video several days ago, windows movie maker kills me. And I promised to write this tutorial by the end of this week and I did it :) If you have any problems or other inspiring ideas please drop comments below, I'll try my best to answer iot as well as hear your inspiring ideas! Have a good day, hoho