Relay does not want to switch

hello everyone,
i want to get relays to switch by sending characters into a bluetooth module HC-05. when i sent characters to the module nothing happens. someone who can help me with it?? im fairly new to using arduino so it could be that i made some stupid mistakes

#include <SoftwareSerial.h>

//Bluetooth Tx pin to Arduino 1
//Bluetooth Rx pin to Arduino 0
#define rxPin 2
#define txPin 3
SoftwareSerial Bluetooth(rxPin,txPin);

#define RELAY_1 4
#define RELAY_2 5
#define RELAY_3 6
#define RELAY_4 7
#define RELAY_5 8
#define RELAY_6 9
#define RELAY_7 10
#define RELAY_8 11
#define RELAY_9 12

String data = "";

void setup() {
  
pinMode(RELAY_1, OUTPUT); //Relay 1
pinMode(RELAY_2, OUTPUT); //Relay 2
pinMode(RELAY_3, OUTPUT); //Relay 3
pinMode(RELAY_4, OUTPUT); //Relay 4
pinMode(RELAY_5, OUTPUT); //Relay 5 
pinMode(RELAY_6, OUTPUT); //Relay 6 
pinMode(RELAY_7, OUTPUT); //Relay 7 
pinMode(RELAY_8, OUTPUT); //Relay 8 
pinMode(RELAY_9, OUTPUT); //Relay 9  
  
  
  Serial.begin(115200);
  Serial.println("Arduino serial initialize");
  
  Bluetooth.begin(9600);
  Serial.println("Bluetooth software serial initialize");
}

