Well, in this post I want to share a small sketch which can turn on and off a relay or what ever you want via bluetooth serial monitor.
To control one thing with smart phone and normal switch we have to use "Two way switching method" like the picture below.
As you can see in picture above that you can turn on/off the light by using relay and switch.
Let's see the sketch now.
int relay = 9; void setup() { Serial.begin(9600); Serial.setTimeout(100); pinMode(relay, OUTPUT); } String str; void loop() { if(Serial.available()) { str = Serial.readStringUntil('\n'); if(str == "z") { digitalWrite(relay, LOW); Serial.println("z"); } if(str == "x") { digitalWrite(relay, HIGH); Serial.println("x"); } } } |
Pin 9 is for relay.
When you send letter z via serial monitor, it will turn on/off the relay and same with the letter x.
Note:
You probably thinking that why letter z and x working as same instead of telling us in the light is on or off.
Well, that's because if you make letter z to turn on the light and at the same time it tells you that the light is on, but some one else turns off the light by using the normal switch, so in this case even though the relay is on (which turned on the light) but the light will be off because of the normal switch (which turned off the light).
Please watch the video to see the sketch in action.
What do you need?
1x Aduino bluetooth transceiver.
1x 10k ohm resistor.
1x 20k ohm resistor.
1x Relay
1x Switch
Few wires and download an app called BlueSerial Beta from app store. That's all. :)
***
Tags:
Arduino bluetooth relay control
No comments:
Post a Comment