Switching a 12v relay with Arduino

Thread Starter

AdhesiveLamb

Joined Feb 16, 2019
19
I can't seem to figure out this 6 channel relay. (https://www.banggood.com/BESTEP-6-C...n-For-Arduino-p-1356223.html?cur_warehouse=CN) Whenever I send power to the system all of the LEDs on the relay turn on. When I send the relay a digital write high command the corresponding LED on the relay gets dimmer but the relay itself doesn't switch. Currently I have Vcc on the relay going to the +12v rail and GND on the relay going to -12V rail and to arduino GND. Is it because the relay is a low level trigger? If so is there any way around this?

I can take more pictures of my setup if necessary. Been stuck on this for a couple hours, any help would be greatly appreciated. Thanks!
 

Attachments

djsfantasi

Joined Apr 11, 2010
6,974
How are the pins on the Arduino (which go to IN1-IN6) initialized?

Can you provide your Arduino sketch. Use code tags (start your listing with:
“[ code=C]”​
and end it with:
“[ /code]”)​

And to activate the relay, you need to output LOW on the pin:
digitalWrite(yourPin,LOW);​
 

Thread Starter

AdhesiveLamb

Joined Feb 16, 2019
19
C:
int relief1 = 5;
int relief2 = 6;
int topValve = 7;
int pumpSwitch = 8;
int sensorSwitch = 9;

int pressureAnalog = 0;
int batteryAnalog = 1;

int tx = 1;
int rx = 0;
char inSerial[15];
int pressurevalue = 1;

void setup() {
  Serial.begin(9600);  //sets the baud rate
  pinMode(pumpSwitch, OUTPUT); //defines pins as either output or input
  pinMode(topValve, OUTPUT);
  pinMode(relief1, OUTPUT);
  pinMode(relief2, OUTPUT);
  pinMode(pressureAnalog, INPUT);
  pinMode(batteryAnalog, INPUT);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);

}

