0

I'm trying to control three relays with the Arduino Uno rev3 and a HC-05 bluetooth module from Elecfreaks.

I've wrote this code:

// the comments are the part of the code that I use to replace the Serial by default to be able to use the HC-05 module 
//#include <SoftwareSerial.h>
int relay1 = 2;
int relay2 = 3;
int relay3 = 4;
int relay1estatus = 0;
int relay2estatus = 0;
int relay3estatus = 0;
char command;
//SoftwareSerial bluetooth(5,6);

void setup() {
 Serial.begin(9600); //bluetooth.begin(9600);
 pinMode(relay1, OUTPUT);
 pinMode(relay2, OUTPUT);
 pinMode(relay3, OUTPUT);
 digitalWrite(relay1, LOW);
 digitalWrite(relay2, LOW);
 digitalWrite(relay3, LOW);
}

void loop() {
 while(Serial.available() > 0){ //bluetooth.available()
  command = (byte)Serial.read(); //(byte)bluetooth.read();
  Serial.println(command); //bluetooth.println(command)

  if (command == 'a'){
   if (relay1estatus == 0){
    digitalWrite(relay1, HIGH);
    relay1estatus = 1;
   }else{
    digitalWrite(relay1, LOW);
    relay1estatus = 0;
   }
  }

 if (command == 'b'){ 
  if (relay2estatus == 0){
   digitalWrite(relay2, HIGH);
   relay2estatus = 1;
  }else{
   digitalWrite(relay2, LOW);
   relay2estatus = 0;
  }
 }

 if (command == 'c'){
  if (relay3estatus == 0){
   digitalWrite(relay3, HIGH);
   relay3estatus = 1;
  }else{
   digitalWrite(relay3, LOW);
   relay3estatus = 0;
   }
  }
 }
} 

The problem is that this code works perfectly using the Arduino IDE serial monitor, but when I try it using instead of the default serial methods, the SoftwareSerial ones it doesn't work.

Doesn't work in a very strange way because I can switch one by one the relays between on or off but, when I have two of them activated seems like freezes, I can't switch the third one on or off and neither I can switch the other two again.

To send the 'commands' through bluetooth I use the Bluetooth Terminal app available in the PlayStore. (I've just posted the same question in stackoverflow but now I think here's the best place to ask it)

0

I don't see a problem with the code indicated for bluetooth; if that code is indeed ok, then probably you should look for hardware issues; first, look for low +5V when multiple relays are turned on. (Attach a voltmeter or a scope to +5, see if it changes when a second relay comes on, etc.) Second, look (with a storage scope) for glitches when the relays switch. If you don't have a storage scope, try testing with a separate power supply for the relays. (Tie the grounds of things together, but give the Arduino and the HC-05 +V that's isolated from relay +V.)

More seriously, your code is overweight. For example, replace

   if (relay1estatus == 0){
    digitalWrite(relay1, HIGH);
    relay1estatus = 1;
   } else {
    digitalWrite(relay1, LOW);
    relay1estatus = 0;
   }

with

    relay1estatus = 1 - relay1estatus;
    digitalWrite(relay1, relay1estatus);
  • Thanks a lot! This issue have been driving me mad for some days because I was sure it wasn't a software problem.. also I had the module connected at 3V and that cause some problems with the power as I saw, so I've connected the relays and the bluetooth in parallel to 5V and I've also added two capacitors just in case. – percidon Nov 5 '15 at 21:07

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.