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...
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
....
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:
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.
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.
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?)
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.