Pages

Friday, April 10, 2015

Relay Commander





Ok, this is going to be one lengthy entry. I am going to tell you how to switch the lights in your home (or sunshades, or whatever electrical applience) on or off with your Android phone using bluetooth.

So this involves electronics, programming the Attiny85 and making an Android App. All sounds complicated but is in reality much easier as it sounds.

In a previous entry I told you I ordered some HC06 bluetooth modules and I also told you that you could use them even with an Attiny85. So that is what we are going to do. I am also giving you the source code of the App Inventor program and the Arduino code so you can modify this for your own needs.

We are going to attach a bluetooth module to the Attiny85. This uses two of the pins of the Attiny85. So we still have 3 pins left. On these pins we are going to attach a relay.

Why a relay. Well that is obvious. The Attiny can not control 220 volts directly. The small tiny can just put out 5 volts at a very low current like 20 milivolts. And qwe want to control 220 volt 35 watt lamps or whatever.

So first I will tell you how to attach a relay to an Ayttiny85. Here are the schematics.

Ok in this picture you can see a resistor of 2.2k which is attached to the base of a BC547 transistor. At the other side of the resistor we will attach the Attiny85. The transistor will amplify the signal it receives from the tiny. The amplified signal will then activate the coil of the relay which will then activate and put on the ;light or whatever. Please note the diode which is connected parallel on the relay coil but in the false direction. This will prevent any current floating back into the circuit and ruining your transistor or Attiny85.

Testing the circuit so far is easy. just connect the polarity tester which is described on this webpage (click here) to where the relay should be connected and both leds should be off or shine faint. Now when you attach the loose end from the resistor to the 5 volts power supply one of the leds should light up. If that is the case youre ok. If not check the wiring again.

Next step is to connect the Attiny85 and the HC06 bluetooth module.




As you can see pin 7 of the Arduino is attached to the resistor. This will actually switch the relay on and off. Pin 5 and 6 are attached to the bluetooth module. I will give you a breadboard setup of this circuit.



For clarity I have left out the 5 volt power line and the ground line to the Attiny85 and the HC06 module as that speaks for itself.

So this part is ready now the software for the Attiny.

To have the Attiny85 talk to the bluetooth module we need to include a library in our sourcecode being the SoftwareSerial library which is standard included in the Arduino IDE.

So the code looks like this:



The code is simple. it polls the serial connection for a character and then on receiving the character it decides to put the Arduino pin High or Low. There are a lot of comments in to make life easier for you. I decided to only put the code for one relay in. Expanding to 2 or 3 relays should be easy for you.

Next step is to make an app in Mit's App Inventor. I wrote about app inventor before on this blog and you can find that entry here (click).

The code is very simple and self explanatory.



For those amongst you who have no experience with app-inventor maybe a short explanation will help you.
First the Listpicker command helps us to make a bluetooth connection. Next you see the buttons which are present on the screen. There are 6 buttons. Each relay has 2 buttons on for setting the relay on and one for putting it off. Well the code for that is easy. For relay 1 we send a 0 or a 1 to the Attiny over bluetooth. For relay 2 we send a 2 or 3 and for the last relay we will send 4 or 5. That all that is to it.

On your Android phone or tablet it will look like this.



To install this app go to the bottom of this screen and click on the download button at the apk file. Do this off course on your phone or tablet. You can do it on your PC to. Then you will have to transfer it to the phone or tablet manually. The apk file will be in the doewnload folder of your phone/tablet. Now click on it and Android asks if it has to be installed. Off course it has to. Then activate Bluetooth on the phone. Next start the app and first click on the Pick your bluetooth adapter text. You will see a list off blutooth devices and the HC06 will be one of them. Click on that and when the connection has been established the light on the HC06 will steady.

Detailed information on installing the app can be found in this story where we do something similar.

Now you can use the buttons on the phone to set the relay's on or off.

That's it. Easy. piece of cake.

Just a warning.
Connecting a relay to your home appliences involves high voltage and current. Please be carefull and only do this if you have experience with electricity. I do not take any responsibility for any mishaps, fires or electrocutions that may happen to you. You do this on your own risk.

Rests only one thing: the source files for Arduino and Mit App inventor and the Android APK file.

So till next time have fun.

Ohh and for those amongst you that are curious about the colours of the burrons. You know where this will lead to don't you........ In the end we are going to control a led strip with our phone.



Relais01attiny.ino


Relaycommander.aia


Relaycommander.apk