fault input ESP32 showing

67 views Asked by At

I am using the capacitive sensor to detect an object. The sensor required 24V to operate but ESP32 runs on 2.2 to 3.6V.The input from sensor also gonna be 24V. so I can't directly connect sensor to ESP32, it will damage my ESP32. For that I used Relay I connect the Sensor with Relay, and Relay with ESP32.

The output wire of the Sensor and the negative wire of the Sensor is connected with the Relay coil.

I use used 25pin as an Output and 34pin as an Input of the ESP32.

25pin is connected to the Relay 12port which is normally close with 4port and normally open with 8port, and 34pin is connected to the 8port.

When Sensor detect something sensor light turns ON. and also the LED of ESP32 turns ON and also prints on serial monitor "Detected", and the Sensor light, LED remains turned ON and printing "Detected" continuously until the object is in contact with Sensor.

But when there is object the Sensor's light remains turned OFF. But sometimes the LED turns ON and "detected" is start printing on serial monitor even if there is no object. Also if I remove the input pin(34pin) from the relay(8port). still sometime it print "detected" and LED also turned ON.

Firsly I declared pins digital now I declared pins analog.

But still same result, but now I'm filering the data by adding the 10 sensor's input and dividing the sum by 10. Which works very well but still sometime the same issue occur. I think I have clear the buffer of ESP32 to resolve this issue. I don't know how to clear the buffer. Or use millis()?

The code is given below:

const int input=34;
const int output=25;
const int led=2;

void setup() 
{
  pinMode(input,INPUT);
  pinMode(output,OUTPUT);
  pinMode(led,2);
  Serial.begin(300);
  digitalWrite(output,HIGH);
}

void loop() 
{
  int detect=0;
  
  for(int i=0;i<10;i++)
  {
    detect+=analogRead(input);
  }
  
  detect=detect/10;
  if(detect>4000)
  {
    Serial.println("Detected");
    digitalWrite(led,HIGH);
    detect=0;
  }
  else
  {
    Serial.println("Not Detected");
    digitalWrite(led,LOW);
    detect=0;
  }
}
1

There are 1 answers

0
Clifford On

It is not in scope on this site to determine whether your hardware is correct, but a relay is a digital state switch, high or low. As such reading it via an analogue input makes little sense.

Your earlier attempt that you have not shown using a digital input is of more interest, the issues you have in both cases however are almost certainly due to switch bounce. Since you mention the relay coil, it is clear that you are not using a solid state relay, and as such it will be subject to switch-bounce.

Switch-bounce is an issue with electro-mechanical contacts, where the voltage will vary for a short period as the contacts open or close. The timing of the switch bounce state changing and your polling the switch can lead to incorrect states being read - especially if you are implementing state change detection.

To resolve this issue, your software must implement a "de-bouncing" algorithm. A simple method is to timestamp any change of state, and then accept the state when enough time has passed that any switch bounce has settled (20-30ms is normally ample).

For example:

void loop() 
{
  // Debounce time in milliseconds
  static const int DEBOUNCE_MS = 30 ;
  
  // Initial state
  static int proximity_detect_state = digitalRead( input ) ;

  // Debounce timestamp and state
  static unsigned long state_change_timestamp = 0;
  static bool debouncing = false ;

  // Get input state and time
  int input_state = digitalRead( input ) ;
  int now = millis() ;
  
  // Debounce input...
  if( input_state != proximity_detect_state )
  {
    // State changed, timestamp it.
    state_change_timestamp = now ;
    debouncing = true ;
  }
  else if( debouncing && 
           now - state_change_timestamp > DEBOUNCE_MS )
  {
    // Steady state for debounce period, accept it as a new state
    proximity_detect_state = input_state ;
    debouncing = false ;
  }
  
  // On change of debounced state...  
  // Force state change of first iteration
  static int previous_proximity_detect_state = proximity_detect_state == 0 ? 1 : 0 ;
  if( proximity_detect_state != previous_proximity_detect_state )
  {
      previous_proximity_detect_state = proximity_detect_state ;
      
      if( proximity_detect_state != 0 )
      {
        Serial.println( "Detected" ) ;
        digitalWrite(led,HIGH);
      }
      else
      {
        Serial.println("Not Detected");
        digitalWrite(led,LOW);
      }
  }
}

The part: // On change of debounced state... above is only necessary to prevent continuous output on the serial port. Repeatedly setting the LED state is benign, so without the serial output, previous_proximity_detect_state is not required and you can simply have:

digitalWrite( led, proximity_detect_state ? HIGH : LOW ) ;

in place of that entire block.

A simpler solution if a strictly deterministic real-time deterministic response is not required is to simply poll the input state at an interval somewhat higher than the switch bounce time. For example:

void loop() 
{
  static const int POLL_INTERVAL_MS = 100 ;
  
  // Initial state and time
  static int proximity_detect_state = digitalRead( input ) ;
  static unsigned long poll_timestamp = millis() ;

  // Get current time
  int now = millis() ;

  // If time to poll input...
  if( now - millis() >= POLL_INTERVAL_MS )
  {
    proximity_detect_state = digitalRead( input ) ;
  }
    
  digitalWrite( led, proximity_detect_state ? HIGH : LOW ) ;
}

Comparing the two solutions, in the first, the change of state detection occurs deterministically 30ms after the event. Any jitter is down to the physical bounce duration of the relay. Whilst in the second the detection occurs anytime between 0 to 100ms after the actual the event. Clearly you could decrease the polling interval, but it would remain variable and in some cases you might want something far more deterministic.

Another solution of course is to use a solid-state relay rather than an electro-machanical one. But that is not really in scope on this site. It would have other advantages with respect to EMC, power consumption and durability too.