Level switch false triggering with arduoino nano

I'm using level float switch with arduino nano with 3v supply from on board arduino pin. Level float is connected to arduino nano with 4 to 5 meters long wire.

On the other end, an alarm is connected to arduino via 5v realay module.

The problem I'm facing is, I'm getting false triggers randomly. Please suggest.

I'm using level float switch with arduino nano.

Level float is connected to arduino nano with 4 to 5 meters long wire.

On the other end, an alarm is connected to arduino via 5v realay module.

Arduino is powered with 5V 2A adapter.

The problem I'm facing is, I'm getting false triggers of alarm whenever I switch on or off any other appliance in home. Please suggest.

Please suggest

Post a schematic

Hi,

1.Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

2.Can you please post your code?

3.Have you got a pullup or pulldown resistor on the float switch input pin?

4.Have you tried putting a 0.1uF capacitor from the input pin the ground to bypass any noise?

5.Is the float switch wiring near or running along side any mains power wiring?

Thanks.. Tom.. :)

I don't see a schematic attached.

You need an external pull-down or pull-up resistor or use the internal pull-up resistor to prevent floating inputs that pick up noise from the environment.

This topic has been merged into Level switch false triggering with arduoino nano.

@Dnyaan, do not cross-post. Threads merged.

You were warned about cross posting. Another merge SHEESH

Do it again and you will be on a time out.

Please read THIS

Bob.

Sorry, that happened by mistake.

I have attached the schematic now.

Here is the code:

const int sensorPin = 2;     // the number of the pushbutton pin
const int alarmPin = 3;      // the number of the LED pin
int i=0;


int sensorState = 0;         // variable for reading the sensor status


void setup() {    
  // initialize the ALARM pin as an output:
  pinMode(alarmPin, OUTPUT);
  // initialize the SENSOR pin as an input:
  pinMode(sensorPin, INPUT); 
}


void loop(){

 // read the state of the sensor value:
  sensorState = digitalRead(sensorPin);

  if (sensorState == HIGH) {
    
    while (i < 3 )        // To repeat the alarm three times
     {
      digitalWrite(alarmPin, HIGH);
      delay(1000);
      digitalWrite(alarmPin, LOW);
      delay(10000);       // 10 sec delay between the 3 alerts
      i++ ;
     } 
  }
    
      else {
       i=0;      // reset i
       digitalWrite(alarmPin, LOW);
    }
  }

can you elaborate on false triggers? does it trigger with no activity, or is it giving multiple triggers once an initial trigger?

Reason I ask is you have no debouncing (the guts in the sensor literally can bounce giving multiple readings). So some timing code needs to be added (if last elapsed trigger time > some value it's probably not bouncing so consider a trigger... type thing).

Suggest putting a pull down resistor on the sensor, for all I know it's floating output when not sensing.

How to post an image so that we don't have to download it.

The way that the switch is wired calls for a pull up resistor. There are internal pullups that can be enabled with pinMode(INPUT_PULLUP), but they are very weak (30K to 50K) so probably will not be enough for the long wires that you have. I suggest an external pullup of 5K to start. It you still have false triggers, lower the value of the resistor (should not need to go below 1K or so). A 0.1uF cap across the switch, as recommended by Tom, will also help. The 0.1 cap and the pullup form a low pass filter that will block the higher frequency noise.

KrisKasprzak: can you elaborate on false triggers? does it trigger with no activity, or is it giving multiple triggers once an initial trigger?

Reason I ask is you have no debouncing (the guts in the sensor literally can bounce giving multiple readings). So some timing code needs to be added (if last elapsed trigger time > some value it's probably not bouncing so consider a trigger... type thing).

Suggest putting a pull down resistor on the sensor, for all I know it's floating output when not sensing.

I'm getting false triggers of alarm whenever I switch on or off any other appliance in home.

That is because of the antenna effect of the long wires picking up noise. The pullup and cap should overcome the noise. If those do not remedy the false triggers you may need to add shielding to the switch wires. Of course, you must also make sure that the low level signal wires (switch wires) are as far as physically possible from the high current carrying wires to avoid inductive coupling.

groundFungus: That is because of the antenna effect of the long wires picking up noise. The pullup and cap should overcome the noise. If those do not remedy the false triggers you may need to add shielding to the switch wires. Of course, you must also make sure that the low level signal wires (switch wires) are as far as physically possible from the high current carrying wires to avoid inductive coupling.

Switch wires are nowhere near to AC supply. They actually run to the tank on terrace from my window.

That is good. Did you try the pullup and cap yet?

This is essentially the same problem as de-bouncing.

You do not just read the switch once before you act on it, you use the loop() to read it again and again and only consider it actuated when it reads the same every time while millis() advances by 10; that is to say it must stay closed for all of 10 milliseconds.

This is far more effective than fiddling about with capacitors though the capacitor will not hurt (much). This is what microcontrollers are for - to solve problems.

Now with proper coding, you can get rid of the delay() functions and enable your device to do all of the alarm function while "simultaneously" performing a number of other tasks as well. Otherwise what you have described so far could just as easily be done by a 555 timer or two.

I suggest you have a go at figuring this out; if you are really stuck I can give you some code to do this properly. :grinning:

Hi,
OPs circuit;


Tom.. :slight_smile:

You can try something like https://forum.arduino.cc/index.php?topic=432333.msg2981473#msg2981473

sterretje: You can try something like https://forum.arduino.cc/index.php?topic=432333.msg2981473#msg2981473

You can but - hey, you have a microprocessor! Use it! :roll_eyes: