Ultrasonic-sensor Why i need to touch trig cable ,For the sensor to work¡? .-.

Sooo he is the problem …
I cant solve it anyway… I’ts a liitle curius

First of all i gona left the code and an image of the circuit

Here is the code

#include<SPI.h>
#include<MFRC522.h>
#include<Servo.h>


#define trigPin 2
#define echoPin 6
#define led




//SERVO INSTANCE
Servo servoInstance;
int abierto=180;
int cerrado=0;

//creating mfrc522 instance
#define RSTPIN 9
#define SSPIN 10
MFRC522 rc(SSPIN, RSTPIN);

int readsuccess;

/* the following are the UIDs of the card which are authorised
    to know the UID of your card/tag use the example code 'DumpInfo'
    from the library mfrc522 it give the UID of the card as well as 
    other information in the card on the serial monitor of the arduino*/

    //byte defcard[4]={0x32,0xD7,0x0F,0x2B}; // if you only want one card
byte defcard[][4]={{0xC6,0x5B,0x64,0xF8},{0x32,0xD7,0x0F,0xB}}; //for multiple cards
int N=2; //change this to the number of cards/tags you will use
byte readcard[4]; //stores the UID of current tag which is read

void setup() {
Serial.begin(9600);
servoInstance.attach(3);


SPI.begin();
rc.PCD_Init(); //initialize the receiver  
rc.PCD_DumpVersionToSerial(); //show details of card reader module

pinMode(5,OUTPUT); //led for authorised
pinMode(4,OUTPUT); //led for not authorised

Serial.println(F("the authorised cards are")); //display authorised cards just to demonstrate you may comment this section out
for(int i=0;i<N;i++){ 
  Serial.print(i+1);
  Serial.print("  ");
    for(int j=0;j<4;j++){
      Serial.print(defcard[i][j],HEX);
      }
      Serial.println("");
     }
Serial.println("");

Serial.println(F("Scan Access Card to see Details"));
}


void loop() {
  
readsuccess = getid();

if(readsuccess){
 
  int match=0;

//this is the part where compare the current tag with pre defined tags
  for(int i=0;i<N;i++){
    Serial.print("Testing Against Authorised card no: ");
    Serial.println(i+1);
    if(!memcmp(readcard,defcard[i],4)){
      match++;
      }
    
  }
    
   if(match)
      {Serial.println("CARD AUTHORISED");
        digitalWrite(5,HIGH);
        delay(2000);
        digitalWrite(5,LOW);
        delay(500); 
        servoInstance.write(abierto); 
        
        int distancia = 20;
//Marcadior


        
        while(distancia >=0 ) {
        long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distancia = (duration/2) / 29.1;
        Serial.print(distancia);
        Serial.print(" cm");
        delay(500);
        }
         digitalWrite(4,HIGH);
         delay(200);
         servoInstance.write(cerrado);
         digitalWrite(4,LOW);
      }
     else {
      Serial.println("CARD NOT Authorised");
      digitalWrite(4,HIGH);
        delay(2000);
        digitalWrite(4,LOW);
        delay(500); 
   
      }
  
  }
}
//function to get the UID of the card
int getid(){  
  if(!rc.PICC_IsNewCardPresent()){
    return 0;
  }
  if(!rc.PICC_ReadCardSerial()){
    return 0;
  }
 
  
  Serial.println("THE UID OF THE SCANNED CARD IS:");
  
  for(int i=0;i<4;i++){
    readcard[i]=rc.uid.uidByte[i]; //storing the UID of the tag in readcard
    Serial.print(readcard[i],HEX);
    
  }
   Serial.println("");
   Serial.println("Now Comparing with Authorised cards");
  rc.PICC_HaltA();
  
  return 1;
}

And here is the image of the circuit

I will explain a bit what is happening

I want a code that when it detects a distance of less than 8 cm, put the servomotor to 0

The problem is that when I start for the first time the distances mark 0 cm

After a lot of attempts, I discovered something weird

When I touch the cable, even in an isolated part or without even touching it

It starts to work and when I do not touch it it stops working

I can even use a pair of tweezers as an antenna and when I walk away it also stops working

I have tried everything …
I hope someone knows what is happening here

I f you want i upload an video…

Link: Any idea ?? How to solve static?

Here is the image problem — ImgBB

Here is the code

https://github.com/Adrianh77/AAC.git

Always post code to the forum (and use code tags around it)!

Same applies to images. Images loaded to other sites tend to be not accessible anymore after short time frames.

When I touch the cable, even in an isolated part or without even touching it

