digitalRead and level probe sensor

I'm still here...
Now I've problem with the digitalRead... I've a level probe that "returns" HIGH when the water level is below the threshold else LOW...
The probe works also with 5v so I connect a wire to the 5v arduino's pin and the second wire to the pin 7

The sketch correctly recognizes when the probe is HIGH, but when the probe is LOW the sketch starts to "loop" reading LOW and HIGH when the correct value is only LOW...

here is the code and the output...

void setup(){
  bool thisVal = false, lastVal = false;
  Serial.begin(9600);
  Serial.println("Start");  
  pinMode(7, INPUT);
  
  while(true){
    delay(50);
    if(digitalRead(7) == HIGH)
      thisVal = true;
    else
      thisVal = false;
    
      
    if(thisVal != lastVal){
      Serial.print("Probe change, now is ");
      Serial.println(thisVal ? "HIGH" : "LOW");
      lastVal = thisVal;
    }
  }
}

void loop(){
}
Start
Probe change, now is HIGH // right
Probe change, now is LOW // right
Probe change, now is HIGH // hereafter the probe is "LOW" but arduino continues to recognize changes...
Probe change, now is LOW
Probe change, now is HIGH
Probe change, now is LOW
Probe change, now is HIGH
....

p.s. I know that the if else could be

thisVal = ((digitalRead(7) == HIGH) ? true : false);

or better

thisVal = digitalRead(7);

but it's a sample code 8)

Without pull-up or pull-down resistors, the pin floats when there is no voltage applied. This is what you are seeing. Turn on the internal pullup resistor, or add an external pull-up or pull-down resistor.

Let me understand:
pull-up & pull-down resistor is simply a resistance, isn't??
And pull-up resistor (that I understand is favorite to a pull-down resistor because uses less corrent) could be abought 1,5k Omh right?

I've searched a little... and I've still some trouble...

If I've the probe level sensor (above mentionated) and I would like to use a pull-up resitor i must do like this:

Arduino ground --> probe level sensor --> Arduino pin 7 <-- resistor 1,5k <-- Arduino +5v

right? in a nutshell I have to link the end of the two circuits toghether and to connect everything to Arduino (e.g. on pin 7) ?

Without knowing the details of your level sensor, it is impossible to say what value of the pullup or pulldown resistor is needed. The Arduino has an internal pullup of about 20k which can be turned on by using digitalWrite to write a HIGH to the digital input pin, and that may or may not suffice for your applcation. If you want a more precise answer, you will need to provide details of the sensor.

Unfortunately I haven't details more than the follow link...

But I think that 20k could be enough...
It's better to use a pull-up or a pull-down?

p.s. can you make an example of code that use arduino pull-up?

From that link, it looks to me that the sensor is a simple float switch, so the pullup or pulldown value is not critical. At the low currents involved, there is the possibility that the contacts may oxidise, in which case a capacitor connected in parallel with the sensor may help to avoid it.

To use the internal pullup:

  pinMode(7, INPUT);
  digitalWrite(7, HIGH);

Using digitalWrite to write HIGH to an input pin enables the pullup.

I'm very low in electronic...

is correct the circuit attacched?

capacitor of wich capacity?

p.s. if I want use a resistor instead of "Arduino pin 7", in the same schema I've to connect "old arduino pin 7" to +5 and insert a resistor (of what? 1k?)

Schema arduino.pdf (13.9 KB)

Hi,

You don't need 2 Arduino pins, you only need one. Set it up using the pinMode(pin, INPUT) and digitalWrite(pin, HIGH) calls. When you want to read it, do a digitalRead(pin) from the same pin. It will read LOW if the switch is activated, HIGH if not.

In the first instance I'd use a 100nF capacitor. If the switch contacts become unreliable, try a larger one, e.g. 10uF.

Perfect it works! thanks! (to try it I've not used the capacitor, i've made 20 switch of value..)

p.s. the capacitor must be connect in parallel whit the probe, so it should be connect to the gnd and pin 7 right? take a wire by gnd, duplicate it (one for the probe and the other one for the capacitor), get the two wire (probe and capacitor) and reunite it before entering the pin right?

Brig:
p.s. the capacitor must be connect in parallel whit the probe, so it should be connect to the gnd and pin 7 right? take a wire by gnd, duplicate it (one for the probe and the other one for the capacitor), get the two wire (probe and capacitor) and reunite it before entering the pin right?

Yes. Preferably, the ground side of the capacitor should be connected to the ground side of the sensor, and then a wire or PCB trace taken from that connection to Arduino ground. Don't connect the ground side of the cap and the ground side of the sensor separately to Arduino ground, otherwise the current pulse when the capacitor discharges through the sensor might cause something to malfunction.