how to program bluetooth control for 2 relay module [SOLVED]

Hello all,

I am currently working on a project that uses relays, a little different than the one found in your article here. http://waihung.net/bluetooth-controlled-relay/

I've attached a picture below of both the relay module and the bluetooth (JY-MCU HC 05). I am using the Arduino Uno as of now and the IN1 and IN2 from the relay module are in pins 6 and 7 respectfully. I modified the code that was provided in order to control 2 relays instead of one. I've attached the code also.

As of now, I can get the IN1 connected relay to turn on and off but not the 2nd relay, I am wondering what I am missing in my code that would make this work.

Additionally, I had to split the wires leading from both the relay module and the bluetooth module in order to connect both to the 5V pin on the Arduino. Is there a better way to do this? because both need a source of power.

Thanks!

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,7);

//----Global variables for the pins---//
int pin1 = 6; 
int pin2 = 7;

//Initialise for the Arduino setup for OUTPUT to the pins designated as ints
void setup() 
{
  Serial.begin(9600);
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);
  digitalWrite(pin1,LOW);
  digitalWrite(pin2,LOW);
  }

void loop()
{
  
  if (Serial.available()) {
    
    delay(100);
    
 while(Serial.available() > 0){
  {
    if((Serial.read())=='1')
    {
      if(digitalRead(pin1)==LOW)
       {
        digitalWrite(pin1,HIGH);
        Serial.println("TURNED OFF Relay 1");
       }
      else
      {
      digitalWrite(pin1,LOW);
      Serial.println("TURNED ON Relay 1");
      }
	  
      if((Serial.read())=='2')
      {
        if(digitalRead(pin2)==LOW)
        {
          digitalWrite(pin2,HIGH);
          Serial.println("Turned OFF Relay 2");
        }
        else
        {
          digitalWrite(pin2,LOW);
          Serial.println("Turned ON Relay 2");
        }
		}
		}
		}
		}
		}
		}

HC 05 bluetooth module.jpg

2 relay module.jpg

The problem his there:

      if((Serial.read())=='2')

or there:

if(digitalRead(pin2)==LOW)

If you do something like:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,7);

//----Global variables for the pins---//
int pin1 = 6; 
int pin2 = 7;

intpinLED = 8;

//Initialise for the Arduino setup for OUTPUT to the pins designated as ints
void setup() 
{
  Serial.begin(9600);
  pinMode(pin1,OUTPUT);
  pinMode(pin2,OUTPUT);
  digitalWrite(pin1,LOW);
  digitalWrite(pin2,LOW);

  pinMode(pinLED,OUTPUT);
  digitalWrite(pinLED,HIGH);
  }

void loop()
{
  
  if (Serial.available()) {  
    delay(100);
    while(Serial.available() > 0){
      digitalWrite(pinLED,LOW);
      if((Serial.read())=='1') {
        if(digitalRead(pin1)==LOW) {
        digitalWrite(pin1,HIGH);
        Serial.println("TURNED OFF Relay 1");
      }
      else {
        digitalWrite(pin1,LOW);
        Serial.println("TURNED ON Relay 1");
      }

      if((Serial.read())=='2') {
        digitalWrite(pinLED,HIGH);
        if(digitalRead(pin2)==LOW) {
          digitalWrite(pin2,HIGH);
          Serial.println("Turned OFF Relay 2");
        }
        else{
          digitalWrite(pin2,LOW);
          Serial.println("Turned ON Relay 2");
        }
      }
    }
}

you can understand here is it.

Nevermind my last post. The problem is, with no doubt here:

SoftwareSerial mySerial(6,7);

This 2 pins can't be serial and at the same time outputs!

Ok I see. Should I assign different output pins in the mySerial function?

I don't know how your setup is assembled, but you have 2 options:

  • or you change the pins of the "mySerial";
  • or you change the pins of the relays.

Do what is easiest for you!

Hey luisilva,

I was able to figure out a way to get to work like I wanted. I got some help and was able to get the bluetooth to communicate with the code in mySerial, and have characters "3" and "4" turn on the relays and "1" and "2" to turn off the relays.

Thanks for your help!

OK. It's the same that I say.
Can you please change the subject of the first post to "[SOLVED]...".

Good luck to the rest of the project.