It starts to work and when I do not touch it it stops working

Sounds like broken wire. Check all the wires with a multimeter.

pylon:
Sounds like broken wire. Check all the wires with a multimeter.

And flex the wire while doing the meter test. If not, you could test the bad one while it's got continuity and not know.

Sorry I tried to put the image with the option but never appeared

I put the code right now

#include<SPI.h>
#include<MFRC522.h>
#include<Servo.h>


#define trigPin 2
#define echoPin 6
#define led




//SERVO INSTANCE
Servo servoInstance;
int abierto=180;
int cerrado=0;

//creating mfrc522 instance
#define RSTPIN 9
#define SSPIN 10
MFRC522 rc(SSPIN, RSTPIN);

int readsuccess;

/* the following are the UIDs of the card which are authorised
    to know the UID of your card/tag use the example code 'DumpInfo'
    from the library mfrc522 it give the UID of the card as well as 
    other information in the card on the serial monitor of the arduino*/

    //byte defcard[4]={0x32,0xD7,0x0F,0x2B}; // if you only want one card
byte defcard[][4]={{0xC6,0x5B,0x64,0xF8},{0x32,0xD7,0x0F,0xB}}; //for multiple cards
int N=2; //change this to the number of cards/tags you will use
byte readcard[4]; //stores the UID of current tag which is read

void setup() {
Serial.begin(9600);
servoInstance.attach(3);


SPI.begin();
rc.PCD_Init(); //initialize the receiver  
rc.PCD_DumpVersionToSerial(); //show details of card reader module

pinMode(5,OUTPUT); //led for authorised
pinMode(4,OUTPUT); //led for not authorised

Serial.println(F("the authorised cards are")); //display authorised cards just to demonstrate you may comment this section out
for(int i=0;i<N;i++){ 
  Serial.print(i+1);
  Serial.print("  ");
    for(int j=0;j<4;j++){
      Serial.print(defcard[i][j],HEX);
      }
      Serial.println("");
     }
Serial.println("");

Serial.println(F("Scan Access Card to see Details"));
}


void loop() {
  
readsuccess = getid();

if(readsuccess){
 
  int match=0;

//this is the part where compare the current tag with pre defined tags
  for(int i=0;i<N;i++){
    Serial.print("Testing Against Authorised card no: ");
    Serial.println(i+1);
    if(!memcmp(readcard,defcard[i],4)){
      match++;
      }
    
  }
    
   if(match)
      {Serial.println("CARD AUTHORISED");
        digitalWrite(5,HIGH);
        delay(2000);
        digitalWrite(5,LOW);
        delay(500); 
        servoInstance.write(abierto); 
        
        int distancia = 20;
//Marcadior


        
        while(distancia >=0 ) {
        long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distancia = (duration/2) / 29.1;
        Serial.print(distancia);
        Serial.print(" cm");
        delay(500);
        }
         digitalWrite(4,HIGH);
         delay(200);
         servoInstance.write(cerrado);
         digitalWrite(4,LOW);
      }
     else {
      Serial.println("CARD NOT Authorised");
      digitalWrite(4,HIGH);
        delay(2000);
        digitalWrite(4,LOW);
        delay(500); 
   
      }
  
  }
}
//function to get the UID of the card
int getid(){  
  if(!rc.PICC_IsNewCardPresent()){
    return 0;
  }
  if(!rc.PICC_ReadCardSerial()){
    return 0;
  }
 
  
  Serial.println("THE UID OF THE SCANNED CARD IS:");
  
  for(int i=0;i<4;i++){
    readcard[i]=rc.uid.uidByte[i]; //storing the UID of the tag in readcard
    Serial.print(readcard[i],HEX);
    
  }
   Serial.println("");
   Serial.println("Now Comparing with Authorised cards");
  rc.PICC_HaltA();
  
  return 1;
}

And here is a video of whats happening

If it’s static (which is the most likely), how do I resolve it?

Link: Any idea ?? How to solve static?

If you put your hand on the wire, does it show the correct distance or just any distance?

Your hand builds a capacitor with the wire and so the circuit may just detect the touch and not the distance of the sensor.

It appears OP is using the HC-SR04 ultrasound sensor (but they forgot to mention this explicitly).

A broken wire is almost certainly the culprit. That, or a poor solder joint/connector. Not static or capacitance: the first is likely to simply break stuff, the second is irrelevant for this circuit, at least at the values of change of stray capacitance resulting from touching a wire.

Happens all the time when prototyping and moving a wire to make it work/not work is the typical symptom.