I'm making a Bluetooth garage door opener using an Arduino pro mini 5v 16 mhz (sparkfun version), this bluetooth module, and this relay.
Here's my code:
char val; // variable to receive data from the serial port
int relayPin = 8; // relay on pin 8
void setup()
{
pinMode(relayPin, OUTPUT); //pin relay control is hooked up to
pinMode(4, OUTPUT); // constant power for relay power
pinMode(5, OUTPUT);// constant power for bluetooth module vcc
digitalWrite(4, HIGH); // constant power for relay power
digitalWrite(5, HIGH); // constant power for bluetooth module vcc
Serial.begin(9600); // start serial communication at 9600bps
Serial.println("READY");
}
void loop()
{
if( Serial.available() ) // if data is available to read
{
val = Serial.read(); // read it and store it in 'val'
}
if( val == 'o' || val == 'O' )
{
digitalWrite(relayPin, HIGH);
}
else if( val == 'c' || val == 'C' )
{
digitalWrite(relayPin, LOW);
}
delay(100); // wait 100ms for next reading
}
On the mobile end I use a serial Bluetooth terminal.
Please note that the relay is connected by the normally open leg.
It all works great when I send the command it opens the relay connected to my garage switch which opens the door. I'm having two issues though. The first issue is that sometimes when I cycle the power on the Arduino it cycles the relay opens the garage door. The second is that sometimes when I connect to the Bluetooth (even before I send any command or even open my terminal) it cycles the relay and opens the door.
Any ideas why this is happening or how I can prevent it?
pinMode(4, OUTPUT); // constant power for relay power
pinMode(5, OUTPUT);// constant power for bluetooth module vcc
digitalWrite(4, HIGH); // constant power for relay power
digitalWrite(5, HIGH); // constant power for bluetooth module vcc
It looks as though you're trying to use an output pin as a power source for your relay board and another output pin as a power source for your BT device?
I changed that and it wasn't the problem (although still a good thing to do). The relay is behaving very weird. When I plug in the arduino the relay digital port is set to off (verified by an led wired in parallel). The relay still turns on. When I set the pin to high the relay turns off. Here's the strangest part. If I unplug the relay signal which is connected to the arduino digital, it then turns off. It is confusing since it should not be getting power just as it is not getting power when the pin is low. Is it something to do with the common ground?
At reboot all pins are set as input per default.
So you cannot be sure of they are high or low, they have a "floating" voltage.
In general:
Don't use pin 0,1,13 (Serial RX<TX and Led) and don't assume the pins are low.
If you want to make sure your pin 8 is low at boot time, pull it down with a resistor (5k or so) to the ground.
I figured out a solution. Thanks for the pull down resistor idea. I looked up more and learned new stuff! Anyways the pull down resistor did not completely solve it. After messing around a bit with my relay board I discovered that for some reason when I ground the input pin it turns on the relay. So what I did is I then mapped the relay 5v vcc to a pin and then cycled that. Now it works. I'm not using the relay board it as intended, but I've been testing it for a while and it works so... Thank you everyone for the suggestions.