Relay is not working (Urgent Assistance)

My relay is not functioning at all, it is a code error because it works with a simple code.

My relay is on pin 13 and my device is connected to the NO ( Normally Open), in the while loop I want the relay to close so I put digitalWrite(13, LOW); But it is not working at all.

Below is my code please try to assist

#include <SPI.h>
#include <Pixy2.h>

Pixy2 pixy;

////////////////////////////////////////////////////////

// ENA IN1 IN2 IN3 IN4 ENB
int myPins[6] = {5, 6, 7, 8, 9, 10}; // all Digital pins from the motor driver
int enable1 = 5; // Motor Driver Enable 1
int enable2 = 10; // Motor Driver Enable 2
static int i=0;
int signature, x, y, width, height;
unsigned int newarea = 0;

void setup() {
Serial.begin(115200);
Serial.print("Starting...\n");
// pixy.init();
pinMode(13, OUTPUT); // for the relay of the collection system

for (int i = 0; i < 6; i++) {
pinMode(myPins[i], OUTPUT);
}
pixy.init();
pixy.setLamp(0,0); // switching off the Pixy built in Lights
// digitalWrite(13,HIGH) ;

}

void loop() {

  // Detection system 
uint16_t blocks;                               // Maximum number of blocks that getBlocks() will return.
blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
x = pixy.ccc.blocks[i].m_x;                    //get x position
y = pixy.ccc.blocks[i].m_y;                    //get y position
width = pixy.ccc.blocks[i].m_width;            //get width
height = pixy.ccc.blocks[i].m_height;          //get hight

/////////////
// digitalWrite(13,HIGH) ;

if(signature == 1) {

// digitalWrite(13,LOW) ;

delay(350);
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);
delay(30);
moveRobot(-110,0);
delay(500); // time to centre the robot to the ball
digitalWrite(enable1, LOW);
digitalWrite(enable2, LOW);

// code of area
///////////////////
newarea = width * height; //calculate the object area
Serial.print(newarea); // print area of the ball
Serial.print('\n');
while (newarea < 3000 && signature == 1)//go forward if object too small
{
// delay(10);
digitalWrite(13,LOW) ; // Start the vacume

moveRobot(100,100);                              //go forward 
delay (10); 
                 
blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
x = pixy.ccc.blocks[i].m_x;                    //get x position
y = pixy.ccc.blocks[i].m_y;                    //get y position
width = pixy.ccc.blocks[i].m_width;            //get width
height = pixy.ccc.blocks[i].m_height;          //get Height
newarea = width * height;                      //calculate the object area

      Serial.print(newarea);                   // print signature area
 //     Serial.print("NEXT --->");
      Serial.print('\n');
    
  }
  delay (50);
   digitalWrite(enable1, LOW);
   digitalWrite(enable2, LOW); 
   //digitalWrite( 13,LOW) ; 

// delay (10);

   delay (5000) ; // time for collection system to work 

digitalWrite(13,HIGH) ;

}

else {
digitalWrite(13,HIGH) ; // Start the vacume

moveRobot(140,0); // keep searching in circle for the ball

delay (10);

}

}

void moveRobot(int leftSpeed, int rightSpeed)
{
if (leftSpeed >= 0) {
digitalWrite(myPins[1], 0);
digitalWrite(myPins[2], 1);
}
else {
digitalWrite(myPins[1], 1);
digitalWrite(myPins[2], 0);
}

if (rightSpeed >= 0) {
digitalWrite(myPins[3], 0);
digitalWrite(myPins[4], 1);
}
else {
digitalWrite(myPins[3], 1);
digitalWrite(myPins[4], 0);
}

analogWrite(myPins[0], abs(leftSpeed));
analogWrite(myPins[5], abs(rightSpeed));
}

Could you edit your post to put you code in code tags?

As Idahowalker said.

You have part of the code in code tags (the shaded parts) but not all . Makes trying to make sense of your code very difficult.

Have you tried troubleshooting your code? I would start with the minimum code you can (i.e. with all but one feature commented out) Get that running then add little by little the remaining functions.

In the code you posted in the while loop,
digitalWrite(13,low) is commented out
digitalWrite(13,high) is still there?

Some relays boards are working inverted as there is an open collector circuit driving the relay.

Second point: how do you feed your relay, it could be that you are overcharging your board with the current drawn by the relay coil.

using </>

suggest using unique serial.prints to verify when calls to digitalWrite (13 are made. start with the basics

This all depends on how you have wired the relay module (high side/low side) and how, if any, you have set the jumper(s).
Pin 13 is not an ideal choice with the Uno/Nano etc because this is also the built in LED pin which flashes at system start.

Thank you all for the responses.
This is the cleaned code below and also I taged it.

In line 31 I set the relay to off digitalWrite(13, HIGH) ;

Then in line 69 inside the while loop once the statement is true the first line of code is to set the relay to 1 by using digitalWrite(13, LOW) ;

Then in line 94 once the while loop is done I set it to zero by digitalWrite(13, HIGH) ;

Also in line 99 in the else of the IF statement I set it to zero by digitalWrite(13,HIGH) ;

My relay is power on from the 5V pin from the Arduino (it is the only pin of the 5v) and the IN1 on pin 13, Ground on a GND pin from the Arduino and the connected device to the relay has its own power source.

Moreover, the relay is not broken, as I have tried a smaller blink code on it and it is working perfectly.

I don't understand why no signal is given out by PIN 13 in my code in the while or outside the while

#include <SPI.h>
#include <Pixy2.h>


Pixy2 pixy;

////////////////////////////////////////////////////////

// ENA IN1 IN2 IN3 IN4 ENB
int myPins[6] = {5, 6, 7, 8, 9, 10}; // all Digital pins from the motor driver
int enable1 = 5; // Motor Driver Enable 1
int enable2 = 10; // Motor Driver Enable 2
static int i=0;
int signature, x, y, width, height;
unsigned int newarea = 0;

int vac = 13; 

void setup() {
  Serial.begin(115200);
  Serial.print("Starting...\n");
  //  pixy.init();
    pinMode(13, OUTPUT); // for the relay of the collection system 


  for (int i = 0; i < 6; i++) {
    pinMode(myPins[i], OUTPUT);
  }
  pixy.init();  
  pixy.setLamp(0,0); // switching off the Pixy built in Lights 
     digitalWrite(13,HIGH) ;   // to set the relay to 0 from the begining 


}

void loop() {

      // Detection system 
    uint16_t blocks;                               // Maximum number of blocks that getBlocks() will return.
    blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
    signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
    x = pixy.ccc.blocks[i].m_x;                    //get x position
    y = pixy.ccc.blocks[i].m_y;                    //get y position
    width = pixy.ccc.blocks[i].m_width;            //get width
    height = pixy.ccc.blocks[i].m_height;          //get hight
    
   /////////////

  if(signature == 1) {
    

  delay(350);
  digitalWrite(enable1, LOW);
  digitalWrite(enable2, LOW);
  delay(30);
   moveRobot(-110,0);
  delay(500); // time to centre the robot to the ball 
   digitalWrite(enable1, LOW);
  digitalWrite(enable2, LOW);

  //  code of area 
///////////////////
          newarea = width * height; //calculate the object area
          Serial.print(newarea); // print area of the ball
          Serial.print('\n');
   while (newarea < 3000 && signature == 1)//go forward if object too small
      {
      //  delay(10);
            digitalWrite(13,LOW) ;                       // Start the vacume 

    moveRobot(100,100);                              //go forward 
    delay (10); 
                     
    blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
    signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
    x = pixy.ccc.blocks[i].m_x;                    //get x position
    y = pixy.ccc.blocks[i].m_y;                    //get y position
    width = pixy.ccc.blocks[i].m_width;            //get width
    height = pixy.ccc.blocks[i].m_height;          //get Height
    newarea = width * height;                      //calculate the object area

          Serial.print(newarea);                   // print signature area
          Serial.print("NEXT --->");

          Serial.print('\n');
  
      }
      delay (50);
       digitalWrite(enable1, LOW);
       digitalWrite(enable2, LOW); 
 //  delay (10); 

       delay (5000) ; // time for collection system to work 
 digitalWrite(13,HIGH) ;  

    }
    
  else   {
           digitalWrite(13,HIGH) ;                       // Start the vacume 

 moveRobot(140,0); // keep searching in circle for the ball
 
  delay (10); 

    }

}



void moveRobot(int leftSpeed, int rightSpeed)
{
  if (leftSpeed >= 0) {
    digitalWrite(myPins[1], 0);
    digitalWrite(myPins[2], 1);
  }
  else {
    digitalWrite(myPins[1], 1);
    digitalWrite(myPins[2], 0);
  }

  if (rightSpeed >= 0) {
    digitalWrite(myPins[3], 0);
    digitalWrite(myPins[4], 1);
  }
  else {
    digitalWrite(myPins[3], 1);
    digitalWrite(myPins[4], 0);
  }

  analogWrite(myPins[0], abs(leftSpeed));
  analogWrite(myPins[5], abs(rightSpeed));
}```

Have you tried

void loop() {

      // Detection system 
    //uint16_t blocks;                               // Maximum number of blocks that getBlocks() will return.
    //blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
    //signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
    //x = pixy.ccc.blocks[i].m_x;                    //get x position
    //y = pixy.ccc.blocks[i].m_y;                    //get y position
    //width = pixy.ccc.blocks[i].m_width;            //get width
   // height = pixy.ccc.blocks[i].m_height;          //get hight
    
   /////////////

  //if(signature == 1) {
    

  //delay(350);
  //digitalWrite(enable1, LOW);
  //digitalWrite(enable2, LOW);
  //delay(30);
   //moveRobot(-110,0);
  //delay(500); // time to centre the robot to the ball 
   //digitalWrite(enable1, LOW);
  //digitalWrite(enable2, LOW);

  //  code of area 
///////////////////
     //     newarea = width * height; //calculate the object area
       //   Serial.print(newarea); // print area of the ball
         // Serial.print('\n');
  // while (newarea < 3000 && signature == 1)//go forward if object too small
     // {
      //  delay(10);
            digitalWrite(13,LOW) ;                       // Start the vacume 

    ///moveRobot(100,100);                              //go forward 
    //delay (10); 
                     
    //blocks = pixy.ccc.getBlocks();                 //receive data from pixy 
    //signature = pixy.ccc.blocks[i].m_signature;    //get object's signature
 //   x = pixy.ccc.blocks[i].m_x;                    //get x position
    //y = pixy.ccc.blocks[i].m_y;                    //get y position
   // width = pixy.ccc.blocks[i].m_width;            //get width
    //height = pixy.ccc.blocks[i].m_height;          //get Height
    //newarea = width * height;                      //calculate the object area

       //   Serial.print(newarea);                   // print signature area
         // Serial.print("NEXT --->");

          //Serial.print('\n');
  
//      }
   //   delay (50);
      // digitalWrite(enable1, LOW);
      // digitalWrite(enable2, LOW); 
 //  delay (10); 

       delay (5000) ; // time for collection system to work 
 digitalWrite(13,HIGH) ;  

    //}
    
  //else   {
     //      digitalWrite(13,HIGH) ;                       // Start the vacume 

// moveRobot(140,0); // keep searching in circle for the ball
 
  //delay (10); 

    //}
delay(5000); //<<< used for trouble shooting.
}

Does that enable/disable the relay?

How is that supposed to work?

Hi @ zyadessam,

I don't know if I'm correct, but you are including the SPI library.
If you are using UNO, then SPI uses pin 13 and it is not recommended that you connect the relay to pin 13.
https://www.arduino.cc/en/reference/SPI
RV mineirin

Hi,
Welcome to the forum.

What model Arduino are you using?
Can you please post a circuit diagram?
Just a hand drawn picture would be fine.

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