void Check_Protocol(char inStr[]){  //establishes text input for interaction
  int i=0;
  int m=0;
  Serial.println(inStr);

  if(!strcmp(inStr,"pump on")){  //pump ON
  digitalWrite(pumpSwitch, HIGH);
  Serial.println("Pump is on");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
 
  if(!strcmp(inStr,"pump off")){  //pump OFF
  digitalWrite(pumpSwitch, LOW);
  Serial.println("Pump is off");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
 
  if(!strcmp(inStr,"valve on")){  //topvalve ON
  digitalWrite(topValve, HIGH);
  Serial.println("Valve is closed");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}

  if(!strcmp(inStr,"valve off")){  //topvalve OFF
  digitalWrite(topValve, LOW);
  Serial.println("Valve is open");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
 
  if(!strcmp(inStr,"relief  1")){  //relief valve 1
  digitalWrite(relief1, HIGH);
  Serial.println("Relief 1 activated");
  delay(2000);
  digitalWrite(relief1, LOW);
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}

  if(!strcmp(inStr,"relief 2")){  //relief valve 2
  digitalWrite(relief2, HIGH);
  Serial.println("Relief 2 activated");
  delay(2000);
  digitalWrite(relief2, LOW);
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
  if(!strcmp(inStr,"Start")){
  //startloop();
  }

  else{
  for(m=0;m<11;m++){
  inStr[m]=0;
  }
  i=0;

}}
void loop() {
  int i=0;
  int m=0;
  delay(500);
  if (Serial.available() > 0) {  //checks if data is available in the serial port 
  while (Serial.available() > 0) {
  inSerial[I]=Serial.read();
  i++;  
  }
  inSerial[I]='\0';
  Check_Protocol(inSerial);

}}
void startloop(){

  while (pressurevalue!=0) {
  int pressurevoltage = analogRead(pressureAnalog);
  int pressurevalue = (pressurevoltage*.36)-44.98  ;
  Serial.print("Pressure in the pod is: ");
  Serial.print(pressurevalue);
  Serial.println("inHg");
  delay(2000);
  
  
  float batteryvoltage = analogRead(A1);
  int batteryNumber = (int)batteryvoltage*10;
  //round(batteryvoltage);
  //int  batteryvolt = (int) batteryvoltage;
  int batteryvalue;
  switch (batteryNumber){
  case 128:
  batteryvalue = 10;
  break;
  case 127:
  batteryvalue = 20;
  break;
  case 126:
  batteryvalue = 30;
  break;
  case 125:
  batteryvalue = 40;
  break;
  case 124:
  batteryvalue = 50;
  break;
  case 123:
  batteryvalue = 60;
  break;
  case 122:
  batteryvalue = 70;
  break;
  case 121:
  batteryvalue = 80;
  break;
  case 120:
  batteryvalue = 90;
  break;
  case 110:
  batteryvalue = 100;
  break;
  }
  Serial.print("Battery life remaining: ");
  Serial.print(batteryvalue);
  Serial.println("%");
  delay(2000);
  //if(pressurevalue > 22){
  //digitalWrite(sensorswitch, LOW);
  //digitalWrite(pumpswitch, LOW);
  //}
  //else{
  //digitalWrite(pumpswitch,HIGH);
  //digitalWrite(sensorswitch, HIGH);
  //}

  }  }
The code is pretty hideous and there's some irrelevant stuff like the analog stuff, right now I'm just trying to get relay to switch when I send the digital commands[/I][/I][/code]
 

Thread Starter

AdhesiveLamb

Joined Feb 16, 2019
19
Something else interesting happened that may be useful to know. I plugged the arduino into my computer through the USB to revert back to an old code. After uploading it, the relay LEDs would turn on and off like they were supposed to but I still couldn't hear the relay switching. I'm not sure how the relay was even receiving power as I didn't have the 12v rail powered, it must have been recieving power from the Arduino somehow.

Here's the code I reverted back to:
C:
int pumpswitch = 8; //defines variables corresponding to pins
int relief1 = 5;
int relief2 = 6;
int topvalve = 7;
int sensorswitch = 10;
int buttonswitch = 3;
int buttonstate =  0;
int tx = 1;
int rx = 0;
char inSerial[15];


void setup(){
  Serial.begin(9600);  //sets the baud rate
  pinMode(pumpswitch, OUTPUT); //defines pins as either output or input
  pinMode(topvalve, OUTPUT);
  pinMode(buttonswitch, INPUT);
  pinMode(relief1, OUTPUT);
  pinMode(relief2, OUTPUT);
//pinMode(powerinput, INPUT);
//pinMode(pressureinput, INPUT);
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
 // allpinslow();  //ensures all pins are off during initialization
}

void loop(){
  int i=0;
  int m=0;
  delay(500);   
  if (Serial.available() > 0) {  //checks if data is available in the serial port   
  while (Serial.available() > 0) {
  inSerial[i]=Serial.read();
  i++;   
  }
  inSerial[i]='\0';
  Check_Protocol(inSerial);

  buttonstate = digitalRead(buttonswitch);
   
  if (buttonstate == HIGH) {
  digitalWrite(pumpswitch, HIGH);
  }  
  else {
  digitalWrite(pumpswitch, LOW);
  }

  int pressurevoltage = analogRead(A0);
  int pressurevalue = (pressurevoltage*.36)-44.98  ;
  Serial.print("Pressure is: ");
  Serial.print(pressurevalue);
  Serial.println("inHg");
  delay(1000);
   
  int batteryvoltage = analogRead(A1);
  //int batteryvalue = batteryvoltage*1.2;
  Serial.print("Battery life remaining: ");
  Serial.print(batteryvoltage);
  Serial.println("%");
  delay(1000);
  }}
   
void allpinslow()
{
digitalWrite(pumpswitch, LOW);
digitalWrite(topvalve, LOW);
}   

void Check_Protocol(char inStr[]){  //establishes text input for interaction
  int i=0;
  int m=0;
  Serial.println(inStr);

  if(!strcmp(inStr,"pump on")){  //pump ON
  digitalWrite(pumpswitch, HIGH);
  Serial.println("Pump is on");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
   
  if(!strcmp(inStr,"pump off")){  //pump OFF
  digitalWrite(pumpswitch, LOW);
  Serial.println("Pump is off");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
   
  if(!strcmp(inStr,"valve on")){  //topvalve ON
  digitalWrite(topvalve, HIGH);
  Serial.println("Valve is closed");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}

  if(!strcmp(inStr,"valve off")){  //topvalve OFF
  digitalWrite(topvalve, LOW);
  Serial.println("Valve is open");
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}
   
  if(!strcmp(inStr,"relief  1")){  //relief valve 1
  digitalWrite(relief1, HIGH);
  Serial.println("Relief 1 activated");
  delay(2000);
  digitalWrite(relief1, LOW);
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}

  if(!strcmp(inStr,"relief 2")){  //relief valve 2
  digitalWrite(relief2, HIGH);
  Serial.println("Relief 2 activated");
  delay(2000);
  digitalWrite(relief2, LOW);
  for(m=0;m<11;m++){
  inStr[m]=0;}
  i=0;}

  else{
  for(m=0;m<11;m++){
  inStr[m]=0;
  }
  i=0;

}}
 

djsfantasi

Joined Apr 11, 2010
6,974
There’s a couple things I noticed. First, you need to denounce your button input.

Secondly, when you initialize each pin for output, what do you want it to default to? I typically (although not necessarily required), issue a digitalWrite() to specifically set the preferred startup state.

Also, outputting a LOW should activate the relay. To check this, temporarily patch a relay board to ground on the Arduino. The relay should click.

I’d write a simple test sketch that initialized just one pin to the relay. Then, I’d code the loop() to output a LOW, delay a few seconds, output a HIGH and then delay a few seconds. The relay should click on and off... if it doesn’t something is amiss.

Once you get one pin working, test the remaining pins.
 

Thread Starter

AdhesiveLamb

Joined Feb 16, 2019
19
I tested out this very simple code but it behaved in a similar fashion. The corresponding LED would get dimmer then brighter ad nauseam. Leading me to believe that I have something wired incorrectly
C:
int bop=5;
void setup() {
  Serial.begin(9600);
  pinMode(bop, OUTPUT);

}

void loop() {
  digitalWrite(bop, HIGH);
  delay(1000);
  digitalWrite(bop, LOW);
  delay(1000);
}
 

Thread Starter

AdhesiveLamb

Joined Feb 16, 2019
19
I may have figured it out. I ended up hooking up the Vcc on the relay to the 5V on the Arduino (instead of the 12V rail). I also added a wire from the 12V rail to the JDVcc the relay is actually switching now.
 
Unfortunatey, you have to learn Chinglish. it's tough sometimes.

e.g.
1. S1-S6 is the high level trigger selection of relay 1 Channel - 6 Channel.
2. When COM is short connected with LOW, the corresponding relay is LOW level trigger, while when COM is short connected with high end is high level trigger.

We might call S1-S6 J1 through J6 for jumper. S is Switch, so that's close to a jumper, right?

"short connected" "short wire" or jumper.

Lousy pictures too.

You know the relay is operated with logic levels, but the relay power is 12 V.

Then there is what you probaby found out already. When a processor powers up, the outputs are inputs.
Honestly, you don't want stuff to move, Supposedly, you had wires connecting a computer to a bunch of valves. If you unplug the computer, you may want all of the valves to close.

Look at the ULN200x series of chips for driving relays.

An open input is OFF
An input to ground is OFF
A high is on.

An unused TTL input floats high and that's a problem, but not with the ULN200x series of chips.
 
Top