void loop() {
  
  while(Bluetooth.available() > 0){
    data = Bluetooth.readString();
    Serial.println(data);
    
    data.toLowerCase();
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 1
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    if(data == "valve 1 on"){
      digitalWrite(RELAY_1 , HIGH);
      digitalWrite(RELAY_9 , HIGH);}
    else if(data == "Valve 1 off"){
      digitalWrite(RELAY_1 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 2
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "valve 2 on"){
      digitalWrite(RELAY_2 , HIGH);
      digitalWrite(RELAY_9 , HIGH);}
    else if(data == "Valve 2 off"){
      digitalWrite(RELAY_2 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 3
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "Valve 3 on"){
      digitalWrite(RELAY_3 , HIGH);
      digitalWrite(RELAY_9 , HIGH);}
    else if(data == "Valve3 off"){
      digitalWrite(RELAY_3 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 4
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "valve 4 on"){
      digitalWrite(RELAY_4 , HIGH);
      digitalWrite(RELAY_9 , HIGH);}
    else if(data == "Valve 4 off"){
      digitalWrite(RELAY_4 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 5
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "Valve 5 on"){
      digitalWrite(RELAY_5, HIGH);
      digitalWrite(RELAY_9, HIGH);}
    else if(data == "Valve 5 off"){
      digitalWrite(RELAY_5 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 6
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "valve 6 on"){
      digitalWrite(RELAY_6, HIGH);
      digitalWrite(RELAY_9, HIGH);}
    else if(data == "Valve 6 off"){
      digitalWrite(RELAY_6 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN 
    //Control Valve 7
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "valve 7 on"){
      digitalWrite(RELAY_7 , HIGH);
      digitalWrite(RELAY_9 , HIGH);}
    else if(data == "Valve 7 off"){
      digitalWrite(RELAY_7 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    //Control Valve 8
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
    else if(data == "valve 8 on"){
      digitalWrite(RELAY_8 , HIGH);
      digitalWrite(RELAY_9 , HIGH);}
    else if(data == "Valve 8 off"){
      digitalWrite(RELAY_8 , LOW);
      digitalWrite(RELAY_9 , LOW);}
    //NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN   
  }    
}

To better see what you receive, change the above to

Serial.print("[");
Serial.print(data);
Serial.println("]");

If the closing ] is on a new line, it can explain the behaviour.

Better would even be to print the received characters one by one as hex so you can see if you receive a '\r' (0x13) 0x0D (13 decimal) and/or '\n' (0x10) 0x0A (10 decimal) at the end.

What is the sender?

1 Like

Unless you’re supremely confident, it’s also worth posting your circuit diagram including power supplies and other extras l

i'm just sending the commands from my phone

As @lastchancename said, showing us your circuit diagram would help a lot

could you explain this in an easier language, it's my first time using arduino so i need to learn the language a little bit

Your relay modules don't work because they are not being powered.

You need a power supply to power them. You can power a single relay off an Arduino, maybe two. But not 9 relays.

You can't even power 1 relay from an Arduino powered by a 9V battery.

yeah my bad normally they are being powered by a 5V powersupply


Please post an accurate schematic (schematic, not wiring diagram).

the soil moisture sensor has to give a signal to pin 9

Good luck.

1 Like

these are the schematics i made on e-plan around this project. this is the most accurate i have. it normally works with a wemos D1 R2 but i can't get the internet from school so i had to use a HC-05.




The ‘transformer’ at the lower left is curious.
Apart from anything else, you’re feeding the circuit with AC not DC

Why are you transmitting an 11 or 12 character ASCII string to represent the ON or OFF state of one valve? You could send one binary word (two bytes) to represent the states of all 9 valves at once.

The valve on pin 9 has to work by the sensors on picture 3 off my eplan schematics. Valve on pin 12 has to work when any other valve opens. The rest work just randomly when u send to the HC-05 that they have to work, so they could all work apart or all togheter.

The valve's work on 24Vac.

When you type something in e.g. a text editor and you press enter to go to a new line, one or two additional characters are placed in your text that indicate that the next typed character will be on a new line.

Let's say that you're typing something (example hello) in serial monitor and you press <enter> or click `. Depending on the line-ending setting of serial monitor, you are are actually sending

setting data hex
No line ending hello 68 65 6C 6C 6F
Newline hello\n 68 65 6C 6C 6F 0A
Carriage return hello\r 68 65 6C 6C 6F 0D
Both NL and CR hello\r\n 68 65 6C 6C 6F 0D 0A

The application on your cell phone might do the same. To see what you are actually receiving, the below shows the basics.

String data;

void setup()
{
  Serial.begin(115200);

  data = "hello";
  Serial.print("printing \"hello\"\t");
  printText(data);
  printHex(data);
  data = "hello\r";
  Serial.print("printing \"hello\\r\"\t");
  printText(data);
  printHex(data);
  data = "hello\n";
  Serial.print("printing \"hello\\n\"\t");
  printText(data);
  printHex(data);
  data = "hello\r\n";
  Serial.print("printing \"hello\\r\\n\"\t");
  printText(data);
  printHex(data);


}

void loop()
{
  // put your main code here, to run repeatedly:

}

/*
   Print a String
    In:
      String object
*/
void printText(String dta)
{
  Serial.print("[");
  int cnt = Serial.print(dta);
  Serial.println("]");
  Serial.print("Num characters printed: ");
  Serial.println(cnt);
}

/*
   Print a String in hex
    In:
      String object reference
*/
void printHex(String &dta)
{
  int cnt = 0;
  while (dta.charAt(cnt) != '\0')
  {
    if (dta.charAt(cnt) < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(dta.charAt(cnt), HEX);
    Serial.print(" ");
    cnt++;
  }
  Serial.println();
}

and the output looks like

09:35:17.861 -> printing "hello"	[hello]
09:35:17.861 -> Num characters printed: 5
09:35:17.861 -> 68 65 6C 6C 6F 
09:35:17.861 -> printing "hello\r"	[hello
]
09:35:17.861 -> Num characters printed: 6
09:35:17.861 -> 68 65 6C 6C 6F 0D 
09:35:17.861 -> printing "hello\n"	[hello
09:35:17.861 -> ]
09:35:17.861 -> Num characters printed: 6
09:35:17.894 -> 68 65 6C 6C 6F 0A 
09:35:17.894 -> printing "hello\r\n"	[hello
09:35:17.894 -> ]
09:35:17.894 -> Num characters printed: 7
09:35:17.894 -> 68 65 6C 6C 6F 0D 0A 

If you see 0D (CR, Carriage Return) and/or 0A (NL, New Line), you will have to either include them in the compare or you have to strip them off.

Some remarks:

  1. Serial.print returns the number of characters that were send over the serial port; that is used in above code to show how many characters were printed.
  2. The code uses two functions; the first one uses a String as the argument, the second one use a reference to the String. With the first one, the full String object is placed on the stack and wastes memory, the second one only places a reference (two bytes on 8-bit microcontrollers) to the String object on the stack.
  3. Serial monitor has its own behaviour when it comes to handling received CR and NL.

Note:
I corrected the mistake in post #2 where I accidentally mixing up decimal and hexadecimal.

E-plan should be able to EXPORT jpg images, check in the files tab.
Screen shots lack resolution and waste screen space.

Thanks.. Tom... :smiley: :+1: :coffee: :australia: