Introduction: Control Relays With a Custom Android App!!
This here is a final project that I made for my micro controller class at my local junior college.
Its a 4 channel relay board that is controlled via a Bluetooth device as well as an android app for a simple interface to control the individual relays.
As far as my understanding of them goes, a relay is a sort of switch allowing low powered circuitry to toggle higher powered devices such as motors and provide an isolation between the two. I am still a student so I'm learning as I go making this a learning experience for me as well as anyone else that may wish to tackle this project!
What you'll need for this project is an understanding of how to create a program within the arduino IDE and if you wish to create your own app (optional) an understanding of how to use the MIT App Inventor would be a great help as it would have saved me a good bit of time.
materials:
an arduino compatible board (I used the arduino uno)
a relay board (this depends on how many things you wish to control, I had a 4 channel from seeedstudio.com)
a Bluetooth transceiver such as the HC-05
a Bluetooth capable device such as a phone or tablet
A warning: Relays can be used to control higher powered devices that could potentially expose the user to dangerous voltages! That being said, I do not assume any kind of responsibility for any injury or damages and suggest that you exercise caution.
Step 1: Wiring It Up.
My relay board came in shield form so it was as simple as plug and play, however many relay boards do not so for the sake of teaching i will be explaining how to wire up one that does not stack on the arduino.
first we'll start off wiring up the GROUND and 5V pins.
next is to wire each relay to the board
relay 1 - pin 7
relay 2 - pin 6
relay 3 - pin 5
relay 4 - pin 4
if you wish to test if your wiring is correct simply upload the blink code in the examples area (after making sure the pin that is supposed to blink is changed in the code to any of the relay pins). If you aren't using a solid state relay you should be able to hear an audible click coming from your relay! For redundancy I used the continuity tester on my multimeter which would beep each time the circuit was closed further confirming my relay was switching as it should. Testing things as you go makes for less potential hair pulling due to problems in the future.
Step 2: Wiring Up the Bluetooth Module.
The Bluetooth module was really easy to wire up, aside from the power and ground wires you only need one other wire.
The HC-05 module has labeled pins so wiring it up is pretty self explanatory.
A couple notes about it though.The En pin is the enable pin allowing you to change the settings of the module itself (such as visible name and password) using AT commands. The TX pin is the transmit pin and is usually wired to the RX pin of the arduino and vice versa for the RX pin. THE ARDUINO CANNOT RECEIVE AN UPLOAD WHEN THE RX PIN IS OCCUPIED so make sure to unplug the bluetooth module when uploading your code.
And finally the state pin is a status monitoring pin. wiring an led to it allows you to identify what state the module is in (like if it's paired to a device or not).
Step 3: Making It Work.
The last thing to do to get this thing working is to upload the code for it and get the thing running.
A quick note:
bluetooth likes to send the ascii code for the decimal characters to be sent.
for example in the code we send a number 1 but the ascii equivalent 49 is received. To make things happen we can send a decimal number but watch for an ascii code to show up.
Attachments
Step 4: The App!!
This here is an optional step but for this to work over bluetooth you need at least some kind of bluetooth terminal application available at the app store. These work great but I wanted my own custom interface to control my devices the way I wanted.
I used the MIT App Inventor 2 to create my app. They have an interface much like the "scratch for arduino" that many use to first get into programming arduinos.
if any of you feel like using a copy of my app feel free to use it, but it will need modification to use with more than a
4 channel relay board.
the .apk file is the one to install onto your phone. the .aia file is uploaded to MIT app inventor to change it.
feel free to modify or change anything here to fit your needs!
52 Comments
4 years ago
Good day to you,
Just wanted to say thanks LOADS for the 'ible - an amazingly awesome tutorial for folks learning !! YOU ROCK !!!
5 years ago
Thank you so much its so helpfull...
6 years ago
Using linked .apk, I get a "Runtime error. Permission Denial: starting Intent{act=android.intent.action.MAIN cmp=com.android.vending/com.google.android.finsky.setup.
there is more, but end is
requires com.android.vending.setup.PLAY_SETUP_SERVICE
any thoughts? this looked like just what I needed to learn with
I also tried to "build new apk, same result.
Using Gal S4
TIA
Reply 6 years ago
I made this app over a year ago and there have been a few updates to android phones since then. im assuming that would be the reason it's not working any longer because I get the exact same error on my phone. what could be done though is to upload the .apk to the app inventor and tweak it till it works with the current version of the phones.
Reply 6 years ago
Thanks for the reply, I am learning app inventor and will soon be able to make use of and alter the framework of the app you made,
Thanks again, should I stumble across the needed change I will post.
6 years ago
Hi Bodhi,
Very neat and handy instructable. I want to make a setup similar to this for powering on my air condition before I come home from work (India is very hot !!) But how do I make the command to android phone to turn on the Bluetooth relay when I am at work?
I think it would need one more app which can recognize a special command sms and when the android phone receives a sms, this auxiliary app recognizes the unique code in sms and triggers the corresponding relay to turn on. But This would require programming skills which I don't have. Any info would be helpful. Thanks
Reply 6 years ago
Well first off the Bluetooth won't have that kind of range to reach your home from work. You'll need some kind of web service to do that. If you don't know how to do that yourself, a website called carriots has a free (at least for the first one) intertnet of things service that will let you control the relays via the internet. As for programming the relays, it should be similar to how I've done it. Have them wait and watch for you to send something and, once they see it, perform an action such as turning on or off. Hope that helps
Reply 6 years ago
Thanks Bodhi3523.
7 years ago
Very comprehensive guide, great work.
7 years ago
Your project is awesome :)
I would be happy if you could help me: add a timer Button!!!
On or Off by timer (Selection of 5 ,10 ,15 Minutes -For example)
thanks
david
7 years ago on Step 4
wow thanks very nice tutorial... can you make it via http and not via bluetooth ???
like to use a static ip 192.168.10.10:4000 ....
Reply 7 years ago on Step 4
sorry for the late reply, im still a bit new to this. I'm sure you could but i have no idea how to go about it. if you find out how let me know haha
Reply 7 years ago
I had the same idea, I am currently setting up the ethernet board and will try to adjust his app to work through http this week. It has been a long time since I used appinventor but think I'll have it up and running next weekend.
Reply 7 years ago
cool I'd love to see the finished product if either of you guys feel like linking it to me!
Reply 7 years ago
you know, I have a WiFi and Ethernet shield but haven't gotten around to trying either of them yet so I'm not sure. I'll have to try it out.
7 years ago on Step 4
Very nice, Thank you for the aia file.
Reply 7 years ago on Step 4
no problem! feel free to make it better haha
Reply 7 years ago
no problem!
7 years ago
My final year project is to create an android app for my school with the following features 1) should have a logging panel for 2) profile where user can upload profile change password etc 3)a forum where student can post 4) panel where student can record lecture audio and Video 5)panel where student can access school lecture note. And I don't know how to start please I need help
Reply 7 years ago
my best advice would be to find something that is similar and modify it to fit your needs. the MIT app inventor has a kind of marketplace that other users upload their projects allowing you to try them out or modify them. my app above is a highly modified Bluetooth terminal. I hope that helps you get started!